本文實(shí)例為大家分享了opengl實(shí)現(xiàn)任意兩點(diǎn)間畫(huà)圓柱體的具體代碼,供大家參考,具體內(nèi)容如下
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:主機(jī)域名、虛擬主機(jī)、營(yíng)銷軟件、網(wǎng)站建設(shè)、寧陜網(wǎng)站維護(hù)、網(wǎng)站推廣。
1、問(wèn)題提出
兩點(diǎn)間畫(huà)線簡(jiǎn)單:
glBegin(GL_LINES); //注意是LINES不是LINE,這個(gè)錯(cuò)誤一定要注意。
glVertexf(x1, y1, z1);
glVertexf(x2, y2, z2);
glEnd();
畫(huà)線函數(shù)不會(huì)影響opengl的矩陣堆棧。
但是很多時(shí)候線條效果會(huì)比較差,比如我要做一個(gè)骨骼動(dòng)畫(huà),關(guān)節(jié)點(diǎn)間的骨頭用線條太難看,即使使用glLineWidth設(shè)置線寬,視覺(jué)效果還是一塌糊涂。還有利用分形繪制3D樹(shù)的時(shí)候,樹(shù)干用線條(寬線條)繪制效果也不佳。所以此時(shí)需要實(shí)現(xiàn)一個(gè)函數(shù),3D空間中任意兩點(diǎn)間用幾何體繪制,我下面介紹一種思路。
2、原理介紹
要在A(x1,y1,z1), B(x2,y2,z2)之間繪制圓柱體,首先在原點(diǎn)處,沿著Y軸方向完成幾何體繪制,然后旋轉(zhuǎn)到AB向量方向,最后平移到A點(diǎn)處。關(guān)鍵在旋轉(zhuǎn)矩陣的計(jì)算,使用向量叉乘:AB向量和Y軸單位向量叉乘計(jì)算出右手side向量,然后side單位化,side和AB叉乘計(jì)算出最終的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(); // 平移到起始點(diǎn) glTranslated( x0, y0, z0 ); // 計(jì)算長(zhǎng)度 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; // 計(jì)算變換矩陣 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 ); // 圓柱體參數(shù) GLdouble radius= 20; // 半徑 GLdouble slices = 8.0; // 段數(shù) GLdouble stack = 3.0; // 遞歸次數(shù) gluCylinder( quad_obj, radius, radius, bone_length, slices, stack ); glPopMatrix(); }
上面的代碼繪制圓柱體使用了glu幾何庫(kù),如果繪制其他幾何體:比如四棱錐,或其它幾何體,只需要修改下面的框架:
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(); // 平移到起始點(diǎn) glTranslated( x0, y0, z0 ); // 計(jì)算長(zhǎng)度 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; // 計(jì)算變換矩陣 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 ); // 原點(diǎn)處向Y軸方向繪制幾何體 renderGeometryInYAxis(); glPopMatrix(); }
注意上面的renderGeometryInYAxis();必須是在Y軸上繪制幾何體。
3、測(cè)試代碼:
#include#include #include #include 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(); // 平移到起始點(diǎn) glTranslated( x0, y0, z0 ); // 計(jì)算長(zhǎng)度 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; // 計(jì)算變換矩陣 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 ); // 圓柱體參數(shù) GLdouble radius= 20; // 半徑 GLdouble slices = 8.0; // 段數(shù) GLdouble stack = 3.0; // 遞歸次數(shù) gluCylinder( quad_obj, radius, radius, bone_length, slices, stack ); glPopMatrix(); }
最終效果圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。