glu中提供了一个绘制圆柱体的函数:
void gluCylinder( GLUquadric* quad,
GLdouble base,
GLdouble top,
GLdouble height,
GLint slices,
GLint stacks )
使用方法是:
GLUquadric *pObj;
pObj = gluNewQuadric();
调用gluCylinder函数时,将pObj作为第一个参数传入。
gluDeleteQuadric(pObj);
这样子绘制出的圆柱体是空心的:
我根据gluCylinder函数写了个函数,将两边封顶。
- 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();
- }
最终绘制出的圆柱体: