opengl实现任意两点间画圆柱体

1,问题提出

两点间画线简单:

glBegin(GL_LINES);  //注意是LINES不是LINE,这个错误一定要注意。

glVertexf(x1, y1, z1);

glVertexf(x2, y2, z2);

glEnd();

画线函数不会影响opengl的矩阵堆栈。

但是很多时候线条效果会比较差,比如我要做一个骨骼动画,关节点间的骨头用线条太难看,即使使用glLineWidth设置线宽,视觉效果还是一塌糊涂。还有利用分形绘制3D树的时候,树干用线条(宽线条)绘制效果也不佳。所以此时需要实现一个函数,3D空间中任意两点间用几何体绘制,我下面介绍一种思路。

2,原理介绍

要在A(x1,y1,z1), B(x2,y2,z2)之间绘制圆柱体,首先在原点处,沿着Y轴方向完成几何体绘制,然后旋转AB向量方向,最后平移到A点处。关键在旋转矩阵的计算,使用向量叉乘:AB向量和Y轴单位向量叉乘计算出右手side向量,然后side单位化,side和AB叉乘计算出最终的up方向。

代码如下:

[cpp]  view plain  copy
  1. void RenderBone(float x0, float y0, float z0, float x1, float y1, float z1 )  
  2. {  
  3.     GLdouble  dir_x = x1 - x0;  
  4.     GLdouble  dir_y = y1 - y0;  
  5.     GLdouble  dir_z = z1 - z0;  
  6.     GLdouble  bone_length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );  
  7.     static GLUquadricObj *  quad_obj = NULL;  
  8.     if ( quad_obj == NULL )  
  9.         quad_obj = gluNewQuadric();  
  10.     gluQuadricDrawStyle( quad_obj, GLU_FILL );  
  11.     gluQuadricNormals( quad_obj, GLU_SMOOTH );  
  12.     glPushMatrix();  
  13.     // 平移到起始点  
  14.     glTranslated( x0, y0, z0 );  
  15.     // 计算长度  
  16.     double  length;  
  17.     length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );  
  18.     if ( length < 0.0001 ) {   
  19.         dir_x = 0.0; dir_y = 0.0; dir_z = 1.0;  length = 1.0;  
  20.     }  
  21.     dir_x /= length;  dir_y /= length;  dir_z /= length;  
  22.     GLdouble  up_x, up_y, up_z;  
  23.     up_x = 0.0;  
  24.     up_y = 1.0;  
  25.     up_z = 0.0;  
  26.     double  side_x, side_y, side_z;  
  27.     side_x = up_y * dir_z - up_z * dir_y;  
  28.     side_y = up_z * dir_x - up_x * dir_z;  
  29.     side_z = up_x * dir_y - up_y * dir_x;  
  30.     length = sqrt( side_x*side_x + side_y*side_y + side_z*side_z );  
  31.     if ( length < 0.0001 ) {  
  32.         side_x = 1.0; side_y = 0.0; side_z = 0.0;  length = 1.0;  
  33.     }  
  34.     side_x /= length;  side_y /= length;  side_z /= length;  
  35.     up_x = dir_y * side_z - dir_z * side_y;  
  36.     up_y = dir_z * side_x - dir_x * side_z;  
  37.     up_z = dir_x * side_y - dir_y * side_x;  
  38.     // 计算变换矩阵  
  39.     GLdouble  m[16] = { side_x, side_y, side_z, 0.0,  
  40.         up_x,   up_y,   up_z,   0.0,  
  41.         dir_x,  dir_y,  dir_z,  0.0,  
  42.         0.0,    0.0,    0.0,    1.0 };  
  43.     glMultMatrixd( m );  
  44.     // 圆柱体参数  
  45.     GLdouble radius= 20;        // 半径  
  46.     GLdouble slices = 8.0;      //  段数  
  47.     GLdouble stack = 3.0;       // 递归次数  
  48.     gluCylinder( quad_obj, radius, radius, bone_length, slices, stack );   
  49.     glPopMatrix();  
  50. }  
 

上面的代码绘制圆柱体使用了glu几何库,如果绘制其他几何体:比如四棱锥,或其它几何体,只需要修改下面的框架:

