I would like to create a sphere with variable tesselation (in both horizontal and vertical aspect), this is my algorithm:
const int amount = (sectorCount + 1) * (stackCount + 1);
M3DVector3f *verts = (M3DVector3f*)malloc(sizeof(float) * 3 * amount);
M3DVector4f *colors = (M3DVector4f*)malloc(sizeof(float) * 4 * amount);
float sectorStep = 2 * GL_PI / sectorCount;
float stackStep = GL_PI / stackCount;
float sectorAngle, stackAngle;
int vCount = 0;
for (int i = 0; i <= stackCount; ++i)
{
stackAngle = GL_PI / 2 - i * stackStep; // starting from pi/2 to -pi/2
float xy = radius * cosf(stackAngle); // r * cos(u)
float z = radius * sinf(stackAngle); // r * sin(u)
// add (sectorCount+1) vertices per stack
// the first and last vertices have same position and normal, but different tex coords
for (int j = 0; j <= sectorCount; ++j)
{
sectorAngle = j * sectorStep; // starting from 0 to 2pi
// vertex position (x, y, z)
float x = xy * cosf(sectorAngle); // r * cos(u) * cos(v)
float y = xy * sinf(sectorAngle); // r * cos(u) * sin(v)
m3dLoadVector3(verts[vCount], x, y, z);
m3dLoadVector4(colors[vCount], 0, 1, 0, 1);
vCount++;
}
}
This code works fine in terms of correct vertice coordinates.
Unfortunately the vertices aren't procuded in "correct order" so that GL_TRIANGLE_STRIP (or any other kind of triangle placement) produces a smooth sphere, it more looks like this (see attached images).
Unfortunately I also have no clue about how to change the algorithm to make GL_TRIANGLE_STRIP work. Any ideas?