PDA

View Full Version : glRotatef in ortho perspective

question2
06-02-2015, 09:00 AM
Hi all,
I had aspired to use ortho perspective but I've discovered that rotation about the x axis doesn't work properly on my system. Is this to be expected?
Sample code:

theta += 2;
glPushMatrix ();
glTranslatef (50,50,0);
glRotatef (theta,1,0,0);
glBegin (GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex2d(0.0,0.0);
glVertex2d(100,0.0);
glVertex2d(100,100);
glColor3f(0.0, 100, 0.0);
glVertex2d(0.0,0.0);
glVertex2d(0.0,100);
glVertex2d(100,100);
glEnd();
glPopMatrix ();

Alfonse Reinheart
06-02-2015, 09:12 AM
Hi all,
I had aspired to use ortho perspective but I've discovered that rotation about the x axis doesn't work properly on my system.

In what way does it not "work properly"?

question2
06-02-2015, 09:18 AM
In what way does it not "work properly"?

The rectangle (two triangles) appears only at certain angles, e.g. -15 to 15 degrees. Otherwise it completely disappears. If you wanted I could make a video and upload that somewhere.

GClements
06-02-2015, 09:27 AM
It would probably simpler to just post the rest of your code, i.e. the part where the bug is (there's nothing wrong with the fragment you posted).

question2
06-02-2015, 09:50 AM
It would probably simpler to just post the rest of your code, i.e. the part where the bug is (there's nothing wrong with the fragment you posted).

Other than that there is only initialization code. But I think I see what the problem is. Consider the code below. I change the vertex coordinates to be on both sides of the X and Y axes.
However when I rotate about the x-axis, it actually seems to be rotating about a line that is 50 points below the X axis i.e. y=-50. You can try for yourself.

When I change the axis of rotation to Y, a similar thing occurs: glRotatef rotates about a line that is not the Y axis but is maybe x=50.

void draw_scene ()
{
glMatrixMode (GL_PROJECTION);
glOrtho(0.0f, width, height, 0.0f, 0.0f, 1.0f);
glMatrixMode (GL_MODELVIEW);

glDisable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDisable(GL_BLEND);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glColor3f (1.0, 1.0, 1.0);
glClear (GL_COLOR_BUFFER_BIT);

// draw the rectangle
glPushMatrix ();
glTranslatef (100,100,0);
glRotatef (theta,1,0,0);
glScalef (2,2,1);
glBegin (GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex2d(-50.0,-50.0);
glVertex2d(50,-50.0);
glVertex2d(50,50);
glColor3f(1, 1, 0.0);
glVertex2d(-50.0,-50.0);
glVertex2d(-50.0,50);
glVertex2d(50,50);
glEnd();
glPopMatrix ();

glutSwapBuffers ();
}

GClements
06-02-2015, 10:12 AM
Other than that there is only initialization code.

glOrtho(0.0f, width, height, 0.0f, 0.0f, 1.0f);

The rectangle is 100 units across, but the near and far planes are only one unit apart, and the rotation axis is coincident with the near plane. Except when the rectangle is almost exactly vertical, most of it is being clipped away by the near and far planes. Try:

glOrtho(0.0f, width, height, 0.0f, -100.0f, 100.0f);

question2
06-02-2015, 10:21 AM
The rectangle is 100 units across, but the near and far planes are only one unit apart, and the rotation axis is coincident with the near plane. Except when the rectangle is almost exactly vertical, most of it is being clipped away by the near and far planes. Try:

glOrtho(0.0f, width, height, 0.0f, -100.0f, 100.0f);

That fixed it! Thanks.