开发者

OpenGL projection question: Why I can't see anything on the screen?

void init (void)
{
    glClearColor (1.0, 1.0, 1.0, 0.0);

    //glMatrixMode(GL_MODELVIEW);
    //gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);

    glMatrixMode(GL_PROJECTION);
    gluPerspective(45, 2, -1, 1);
    //glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar);
    //gluPerspective(45.0, 45, -1, 1);
}

void displayFcn (void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(0.0, 1.0, 0.0);
    glPolygonMode(GL_FRONT, GL_FILL);
    glPolygonMode(GL_BACK, GL_LINE);

    glBegin(GL_TRIANGLES);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.1, 0.0, 0.0);
        glVertex3f(0.50, 0.866025, 0.0);    
    glEnd();

    glFlush();
}

void reshapeFcn(GLint newWidth, GLint newHeight)
{
    glViewport(0,0,newWidth, newHeight);

    winWidth = newWidth;
    winHeight = newHeight;

}
void main(int argc, char** ar开发者_如何学Cgv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(400,200);
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("Test");

    init();
    glutDisplayFunc(displayFcn);
    glutReshapeFunc(reshapeFcn);
    glutMainLoop();
}

Could someone explain a little bit and give suggestions how to make that triangle visible.


I don't think that you're allowed to set 'zNear' negative. That will do weird things to the depth buffer and mean that you see things behind the eye. Docs say it's always positive. You're also liable to get somewhat strange results if you fix the aspect ratio to 2, regardless of the aspect ratio of the window.

You also need to set the current matrix back to MODEL_VIEW as datenwolf has shown. You don't need to use gluLookAt, but you need to do something to move the triangle away from the origin where the eye is located. You could do that by setting the z component to some negative value, or by applying a translation before the vertices:

glPushMatrix();
glTranslated(0,0,-5);
glBegin(GL_TRIANGLES);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.1, 0.0, 0.0);
    glVertex3f(0.50, 0.866025, 0.0);    
glEnd();
glPopMatrix();


gluPrespective and gluLookAt multiply on top of the current matrix on the selected stack. You need to load an identity first to make sense. Also you need to set a viewport before rendering. Best practice is to set all matrices and the viewport in the display function, and nowhere else. Sticking to that rule will make your life a lot easier. Also in OpenGL one usually doesn't have a dedicated initializtion phase. Resources are loaded on demand.

void displayFcn (void)
{
    glViewport(0,0,winWidth, winHeight);

    glClearColor (1.0, 1.0, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, 2, 1, 10);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);

    glColor3f(0.0, 1.0, 0.0);
    glPolygonMode(GL_FRONT, GL_FILL);
    glPolygonMode(GL_BACK, GL_LINE);

    glBegin(GL_TRIANGLES);
        glVertex3f(0.0, 0.0, 0.0);
            glVertex3f(0.1, 0.0, 0.0);
        glVertex3f(0.50, 0.866025, 0.0);    
    glEnd();

    glFinish();
}

void reshapeFcn(GLint newWidth, GLint newHeight)
{
    winWidth = newWidth;
    winHeight = newHeight;
    glutPostRedisplay();
}

void main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(400,200);
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("Test");

    glutDisplayFunc(displayFcn);
    glutReshapeFunc(reshapeFcn);
    glutMainLoop();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