Sign in to follow this  
MARS_999

Sphere generation and calculating indices windings

Recommended Posts

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!

[code]
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);
}
};
[/code] Edited by MARS_999

Share this post


Link to post
Share on other sites
i think the issue is with the for() loops in the indices not being rings-1 and sector-1? Would that be correct?

Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this