Jump to content
  • Advertisement
Sign in to follow this  

OpenGL camera movement

This topic is 3843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey, i'm new to opengl and i need some help. lately i've been experimenting with camera movement and i was able to figure out how to move and rotate it using the arrow keys. however, when i want the camera to move "forward" or "backward", it moves on a slight angle as opposed to straight forward or backward. is there a way i can fix this? i tried changing where the camera started, which direction it's facing, and altering the formulas for the arrow keys. any help would be greatly appreciated. here's my code so far. ignore anything irrelevant! lol
#include <windows.h>
#include <gl.h>
#include <glut.h>
#include <math.h>

//angle of rotation
float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 90, angle=0.0;

void cube (void) {
    glColor3f(1.0f, 0.0f, 0.0f);

void walls (void){
	glColor3f(0.75f, 0.75f, 1.0f);

void init (void) {
    glEnable (GL_DEPTH_TEST); //enable the depth testing
    //glEnable (GL_LIGHTING); //enable the lighting
    //glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light
    glShadeModel (GL_SMOOTH); //set the shader to smooth shader

void camera (void) {

void display (void) {
    glClearColor (0.0,0.0,0.0,1.0); //clear the screen to black
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the color buffer and the depth buffer
	gluLookAt (1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0); //camera position, x,y,z, looking at x,y,z, Up Positions of the camera

	glColor3f(0.75f, 1.00f, 0.75f);

    //cube(); //call the cube drawing function
    glutSwapBuffers(); //swap the buffers
    angle++; //increase the angle

void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h); //set the viewport to the current window specifications
    glMatrixMode (GL_PROJECTION); //set the matrix to projection
    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0); //set the perspective (angle of sight, width, height, , depth)
    glMatrixMode (GL_MODELVIEW); //set the matrix back to model

void processNormalKeys(unsigned char key, int x, int y) {

    if (key == 27)
        glutLeaveGameMode(); //set the resolution how it was
        exit(0); //quit the program

void processSpecialKeys(int key, int x, int y) {

    switch(key) {
        case GLUT_KEY_RIGHT:
            yrot += 1;
            if (yrot > 360) yrot -= 360;
        case GLUT_KEY_LEFT:
            yrot -= 1;
            if (yrot < -360) yrot += 360;
        case GLUT_KEY_PAGE_UP:
            xrot += 1;
            if (xrot >360) xrot -= 360;
        case GLUT_KEY_PAGE_DOWN:
            xrot -= 1;
            if (xrot < -360) xrot += 360;
        case GLUT_KEY_UP:
            float xrotrad, yrotrad;
            yrotrad = (yrot / 180 * 3.141592654f);
            xrotrad = (xrot / 180 * 3.141592654f);
            xpos += float(sin(yrotrad))/10;
            zpos -= float(cos(yrotrad))/10;
            ypos -= float(sin(xrotrad))/10;
        case GLUT_KEY_DOWN:
            xrotrad, yrotrad;
            yrotrad = (yrot / 180 * 3.141592654f);
            xrotrad = (xrot / 180 * 3.141592654f);
            xpos -= float(sin(yrotrad))/10;
            zpos += float(cos(yrotrad))/10;
            ypos += float(sin(xrotrad))/10;

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH); //set the display to Double buffer, with depth
    glutGameModeString( "990x768:32@75" ); //the settings for fullscreen mode
    glutEnterGameMode(); //set glut to fullscreen using the settings in the line above
    init (); //call the init function
    glutDisplayFunc (display); //use the display function to draw everything
    glutIdleFunc (display); //update any variables in display, display can be changed to anyhing, as long as you move the variables to be updated, in this case, angle++;
    glutReshapeFunc (reshape); //reshape the window accordingly


    glutMainLoop (); //call the main loop
    return 0;
[Edited by - padlock87 on February 7, 2008 9:21:23 PM]

Share this post

Link to post
Share on other sites
Rotated movement is not as simple as a triangle (dist*sin(angle) or cos())

You will need 3D rotation equations:
assuming you have an axis of
|__ x

for rotation about the Y axis (left-right):
x' = distance*cos(yrotation)-distance*sin(yrotation)
z' = distance*cos(yrotation)+distance*sin(yrotation)

for rotation about the X axis (up-down):
y' = distance*cos(xrotation)-distance*sin(xrotation)
z' = distance*cos(xrotation)+distance*sin(xrotation)

for rotation about the Z axis (roll):
z' = distance*cos(zrotation)-distance*sin(zrotation)
x' = distance*cos(zrotation)+distance*sin(zrotation)

you will need to multiply the rotations together to get the full rotation

Google "3D Rotation" or "Matrix Rotation" and you'll get so much information you won't know what to do...

Quaternion is another method which is extremely powerful (a littler harder to understand tho). Matrix math (you may have to build in some matrix functions for them to work) is easier to understand, a little more cumbersome. And straight math (what I mentioned above) is pretty easy to understand but getting an arbitrary axis is a bit harder. Good luck

Share this post

Link to post
Share on other sites
Hey padlock, Im also a noob in opengl. Can you explain me how you used the camera(); function wrt the glulookat(); command to transform the camera. By the way you have made a really nice code for using glulookat();

Share this post

Link to post
Share on other sites
thanks for the compliment! lol

to be honest, i have no idea how i did it! lol i kind of just searched online for a couple of hours and copied and pasted code together. it was more of a trial and error kind of thing. i'd like to say that i knew what i was doing, but i'd be lying lol.

i can't explain camera() because i have no idea what those functions do. however, i can (hopefully) explain gluLookAt. somebody please correct me if any of this is wrong!

the first three numbers designate the camera position. this is pretty straight forward. its just a simple (x,y,z) coordinate.

the second three numbers designate the coordinates where the camera is looking. for example, if your code is gluLookAt (1.0, 1.0, 1.0, 0.0, 0.0, 0.0,... the camera is positioned at (1,1,1) and is looking down at (0,0,0)

the last three numbers really confused me at first. for a while i couldn't find anything online that explained to it in a way that i understood. basically, this coordinate tells the computer which way is up. i think that its based from (0,0,0) but im not sure. so for most programs, it would probably be (0,1,0)

i hope this helps!

Share this post

Link to post
Share on other sites
ya you are right about the first two sets of vectors that is the view and the position. The third vector is an up vector. This is the y-axis vector. Mostly it is passed as (0,1,0) because it is 1 unit above the origin that is the camera just rests on the grid(if their is any)focusing at the negative z-axis. If you want to tilt your camera then you can modify your up vector.
You can find more in the OpenGL Programming Guide. Its a really good book.
Yesterday i found tutorials about the camera stuff.
->Nate robins tutorials
->Apron tutorials(implemented openGL in win32). A little complex but You will understand. Good tutorials.(google it).
I said your tutorials where good because they where simple compared to the tutorials i found on the net.
Thanks for your reply.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!