1

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.

vyegorov
  • 21,787
  • 7
  • 59
  • 73
Kris
  • 398
  • 4
  • 9
  • 3
    In OpenGL you don't initialize. You set state. Most of what you do there in initGL actually belongs into the drawing code; setting the matrices (you're missing reset to identity BTW). – datenwolf May 06 '12 at 08:27
  • Thanks, my problem was caused by not resetting to the identity. – Kris May 06 '12 at 15:20

2 Answers2

0

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 (e.g. +/- 2.0f) and moving the camera closer; (4.0f, 4.0f, 4.0f).

Stefan Hanke
  • 3,458
  • 2
  • 30
  • 34
0

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.

fintelia
  • 1,201
  • 6
  • 17