【问题标题】:OpenGL gaps within sphere球内的OpenGL间隙
【发布时间】:2014-12-08 22:26:53
【问题描述】:

我最近一直在尝试使用三角形在 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;
    }
}

问题

【问题讨论】:

  • screenToBuffer(v, 1); 是做什么的?
  • 将顶点发送到 VBO。
  • 渲染代码在哪里?
  • 点计算看起来不错。但除此之外,您还需要一个索引数组来定义正确的连接性,以便从这些点创建三角形。要么这样,要么你必须重复点。

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


【解决方案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

【讨论】:

  • 简单地用 OP 代码中显示的点绘制一个三角形条带看起来仍然同样错误。这些点的计算顺序不适合用三角形条带渲染球体。
  • 我只是假设他已经禁用了剔除面部。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
  • 1970-01-01
相关资源
最近更新 更多