Pro OpenGL ES for iOS

(singke) #1

68 CHAPTER 3: Building a 3D World^


Here is what is happening:
„ Lines 1 and 2 specify the distances of the near and far clipping planes.
These two values say that anything further than 1,000 or closer than .1
will be filtered out. (‘‘A thousand what?’’ you might ask. Just a
thousand; the units can be up to you. They could be light-years or
cubits, it doesn’t matter.)
„ Line 3 sets the field of view to 60 degrees.
„ Lines 4 and 5 calculate the aspect ratio of the final screen. Its height
and width values clamp the FOV to the height; flipping it would make it
relative to the width. So if we want the field-of-view to be 60 degrees,
similar to that of a wide-angle lens, it will be based on the height of our
window and not the width. This is important to know when rendering
to a non-square screen.
„ Because the frustum affects the projection matrix, we need to ensure
that it is activated instead of the modelview matrix, in line 6.
„ Line 7 has the duty of calculating a size value needed to specify the
left/right and top/bottom limits of the viewing volume, as shown in
Figure 3-3. This can be thought of as your virtual window into the 3D
space. With the center of the screen being the origin, you need to go
from ----size to +size in both dimensions. That is why the field is divided
by two------the window will go from -30 degrees to +30 degrees.
Multiplying size by zNear merely adds a scaling hint of sorts. Finally,
divide the bottom/top limits by the aspect ratio to ensure your square
will really be a square.
„ Now in line 8, we can plug those values into glFrustumf(); and in line
9, pass the actual pixel dimension of the viewport.
„ Don’t forget to reset the matrix mode back to modelview just to be a
good neighbor, as in line 10.
SetClipping() needs to be called only once at the start unless you want to change the
‘‘power’’ of your lens. More complex situations might need to vary the zNear/zFar values
to handle variances in depth or to use a different field of view to zoom in on a specific
target. But you can add it into viewDidLoad() with the following two lines:
[EAGLContext setCurrentContext:self.context];

[self setClipping];
Free download pdf