Incomplete OpenGL

I want to draw a sphere using VBO for vertex coordinates, colors and UV for texture. My problem is that the sphere is not "closed", there is a hole at the origin. I know this is because my code depends on (1 / segments) the distance between each vertex; I work with segments = 40. I know that if I raise this value, the hole will be lower, but the program will be slower. I do not know if there is a way to eliminate the hole without raising the variable.

Here is the code:

for(int i = 0; i <= segments; i++){

    double lat0 = pi * (-0.5 + (double)(i - 1) / segments);
    double z0 = sin(lat0);
    double zr0 = cos(lat0);
    // lat1 = [-pi/2..pi/2]
    double lat1 = pi * (-0.5 + (double)i / segments);
    double z1 = sin(lat1);
    double zr1 = cos(lat1);

    for (int j = 0; j <= segments; j++){    // Longitud
        // lng = [0..2*pi]
        double lng = 2 * pi * (double)(j - 1) / segments;
        double x = cos(lng);
        double y = sin(lng);
        //glNormal3f(x * zr0, y * zr0, z0); // Normals
        ballVerts.push_back(x * zr0); //X
        ballVerts.push_back(y * zr0); //Y
        ballVerts.push_back(z0);      //Z

        ballVerts.push_back(0.0f);
        ballVerts.push_back(0.0f);
        ballVerts.push_back(0.0f);
        ballVerts.push_back(1.0f); //R,G,B,A

        texX = abs(1 - (0.5f + atan2(z0, x * zr0) / (2.0 * pi)));
        texY = 0.5f - asin(y * zr0) / pi;
        ballVerts.push_back(texX);  // Texture coords
        ballVerts.push_back(texY);  // U, V


        //glNormal3f(x * zr1, y * zr1, z1); //Normals
        ballVerts.push_back(x * zr1); //X
        ballVerts.push_back(y * zr1); //Y
        ballVerts.push_back(z1);      //Z
        ballVerts.push_back(0.0f);
        ballVerts.push_back(0.0f);
        ballVerts.push_back(1.0f);
        ballVerts.push_back(1.0f); //R,G,B,A

        texX = abs(1 - (0.5f + atan2(z1, x * zr1) / (2.0 * pi)));
        texY = 0.5f - asin(y * zr1) / pi;
        ballVerts.push_back(texX);  // Texture coords
        ballVerts.push_back(texY);

    }
}
// Create VBO....

And this is the result that I have:

enter image description here

+4
source share
1 answer

, . , :

for(int i = 0; i <= segments; i++){
    double lat0 = pi * (-0.5 + (double)(i - 1) / segments);

i = 0 -0.5 * pi, , .

segments, segments . , 0 segments, segments + 1 .

- 1:

for(int i = 1; i <= segments; i++){
    double lat0 = pi * (-0.5 + (double)(i - 1) / segments);

, , 0 . :

for(int i = 0; i < segments; i++){
    double lat0 = pi * (-0.5 + (double) / segments);
    ...
    double lat1 = pi * (-0.5 + (double)(i + 1) / segments);
+2

All Articles