Pro OpenGL ES for iOS

(singke) #1

72 CHAPTER 3: Building a 3D World^


immediately as called. Following is the same transformation sequence, but in OpenGL
ES 2:
baseModelViewMatrix = GLKMatrix4Scale(baseModelViewMatrix,scale,scale,scale);
baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, _rotation, 1.0, 0.5,
0.0);
modelviewMatrix = GLKMatrix4MakeTranslation(0.0, offset, -6.0);
modelviewMatrix = GLKMatrix4Multiply(modelviewMatrix, baseModelViewMatrix);
One final transformation command to be aware of right now is glScalef(), used for
resizing the model along all three axes. Let’s say you need to double the height of the
cube. You would use the line glScalef(1,2,1). Remember that the height is aligned with
the Y-axis, while width and depth are X and Z, which we don’t want to touch.
Now the question is, where would you put the line to ensure that the geometry of the
cube is the only thing affected, as in Figure 3-11 (left), before or after the calls to
glRotatef() in drawInRect()?
I f y o u s a i d a f t e r -----as in the following example:
glTranslatef(0.0, (GLfloat)(sinf(transY)/2.0), z);

glRotatef(spinY, 0.0, 1.0, 0.0);
glRotatef(spinX, 1.0, 0.0, 0.0);
glScalef(1,2,1);
-----you’d be right. The reason why this works is that because the last transformation in
the list is actually the first to be executed, you must put scaling ahead of any other
transformations if all you want is to resize the object’s geometry. Put it anywhere else,
and you could end up with something like Figure 3-11 (right). So, what’s happening
there? The following was generated with the code snippet:
glTranslatef(0.0, (GLfloat)(sinf(transY)/2.0), z);

glScalef(1,2,1);

glRotatef(spinY, 0.0, 1.0, 0.0);
glRotatef(spinX, 1.0, 0.0, 0.0);
Free download pdf