Question

I'm trying to draw a sphere using gluSphere. The sphere appears but certain patches are missing whether I draw it as a filled polygons or a wire frame.

int width=800, height=600;

    void myinit(void)
{
//      set up overall light data, including specular=ambient=light colors
    GLfloat light_position[]={ 10.0, 10.0, -10.0, 1.0 };
    GLfloat light_color[]={ 1.0, 1.0, 1.0, 1.0 };
    GLfloat ambient_color[]={ 0.0, 0.2, 0.2, 1.0 };
    GLfloat mat_specular[]={ 1.0, 1.0, 1.0, 1.0 };

    glClearColor( 0.0, 1.0, 1.0, 0.0 );
    glShadeModel(GL_SMOOTH);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular );
    glLightfv(GL_LIGHT0, GL_POSITION, light_position );
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color );
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_color );
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color );

/* attributes */
    glEnable(GL_LIGHTING);   // so lighting models are used
    glEnable(GL_LIGHT0);     // we'll use LIGHT0
    glEnable(GL_DEPTH_TEST); // allow z-buffer display
    //glClearDepth(1);
}

void drawHead(){
GLUquadric *sphere=gluNewQuadric();
gluQuadricDrawStyle( sphere, GLU_FILL);
    gluQuadricNormals( sphere, GLU_SMOOTH);
gluQuadricOrientation( sphere, GLU_OUTSIDE);
gluQuadricTexture( sphere, GL_TRUE);

//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glPushMatrix();
glTranslated(0,0,-5);
glRotated(30,1,1,1);
gluSphere(sphere,2.0,15,15);
glPopMatrix();
}

void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,0);
drawHead();
glutSwapBuffers();
}

void main(int argc, char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(width,height);
glutInitWindowPosition(100,200);
glutCreateWindow("Automaton");

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0,1.0,0.0,20);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
myinit();
glutDisplayFunc(display);

glutMainLoop();

}
Was it helpful?

Solution

As per the docs, "...zNear must never be set to 0."

Bump your zNear out a bit, and your zFar a bit more:

gluPerspective( 90.0, 1.0, 1.0, 200.0 );

Complete:

#include <GL/glut.h>

int width=800, height=600;

void myinit(void)
{
    // set up overall light data, including specular=ambient=light colors
    GLfloat light_position[]={ 10.0, 10.0, -10.0, 1.0 };
    GLfloat light_color[]={ 1.0, 1.0, 1.0, 1.0 };
    GLfloat ambient_color[]={ 0.0, 0.2, 0.2, 1.0 };
    GLfloat mat_specular[]={ 1.0, 1.0, 1.0, 1.0 };

    glClearColor( 0.0, 1.0, 1.0, 0.0 );
    glShadeModel(GL_SMOOTH);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular );
    glLightfv(GL_LIGHT0, GL_POSITION, light_position );
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color );
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_color );
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color );

    /* attributes */
    glEnable(GL_LIGHTING);   // so lighting models are used
    glEnable(GL_LIGHT0);     // we'll use LIGHT0
    glEnable(GL_DEPTH_TEST); // allow z-buffer display
}

void drawHead()
{
    GLUquadric *sphere=gluNewQuadric();
    gluQuadricDrawStyle( sphere, GLU_FILL);
    gluQuadricNormals( sphere, GLU_SMOOTH);
    gluQuadricOrientation( sphere, GLU_OUTSIDE);
    gluQuadricTexture( sphere, GL_TRUE);

    glPushMatrix();
    glTranslated(0,0,-5);
    glRotated(30,1,1,1);
    gluSphere(sphere,2.0,15,15);
    glPopMatrix();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,0);
    drawHead();
    glutSwapBuffers();
}

void main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize(width,height);
    glutInitWindowPosition(100,200);
    glutCreateWindow("Automaton");

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective( 90.0, 1.0, 1.0, 200.0 );

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    myinit();
    glutDisplayFunc(display);

    glutMainLoop();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top