绘制球体时缺少三角形

时间:2014-04-25 15:59:01

标签: c++ opengl 3d geometry

我试图用openGL绘制一个球体,但我无法找到我的错误...

只绘制了一半的三角形,如here in the picture

到目前为止,这是我的算法:

// The angle step used in iteration
float a = (2.0f*M_PI)/8.0;

float c = 0.0f;

for (float theta = 0.0f; theta < 2.0f*M_PI; theta += a, c += a/2.0f)

for (float phi = 0.0f; phi < 2.0f*M_PI; phi += a) {
    // Here something is missing...

    glBegin(GL_TRIANGLES);

    float p_1[3] = {sin(theta)*cos(phi+c), 
    sin(theta)*sin(phi+c), 
    cos(theta)};
    glVertex3f(p_1[0], p_1[1], p_1[2]);

    float p_3[3] = {sin(theta+a)*cos(phi+c+a/2.0f), 
    sin(theta+a)*sin(phi+c+a/2.0f), 
    cos(theta+a)};
    glVertex3f(p_3[0], p_3[1], p_3[2]);


    float p_2[3] = {sin(theta)*cos(phi+c+a), 
    sin(theta)*sin(phi+c+a), 
    cos(theta)};
    glVertex3f(p_2[0], p_2[1], p_2[2]);   

    glEnd();
}

2 个答案:

答案 0 :(得分:0)

可能是你的三角形有一半的方向错误吗? http://math.hws.edu/graphicsnotes/c3/s2.html

答案 1 :(得分:0)

此代码有几个问题:

  1. 对循环条件使用浮点测试。请记住,浮点计算并不准确。你有大约50%的机会比你想要的更多次地完成循环。
  2. 使用标准球面坐标时,theta的范围应为0..pi,而不是0..2 * pi。
  3. 您需要在循环内生成四边形,而不是三角形。想象一张地图。如果你看两条经度线和两条纬度线之间的区域,它有4个角。
  4. 我不明白你的价值c在做什么。不确定这是否是一个问题,或者我还没有得到它。
  5. 不是正确性问题,但您可以将glBeginglEnd来电置于循环之外。
相关问题