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方向。
代码如下:
- void RenderBone(float x0, float y0, float z0, float x1, float y1, float z1 )
- {
- GLdouble dir_x = x1 - x0;
- GLdouble dir_y = y1 - y0;
- GLdouble dir_z = z1 - z0;
- GLdouble bone_length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );
- static GLUquadricObj * quad_obj = NULL;
- if ( quad_obj == NULL )
- quad_obj = gluNewQuadric();
- gluQuadricDrawStyle( quad_obj, GLU_FILL );
- gluQuadricNormals( quad_obj, GLU_SMOOTH );
- glPushMatrix();
- // 平移到起始点
- glTranslated( x0, y0, z0 );
- // 计算长度
- double length;
- length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );
- if ( length < 0.0001 ) {
- dir_x = 0.0; dir_y = 0.0; dir_z = 1.0; length = 1.0;
- }
- dir_x /= length; dir_y /= length; dir_z /= length;
- GLdouble up_x, up_y, up_z;
- up_x = 0.0;
- up_y = 1.0;
- up_z = 0.0;
- double side_x, side_y, side_z;
- side_x = up_y * dir_z - up_z * dir_y;
- side_y = up_z * dir_x - up_x * dir_z;
- side_z = up_x * dir_y - up_y * dir_x;
- length = sqrt( side_x*side_x + side_y*side_y + side_z*side_z );
- if ( length < 0.0001 ) {
- side_x = 1.0; side_y = 0.0; side_z = 0.0; length = 1.0;
- }
- side_x /= length; side_y /= length; side_z /= length;
- up_x = dir_y * side_z - dir_z * side_y;
- up_y = dir_z * side_x - dir_x * side_z;
- up_z = dir_x * side_y - dir_y * side_x;
- // 计算变换矩阵
- GLdouble m[16] = { side_x, side_y, side_z, 0.0,
- up_x, up_y, up_z, 0.0,
- dir_x, dir_y, dir_z, 0.0,
- 0.0, 0.0, 0.0, 1.0 };
- glMultMatrixd( m );
- // 圆柱体参数
- GLdouble radius= 20; // 半径
- GLdouble slices = 8.0; // 段数
- GLdouble stack = 3.0; // 递归次数
- gluCylinder( quad_obj, radius, radius, bone_length, slices, stack );
- glPopMatrix();
- }
上面的代码绘制圆柱体使用了glu几何库,如果绘制其他几何体:比如四棱锥,或其它几何体,只需要修改下面的框架:
- void RenderBone(float x0, float y0, float z0, float x1, float y1, float z1 )
- {
- GLdouble dir_x = x1 - x0;
- GLdouble dir_y = y1 - y0;
- GLdouble dir_z = z1 - z0;
- GLdouble bone_length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );
- glPushMatrix();
- // 平移到起始点
- glTranslated( x0, y0, z0 );
- // 计算长度
- double length;
- length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );
- if ( length < 0.0001 ) {
- dir_x = 0.0; dir_y = 0.0; dir_z = 1.0; length = 1.0;
- }
- dir_x /= length; dir_y /= length; dir_z /= length;
- GLdouble up_x, up_y, up_z;
- up_x = 0.0;
- up_y = 1.0;
- up_z = 0.0;
- double side_x, side_y, side_z;
- side_x = up_y * dir_z - up_z * dir_y;
- side_y = up_z * dir_x - up_x * dir_z;
- side_z = up_x * dir_y - up_y * dir_x;
- length = sqrt( side_x*side_x + side_y*side_y + side_z*side_z );
- if ( length < 0.0001 ) {
- side_x = 1.0; side_y = 0.0; side_z = 0.0; length = 1.0;
- }
- side_x /= length; side_y /= length; side_z /= length;
- up_x = dir_y * side_z - dir_z * side_y;
- up_y = dir_z * side_x - dir_x * side_z;
- up_z = dir_x * side_y - dir_y * side_x;
- // 计算变换矩阵
- GLdouble m[16] = { side_x, side_y, side_z, 0.0,
- up_x, up_y, up_z, 0.0,
- dir_x, dir_y, dir_z, 0.0,
- 0.0, 0.0, 0.0, 1.0 };
- glMultMatrixd( m );
- // 原点处向Y轴方向绘制几何体
- renderGeometryInYAxis();
- glPopMatrix();
- }
注意上面的renderGeometryInYAxis();必须是在Y轴上绘制几何体。
3,测试代码:
- #include <gl/glut.h>
- #include <cstdio>
- #include <cstdlib>
- #include <cmath>
- void init(void);
- void reshape(int w,int h);
- void display(void);
- void RenderBone(float x0, float y0, float z0, float x1, float y1, float z1 );
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize (500, 500);
- glutInitWindowPosition (100, 100);
- glutCreateWindow("Sphere");
- init ();
- glutReshapeFunc(reshape);
- glutDisplayFunc(display);
- glutMainLoop();
- return 0;
- }
- void init (void)
- {
- glClearColor (0.0, 0.0, 0.0, 0.0);
- glClearDepth(1);
- glShadeModel(GL_SMOOTH);
- GLfloat _ambient[]={1.0,1.0,1.0,1.0};
- GLfloat _diffuse[]={1.0,1.0,0.0,1.0};
- GLfloat _specular[]={1.0,1.0,1.0,1.0};
- GLfloat _position[]={200,200,200,0};
- glLightfv(GL_LIGHT0,GL_AMBIENT,_ambient);
- glLightfv(GL_LIGHT0,GL_DIFFUSE,_diffuse);
- glLightfv(GL_LIGHT0,GL_SPECULAR,_specular);
- glLightfv(GL_LIGHT0,GL_POSITION,_position);
- glEnable(GL_TEXTURE_2D);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_DEPTH_TEST);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
- }
- void reshape(int w, int h)
- {
- glViewport (0, 0, (GLsizei) w, (GLsizei) h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0, 500, 0.0, 500, -500, 500);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void display(void)
- {
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- {
- RenderBone(100, 100, 100, 200, 300, 500);
- } glPopMatrix();
- glFlush();
- glutPostRedisplay();
- }
- void RenderBone(float x0, float y0, float z0, float x1, float y1, float z1 )
- {
- GLdouble dir_x = x1 - x0;
- GLdouble dir_y = y1 - y0;
- GLdouble dir_z = z1 - z0;
- GLdouble bone_length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );
- static GLUquadricObj * quad_obj = NULL;
- if ( quad_obj == NULL )
- quad_obj = gluNewQuadric();
- gluQuadricDrawStyle( quad_obj, GLU_FILL );
- gluQuadricNormals( quad_obj, GLU_SMOOTH );
- glPushMatrix();
- // 平移到起始点
- glTranslated( x0, y0, z0 );
- // 计算长度
- double length;
- length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );
- if ( length < 0.0001 ) {
- dir_x = 0.0; dir_y = 0.0; dir_z = 1.0; length = 1.0;
- }
- dir_x /= length; dir_y /= length; dir_z /= length;
- GLdouble up_x, up_y, up_z;
- up_x = 0.0;
- up_y = 1.0;
- up_z = 0.0;
- double side_x, side_y, side_z;
- side_x = up_y * dir_z - up_z * dir_y;
- side_y = up_z * dir_x - up_x * dir_z;
- side_z = up_x * dir_y - up_y * dir_x;
- length = sqrt( side_x*side_x + side_y*side_y + side_z*side_z );
- if ( length < 0.0001 ) {
- side_x = 1.0; side_y = 0.0; side_z = 0.0; length = 1.0;
- }
- side_x /= length; side_y /= length; side_z /= length;
- up_x = dir_y * side_z - dir_z * side_y;
- up_y = dir_z * side_x - dir_x * side_z;
- up_z = dir_x * side_y - dir_y * side_x;
- // 计算变换矩阵
- GLdouble m[16] = { side_x, side_y, side_z, 0.0,
- up_x, up_y, up_z, 0.0,
- dir_x, dir_y, dir_z, 0.0,
- 0.0, 0.0, 0.0, 1.0 };
- glMultMatrixd( m );
- // 圆柱体参数
- GLdouble radius= 20; // 半径
- GLdouble slices = 8.0; // 段数
- GLdouble stack = 3.0; // 递归次数
- gluCylinder( quad_obj, radius, radius, bone_length, slices, stack );
- glPopMatrix();
- }
最终效果图: