在不使用gluSphere()的情况下在OpenGL中绘制Sphere?

是否有任何教程可以解释如何无需使用OpenGL在OpenGL中绘制球体gluSphere()?


OpenGL的许多3D教程都在立方体上。我已经搜索过,但是大多数使用绘制球体的解决方案都可以使用gluSphere()。还有具有代码在绘制球体网站这个网站,但它并不能解释背后绘制球体数学。我还有其他版本的如何在多边形中而不是在该链接中以四边形绘制球体。但是同样,我不明白如何用代码绘制球体。我希望能够进行可视化,以便在需要时可以修改球体。


UYOU
浏览 699回答 3
3回答

MYYA

快速解释了示例中的代码。您应该研究一下功能void drawSphere(double r, int lats, int longs):void drawSphere(double r, int lats, int longs) {&nbsp; &nbsp; int i, j;&nbsp; &nbsp; for(i = 0; i <= lats; i++) {&nbsp; &nbsp; &nbsp; &nbsp; double lat0 = M_PI * (-0.5 + (double) (i - 1) / lats);&nbsp; &nbsp; &nbsp; &nbsp; double z0&nbsp; = sin(lat0);&nbsp; &nbsp; &nbsp; &nbsp; double zr0 =&nbsp; cos(lat0);&nbsp; &nbsp; &nbsp; &nbsp; double lat1 = M_PI * (-0.5 + (double) i / lats);&nbsp; &nbsp; &nbsp; &nbsp; double z1 = sin(lat1);&nbsp; &nbsp; &nbsp; &nbsp; double zr1 = cos(lat1);&nbsp; &nbsp; &nbsp; &nbsp; glBegin(GL_QUAD_STRIP);&nbsp; &nbsp; &nbsp; &nbsp; for(j = 0; j <= longs; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double lng = 2 * M_PI * (double) (j - 1) / longs;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double x = cos(lng);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double y = sin(lng);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glNormal3f(x * zr0, y * zr0, z0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glVertex3f(r * x * zr0, r * y * zr0, r * z0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glNormal3f(x * zr1, y * zr1, z1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glVertex3f(r * x * zr1, r * y * zr1, r * z1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; glEnd();&nbsp; &nbsp; }}参数lat定义了您的球体中要有多少条水平线以及lon几条垂直线。r是球体的半径。现在在lat/上进行了两次迭代,lon并使用简单的三角函数计算了顶点坐标。现在计算出的顶点利用发送到您的GPU glVertex...()的GL_QUAD_STRIP,这意味着你要发送形成与先前2派四每两个顶点。现在,您只需要了解三角函数的工作方式即可,但是我想您可以轻松地弄清楚。
打开App,查看更多内容
随时随地看视频慕课网APP