views:

1829

answers:

1

I've created this basic 3D Demo using OpenGL/SDL. I handled the keyboard callback so I can "strafe" left and right using 'a' and 's' and move forward and backward using 's' and 'w'.

However, I would like to now make it so I can control the direction my camera is "looking" based off my mouse movements. Just like in a FPS shooter when you move the mouse around it makes the camera look around in various directions.

Does anyone have any idea how I could utilize the mouse callbacks to "points" the camera class correctly when I move the mouse?

#include "SDL.h"
#include "Camera.h"

Camera cam;
Scene scn;

//<<<<<<<<<<<<<<<<<myKeyboard>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void myKeyboard(unsigned char key, int x, int y)
{
  switch(key)
    {
    case 's': cam.slide(0.0, 0.0, 0.2); break;
    case 'w': cam.slide(0.0, 0.0, -0.2); break;
    case 'a': cam.yaw(-1.0); break;
    case 'd': cam.yaw(1.0); break;


    case 27: exit(0);
    }
  glClear(GL_COLOR_BUFFER_BIT);
  glutPostRedisplay();
}

void displaySDL( void )
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    scn.drawSceneOpenGL();
    glFlush();
    glutSwapBuffers();
}

int main( int argc, char* argv[] )
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("SDL Sence With Camera");
    glutKeyboardFunc(myKeyboard);
    glutDisplayFunc(displaySDL);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_NORMALIZE);
    glViewport(0, 0, 640, 480);
    scn.read("fig5_63.dat");
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    scn.makeLightsOpenGL();

    cam.set(2.3, 1.3, 2.0, 0, 0.25, 0, 0, 1, 0);
    cam.setShape(30.0f, 64.0f/48.0f, 0.5f, 50.0f);

    glutMainLoop();
    return 0;
}

This is a tar with my SDL file, and the file I pasted above and my Camera class. http://www.filedropper.com/fpsdemotar

If someone can give me some tips for what algorithm I should use when processing mouse callbacks in terms of pointing the camera I would appreciate it.

Thanks!

+3  A: 

Mouse moving up/down -> pitch, Mouse moving right/left -> yaw.

I do not believe having your 'a' and 'd' keys be yaw is accurate.

Actually, your whole setup is a bit odd to me, since from a geometric standpoint, I view the coordinate as (x, y, z). You set s and w to go "up" and "down" (z), instead of "forward" and "back" (y). I see it as a xy graph that has been set flat on a table and you are looking at it from above. Moving close to it decreases z, which is coming out of the plane.

Here is how I would have it setup:

w -> slide(0, 0.2, 0); // y
s -> slide(0, -0.2, 0);
a -> slide(-0.2, 0, 0); // x
d -> slide(0.2, 0, 0);

//The following goes in your mouse event handler or something:
pitch(newMouseLocation.y - oldMouseLocation.y); // mouse y is related to pitch
yaw(newMouseLocaiton.x - oldMouseLocation.x); // mouse x is related to yaw

I realize that you do not need to follow this coordinate convention, but it just seems more intuitive for me.

I hope this helps.

CookieOfFortune
Thank you for your input CookieOfFortune. Some guides I have stumbled on to suggest centering the mouse to the center of the screen. Do you think I should do this or not?
KingNestor
One thing I was considering, was each mouse callback handling the pitch, yaw like you suggest then forcing the mouse to move back to the center so you never reach the ledge. What do you think?
KingNestor
Hmmm... does mousemove stop registering a change if it hits the edge? If that is the case (and I do believe that it is), then yes, you should center the mouse (But only if you have focus!!, don't want someone alt-tabbing and then be unable to move their mouse!).
CookieOfFortune