opengl 绘制实心圆柱体
2015-06-07 17:11
405 查看
glu中提供了一个绘制圆柱体的函数:
void gluCylinder( GLUquadric* quad,
GLdouble base,
GLdouble top,
GLdouble height,
GLint slices,
GLint stacks )
使用方法是:
GLUquadric *pObj;
pObj = gluNewQuadric();
调用gluCylinder函数时,将pObj作为第一个参数传入。
gluDeleteQuadric(pObj);
这样子绘制出的圆柱体是空心的:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/dizuo/EntryImages/20090309/1.jpg)
我根据gluCylinder函数写了个函数,将两边封顶。
[cpp] view
plaincopy
void mySolidCylinder( GLUquadric* quad,
GLdouble base,
GLdouble top,
GLdouble height,
GLint slices,
GLint stacks )
{
glColor3f(84.0/255, 0.0, 125.0/255.0);
gluCylinder(quad, base, top, height, slices, stacks);
//top
DrawCircleArea(0.0, 0.0, height, top, slices);
//base
DrawCircleArea(0.0, 0.0, 0.0, base, slices);
}
GLvoid DrawCircleArea(float cx, float cy, float cz, float r, int num_segments)
{
GLfloat vertex[4];
const GLfloat delta_angle = 2.0*M_PI/num_segments;
glBegin(GL_TRIANGLE_FAN);
vertex[0] = cx;
vertex[1] = cy;
vertex[2] = cz;
vertex[3] = 1.0;
glVertex4fv(vertex);
//draw the vertex on the contour of the circle
for(int i = 0; i < num_segments ; i++)
{
vertex[0] = std::cos(delta_angle*i) * r + cx;
vertex[1] = std::sin(delta_angle*i) * r + cy;
vertex[2] = cz;
vertex[3] = 1.0;
glVertex4fv(vertex);
}
vertex[0] = 1.0 * r + cx;
vertex[1] = 0.0 * r + cy;
vertex[2] = cz;
vertex[3] = 1.0;
glVertex4fv(vertex);
glEnd();
}
[cpp] view
plaincopy
void mySolidCylinder( GLUquadric* quad,
GLdouble base,
GLdouble top,
GLdouble height,
GLint slices,
GLint stacks )
{
glColor3f(84.0/255, 0.0, 125.0/255.0);
gluCylinder(quad, base, top, height, slices, stacks);
//top
DrawCircleArea(0.0, 0.0, height, top, slices);
//base
DrawCircleArea(0.0, 0.0, 0.0, base, slices);
}
GLvoid DrawCircleArea(float cx, float cy, float cz, float r, int num_segments)
{
GLfloat vertex[4];
const GLfloat delta_angle = 2.0*M_PI/num_segments;
glBegin(GL_TRIANGLE_FAN);
vertex[0] = cx;
vertex[1] = cy;
vertex[2] = cz;
vertex[3] = 1.0;
glVertex4fv(vertex);
//draw the vertex on the contour of the circle
for(int i = 0; i < num_segments ; i++)
{
vertex[0] = std::cos(delta_angle*i) * r + cx;
vertex[1] = std::sin(delta_angle*i) * r + cy;
vertex[2] = cz;
vertex[3] = 1.0;
glVertex4fv(vertex);
}
vertex[0] = 1.0 * r + cx;
vertex[1] = 0.0 * r + cy;
vertex[2] = cz;
vertex[3] = 1.0;
glVertex4fv(vertex);
glEnd();
}
最终绘制出的圆柱体:
void gluCylinder( GLUquadric* quad,
GLdouble base,
GLdouble top,
GLdouble height,
GLint slices,
GLint stacks )
使用方法是:
GLUquadric *pObj;
pObj = gluNewQuadric();
调用gluCylinder函数时,将pObj作为第一个参数传入。
gluDeleteQuadric(pObj);
这样子绘制出的圆柱体是空心的:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/dizuo/EntryImages/20090309/1.jpg)
我根据gluCylinder函数写了个函数,将两边封顶。
[cpp] view
plaincopy
void mySolidCylinder( GLUquadric* quad,
GLdouble base,
GLdouble top,
GLdouble height,
GLint slices,
GLint stacks )
{
glColor3f(84.0/255, 0.0, 125.0/255.0);
gluCylinder(quad, base, top, height, slices, stacks);
//top
DrawCircleArea(0.0, 0.0, height, top, slices);
//base
DrawCircleArea(0.0, 0.0, 0.0, base, slices);
}
GLvoid DrawCircleArea(float cx, float cy, float cz, float r, int num_segments)
{
GLfloat vertex[4];
const GLfloat delta_angle = 2.0*M_PI/num_segments;
glBegin(GL_TRIANGLE_FAN);
vertex[0] = cx;
vertex[1] = cy;
vertex[2] = cz;
vertex[3] = 1.0;
glVertex4fv(vertex);
//draw the vertex on the contour of the circle
for(int i = 0; i < num_segments ; i++)
{
vertex[0] = std::cos(delta_angle*i) * r + cx;
vertex[1] = std::sin(delta_angle*i) * r + cy;
vertex[2] = cz;
vertex[3] = 1.0;
glVertex4fv(vertex);
}
vertex[0] = 1.0 * r + cx;
vertex[1] = 0.0 * r + cy;
vertex[2] = cz;
vertex[3] = 1.0;
glVertex4fv(vertex);
glEnd();
}
[cpp] view
plaincopy
void mySolidCylinder( GLUquadric* quad,
GLdouble base,
GLdouble top,
GLdouble height,
GLint slices,
GLint stacks )
{
glColor3f(84.0/255, 0.0, 125.0/255.0);
gluCylinder(quad, base, top, height, slices, stacks);
//top
DrawCircleArea(0.0, 0.0, height, top, slices);
//base
DrawCircleArea(0.0, 0.0, 0.0, base, slices);
}
GLvoid DrawCircleArea(float cx, float cy, float cz, float r, int num_segments)
{
GLfloat vertex[4];
const GLfloat delta_angle = 2.0*M_PI/num_segments;
glBegin(GL_TRIANGLE_FAN);
vertex[0] = cx;
vertex[1] = cy;
vertex[2] = cz;
vertex[3] = 1.0;
glVertex4fv(vertex);
//draw the vertex on the contour of the circle
for(int i = 0; i < num_segments ; i++)
{
vertex[0] = std::cos(delta_angle*i) * r + cx;
vertex[1] = std::sin(delta_angle*i) * r + cy;
vertex[2] = cz;
vertex[3] = 1.0;
glVertex4fv(vertex);
}
vertex[0] = 1.0 * r + cx;
vertex[1] = 0.0 * r + cy;
vertex[2] = cz;
vertex[3] = 1.0;
glVertex4fv(vertex);
glEnd();
}
最终绘制出的圆柱体:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/dizuo/EntryImages/20090309/2633722128666250000.jpg)
相关文章推荐
- OPTIONS——SIP学习笔记(七)
- nginx 安装配置 for window
- centos6.6,7minimal安装之后,不能补全
- Nginx整合PHP原理
- Hadoop学习(1)----HDFS详解
- linux下nginx+php+mysql 自助环境搭建
- linux 逻辑语句判断
- 117 Tomcat 系统架构与设计模式,第 1 部分: 工作原理
- linux下nginx+php+mysql一键安装
- JMX监控centos下的tomcat
- Ubuntu12.04安装配置Nginx Tomcat环境
- U盘安装Centos7
- Vi编辑器常用快捷键
- LNMP中APACHE配置虚拟主机和个人主页
- linux 命令总结大全
- s5pv210 linux3.8.3内核移植之一:编译体验
- HDInsight-Hadoop现实(两)传感器数据分析
- win7-64-nginx-mysql-php
- Shell if 命令参数
- linux --> 动态库和静态库