Search code examples
openglglutglu

Sphere is not visible with glutSolidSphere()


Below is my code to display solid sphere. I've used ModelMatrix to display. But I'm unable to see the solid. Should anything like projection to be added. But I've no need for projections currently.

    #include <stdlib.h>
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <GL/glut.h>
    #include<windows.h>
    static void Init() {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    }


    static void display() {
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
        glColor3d(1,1,0);
      glutSolidSphere(3,4,4);
    }

    int main(int argc, char** argv) {

      glutInit(&argc,argv);
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
      glutInitWindowSize( 550, 550);
      glutInitWindowPosition( 50, 50);
      glutCreateWindow( "Balloon");

      glutDisplayFunc(display);
      Init();
      glutMainLoop();
      return 0;
    }

Solution

  • Setting the modelview matrix to identity essentially puts the camera directly at the origin. Your sphere is similarly being rendered at the origin, so your viewpoint is at the center of the sphere. Depending on how glut is rendering, this may render your sphere invisible, because you're looking at the back-faces of the geometry.

    Also, by not setting a projection matrix you're implicitly using the default projection, which is an orthogonal projection that's bounded to the range of [-1, 1] in all three dimensions. Since you're sphere's radius is 3, all of your geometry is outside the projection frustum, so it's being culled.

    Try using gluLookAt to position the camera and where it's looking. I would suggest that you use gluLookAt to position the camera at 0,0,1 looking at the origin, and then draw your sphere with a radius of 0.5. This should make it visible, since you're no longer pushing the surface of the sphere outside of the projection frustum and you're no longer inside the sphere.