Sphere generation and calculating indices windings

Started by
0 comments, last by MARS_999 11 years, 8 months ago
Here is the code I am using, it produces a sphere, but the indices are not correct as I have a rendering error with a quad tearing off the sphere and can only assume the indices are incorrect?

Thanks!


class SolidSphere
{
protected:
std::vector<GLfloat> vertices, normals, texcoords;
std::vector<GLuint> indices;
public:
SolidSphere(){}
~SolidSphere(){}
void Generate(float radius, int rings, int sectors)
{
float const R = 1.0f/(float)(rings-1);
float const S = 1.0f/(float)(sectors-1);
int r, s;
vertices.resize(rings * sectors * 3);
normals.resize(rings * sectors * 3);
texcoords.resize(rings * sectors * 2);
std::vector<GLfloat>::iterator v = vertices.begin();
std::vector<GLfloat>::iterator n = normals.begin();
std::vector<GLfloat>::iterator t = texcoords.begin();
for(r = 0; r < rings; ++r)
for(s = 0; s < sectors; ++s)
{
float const y = sinf( -M_PI_2 + M_PI * r * R);
float const x = cosf(2*M_PI * s * S) * sinf(M_PI * r * R);
float const z = sinf(2*M_PI * s * S) * sinf(M_PI * r * R);

*t++ = s*S;
*t++ = r*R;
*v++ = x * radius;
*v++ = y * radius;
*v++ = z * radius;
*n++ = x;
*n++ = y;
*n++ = z;
}
indices.resize(rings * sectors * 4);
std::vector<GLuint>::iterator i = indices.begin();
for(r = 0; r < rings; ++r)
for(s = 0; s < sectors; ++s)
{
*i++ = r * sectors + s;
*i++ = r * sectors + (s+1);
*i++ = (r+1) * sectors + (s+1);
*i++ = (r+1) * sectors + s;
}
}
void Draw(GLfloat x, GLfloat y, GLfloat z)
{
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glEnable(GL_TEXTURE_2D);
//glEnable(GL_LIGHT0);
//glEnable(GL_LIGHTING);
glPushMatrix();
glTranslatef(x, y, z);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &vertices[0]);
glNormalPointer(GL_FLOAT, 0, &normals[0]);
glTexCoordPointer(2, GL_FLOAT, 0, &texcoords[0]);
glDrawElements(GL_QUADS, indices.size(), GL_UNSIGNED_INT, &indices[0]);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
};
Advertisement
i think the issue is with the for() loops in the indices not being rings-1 and sector-1? Would that be correct?

Thanks!

This topic is closed to new replies.

Advertisement