多采样背景不显示

时间:2018-09-03 01:56:15

标签: c++ opengl glut orthographic

我试图用恒星背景说明海王星周围的海卫一和变形星的轨道。我决定通过对白色点(星)阵列进行多重采样来绘制模板背景。有人可以解释为什么我的阵列不显示吗?

*在Display()中创建的Init()中调用了背景。

完整代码在这里:

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;
}

1 个答案:

答案 0 :(得分:1)

绘制起点,然后设置透视投影矩阵和视图矩阵。 “星星”不在视口上。

使用大约1/500的比例将点放入剪辑空间:

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

,在绘制星星之前先设置正交投影:

// Stars Background

glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();

glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
gluOrtho2D( 0, 1000, 0, 1000 );

glCallList(1);

glPopMatrix();

glMatrixMode( GL_MODELVIEW );
glPopMatrix();