Search code examples
c++openglglutorthographic

MultiSampling Background Not Displaying


I'm trying to illustrate orbits of Triton and Proteus around Neptune with a background of stars. I decided to sketch a template background by multisampling an array of white dots ( stars ) as the background. Could someone explain why my array isn't displaying?

*Background is called in Display() created in Init().

Full Code here:

int triton = 0;
int proteus = 0;

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_SMOOTH);

    // Material Specs
    GLfloat mat_specular[] = { 0.8, 0.8, 0.9, 0.1 };
    GLfloat mat_shininess[] = { 40.0 };
    GLfloat lightDiffuse[] = { 1.0, 1.0, 1.0, 0.8 };
    GLfloat lmodel_ambient[] = { 0.1, 0.2, 0.7, 0.0 };

    // Light 0 Initialized.
    GLfloat light0[] = { 1.0, 1.0, 1.0, 0.9 };
    GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };


    // Mat Specs Implmentations.
    glMaterialfv(GL_FRONT, GL_DIFFUSE, lightDiffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

    // Light 0 implementation
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light0);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light0);


    // Ambient surrounding light on object.
    //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

    // Enable Lighting and Depth
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);


    // Background (Stars: In Progress)
    glNewList(1, GL_COMPILE);   
            glBegin(GL_POINTS);
            glColor3f(1.0, 1.0, 1.0);
            for (int i = 0; i < 1000; i++)
            {
                for (int j = 0; j < 1000; j++)
                {
                    if (((i + j) % 2) == 0)
                    {
                        glVertex2f(2*i, 2*j);
                    }
                }
            }
            glEnd();
        glEndList();
}
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
        glEnable(GL_COLOR_MATERIAL);
        // Neptune
        glColor3f(0.1, 0.1, 0.3); 
        glutSolidSphere(2.0, 100, 100);

        // Triton
        glPushMatrix();
            glColor3f(0.9, 0.7, 0.8); 
            glRotatef((GLfloat)triton, 1.0, 1.0, 1.0);
            glTranslatef(2.5, 0.0, 0.0);
            glRotatef((GLfloat)triton, 1.0, 0.0, 0.0);
            glutSolidSphere(0.35, 100, 100);
        glPopMatrix();

        // Proteus
        glPushMatrix();     
            glColor3f(1.0, 1.0, 1.0); 
            glRotatef((GLfloat)proteus, 0.7, -0.4, 1.0);
            glTranslatef(3.5, 0.0, 0.0);
            glRotatef((GLfloat)proteus, 1.0, 0.0, 0.0);
            glutSolidSphere(0.1, 100, 100);
        glPopMatrix();

        // Stars Background
        glEnable(GL_MULTISAMPLE);
        glPushMatrix();
            glCallList(1); // Not Coding
        glPopMatrix();

        glDisable(GL_COLOR_MATERIAL);
    glPopMatrix();
    glFlush();
    glutSwapBuffers();

}
void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -5.0);
}
void keyboard(unsigned char key, int x, int y)
{
    switch (key) {
        // Triton + Proteus Orbit. 
        case 'a':
            triton = (triton - 2) % 360;
            proteus = (proteus - 5) % 360;
            glutPostRedisplay();
            break;
        case 'd':
            triton = (triton + 2) % 360;
            proteus = (proteus + 5) % 360;
            glutPostRedisplay();
            break;
        case ',': 
            glTranslatef(- 0.3, 0.0, 0.0);
            glutPostRedisplay();
            break;

        case '.':
            glTranslatef(0.3, 0.0, 0.0);
            glutPostRedisplay();
            break;
        case 27: 
            exit(0);
            break;
        default:
            break;
    }
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB | GLUT_MULTISAMPLE);
    glutInitWindowSize(1000, 1000);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Neptune and Space");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}

Solution

  • You you draw the start, then the perspective projection matrix and the view matrix are stills set. The "stars" are not on the viewport.

    Use a scale of about 1/500 to put the points into clip space:

    glVertex2f(2*i / 500.0f, 2*j  / 500.0f);
    

    Or setup an orthographic projection, before you draw the stars:

    // Stars Background
    
    glMatrixMode( GL_MODELVIEW );
    glPushMatrix();
    glLoadIdentity();
    
    glMatrixMode( GL_PROJECTION );
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D( 0, 1000, 0, 1000 );
    
    glCallList(1);
    
    glPopMatrix();
    
    glMatrixMode( GL_MODELVIEW );
    glPopMatrix();