Welcome to the new Linux Foundation Forum!

Perspective help with OpenGL (mesa)

HI Guys,

I am trying to create a perspective of a twisted square (ie, the squares sides getting smaller the further it is away from the camera).

Unfortunately, the scene ends up looking like an orthographic projection. Can anyone suggest what I am doing wrong?

void init() {
glClearColor(1, 1, 1, 0);

// projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 60, 0, 1);
gluLookAt (0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
//glOrtho(-1, 1, -1, 1, 1, -1);

// set up model view
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glRotated(45, 0, 1, 0);
glColor3d(0.5, 1, 0.5);
glutSolidCube(1);
glPopMatrix();

glutSwapBuffers();
}

void reshape(int w, int h) {
glViewport(0, 0, w, h);
}

int main (int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL Window");
init();
glutDisplayFunc(draw);
glutIdleFunc(idle);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}


Many Thanks,

Tom Medhurst

Comments

  • memmem Posts: 4
    The second argument to gluPerspective is the aspect ratio for the projection, simplest way is to use the aspect ratio of your window.

    You also need to take a step back :-)

    In your MODELVIEW matrix, add a translation in the "z" axis of about -10 units. Now you should see your cube in perspective.
  • LegacyUserLegacyUser Posts: 0
    Thanks mem!
    How exactly do I use the aspect ratio of my window?
  • memmem Posts: 4
    You could use width / height for example. There's no "right" value, it depends on what you want to achieve.
Sign In or Register to comment.