百度文库的一篇文章,介绍了OpenGL超级宝典中经常用到的gltDrawTorus的实现原理,图文并茂,很不错。
原文链接:http://wenku.baidu.com/view/57545ef1fab069dc50220140.html
(本文链接位置:http://blog.csdn.net/wlsgzl/article/details/12835639)
读完这篇文章后,我也着手实现了一下,现贴出我的代码。
void DrawTorus(GLfloat fMajorRadius, GLfloat fMinorRadius, GLint nNumMajor, GLint nNumMinor) { GLfloat fr=(fMajorRadius-fMinorRadius)/2; GLfloat fR=fr+fMinorRadius; GLfloat fMajorStep=2.0f*PI/nNumMajor; GLfloat fMinorStep=2.0f*PI/nNumMinor; GLfloat fMajorAngle=0.0f; GLfloat fMajorAngle2=0.0f; GLfloat fMinorAngle=0.0f; GLfloat fMinorAngle2=0.0f; GLfloat fX=0.0f; GLfloat fY=0.0f; GLfloat fZ=0.0f; GLfloat fX2=0.0f; GLfloat fY2=0.0f; GLfloat fZ2=0.0f; glBegin(GL_TRIANGLE_STRIP); for (fMajorAngle=0.0f;fMajorAngle<=2.0*PI;fMajorAngle+=fMajorStep) { for (fMinorAngle=0.0f;fMinorAngle<=2.0f*PI;fMinorAngle+=fMinorStep) { fX=(fR+fr*cos(fMinorAngle))*cos(fMajorAngle); fY=(fR+fr*cos(fMinorAngle))*sin(fMajorAngle); fZ=fr*sin(fMinorAngle); glVertex3f(fX,fY,fZ); fMajorAngle2=fMajorAngle+fMajorStep; fMinorAngle2=fMinorAngle+fMinorStep; fX2=(fR+fr*cos(fMinorAngle2))*cos(fMajorAngle2); fY2=(fR+fr*cos(fMinorAngle2))*sin(fMajorAngle2); fZ2=fr*sin(fMinorAngle2); glVertex3f(fX2,fY2,fZ2); } } glEnd(); }