[cpp]  view plain  copy
  1. void RenderBone(float x0, float y0, float z0, float x1, float y1, float z1 )  
  2. {  
  3.     GLdouble  dir_x = x1 - x0;  
  4.     GLdouble  dir_y = y1 - y0;  
  5.     GLdouble  dir_z = z1 - z0;  
  6.     GLdouble  bone_length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );  
  7.     glPushMatrix();  
  8.     // 平移到起始点  
  9.     glTranslated( x0, y0, z0 );  
  10.     // 计算长度  
  11.     double  length;  
  12.     length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );  
  13.     if ( length < 0.0001 ) {   
  14.         dir_x = 0.0; dir_y = 0.0; dir_z = 1.0;  length = 1.0;  
  15.     }  
  16.     dir_x /= length;  dir_y /= length;  dir_z /= length;  
  17.     GLdouble  up_x, up_y, up_z;  
  18.     up_x = 0.0;  
  19.     up_y = 1.0;  
  20.     up_z = 0.0;  
  21.     double  side_x, side_y, side_z;  
  22.     side_x = up_y * dir_z - up_z * dir_y;  
  23.     side_y = up_z * dir_x - up_x * dir_z;  
  24.     side_z = up_x * dir_y - up_y * dir_x;  
  25.     length = sqrt( side_x*side_x + side_y*side_y + side_z*side_z );  
  26.     if ( length < 0.0001 ) {  
  27.         side_x = 1.0; side_y = 0.0; side_z = 0.0;  length = 1.0;  
  28.     }  
  29.     side_x /= length;  side_y /= length;  side_z /= length;  
  30.     up_x = dir_y * side_z - dir_z * side_y;  
  31.     up_y = dir_z * side_x - dir_x * side_z;  
  32.     up_z = dir_x * side_y - dir_y * side_x;  
  33.     // 计算变换矩阵  
  34.     GLdouble  m[16] = { side_x, side_y, side_z, 0.0,  
  35.         up_x,   up_y,   up_z,   0.0,  
  36.         dir_x,  dir_y,  dir_z,  0.0,  
  37.         0.0,    0.0,    0.0,    1.0 };  
  38.     glMultMatrixd( m );  
  39.       
  40.     // 原点处向Y轴方向绘制几何体  
  41.     renderGeometryInYAxis();  
  42.     glPopMatrix();  
  43. }  
 

注意上面的renderGeometryInYAxis();必须是在Y轴上绘制几何体。

3,测试代码:

