Pro OpenGL ES for iOS

(singke) #1

48 CHAPTER 2: All That Math Jazz^


search through your object list to find which was the most likely target. Untransforming
something requires you to do everything backward. And this is done in the following
way:


  1. Multiply your Modelview matrix with your Projection matrix.

  2. Invert the results.

  3. Convert the screen coordinates of the touch point into the frame of
    reference for your viewport.

  4. Take the results of that and multiply it by the inverted matrix from
    step 2.
    Don’t worry, this will be covered in more detail later in the book.


MATH IN ACTION

Let’s prove that the previous math stuff really is what’s going on in OpenGL ES.
Add the following code somewhere in your Chapter 1 exercise so that you know it will be called—after
OpenGL has been initialized, of course. The best place is at the top of the drawFrame method:
GLfloat mvmatrix[16];

glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); //1
glGetFloatv(GL_MODELVIEW_MATRIX,mvmatrix);
glRotatef(30, 1.0, 0.0, 0.0); //2
glGetFloatv(GL_MODELVIEW_MATRIX,mvmatrix); //3
glRotatef(60, 1.0, 0.0, 0.0); //4
glGetFloatv(GL_MODELVIEW_MATRIX,mvmatrix); //5
Put breakpoints after each call to glGetFloatv() and run.
Line 1 simply initializes the matrix to an unrotated state. Advance to line 2 after fetching the contents of
the current matrix and then examine them in the debugger. You should see something like this:













0 0 0 1


0 0 1 0


0 1 0 0


1 0 0 0


Line 2 rotates the matrix 30 degrees around the x-axis. (I’ll cover glRotatef() in the next chapter.) Go to
line 4 and do the same. What do you see?
Free download pdf