glRotatef

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);

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
glRotatef(m_xrot,1,0,0);
glRotatef(m_yrot,0,1,0);

Or am I not understanding what you want?

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??

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.

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??

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.

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).

~Dave

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.

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…

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.

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);

 glLoadIdentity();
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 glPushMatrix();
 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);
 RenderScene();
 glPopMatrix();

 glFlush();

 if (SwapBuffers(m_pDC->GetSafeHdc()) == FALSE)
 return;

}
void CMyView::OnMouseMove(UINT nFlags, CPoint point)
{
if(m_Tool == ID_ACTION_ROTATE)
{
if(m_leftButtonDown)
{
CSize rotate = m_leftDownPos - point;
m_leftDownPos = point;
m_xRot -= rotate.cy;
m_yRot -= rotate.cx;

InvalidateRect(NULL,FALSE);
}
}
}
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.

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.

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

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)…

gluLookAt()
glRotate() // rotate whole scene

for each object in scene
glPushMatrix()
object.DoTransformations
object.Draw
glPopMatrix()
next object

Thanx…all good now