球体内的OpenGL差距

时间:2014-12-08 22:26:53

标签: c++ opengl 3d geometry glm-math

我最近一直在尝试使用三角形在OpenGL中渲染3D球体。我一直在测试和修改各种网站的代码,最终找到了一个成功的组合。唯一的问题是球体中存在明显的差距。关于会导致这种情况的任何想法?

渲染球体的代码

float Slices = 30;
float Stacks = 60;
float Radius = 20.0;
for (int i = 0; i <= Stacks; ++i){

    float V   = i / (float) Stacks;
    float phi = V * glm::pi <float> ();

    for (int j = 0; j <= Slices; ++j){

        float U = j / (float) Slices;
        float theta = U * (glm::pi <float> () * 4);

        float x = cosf (theta) * sinf (phi);
        float y = cosf (phi);
        float z = sinf (theta) * sinf (phi);
        x *= Radius;
        y *= Radius;
        z *= Radius;

        Vertex *v = new Vertex {{x,y,z},    //Position
                                {255,0,0}}; //Color
        screenToBuffer(v, 1);
        delete []v;
    }
}

问题 enter image description here

1 个答案:

答案 0 :(得分:-1)

尝试将其设置为GL_TRIANGLE_STRIP

可能的问题是它将每组三个顶点视为仅一个三角形。

喜欢这样

Indices:     0 1 2 3 4 5 ...
Triangles:  {0 1 2} {3 4 5}

GL_TRIAGLE_STRIP将执行此操作。

 Indices:     0 1 2 3 4 5 ... 
 Triangles:  {0 1 2}
               {1 2 3}  drawing order is (2 1 3) to maintain proper winding
                 {2 3 4}
                   {3 4 5}

请参阅此答案,了解正确的方法。 https://stackoverflow.com/a/7958376/1943599