[cpp]  view plain  copy
  1. #include <gl/glut.h>  
  2. #include <cstdio>  
  3. #include <cstdlib>  
  4. #include <cmath>  
  5. void    init(void);  
  6. void    reshape(int w,int h);  
  7. void    display(void);  
  8.   
  9. void RenderBone(float x0, float y0, float z0, float x1, float y1, float z1 );  
  10. int main(int argc, char** argv)  
  11. {  
  12.     glutInit(&argc, argv);  
  13.     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);   
  14.     glutInitWindowSize (500, 500);  
  15.     glutInitWindowPosition (100, 100);  
  16.     glutCreateWindow("Sphere");  
  17.     init ();  
  18.     glutReshapeFunc(reshape);  
  19.     glutDisplayFunc(display);  
  20.     glutMainLoop();  
  21.     return 0;  
  22. }  
  23. void init (void)  
  24. {   
  25.     glClearColor (0.0, 0.0, 0.0, 0.0);  
  26.     glClearDepth(1);  
  27.     glShadeModel(GL_SMOOTH);  
  28.     GLfloat _ambient[]={1.0,1.0,1.0,1.0};  
  29.     GLfloat _diffuse[]={1.0,1.0,0.0,1.0};  
  30.     GLfloat _specular[]={1.0,1.0,1.0,1.0};  
  31.     GLfloat _position[]={200,200,200,0};  
  32.     glLightfv(GL_LIGHT0,GL_AMBIENT,_ambient);  
  33.     glLightfv(GL_LIGHT0,GL_DIFFUSE,_diffuse);  
  34.     glLightfv(GL_LIGHT0,GL_SPECULAR,_specular);  
  35.     glLightfv(GL_LIGHT0,GL_POSITION,_position);  
  36.     glEnable(GL_TEXTURE_2D);  
  37.     glEnable(GL_LIGHTING);  
  38.     glEnable(GL_LIGHT0);  
  39.     glEnable(GL_DEPTH_TEST);  
  40.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  
  41. }  
  42. void reshape(int w, int h)  
  43. {  
  44.     glViewport (0, 0, (GLsizei) w, (GLsizei) h);  
  45.     glMatrixMode(GL_PROJECTION);  
  46.     glLoadIdentity();  
  47.     glOrtho(0.0, 500, 0.0, 500, -500, 500);  
  48.     glMatrixMode(GL_MODELVIEW);  
  49.     glLoadIdentity();  
  50. }  
  51. void display(void)  
  52. {  
  53.     glMatrixMode(GL_MODELVIEW);  
  54.     glLoadIdentity();  
  55.     glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);  
  56.     glPushMatrix();  
  57.     {  
  58.         RenderBone(100, 100, 100, 200, 300, 500);  
  59.     } glPopMatrix();  
  60.   
  61.     glFlush();  
  62.     glutPostRedisplay();  
  63. }  
  64. void RenderBone(float x0, float y0, float z0, float x1, float y1, float z1 )  
  65. {  
  66.     GLdouble  dir_x = x1 - x0;  
  67.     GLdouble  dir_y = y1 - y0;  
  68.     GLdouble  dir_z = z1 - z0;  
  69.     GLdouble  bone_length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );  
  70.     static GLUquadricObj *  quad_obj = NULL;  
  71.     if ( quad_obj == NULL )  
  72.         quad_obj = gluNewQuadric();  
  73.     gluQuadricDrawStyle( quad_obj, GLU_FILL );  
  74.     gluQuadricNormals( quad_obj, GLU_SMOOTH );  
  75.     glPushMatrix();  
  76.     // 平移到起始点  
  77.     glTranslated( x0, y0, z0 );  
  78.     // 计算长度  
  79.     double  length;  
  80.     length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );  
  81.     if ( length < 0.0001 ) {   
  82.         dir_x = 0.0; dir_y = 0.0; dir_z = 1.0;  length = 1.0;  
  83.     }  
  84.     dir_x /= length;  dir_y /= length;  dir_z /= length;  
  85.     GLdouble  up_x, up_y, up_z;  
  86.     up_x = 0.0;  
  87.     up_y = 1.0;  
  88.     up_z = 0.0;  
  89.     double  side_x, side_y, side_z;  
  90.     side_x = up_y * dir_z - up_z * dir_y;  
  91.     side_y = up_z * dir_x - up_x * dir_z;  
  92.     side_z = up_x * dir_y - up_y * dir_x;  
  93.     length = sqrt( side_x*side_x + side_y*side_y + side_z*side_z );  
  94.     if ( length < 0.0001 ) {  
  95.         side_x = 1.0; side_y = 0.0; side_z = 0.0;  length = 1.0;  
  96.     }  
  97.     side_x /= length;  side_y /= length;  side_z /= length;  
  98.     up_x = dir_y * side_z - dir_z * side_y;  
  99.     up_y = dir_z * side_x - dir_x * side_z;  
  100.     up_z = dir_x * side_y - dir_y * side_x;  
  101.     // 计算变换矩阵  
  102.     GLdouble  m[16] = { side_x, side_y, side_z, 0.0,  
  103.         up_x,   up_y,   up_z,   0.0,  
  104.         dir_x,  dir_y,  dir_z,  0.0,  
  105.         0.0,    0.0,    0.0,    1.0 };  
  106.     glMultMatrixd( m );  
  107.     // 圆柱体参数  
  108.     GLdouble radius= 20;        // 半径  
  109.     GLdouble slices = 8.0;      //  段数  
  110.     GLdouble stack = 3.0;       // 递归次数  
  111.     gluCylinder( quad_obj, radius, radius, bone_length, slices, stack );   
  112.     glPopMatrix();  
  113. }  
 

最终效果图:

 

猜你喜欢

转载自blog.csdn.net/taiyang1987912/article/details/80776974