首页 > 解决方案 > 如何为球体的一部分生成几何图形

问题描述

生成渲染球体所需点的算法很少,但我找不到一个好的算法,也找不到一个完整的球体算法来为球体的一部分生成点。

假设我有最小/最大纬度、最小/最大经度和球体半径。如何生成能够渲染球体的这一部分的网格?

标签: opengl3dgeometry

解决方案


我制作了半球和半球网格的一部分。我稍微编辑了 songho Ahn 的源代码,您可以参考以下链接:

http://www.songho.ca/opengl/gl_sphere.html

主要方法class SpherebuildVertices*

您可以通过编辑此方法获得所需的结果。

我做了这样的半球:

void HemiSphere::buildVerticesSmooth()
{
...

float sectorStep = 2 *PI / sectorCount;
float stackStep = (PI / 2) / stackCount;
float sectorAngle, stackAngle;

for(int i = 0; i <= stackCount; ++i)
{
    stackAngle = -PI / 2 + i * stackStep;                   // starting from -pi/2 to 0
    xy = radius * cosf(stackAngle);             // r * cos(u)
    z = radius *( 1 + sinf(stackAngle) );              // r * sin(u)
...

在此处输入图像描述

我还制作了半球,切割了它的地板,这样它的底部就有了均匀的地板。为此,保持 xy 坐标不变,只需将 z 坐标编辑为 0。

void HemiSphere::buildVerticesSmooth()
{

....

float sectorStep = 2 *PI / sectorCount;
float stackStep = (PI / 2) / stackCount;
float sectorAngle, stackAngle;
float d = sqrt(radius*radius - flat_radius*flat_radius)/radius;

for(int i = 0; i <= stackCount; ++i)
{
    stackAngle = -PI / 2 + i * stackStep;                   // starting from -pi/2 to 0
    xy = radius * cosf(stackAngle);             // r * cos(u)
    z = radius *( d + sinf(stackAngle) );              // r * sin(u)
    if (z<0.0) z = 0.0;

....

希望这可以帮助。


推荐阅读