0

I have recently been trying to render a 3D sphere in OpenGL using triangles. I have been testing and modifying code from various websites and have finally found a winning combination. The only problem is that there are visible gaps in the sphere. Any thoughts on what would be causing this?

Code to render sphere

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;
    }
}

Problem enter image description here

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982

1 Answers1

-1

Try and set it to GL_TRIANGLE_STRIP​

What might be the problem is that it considers each group of three vertices to be only one triangle.

Like so

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

The GL_TRIAGLE_STRIP will do this.

 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}

See this answer for a proper way to do it. https://stackoverflow.com/a/7958376/1943599

Community
  • 1
  • 1
Mellester
  • 922
  • 7
  • 9
  • Simply drawing a triangle strip with the points shown in the OP's code would still look about equally wrong. The points are not calculated in an order that would be correct to render a sphere with triangle strips. – Reto Koradi Dec 09 '14 at 06:46
  • I just assumed he had cull face disabled. – Mellester Dec 09 '14 at 11:15