# OpenGL Camera

Hi,

I’m new to OpenGL and I have some 3d shapes and such drawn on my screen. Now this is great but I want to be able to move around. I know that there is no actual camera and such but that you have to move the whole scene.

I already have a fuction that sets the camera orientaion and rotation according to keypress and mouse movement. I currently sets some variables called x, y, z, rx and ry.

So how can I translate these values to change the camera? I was looking up on gluLookAt but was wondering if this is the right function to use and how it would fit in.

Thanks,

James

gluLookAt is exactly right (especially if your not using core profile).
First 3 parameters are the camera position in world space and the next 3 are where you want the camera to look at. The last 3 are the orientation of the camera which is usually 0 1 0.
If you are using the core profile you’ll have to build the camera (aka modelview matrix) yourself.

Hi,

I have the camera looking up, down, left and right now using gluLookAt. But when i use WASD to move the camera it spins out of control. Now, I understand that this is because although the camera is moving it is still looking at the same point. How can I make my camera move normally (like in any 3d game)?

Here is my code:

Called once per frame:

``````
keychecker();
gluLookAt(x, y, z, ry, -rx, 1.0f, 0.0f, 1.0f, 0.0f);

``````

keychecker

``````
void keychecker()
{
if (w_pressed)
{
// Move forward
x -= sinf( degToRad( ry ) ) * cosf( -degToRad( rx ) );
y -= sinf( -degToRad( rx ) );
z -= cosf( degToRad( ry ) ) * cosf( -degToRad( rx ) );
}

if(s_pressed)
{
// Move backward
x += sinf( degToRad( ry ) ) * cosf( -degToRad( rx ) );
y += sinf( -degToRad( rx ) );
z += cosf( degToRad( ry ) ) * cosf( -degToRad( rx ) );
}

if(a_pressed)
{
// Strafe left
x += sinf( degToRad( ry - 90) );
z += cosf( degToRad( ry - 90 ) );
}

if(d_pressed)
{
// Strafe right
x += sinf( degToRad( ry + 90 ) );
z += cosf( degToRad( ry + 90 ) );
}

}

``````

Move move events:

``````
void mouseMoveEvent( float dX, float dY )
{
// Look left/right
ry -= dX / 100 * 30;

// Look up/down but only in a limited range
rx += dY / 100 * 30;
if( rx > 90 ) rx = 90;
if( rx < -90 ) rx = -90;
}

void GLFWCALL mouseMoveListener( int x, int y )
{
if( !running )
{
mx0 = x; my0 = y;
return;
}

mouseMoveEvent( (float)(x - mx0), (float)(my0 - y) );
mx0 = x; my0 = y;
}

``````

DegToRad converts from degrees to radians. This code used to work when using Horde3D.

Thanks,

James