【问题标题】:Determining a spheres vertices via polar coordinates, and rendering it通过极坐标确定球体顶点并渲染它
【发布时间】:2013-03-20 18:53:05
【问题描述】:

我正在 Android 设备上使用 OpenGL ES 2.0。

我正在尝试让一个球体启动并运行和绘图。 Currentley,我几乎有一个球体,但显然它做得非常非常错误。

在我的应用程序中,我保存了一个 Vector3 列表,在此过程中我将其转换为 ByteBuffer,然后传递给 OpenGL。 我知道我的代码没问题,因为我有一个立方体和四面体绘图 nicley。 我改变的两个部分是: 确定顶点 绘制顶点。

这里是有问题的代码片段。我究竟做错了什么? 确定极坐标:

private void ConstructPositionVertices()
{
    for (float latitutde = 0.0f; latitutde < (float)(Math.PI * 2.0f); latitutde += 0.1f)
    {
        for (float longitude = 0.0f; longitude < (float)(2.0f * Math.PI); longitude += 0.1f)
        {
            mPositionVertices.add(ConvertFromSphericalToCartesian(1.0f, latitutde, longitude));
        }
    }
}

从极坐标转换为笛卡尔坐标:

public static Vector3 ConvertFromSphericalToCartesian(float inLength, float inPhi, float inTheta)
{
    float x = inLength * (float)(Math.sin(inPhi) * Math.cos(inTheta));
    float y = inLength * (float)(Math.sin(inPhi) * Math.sin(inTheta));
    float z = inLength * (float)Math.cos(inTheta);
    Vector3 convertedVector = new Vector3(x, y, z);
    return convertedVector;
}

画圆:

inGL.glDrawArrays(GL10.GL_TRIANGLES, 0, numVertices);

显然我省略了一些代码,但我很肯定我的错误在于这些片段的某个地方。 我对这些点做的只是将它们传递给 OpenGL,然后调用 Triangles,它应该为我连接这些点.. 对吗?

编辑: 图片可能会很好!

【问题讨论】:

    标签: geometry rendering opengl-es-2.0 polar-coordinates


    【解决方案1】:

    您的 z 必须使用 phi 计算。 float z = inLength * (float)Math.cos(inPhi);

    另外,生成的点不是三角形,所以最好使用 GL_LINE_STRIP

    【讨论】:

    • 是的,我的 Z 错了。使用 Line Strip 绘图,它是一个球体,谢谢!现在我只需要弄清楚如何将这些顶点索引为三角形:x
    【解决方案2】:

    在 Polar sphere 上使用三角带就像成对画点一样简单,例如:

    const float GL_PI = 3.141592f;
    
    GLfloat x, y, z, alpha, beta; // Storage for coordinates and angles        
    GLfloat radius = 60.0f;
    const int gradation = 20;
    
    for (alpha = 0.0; alpha < GL_PI; alpha += GL_PI/gradation)
    {        
        glBegin(GL_TRIANGLE_STRIP);
        for (beta = 0.0; beta < 2.01*GL_PI; beta += GL_PI/gradation)            
        {            
            x = radius*cos(beta)*sin(alpha);
            y = radius*sin(beta)*sin(alpha);
            z = radius*cos(alpha);
            glVertex3f(x, y, z);
            x = radius*cos(beta)*sin(alpha + GL_PI/gradation);
            y = radius*sin(beta)*sin(alpha + GL_PI/gradation);
            z = radius*cos(alpha + GL_PI/gradation);            
            glVertex3f(x, y, z);            
        }        
        glEnd();
    }
    

    输入的第一个点如下公式,第二个点偏移α角的单步(从下一个平行点开始)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      • 2021-11-18
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 2011-12-12
      相关资源
      最近更新 更多