[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Rotating in Perspective Projection

Posted on 2004-04-29
4
Medium Priority
?
1,313 Views
Last Modified: 2013-12-06
Hi everyone,

I currently have an OpenGL program which draws objects in the center of my display area.  I rotate my objects by setting up x and y rotating variables on the arrows key press command such as:
                                       case WM_KEYDOWN:
                  {
                  if(wParam == VK_UP)
                        xRot-= 5.0f;
      
                  if(wParam == VK_DOWN)
                        xRot += 5.0f;

                  if(wParam == VK_LEFT)
                        yRot -= 5.0f;

                  if(wParam == VK_RIGHT)
                        yRot += 5.0f;

                  if(xRot > 356.0f)
                        xRot = 0.0f;

                  if(xRot < -1.0f)
                        xRot = 355.0f;

                  if(yRot > 356.0f)
                        yRot = 0.0f;

                  if(yRot < -1.0f)
                        yRot = 355.0f;

and then in my RenderScene function I simply call:
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);

The object rotates in place in orthographic rotation mode and this is what I want. But when I switch to perspective projection mode, my object rotates in and out of view in both the x and y directions. Suggestions to fix this?
Thanks.
0
Comment
Question by:fsalcedo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 2

Accepted Solution

by:
SWortham earned 1000 total points
ID: 10969872
It sounds like you're also using glTranslatef() after you make the glRotatef() calls.  If this is the case, then try moving your call to glTranslatef() so that it's before the rotation calls.  

So it should be something like this:
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -5.0f);
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);

The order in which you translate and rotate is important in perspective mode.  Think of it this way.  If you call glTranslatef(0,0,-5) first to position the object in front of your viewport, and then rotate the object it will rotate around the point or axis of 0, 0, -5.  But if you rotate the object first, it will rotate around 0,0,0 and then when you call glTranslatef(0,0,-5) afterwards it will basically move the object in the opposite direction in which it was rotated (causing it to move all over the place in & out of your viewport).
0
 
LVL 2

Assisted Solution

by:enkimute
enkimute earned 1000 total points
ID: 10970678
Here in belgium, they teach childeren to read matrix multiplication not as 'multiply by' but as 'multiply after'. I still use the same trick for
opengl ..

You should make it a reflex .. whenever you see glRotate, glTranslate or glScale, just immeadiately think 'after' .. so the piece
of code

gltranslate(...)
glrotate(..,1,0,0)
glrotate(..,0,1,0)

should be read as 'a translate AFTER a rotate around x AFTER a rotate around y) ..

this way its a lot less confusing .. first you rotate the object then you translate it.

also, it would be somewhat more common to order your rotate y,x,z or heading, pitching, banking. This is
important because of the same reason .. doing these multiplications in a different order yields a different result.
So, in other words .. if one writes a rotation vector as (30,20,10) .. he means :

  glRotatef(30,0,1,0);
  glRotatef(20,1,0,0);
  glRotatef(10,0,0,1);

and not

  glRotatef(30,1,0,0);
  glRotatef(20,0,1,0);
  glRotatef(10,0,0,1);

both will give different results !




0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question