2

I needed to draw sphere on OpenGL without using gluSphere() function. I have found somewhere this function:

 void drawSphere(double r, int lats, int longs) { int i, j; for(i = 0; i <= lats; i++) { double lat0 = M_PI * (-0.5 + (double) (i - 1) / lats); double z0 = sin(lat0); double zr0 = cos(lat0); double lat1 = M_PI * (-0.5 + (double) i / lats); double z1 = sin(lat1); double zr1 = cos(lat1); glBegin(GL_QUAD_STRIP); for(j = 0; j <= longs; j++) { double lng = 2 * M_PI * (double) (j - 1) / longs; double x = cos(lng); double y = sin(lng); glNormal3f(x * zr0, y * zr0, z0); glVertex3f(x * zr0, y * zr0, z0); glNormal3f(x * zr1, y * zr1, z1); glVertex3f(x * zr1, y * zr1, z1); } glEnd(); } } 

But I can't understand what it does. I think it draws polyhedron that looks like sphere. Also, I think lat0, lat1 used to determine how far from Z axis vertices will be located.

3
  • Your understanding is mostly correct. Have you ever worked with polar coordinates? This is similar. You can think of latitudes as slices through the sphere, just like on a globe of the earth. For a given latitude, you would need to generate points all around the circle at that latitude. That's what you're seeing with the sines and cosines. Commented Feb 27, 2014 at 6:12
  • so lats are slices and longs are stack? Commented Feb 27, 2014 at 8:06
  • stackoverflow.com/questions/7687148/… Commented Jun 28, 2016 at 21:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.