Friday, December 5, 2008

OpenGL solid sphere without glut

The following function draws a solid sphere in OpenGL without glut.

  1: void solidSphere(GLdouble radius, GLint slices, GLint stacks)
  2: {
  3:   glBegin(GL_LINE_LOOP);
  4:   GLUquadricObj* quadric = gluNewQuadric();
  5: 
  6:   gluQuadricDrawStyle(quadric, GLU_FILL);
  7:   gluSphere(quadric, radius, slices, stacks);
  8: 
  9:   gluDeleteQuadric(quadric);
 10:   glEnd();
 11: 
 12: }
It creates a line loop at line 3. It crates a quadratic object after that (line 4) and sets drawing mode to fill the gaps (line 4).  It draws a sphere using the previously created quadratic object passing the parameters to the function (line 7). Finally it deletes the quadratic object (line 9). The result is same as calling glutSolidSphere() with the same arguments.