您的位置:首页 > 运维架构

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);

这样子绘制出的圆柱体是空心的:



我根据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();

}

最终绘制出的圆柱体:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: