View Full Version : glRotatef

02-16-2001, 06:47 AM
Im not too good with matrices...so i define my ortho projection matrix like so-

gluOrtho2D(1515.0, 1565.0, -3.0, 15.0);

Now if i wanted to rotate it about the x and y axis in OnMouseMove, im wondering what my rotation matrix should look like. This is my code in OnMouseMove:

m_yRot -= (float)(m_leftDownPos.x - point.x)/3.0f;
m_xRot -= (float)(m_leftDownPos.y - point.y)/3.0f;

And so i have in OnPaint:

glRotatef(m_xRot, ?, 0.0, 0.0);
glRotatef(m_yRot, 0.0, ?, 0.0);

02-16-2001, 12:17 PM
glRotatef(m_xRot, ?, 0.0, 0.0);glRotatef(m_yRot, 0.0, ?, 0.0);

The format is : angle,x,y,z
So just stick in

Or am I not understanding what you want?

02-16-2001, 12:24 PM
Its me not understanding how the rotation works. Originally i used glPerspective in my OnSize...the rotation was fine with the glRotatef parameters you suggest. However, i need to define the clipping region cuz im receiving and plotting real data. So i use glOrtho...but the rotation doesnt work. When i attempt it, my rendering disappears...as though it goes out of view. Thats why i thought id have to change the parameters in glRotate. So if glRotatef(m_xRot,1,0,1) is correct, do u know a reason why the attempted rotation loses my view??

02-16-2001, 01:13 PM
What are you attempting to rotate? Lines, Quads, triangles or what? It is possible that you have "flat" quads/triangles that can only be seen on a certain axis.

You do know you can control the clipping plane don't you? Check out the FAQ at the main page of this site, they have pretty good tips & help on this.

02-22-2001, 11:01 AM
The scene is a graph. So im trying to rotate the entire scene. It's just a bunch of lines. What id like to know is why the rotation works using glPerspective and doesnt when using glOrtho??

02-23-2001, 07:54 AM
Originally posted by Elixer:
...It is possible that you have "flat" quads/triangles that can only be seen on a certain axis...

If this is the problem...how do i prevent it?
I dont think i do have a "flat" scene cuz i use glOrtho with zNear, zFar parameters.

02-23-2001, 08:37 AM
Because.. glOrtho2D is for 2 dimensional applications, how can you expect to rotate when the Z axis is defined from -1.0 to 1.0?

If you're using a 3-dimensional volume and want to use glOrtho.. use
glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far)

Where.. Left,right control X; Top,Bottom control Y; and Near,Far control the Z axis viewing volume.

The camera only "sees" those objects in the volume. Therefore, if you use glOrtho2D (Which sets near to -1.0 and far to 1.0), if you attempt to rotate, you could get some wacked results. Hope this helps (if i understand what you're asking correctly).


02-23-2001, 09:04 AM
gluOrtho2D is not a special function for 2D only. It's as much 3D as glOrtho.

Think about what you say. gluOrtho2D sets the near and far plane to -1.0/1.0. So what if you call glOrtho with those parameters? Does it mean that when near/far is exactly -1.0/1.0, the viewfrustum is 2D, and back to 3D as soon it does not equal -1.0/1.0 any more?

gluOrtho2D is actually calling glOrtho.

02-23-2001, 09:10 AM
Thats what i thought Bob. So do you have any opinions on why i cant rotate? I am still having the same issues as before. My rendering disappears as though it goes out of view...

02-23-2001, 10:00 AM
What is the location of the objects you are trying to rotate? If they are not positioned at the origin when you rotate, they could very easily get rotated out of your view. If they are not positioned at the origin, you will need to do something like so...

glTranslatef(obj.x, obj.y, obj.z);
glRotatef(m_xRot, 1.0, 0.0, 0.0);
glRotatef(m_yRot, 0.0, 1.0, 0.0);
glTranslatef(-obj.x, -obj.y, -obj.z);

Remember that due to the fact that the matrices are post-multiplied to the current matrix, the last transformation is effectively done first. (In terms of world coordinates.)

So... in the above code you first..

Translate it to the origin
Rotate it around the Y-axis
Rotate it around the X-axis
Translate it back to the original position

Depending on what you want for your results, you may have to try changing the order of the rotations.

02-23-2001, 11:08 AM
Thank you Deiussum, your suggestion helps.
You're correct in assuming that i am not positioned at the origin when i rotate. I am getting some rotation...but it seems as though im still going out of my view. Its an odd effect...As i rotate in x or y, the clipping region seems to shrink from both sides of the respective axes im rotating in.
Ill post my code again and hope that the problem is obvious...

void CMyView::OnPaint()
CPaintDC dc(this);


glTranslated(m_xMin, m_yMin, m_zMax);
glRotated(m_xRot, 1.0, 0.0, 0.0);
glRotated(m_yRot, 0.0, 1.0, 0.0);
glTranslated(-m_xMin, -m_yMin, -m_zMax);


if (SwapBuffers(m_pDC->GetSafeHdc()) == FALSE)
void CMyView::OnMouseMove(UINT nFlags, CPoint point)
if(m_Tool == ID_ACTION_ROTATE)
CSize rotate = m_leftDownPos - point;
m_leftDownPos = point;
m_xRot -= rotate.cy;
m_yRot -= rotate.cx;

void CMyView::OnSize(UINT nType, int cx, int cy)
glOrtho(m_xMin, m_xMax, m_yMin, m_yMax, -m_zMax, m_zMax);

where m_zMax is 1.0
Thanx for any suggestions.

02-25-2001, 03:11 PM
Try changing the value of m_zMax to a higher number. I'd probably try to set it to something like (m_xMax - m_xMin)/2. (So that the z depth is the same as the x. Just using -1 to 1, you could get the effect you are seeing.

For instance if the visible size in the x dimension is 5, and you draw a line that goes across the screen... as you rotate that, it's going to get clipped by the zMin, zMax planes because you don't have 5 units of depth for it to display the full line.

02-26-2001, 06:22 AM
Ok..that seems to have been the fix. One more question though. Id like the scene to rotate about the center(origin). So the origin is fixed and the scene rotates about it. What parameters would i need to change? ive been fooling with it...but cant find the correct fix.
Thanks again http://www.opengl.org/discussion_boards/ubb/smile.gif

02-26-2001, 08:41 AM
To get the whole scene to rotate, just do a glRotate after any "camera" transformations and before you do your object-specific transformations.

For instance if you are using gluLookAt to simulate a camera movement (which basically just does a set of glTranslate, glRotates for you) then just do something like so (pseudocode)..

glRotate() // rotate whole scene

for each object in scene
next object

02-26-2001, 09:31 AM
Thanx...all good now http://www.opengl.org/discussion_boards/ubb/biggrin.gif