opengl 绘制实心圆柱体

7 篇文章 1 订阅

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 plain copy
  1. void mySolidCylinder( GLUquadric*   quad,  
  2.                      GLdouble base,  
  3.                      GLdouble top,  
  4.                      GLdouble height,  
  5.                      GLint slices,  
  6.                      GLint stacks )  
  7. {  
  8.     glColor3f(84.0/255, 0.0, 125.0/255.0);  
  9.     gluCylinder(quad, base, top, height, slices, stacks);  
  10.     //top   
  11.     DrawCircleArea(0.0, 0.0, height, top, slices);  
  12.     //base   
  13.     DrawCircleArea(0.0, 0.0, 0.0, base, slices);  
  14. }  
  15.   
  16. GLvoid DrawCircleArea(float cx, float cy, float cz, float r, int num_segments)  
  17. {  
  18.     GLfloat vertex[4];   
  19.   
  20.     const GLfloat delta_angle = 2.0*M_PI/num_segments;  
  21.     glBegin(GL_TRIANGLE_FAN);  
  22.   
  23.     vertex[0] = cx;  
  24.     vertex[1] = cy;  
  25.     vertex[2] = cz;  
  26.     vertex[3] = 1.0;          
  27.     glVertex4fv(vertex);  
  28.   
  29.     //draw the vertex on the contour of the circle   
  30.     for(int i = 0; i < num_segments ; i++)  
  31.     {  
  32.         vertex[0] = std::cos(delta_angle*i) * r + cx;  
  33.         vertex[1] = std::sin(delta_angle*i) * r + cy;  
  34.         vertex[2] = cz;  
  35.         vertex[3] = 1.0;  
  36.         glVertex4fv(vertex);  
  37.     }  
  38.   
  39.     vertex[0] = 1.0 * r + cx;  
  40.     vertex[1] = 0.0 * r + cy;  
  41.     vertex[2] = cz;  
  42.     vertex[3] = 1.0;  
  43.     glVertex4fv(vertex);  
  44.     glEnd();  
  45. }  
[cpp]  view plain copy
  1. void mySolidCylinder( GLUquadric*   quad,  
  2.                      GLdouble base,  
  3.                      GLdouble top,  
  4.                      GLdouble height,  
  5.                      GLint slices,  
  6.                      GLint stacks )  
  7. {  
  8.     glColor3f(84.0/255, 0.0, 125.0/255.0);  
  9.     gluCylinder(quad, base, top, height, slices, stacks);  
  10.     //top  
  11.     DrawCircleArea(0.0, 0.0, height, top, slices);  
  12.     //base  
  13.     DrawCircleArea(0.0, 0.0, 0.0, base, slices);  
  14. }  
  15.   
  16. GLvoid DrawCircleArea(float cx, float cy, float cz, float r, int num_segments)  
  17. {  
  18.     GLfloat vertex[4];   
  19.   
  20.     const GLfloat delta_angle = 2.0*M_PI/num_segments;  
  21.     glBegin(GL_TRIANGLE_FAN);  
  22.   
  23.     vertex[0] = cx;  
  24.     vertex[1] = cy;  
  25.     vertex[2] = cz;  
  26.     vertex[3] = 1.0;          
  27.     glVertex4fv(vertex);  
  28.   
  29.     //draw the vertex on the contour of the circle  
  30.     for(int i = 0; i < num_segments ; i++)  
  31.     {  
  32.         vertex[0] = std::cos(delta_angle*i) * r + cx;  
  33.         vertex[1] = std::sin(delta_angle*i) * r + cy;  
  34.         vertex[2] = cz;  
  35.         vertex[3] = 1.0;  
  36.         glVertex4fv(vertex);  
  37.     }  
  38.   
  39.     vertex[0] = 1.0 * r + cx;  
  40.     vertex[1] = 0.0 * r + cy;  
  41.     vertex[2] = cz;  
  42.     vertex[3] = 1.0;  
  43.     glVertex4fv(vertex);  
  44.     glEnd();  
  45. }  

最终绘制出的圆柱体:

 

 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值