Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL Rotation

Tags:

c++

opengl

glut

I'm trying to do a simple rotation in OpenGL but must be missing the point. I'm not looking for a specific fix so much as a quick explanation or link that explains OpenGL rotation more generally.

At the moment I have code like this:

glPushMatrix();
  glRotatef(90.0, 0.0, 1.0, 0.0);
  glBegin(GL_TRIANGLES);        
    glVertex3f( 1.0, 1.0, 0.0 );        
    glVertex3f( 3.0, 2.0, 0.0 );        
    glVertex3f( 3.0, 1.0, 0.0 );        
  glEnd();
glPopMatrix();

But the result is not a triangle rotated 90 degrees.

Edit Hmm thanks to Mike Haboustak - it appeared my code was calling a SetCamera function that use glOrtho. I'm too new to OpenGL to have any idea of what this meant but disabling this and rotating in the Z-axis produced the desired result.

like image 671
Matt Mitchell Avatar asked Aug 23 '08 03:08

Matt Mitchell


People also ask

What does GLM rotate do?

The glm::rotate function multiplies this matrix by a rotation transformation of 180 degrees around the Z axis. Remember that since the screen lies in the XY plane, the Z axis is the axis you want to rotate points around.

What is OpenGL scaling?

Scaling allows you to change the size of an object by however much you want it. To perform scaling you need to use the OpenGL function glScalef() which takes as parameters the x, y, and z scale of the object.

What is a quaternion OpenGL?

A quaternion is a set of 4 numbers, [x y z w], which represents rotations the following way: // RotationAngle is in radians x = RotationAxis. x * sin(RotationAngle / 2) y = RotationAxis. y * sin(RotationAngle / 2) z = RotationAxis.


2 Answers

Ensure that you're modifying the modelview matrix by putting the following before the glRotatef call:

glMatrixMode(GL_MODELVIEW);

Otherwise, you may be modifying either the projection or a texture matrix instead.

like image 100
spate Avatar answered Oct 15 '22 11:10

spate


Do you get a 1 unit straight line? It seems that 90deg rot. around Y is going to have you looking at the side of a triangle with no depth.

You should try rotating around the Z axis instead and see if you get something that makes more sense.

OpenGL has two matrices related to the display of geometry, the ModelView and the Projection. Both are applied to coordinates before the data becomes visible on the screen. First the ModelView matrix is applied, transforming the data from model space into view space. Then the Projection matrix is applied with transforms the data from view space for "projection" on your 2D monitor.

ModelView is used to position multiple objects to their locations in the "world", Projection is used to position the objects onto the screen.

Your code seems fine, so I assume from reading the documentation you know what the nature of functions like glPushMatrix() is. If rotating around Z still doesn't make sense, verify that you're editing the ModelView matrix by calling glMatrixMode.

like image 45
Mike Haboustak Avatar answered Oct 15 '22 10:10

Mike Haboustak