Mirza
Mirza

Reputation: 645

How do I correctly implement a basic first person camera in OpenGL?

I am new to OpenGL and was trying to create a simple maze that i can traverse through using a first person perspective. I have the maze rendering and all that just fine. But my first person camera perspective ends up being more of a third person camera. The camera revolves around a certain point infront of the camera.

My Code for actual rotation and translation

void camera(){
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(pitch, 1, 0, 0);
    glRotatef(yaw, 0, 1, 0);
    glTranslatef(player.x, player.y, player.z);
}

This is also the first translations and rotations that happen in rendering. Thanks for any help.

Upvotes: 0

Views: 3118

Answers (2)

dgraves
dgraves

Reputation: 351

The camera transformation needs to be inverted, so that you are moving the camera position, in this case the position of the player, to the origin:

glTranslatef(-player.x, -player.y, -player.z);

Upvotes: 1

user732933
user732933

Reputation: 278

I suggest that you build your maze, leave it alone, and then use gluLookAt() to have your first-person perspective. What you're doing now is standing still and moving the maze around you. You're doing it the hard way.

Upvotes: 0

Related Questions