Rotating Objects in OpenGL: glRotate or Quaternions?

I'm coding my first engine and I'm trying to have performance in mind since I'll probably use it for mobiles, and I have this question, should you use glRotate or Quaternions for rotating objects in the 3D space?

I have a set of classes that manage the information about all vertices and faces of the objects, and currently when I'm rendering I'm doing:

void DrawObject(Object* object)
{
glTranslate(object->x, object->y, object->z);
glRotate(object->rotationX, 1.0, 0.0, 0.0);
glRotate(object->rotationY, 0.0, 1.0, 0.0);
glRotate(object->rotationZ, 0.0, 0.0, 1.0);

for(int i = 0; i < object->totalFaces; i++)
{
glVertex3f(object->faces[0]->x, object->faces[0]->y, object->faces[0]->z);
glVertex3f(object->faces[1]->x, object->faces[1]->y, object->faces[1]->z);
glVertex3f(object->faces[2]->x, object->faces[2]->y, object->faces[2]->z);
}
}


From what I could understand, glRotate will make the graphics card to do the rotation calculation, while using Quaternions would make the processor do it. But saying I have about 200 objects in a given scene, what is the best acceptable way of handling rotations?

You appear to have some misconceptions about how the graphics pipeline and OpenGL work.

All glRotate does is perform a matrix multiplication and store the result in the OpenGL matrix stack. It doesn't actually cause anything to happen on the GPU. That saved matrix will eventually be sent to the GPU, just like the matrix that would be saved if you using quaternions, turned them into a rotation matrix, and loaded it via glLoadMatrix. The GPU always has to perform a transformation on the geometry you send it, you don't avoid that by using quaternions at all.

There isn't really a useful performance differential between "using glRotate" and "using quaternions." The larger performance concern you probably have is that you are using immediate-mode calls (glVertex) to render. These are the least-efficient means of getting geometry to the card (albeit the simplest); you may want to look into VBOs or similar.

As for your actual question: the graphics pipeline (and OpenGL) only operate in terms of transformation matrices. If you use quaternions, you will need to convert the quaternion representation of your rotation into a matrix representation prior to use; generally one converts a quaternion to an axis-angle rotation matrix, which is exactly the same kind of matrix that glRotate constructs and multiplies on to the matrix stack (but in your case you could just store it to the OpenGL matrix stack using glLoadMatrix).

Although there really isn't going to be a performance difference, I'd recommend you use quaternions for your rotation representation simply for the following reasons:

• You'll get experience with them.
• They have nice properties, such as being more readily interpolatable.
• By relying less on the deprecated fixed-function functions like glRotate, you'll make it easier to eventually move to a more modern version of OpenGL.
• I thought all "gl" commands were processed by the GPU. I wasn't going to use any matrix multiplication, I was planning to use a quaternion function to find where pixel X would be after A graus of rotation in the Yaxis, and I'd store the result within the object and just call the drawing method, but I don't know how which way are better for performance... I guess I'll have to run some tests when I get a full scene completed. Thanks for the VBO info, I'll definitely implement that! – Danicco Jan 21 '13 at 17:03
• In fact, most "gl" functions don't neccessarily cause the GPU to do anything. They just manipulate the CPU-side OpenGL driver state, which is taken into account when you finally tell OpenGL to flush the scene. – user1430 Jan 21 '13 at 17:06
• While you can technically just update the vertex data you send via glVertex (or ultimately VBOs) every frame, doing so is generally rather inefficient. – user1430 Jan 21 '13 at 17:07
• I experienced that glRotate/glTranslate have the same performance issues as glVertex. It's very slow, applying that stuff to the vertices directly or via shader is much faster. – API-Beast Jan 21 '13 at 19:49
• glRotate and glTranslate perform a CPU-side matrix multiplication and write to a state variable which is ultimately sent to the GPU when a frame is rendered (the same as you would do by "sending a matrix to a shader"); they're not inherently slow (but using glVertex is). – user1430 Jan 21 '13 at 20:02