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.

2 comments:

  1. Actually, There is a problem here...
    Not all operations are allowed inside of a glBegin/glEnd code.
    When I was trying to run your code with opengl + python. I got this message:

    invalid operation glEnd 1282

    Take those begin,end tags out... and It works perfectly,
    thanks for your code, I hope what I said can help anyone with this problem,
    thanks

    ReplyDelete