
Rotation using Keyboard/Mouse
Hi,
I have a seemingly basic problem that I can''t quite figure out as I''m
a newbie
I have a model in the centre of my view which I wish to rotate using
keys/mouse. I therefore need to turn a 2d delta/move into a 3d
rotation. I have tried example code from books and can rotate my
model nicely but the effect is non-intuitive. As my model moves its
rotation axis moves with it rather than being relative to the 2d
screen. For example if I rotate my model forward 90 degrees then my
old y axis is now the z axis and the z axis is now the y axis.
The effect I want is if the user now presses the rotate right key the
rotation should now be a rotation around the z axis not the y axis. I
can not use conditional logic as the angles will obviously be
arbitary and not just 90 degrees.
Here is some example code :
LRESULT CGLView::OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam,
BOOL& bHandled)
{
UINT nChar = wParam;
UINT nRepCnt = HIWORD(lParam);
UINT nFlags = LOWORD(lParam);
switch(nChar)
{
case VK_UP:
{
m_xRot-= 5.0f;
if(m_xRot < -1.0f)
{
m_xRot = 355.0f;
}
}
break;
case VK_DOWN:
{
m_xRot += 5.0f;
if(m_xRot > 356.0f)
{
m_xRot = 0.0f;
}
}
break;
case VK_LEFT:
{
m_yRot -= 5.0f;
if(m_yRot < -1.0f)
{
m_yRot = 355.0f;
}
}
break;
case VK_RIGHT:
{
m_yRot += 5.0f;
if(m_yRot > 356.0f)
{
m_yRot = 0.0f;
}
}
break;
}
InvalidateRect(NULL);
UpdateWindow();
return 0;
}
void CGLView::RenderScene(HDC hDC, HGLRC& hRC)
{
wglMakeCurrent(hDC, hRC);
// Clear the color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Save the matrix state and do the rotations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Allow lighting polygon shading ...
// Position light ...
// Translate the whole scene out and into view
glTranslatef(0.0f, 0.0f, -400.0f);
// Perform user view rotations, (in degrees)
glRotatef(static_cast(m_xRot), 1.0f, 0.0f, 0.0f);
glRotatef(static_cast(m_yRot), 0.0f, 1.0f, 0.0f);
// glRotatef(static_cast(m_zRot), 0.0f, 0.0f, 1.0f);
RenderModel();
glLoadIdentity();
// Finish up
glFlush();
}
Thanks
David Marsh

There is a very crude way do do this, but it''s not very nice. You have to remove the glLoadIdentity() at the beginning of your draw code, as well as the 3 viewer rotation. Also, put a glRotatef function under the keys. eg:
case VK_UP
{
glRotatef(-5.0f, 1.0f, 0.0f, 0.0f);
}
This way, the rotations will just keep accumulating and since you don''t clear it, the model will rotate as you want it. around the current position rather than the models position.
- An eye for an eye will make the world go blind -
case VK_UP
{
glRotatef(-5.0f, 1.0f, 0.0f, 0.0f);
}
This way, the rotations will just keep accumulating and since you don''t clear it, the model will rotate as you want it. around the current position rather than the models position.
- An eye for an eye will make the world go blind -
<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement