Jump to content
  • Advertisement
Sign in to follow this  

Sphere generation and calculating indices windings

This topic is 2179 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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?


class SolidSphere
std::vector<GLfloat> vertices, normals, texcoords;
std::vector<GLuint> indices;
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);
glTranslatef(x, y, z);
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]);
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?


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!