0
votes

I am working on a simple OpenGL project. I want to get a simple camera to move in perspective mode.

I keep reading about the projection matrix, gluLookAt, and the model view matrix. I keep reading that all I should do are my perspective calls in the projection matrix and then all of my transformations and camera movement in the model view matrix.

#include "GLheaders.h"

void drawWorldAxis() {
    glLoadIdentity();
    glBegin(GL_LINES);
    glNormal3f(0, 0, 1);
    glColor3ub(255, 0, 0);
    glVertex3f(0,0,0);
    glVertex3f(1,0,0);
    glColor3ub(0, 255, 0);
    glVertex3f(0,0,0);
    glVertex3f(0,1,0);
    glColor3ub(0, 0, 255);
    glVertex3f(0,0,0);
    glVertex3f(0,0,1);
    glEnd();
}

void keyboard(unsigned char key, int x, int y) {
    glutPostRedisplay();
}

static float eye[3] = {.5, .5, .5};
#include <stdio.h>
void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(55.0, 1, .1, 10000);



    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    drawWorldAxis();
    printf("eye at <%f, %f, %f>\n", eye[0], eye[1], eye[2]);
    fflush(stdout);
    gluLookAt(eye[0], eye[1], eye[2], 0, 0, 0, 0, 1, 0);

    eye[0] += .1;
    eye[1] += .1;

    glFlush();
    glutSwapBuffers();
}

void reshape(int w, int h) {
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(55.0, 1, -1, 10000);
    glMatrixMode(GL_MODELVIEW);
    glutPostRedisplay();

}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH);
    glutInitWindowSize(400,400);
    glutCreateWindow("Tiny Test");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);

    glEnable(GL_NORMALIZE);
    glEnable(GL_DEPTH_TEST);

    glutMainLoop();

    return EXIT_SUCCESS;
}

I am expecting this code to display three lines representing the world coordinate system's x, y, and z axises, and as keys are pressed the camera should move and start to look at the origin/coordinate axises from more and more drastic angles.

What is going wrong here? I've been bashing my head into a wall trying to figure out why nothing is moving. It only changes if I put the gluLookAt call in the projection matrix which I keep being told is a terrible idea.

2

2 Answers

2
votes

The coordinate cross is drawn before you set the lookAt matrix, thus the matrix has no effect.

You have to change the order such that the matrix is already present when drawing:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eye[0], eye[1], eye[2], 0, 0, 0, 0, 1, 0);
drawWorldAxis();
printf("eye at <%f, %f, %f>\n", eye[0], eye[1], eye[2]);
fflush(stdout);

Then there is a second problem: You are resetting the model matrix in the first line of drawWorldAxis. Here, you can either remove the glLoadIdentity call or push the previous matrix to the stack first:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eye[0], eye[1], eye[2], 0, 0, 0, 0, 1, 0);

glPushMatrix();    

drawWorldAxis();

glPopMatrix();

printf("eye at <%f, %f, %f>\n", eye[0], eye[1], eye[2]);
fflush(stdout);
0
votes

thanks to @BDL for helping fix this! This is the correct code that I wanted

#include "GLheaders.h"

void drawWorldAxis() {
    glBegin(GL_LINES);
    glNormal3f(0, 0, 1);
    glColor3ub(255, 0, 0);
    glVertex3f(0,0,0);
    glVertex3f(1,0,0);
    glColor3ub(0, 255, 0);
    glVertex3f(0,0,0);
    glVertex3f(0,1,0);
    glColor3ub(0, 0, 255);
    glVertex3f(0,0,0);
    glVertex3f(0,0,1);
    glEnd();
}

void keyboard(unsigned char key, int x, int y) {
    glutPostRedisplay();
}

static float eye[3] = {-.1, -.1, 1};
#include <stdio.h>
void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(55.0, 1, .1, 10000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    printf("eye at <%f, %f, %f>\n", eye[0], eye[1], eye[2]);
    fflush(stdout);
    gluLookAt(eye[0], eye[1], eye[2], 0, 0, 0, 0, 1, 0);
    drawWorldAxis();

    eye[0] += .1;
    eye[1] += .1;

    glFlush();
    glutSwapBuffers();
}

void reshape(int w, int h) {
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(55.0, 1, -1, 10000);
    glMatrixMode(GL_MODELVIEW);
    glutPostRedisplay();

}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH);
    glutInitWindowSize(400,400);
    glutCreateWindow("Tiny Test");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);

    glEnable(GL_NORMALIZE);
    glEnable(GL_DEPTH_TEST);

    glutMainLoop();

    return EXIT_SUCCESS;
}