glm/opengl orbital camera C++

Started by
9 comments, last by steven katic 9 years, 8 months ago

Hi nate?(oops) mynameisnafe,

I see you've worked out the correct angles of rotation input to the camera rotate method: It is the total angle since its creation, and not

the delta angle, as I incorrectly suggested in my first post.

I do not understand what your code is doing. It can get tricky fine tuning the operation of a camera so it's a good idea to come up with a plan.

So, for example, I think about the following:

Decouple the input angles from the input source. Looks like you have done that with the m_horizontalAngle and m_verticalAngle variables. So that's good.

Next, How do we update them? This is not clear to me from the code you posted.
Here is a typical usecase/scenario I would expect to see for the user rotating the camera with the mouse:

Operation: The user presses and holds down a designated mouse button, then drags the mouse to move the camera view around.

The recipe:


// two new variables to add to the current camera rotation angles.
//(Perhaps a little overengineering going on here. But it does indicate our explicit intent for now).
int m_pitchDelta;// = 0; member vars: init somewhere else or make static?
int m_yawDelta;// = 0;

On mouse button down (say typically the left button): Intialise the camera system input parameters for the next rotation.

I see this line "if( pMouse->bRight == true )" in your code and assume it means "if the right mouse button is down"?.


// on right button down set the start position of the new transformation
// values that will modify the current values (in m_horizontalAngle and m_verticalAngle variables) 
if( pMouse->bRight == true) 
{  
	POINT p;  
	GetCursorPos(&p);    
	m_pitchDelta = p.x;  
 	m_yawDelta = p.y;  
}

Note: That's all you need at this stage. I would keep it simple until it works, then add all your other bells & whistles (i.e. stuff related to the angle restrictions

and filtered/timing speeds) they just get in the way for me at the moment.

So, ideally at this moment in the process the Modelview Matrix *V has already been initialized and typically the full MVP has also already been constructed and now all

that is happening each frame is The MVP is passed to the shader, the shader is activated and scene drawn.

Oh,...And you are holding that right mouse button down....(ready to move the mouse).

Now the only change we are interested in is when the mouse moves (with that right button down, basically dragging the camera view around).
We want the Camera to move around relative to the mouse pointer position as it moves.

I cannot quite see this happening in your code. But you can put the camera update code in the WM_MOUSEMOVE case section to make this intention explicit:


case WM_MOUSEMOVE:  {  
 POINT p;  
 GetCursorPos(&p);     
// get the amount of the last mouse movement  
m_pitchDelta -= p.x; 
m_yawDelta -= p.y; 
// modify the current angles   
m_horizontalAngle -= m_yawDelta; 
m_verticalAngle -= m_pitchDelta;     
// ?please explain? I wouldn't modify the source of our input(yet, if ever?)  
// move into rect/window space   
//p.x -= rWindow.left; 
//p.y -= rWindow.top;   
// relative to rect/window centre 
//p.x -= (rWindow.right - rWindow.left)/2;   
//p.y -= (rWindow.bottom-rWindow.top)/2;   
//********* Camera->Update()********************************************// 
//to rotate using euler angles as input.   
glm::mat4 R = glm::yawPitchRoll(m_horizontalAngle, m_verticalAngle,0.0f);      
//Then you could do the following to Update() a camera transformation:   
glm::vec3 T = glm::vec3(0, 0,-dist);   
position = glm::vec3(R * glm::vec4(T,0.0f)); 
m_direction = origin;//glm::normalize(position);  
m_up = glm::vec3(R * glm::vec4(m_real_up, 0.0f)); 
m_right = glm::cross(m_direction,m_up);   
m_viewMatrix = glm::lookAt(position, m_direction,m_up);  
 //************************************************************************//  
 //then update the MVP that typically goes to the shader at render time  
 OnMouseMove(MouseMove(p));        
}

Four important lines here are:

The 2 lines that update the mouse movement since the last movement (would typically be one raw pixel on a responsive ui thread) and,

The 2 lines that Just modify the current angles of camera rotation.

Can you see them?

That makes sense to me so far, what do you think?

Have I missed anything? Give it a go, and let us know maybe.

ps. Ok. Heres's one minor part I missed: where the parameters m_horizontalAngle, m_verticalAngle are converted to radians before being fed to glm::yawPitchRoll() ?

Could be more I missed.

This topic is closed to new replies.

Advertisement