Jump to content

  • Log In with Google      Sign In   
  • Create Account

14 years ago on June 15th Gamedev.net was first launched! We want to thank all of you for being part of our community and hope the best years are ahead of us. Happy birthday Gamedev.net!

Sphere generation and calculating indices windings


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 MARS_999   GDNet+   -  Reputation: 859

Like
0Likes
Like

Posted 01 August 2012 - 10:18 PM

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);
	}
};

Edited by MARS_999, 01 August 2012 - 10:23 PM.


Sponsor:

#2 MARS_999   GDNet+   -  Reputation: 859

Like
0Likes
Like

Posted 01 August 2012 - 10:31 PM

i think the issue is with the for() loops in the indices not being rings-1 and sector-1? Would that be correct?

Thanks!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS