简体   繁体   中英

setting up an opengl perspective projection

I am having an issue setting up the viewing projection. I am drawing a cube with the vertices (0, 0, 0) (0, 0, 1) (0, 1, 1) (0, 1, 0) (1, 0, 0) (1, 1, 0) (1, 1, 1) and (1, 0, 1). This is how I am initializing the view:

void initGL(int x,int y, int w, int h)
{
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH );
    glutInitWindowPosition( x, y );
    glutInitWindowSize( w, h );
    glutCreateWindow( "CSE328 Project 1" );

    glutDisplayFunc(draw);
    glFrontFace(GL_FRONT_AND_BACK);
    glMatrixMode(GL_PROJECTION);
    glFrustum(-10.0, 10.0, -10.0, 10.0, 2.0, 40.0);
    glMatrixMode(GL_MODELVIEW);
    gluLookAt(10, 10, 10, 0.5, 0.5, 0, 0, 1.0, 0);
    glutMainLoop();
}

For some reason, the cube is filling the entire screen. I have tried changing the values of the frustum and lookAt methods, and either the cube is not visible at all, or it fills the entire viewport. In glLookAt I assume the 'eye' is positioned at (10, 10, 10) and looking at the point (0.5, 0.5, 0), which is on the surface of the cube. I thought this would give enough distance so the whole cube would be visible. Am i thinking about this in the wrong way? I have also tried moving the cube in the z direction so that it lies from z = 10 to z = 11, and so is in the clipping plane, but it gives similar results.

The cube has length 1, the viewing volume spans 20 units in x and y dimensions. The cube occupies some pixels in the middle even with orthographic projection; unless there is some other transformation applied during drawing.

I suggest making the frustum smaller (eg +/- 2.0f ) and moving the camera closer; (4.0f, 4.0f, 4.0f) .

Moving the eye position further from the cube by changing the first 3 parameters of gluLookAt() should make it smaller.

You could also replace your call to glFrustum() with a call to gluPerspective() which would make it easier to configure the perspective projection to your liking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM