Search code examples
iosopengl-escore-animation

Understanding OpenGL ES: CATransform3DScale instead of glScalef?


So, I'm taking on OpenGL ES this evening and am working through some sample code to become aquainted. I currently am drawing a cube and ran across two methods of doing (seemingly) the same thing:

Method #1 (more verbose)

CATransform3D currentCalculatedMatrix = CATransform3DIdentity;
CATransform3D scaleTransform = CATransform3DScale(currentCalculatedMatrix, 2.0f, 2.0f, 1.0f);
GLfloat currentModelViewMatrix[16];
[self convert3DTransform:&scaleTransform toMatrix:currentModelViewMatrix];
glLoadMatrixf(currentModelViewMatrix);

Method #2 (much less code)

glScalef(2.0f, 2.0f, 1.0f);

Now, it seems that with both of those examples, my cube is now 100% larger. I'm wondering, in what situations would I want to use method #1 of scaling my ModelView matrix rather than the short and sweet method #2? In my very simple example, it seems that both methods do the exact same thing.


Solution

  • In ES 1.0, those two bits of code do nearly the same thing. glScalef is defined to multiply the current matrix by a scaling matrix. glLoadMatrixf is defined to replace the current matrix with the one specified (glMultMatrixf would multiply the current matrix with the one specified rather than replacing it). Assuming you start with the identity matrix on the stack, you therefore end up with the same result given that you supply a scaling matrix when generating it yourself.

    So in ES 1.0 the latter is advantageous for compactness and because it makes it more obvious what you're doing. The approach whereby you multiply in your latest transform rather than replacing whatever is there is also usually the much neater way to handle hierarchies of geometry.

    However, the matrix stack is removed from ES 2.0 and you're left to fend entirely on your own. So if you're thinking of writing code for ES 2.0 in the future it's probably worthwhile having code that can actually build your matrices around. Apple have announced GLKit for iOS 5 which likely deals with that sort of boilerplate stuff, but we won't know until iOS 5 is out of NDA and anything they've added will obviously be available on iOS 5 only.