Thread: OpenGL Camera Question

  1. #1
    Registered User
    Join Date
    Oct 2006

    Arrow OpenGL Camera Question

    I've been learning OpenGL for about a week now, and am trying to work out how to make the mouse movement affect the camera view (like in a first person shooter). I've got my keys movement perfect now, but thats the easy part.

    If someone could point me in the right direction it would help.


  2. #2

    Join Date
    May 2005
    I calculate the deviation of the mouse from the center of the screen. I then reset the cursor position back to the center of the screen.

    I convert this deviations in the x and y directions into changes in angles.

    SetCursorPos and GetCursorPos are the windows functions I use to get the mouse change.

    Here is the function I wrote to do this. x and y radians are the pitch and yaw of the camera.

    Note this is for a first person shooter style camera, it prevents you from looking further than directly 'up' or 'down' (pi/2 radians or + or - 90 degrees)

    Once I have these angles gathered from the first function, an easy way to apply it to OpenGL is to use the second function to build the appropriate lookat matrix, using the angles and the position of the camera. You call the second function before drawing anything, and it will reflect the camera position and orientation. Keep in mind there are many ways to do this, this is just the most straightforward and basic approach.

    void	FPSCam::GatherInputOverframes()
    	POINT MousePos;
    	static	POINT	NewPos;
    	if(MousePos.x == Export.mVars.MiddleX && MousePos.y == Export.mVars.MiddleY)
    	SetCursorPos(Export.mVars.MiddleX, Export.mVars.MiddleY);
    	float YDeviation = (Export.mVars.MiddleY - MousePos.y);		
    	float XDeviation = (Export.mVars.MiddleX - MousePos.x);		
    	float	framespeed = (Export.mVars.TWOPI*8) * .01;
    	//fixme: i know this used to use screenheight, changed on purpose so it goes same speed along each axis
    	float XDegRad = (Export.mVars.PI * YDeviation) * (1/(Export.mVars.Width*.5)); //radians to rotate view vector about X axis
    	if(XDegRad > 0 && XDegRad > framespeed)
    		XDegRad = framespeed;
    	else	if(XDegRad < 0 && XDegRad < -framespeed)
    		XDegRad = -framespeed;
    	xRadians += XDegRad;
    	this is actually 89 degrees, gimbal lock is impossible in this scenario
    	however in order to have a valid move direction i sorta need to have X and Z components which is impossible
    	if you are looking straight upward
    	if(xRadians > 1.553343) 
    		xRadians = 1.553343;
    		XDegRad = 0;
    	else	if(xRadians < -1.553343)
    		xRadians = -1.553343;
    		XDegRad = 0;
    	float YDegRad = (Export.mVars.PI * XDeviation) * (1/(Export.mVars.Width*.5)); //radians to rotate view vector about Y axis	
    	if(YDegRad > 0 && YDegRad > framespeed)
    		YDegRad = framespeed;
    	else	if(YDegRad < 0 && YDegRad < -framespeed)
    		YDegRad = -framespeed;
    	yRadians	+=	YDegRad;
    the second function. Note that I use radians in my camera and OpenGL takes degrees. You must convert somewhere along these lines (or just save the degrees, rather than radians, in the Camera setup).

    void	R_GenerateViewMatrix(float	pitch, float yaw, float roll, Vector3 & translation)
    Last edited by BobMcGee123; 01-23-2007 at 09:29 AM.
    I'm not immature, I'm refined in the opposite direction.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Here is a camera class I found from CodeColony, it may not be what you want, but you could get some ideas from it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL Question
    By B_Level in forum Game Programming
    Replies: 1
    Last Post: 10-14-2006, 09:04 AM
  2. Question on OpenGL
    By ssharish2005 in forum Game Programming
    Replies: 2
    Last Post: 10-14-2006, 08:29 AM
  3. OpenGL question
    By sand_man in forum Game Programming
    Replies: 4
    Last Post: 11-08-2004, 07:05 AM
  4. OpenGL Camera
    By frenchfry164 in forum Game Programming
    Replies: 15
    Last Post: 12-10-2003, 02:50 PM