Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Moving in 3D space

GeoHoffmanGeoHoffman Member Posts: 176
I am experimenting with openGL for the first time. I am trying to create a scene and then be able to move through it. I want to use both the mouse and keyboard. The mouse will be used to look around the scene and the keyboard will be used to move back and forth (much like unreal tournament). The part I am having problems with is looking around the scene from a fixed reference point. I figure I should startwith a structure like this:

struct PlayerPosition
{
float positionX, positionY, positionZ;
float orientationX, orientationY, orientationZ;
}

to track the position of the camera. Asuming I already have a function to change all the variables in this struct when the player moves, how should I go about my transformations to display the scene based on the position of the player?

Comments

  • gautamgautam Member Posts: 642
    Hi,

    You can either move the camera or everything around the player with respect to player position. You can move the axis using glTranslate or you can use gluLookAt to position the camera (never used it much myself) :)

    : I am experimenting with openGL for the first time. I am trying to create a scene and then be able to move through it. I want to use both the mouse and keyboard. The mouse will be used to look around the scene and the keyboard will be used to move back and forth (much like unreal tournament). The part I am having problems with is looking around the scene from a fixed reference point. I figure I should startwith a structure like this:
    :
    : struct PlayerPosition
    : {
    : float positionX, positionY, positionZ;
    : float orientationX, orientationY, orientationZ;
    : }
    :
    : to track the position of the camera. Asuming I already have a function to change all the variables in this struct when the player moves, how should I go about my transformations to display the scene based on the position of the player?
    :

  • GeoHoffmanGeoHoffman Member Posts: 176
    [b][red]This message was edited by GeoHoffman at 2003-6-2 20:44:36[/red][/b][hr]
    Thank you for your reply. I have one more question.

    Here is part of my code that I use to control the movement of the camera:

    void setCamera()
    {
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    if (GetAsyncKeyState(VK_LEFT)) cameraData.position[0] -= .01;
    if (GetAsyncKeyState(VK_RIGHT)) cameraData.position[0] += .01;
    if (GetAsyncKeyState(VK_UP)) cameraData.position[2] -= .01;
    if (GetAsyncKeyState(VK_DOWN)) cameraData.position[2] += .01;

    glRotatef(cameraData.orientation[0], 1,0,0);
    glRotatef(cameraData.orientation[1], 0,1,0);
    glRotatef(cameraData.orientation[2], 0,0,1);
    glTranslatef(-cameraData.position[0],
    cameraData.position[1],
    cameraData.position[2]);
    }

    and in the winProc (I know, I know, it is not the best place for real time input. I am only experimenting):

    case WM_MOUSEMOVE

    if (LOWORD(lParam) > 400) cameraData.orientation[1] -= 5;
    if (LOWORD(lParam) < 400) cameraData.orientation[1] += 5;
    if (HIWORD(lParam) > 300) cameraData.orientation[0] += 5;
    if (HIWORD(lParam) < 300) cameraData.orientation[0] -= 5;

    SetCursorPos(400,300);

    if (cameraData.orientation[0] > 90) cameraData.orientation[0] = 90;
    if (cameraData.orientation[0] < -90) cameraData.orientation[0] =-90;

    RenderScene();
    SwapBuffers(hDC);
    ValidateRect(hWnd,NULL);

    My problem is this and it is probably very basic. In setCamera() I call glMatrixMode(GL_MODELVIEW); and glLoadIdentity(). This resets the coordinate system to eye coordinates. But my problem is that it also changes it so I can only see 1 to -1 along each axis. I do not understand how to change this so it displays a larger clipping volume. I assume I need a call to gluPerspective() or glOrtho() or something. How should I go about this and where does this code need to be? I can email or post the complete code for the program if needed. It is pretty small so far.


  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    [b][red]This message was edited by Moderator at 2003-6-3 9:20:35[/red][/b][hr]
    Those calls shouldn't reset the axis coordinates like that. Here's how I go about setting up the camera in my OpenGL RPG.
    [code]
    void DrawScreen()
    {
    Player.View.X = Player.Position.X + cos(Player.Heading * (pi / 180)) * 1.25;
    Player.View.Y = Player.Position.Y + sin(Player.Heading * (pi / 180)) * 1.25;
    Player.View.Z = Player.Position.Z + sin(Player.Lookup * (pi / 180)) * 1.25;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt(Player.Position.X, Player.Position.Y, Player.Position.Z, Player.View.X, Player.View.Y, Player.View.Z, 0.0, 1.0, 0.0);
    glNormal3f(0.0f, 1.0f, 0.0f);

    /*
    * Now I draw actors and living things
    * followed by static objects
    * followed by the world!
    */

    SwapBuffers(WinData.DeviceContext);
    return;
    }
    [/code]
    Simple, and gluLookAt() works great! Works with my mouselook and all. I am also using gluLookAt() in a platformer I am playing around with now. However, you will notice that I do NOT call glMatrixMode() every frame. I call that function *ONLY* when I load in a new map. OpenGL will stay set at whatever you call, so unless you continually change the mode for some reason, there's no need for it to be called each frame.

    [red]*EDIT*[/red]

    Posted one tiny bit of code wrong, fixed it though!

  • GeoHoffmanGeoHoffman Member Posts: 176
    Actually, thank you very much. Your reply really helped me solidify my understanding of the transformation pipeline and how to use the modelview and projection matrixes. I was having lots of problems with those. My camera is now able to move through the scene on the XZ plane and rotate around the X and Y axis like I wanted it to.

    -Geo
Sign In or Register to comment.