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.

user1118321
  • 25,567
  • 4
  • 55
  • 86
Mr.D
  • 7,353
  • 13
  • 60
  • 119
  • 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. – user1118321 Feb 27 '14 at 06:12
  • so lats are slices and longs are stack? – Mr.D Feb 27 '14 at 08:06
  • http://stackoverflow.com/questions/7687148/drawing-sphere-in-opengl-without-using-glusphere – Toby Hodges Jun 28 '16 at 21:47

0 Answers0