I see you're generating a UV sphere by building vertices and indices, but there are some issues with your indices generation, especially for the middle bands and the bottom fan. From your description, it looks like the problem is mainly with the indexing logic and how the connections between vertices are computed.
Let's go over the issues step-by-step and fix the code for indices generation:
1. Vertex Layout Summary
- Top pole vertex: index
0
- Intermediate stacks: indices from
1 to (stacks - 1) * slices (because for each stack except poles, we add slices vertices)
- Bottom pole vertex: index
(stacks - 1) * slices + 1
2. Fixing the vertex positioning (your current code snippet):
// Top pole
sphereVertices.push_back({ { 0.0f, 0.0f, radius }, 0.0f, {0.0f, 0.0f} });
// Intermediate stacks
for (int i = 1; i < stacks; ++i) {
float theta = PI * i / stacks;
float sinTheta = sin(theta);
float cosTheta = cos(theta);
for (int j = 0; j < slices; ++j) {
float phi = 2 * PI * j / slices;
float x = radius * sinTheta * cos(phi);
float y = radius * sinTheta * sin(phi);
float z = radius * cosTheta;
sphereVertices.push_back({ { x, y, z }, 0.0f, {0.0f, 0.0f} });
}
}
// Bottom pole
sphereVertices.push_back({ { 0.0f, 0.0f, -radius }, 0.0f, {0.0f, 0.0f} });
You had swapped x and y in your original code (using sin/cos wrongly).
3. Fixing Index Generation
unsigned int topPole = 0;
unsigned int bottomPole = (stacks - 1) * slices + 1;
// Top fan
for (unsigned int i = 0; i < slices; ++i) {
unsigned int next = (i + 1) % slices;
// Triangle connects top pole to top ring
indices.push_back(topPole);
indices.push_back(i + 1);
indices.push_back(next + 1);
}
// Middle bands
for (unsigned int stack = 0; stack < stacks - 2; ++stack) { // stacks-2 because excluding poles
for (unsigned int slice = 0; slice < slices; ++slice) {
unsigned int current = 1 + stack * slices + slice;
unsigned int next = current + slices;
unsigned int nextSlice = (slice + 1) % slices;
unsigned int c1 = current;
unsigned int c2 = 1 + stack * slices + nextSlice;
unsigned int c3 = next;
unsigned int c4 = next + nextSlice - slice; // careful here, you want c4 = next + nextSlice
// Correct c4 computation:
c4 = next + nextSlice;
// Triangle 1
indices.push_back(c1);
indices.push_back(c2);
indices.push_back(c3);
// Triangle 2
indices.push_back(c2);
indices.push_back(c4);
indices.push_back(c3);
}
}
// Bottom fan
unsigned int bottomStart = 1 + (stacks - 2) * slices;
for (unsigned int i = 0; i < slices; ++i) {
unsigned int next = (i + 1) % slices;
indices.push_back(bottomPole);
indices.push_back(bottomStart + next);
indices.push_back(bottomStart + i);
}
Explanation of fixes:
-
Vertex coords: your initial calculation of x and y was swapped (x was assigned sine and y cosine but in spherical coordinates, usually x = r sinθ cosφ and y = r sinθ sinφ).
-
Top fan (cap): vertex indices from 1 to slices are the top ring. Your order in the triangle should be topPole, current, next, so counter-clockwise ordering is preserved.
-
Middle bands: The error was in computing c2 and c4. Using current + nextSlice - slice is problematic. The correct way is:
c1 = current
c2 = first stack's vertex at next slice = 1 + stack * slices + nextSlice
c3 = next stack's vertex at slice = current + slices
c4 = next stack's vertex at next slice = next + nextSlice
-
Bottom fan: similarly, must be wound correctly to preserve face orientation. The order of indices should be consistent. I swapped bottomStart + i and bottomStart + next in the order to fix the winding.
Full corrected example (indices generation only):
unsigned int topPole = 0;
unsigned int bottomPole = (stacks - 1) * slices + 1;
// Top fan
for (unsigned int i = 0; i < slices; ++i) {
unsigned int next = (i + 1) % slices;
indices.push_back(topPole);
indices.push_back(i + 1);
indices.push_back(next + 1);
}
// Middle bands
for (unsigned int stack = 0; stack < stacks - 2; ++stack) {
for (unsigned int slice = 0; slice < slices; ++slice) {
unsigned int current = 1 + stack * slices + slice;
unsigned int next = current + slices;
unsigned int nextSlice = (slice + 1) % slices;
unsigned int c1 = current;
unsigned int c2 = 1 + stack * slices + nextSlice;
unsigned int c3 = next;
unsigned int c4 = next + nextSlice;
// Triangle 1
indices.push_back(c1);
indices.push_back(c2);
indices.push_back(c3);
// Triangle 2
indices.push_back(c2);
indices.push_back(c4);
indices.push_back(c3);
}
}
// Bottom fan
unsigned int bottomStart = 1 + (stacks - 2) * slices;
for (unsigned int i = 0; i < slices; ++i) {
unsigned int next = (i + 1) % slices;
indices.push_back(bottomPole);
indices.push_back(bottomStart + next);
indices.push_back(bottomStart + i);
}
Additional pointers:
-
Make sure stacks and slices are at least 3 or more for meaningful sphere geometry.
-
Check winding order (counter-clockwise or clockwise) based on your rendering requirements; if the sphere appears inside out, swap the order of indices in triangles.
-
You might want to generate proper UV coordinates and normals if you use the sphere for lighting/texturing.
Let me know if you want me to provide a full working example or any additional help!