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

所属分类: 软件编程 / C 语言 阅读数: 97
收藏 0 赞 0 分享

本文实例为大家分享了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(); 
}

 最终效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

用标准c++实现string与各种类型之间的转换

这个类在头文件中定义, < sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。另外,每个类都有一个对应的宽字符集版本
收藏 0 赞 0 分享

C++如何通过ostringstream实现任意类型转string

再使用整型转string的时候感觉有点棘手,因为itoa不是标准C里面的,而且即便是有itoa,其他类型转string不是很方便。后来去网上找了一下,发现有一个好方法
收藏 0 赞 0 分享

C/C++指针小结

要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的类型,指针的值或者叫指针所指向的内存区,还有指针本身所占据的内存区
收藏 0 赞 0 分享

C++ 类的静态成员深入解析

在C++中类的静态成员变量和静态成员函数是个容易出错的地方,本文先通过几个例子来总结静态成员变量和成员函数使用规则,再给出一个实例来加深印象
收藏 0 赞 0 分享

C++类的静态成员初始化详细讲解

通常静态数据成员在类声明中声明,在包含类方法的文件中初始化.初始化时使用作用域操作符来指出静态成员所属的类.但如果静态成员是整型或是枚举型const,则可以在类声明中初始化
收藏 0 赞 0 分享

C++类静态成员与类静态成员函数详解

静态成员不可在类体内进行赋值,因为它是被所有该类的对象所共享的。你在一个对象里给它赋值,其他对象里的该成员也会发生变化。为了避免混乱,所以不可在类体内进行赋值
收藏 0 赞 0 分享

C++中的friend友元函数详细解析

友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类。友元函数的特点是能够访问类中的私有成员的非成员函数。友元函数从语法上看,它与普通函数一样,即在定义上和调用上与普通函数一样
收藏 0 赞 0 分享

static全局变量与普通的全局变量的区别详细解析

以下是对static全局变量与普通的全局变量的区别进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助
收藏 0 赞 0 分享

C++ explicit关键字的应用方法详细讲解

C++ explicit关键字用来修饰类的构造函数,表明该构造函数是显式的,既然有"显式"那么必然就有"隐式",那么什么是显示而什么又是隐式的呢?下面就让我们一起来看看这方面的知识吧
收藏 0 赞 0 分享

教你5分钟轻松搞定内存字节对齐

随便google一下,人家就可以跟你解释的,一大堆的道理,我们没怎么多时间,讨论为何要对齐.直入主题,怎么判断内存对齐规则,sizeof的结果怎么来的,请牢记以下3条原则
收藏 0 赞 0 分享
查看更多