Théo
Théo

Reputation: 13

Generating spheres with vertices and indices?

I'm currently working on an OpenGL project where I should currently generate spheres with vertices and indices.

All vertices represent a point, and indices tells the graphic card to link 3 points as a triangle. Example : indices : {0,1,2} = it will make a triangle with the first, second and third point.

I've managed to make a UV sphere with correct vertices, but I don't know how I can get indices.

Here is my current result : Result of the sphere

And here is my code :

Mesh ObjectFactory::createSphere() {

    // Texture loaded and binded to the shaderprogram
    Texture textures[]{
        Texture("resources/pop_cat.png", "diffuse", 0, GL_RGBA, GL_UNSIGNED_BYTE),
    };

    int numHorizontalSegments = 20;
    int numVerticalSegments = 20;
    Vertex vertices[numVerticalSegments * numVerticalSegments] = {};
    GLuint indices[numVerticalSegments * numVerticalSegments] = {};
    int i = 0;

    for (int h = 0; h < numHorizontalSegments; h++) {
        float angle1 = (h + 1) * M_PI / (numHorizontalSegments + 1);
        for (int v = 0; v < numVerticalSegments; v++) {
            i++;
            float angle2 = v * (2 * M_PI) / numVerticalSegments;
            float x = sinf(angle1) * cosf(angle2);
            float y = cosf(angle1);
            float z = sinf(angle1) * sinf(angle2);
            vertices[i] = Vertex{glm::vec3(x, y, z), glm::vec3(0.83f, 0.70f, 0.44f), glm::vec2(0.0f, 0.0f)};

            indices[i] = i;
        }
    }

    // Store mesh data in vectors for the mesh
    std::vector<Vertex> verts(vertices, vertices + sizeof(vertices) / sizeof(Vertex));
    std::vector<GLuint> ind(indices, indices + sizeof(indices) / sizeof(GLuint));
    std::vector<Texture> tex(textures, textures + sizeof(textures) / sizeof(Texture));
    // Create sphere mesh
    return {verts, ind, tex};
}

Thank you a lot for your help !

Upvotes: 1

Views: 1306

Answers (1)

Yunnosch
Yunnosch

Reputation: 26763

You have created a sphere of vertices by calculating horizontal circles in multiple vertical layers, a UV sphere. Good.

You are just adding indexes once for each vertex for a total of one index per vertex, that is not according to the concept.
What you need to repeatedy do is finding the three indexes in your array of vertices, which make a usable triangle.
Among other things it means that you will name the same vertex index multiple times. Mostly six times, because most of your vertices will be part of six triangles. At least one to the "upper left", one towards the "upper right", "lower left", "lower right"; while there are usually two double directions; e.g. two triangles to the upper right.
"mostly six", because of edge cases like the "north pole" and "south pole"; which participate in many triangles and quads.

Lets looks at a part of your UV sphere:

      V03----------V02----------V01---------V00 
       |            |        __/ |        __/|
       |            |     __/    |  c  __/   |
       |            |  __/       |  __/      |
       |            | /        f | /     d   |
      V13----------V12----------V11----------V10 
       |            |        __/ | e      __/|
       |            |  a  __/    |     __/   |
       |            |  __/       |  __/      |
       |            | /      b   | /         |
      V23----------V22----------V21----------V20 
       |            |            |           |
       |            |            |           |
       |            |            |           |
       |            |            |           |
      V33----------V32----------V31---------V30 

You can see that for the quad in the middle (represented by two triangles "a" and "b"),
you need six index entries, though it only has 4 of the vertices, and each of the vertices will be used even more often, from the other touching quads.

For the triangle "a" you get indexes for the vertexes V11, V12, V22 (mind the orientation, depending on where you want the surface, thinking either always "counter clock" or always "clockwise" will get you where you only need a few tries to get the desired result).
For the triangle "b" you get indexes for the vertexes V11, V22, V21.

Also, the vertex V11 will have to be index again for the triangle "c" and "d", and "e", and "f"; for participating in six triangles or four quads.

You managed to do your UV sphere fine, so I do not think that I need to provide the loop and selection code for getting that done. You managed to visualise the result of your UV sphere, just try and retry after checking the result.

Upvotes: 1

Related Questions