Pro OpenGL ES for iOS

(singke) #1

CHAPTER 3: Building a 3D World (^67)
Defining the Frustum
The last step we need is to specify details on how the vertices are mapped to our screen
using glFrustumf(), as in Listing 3-5. If you’ll recall, the frustum is made of six planes
that enclose the volume that specifies what we can see, something like a camera’s lens.
Listing 3-5. Creating the viewing frustum, added to the View Controller file
-(void)setClipping
{
float aspectRatio;
const float zNear = .1; //1
const float zFar = 1000; //2
const float fieldOfView = 60.0; //3
GLfloat size;
CGRect frame = [[UIScreen mainScreen] bounds]; //4
//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.
aspectRatio=(float)frame.size.width/(float)frame.size.height; //5
//Set the OpenGL projection matrix.
glMatrixMode(GL_PROJECTION); //6
glLoadIdentity();
size = zNear * tanf(GLKMathDegreesToRadians (fieldOfView) / 2.0); //7
glFrustumf(-size, size, -size /aspectRatio, size /aspectRatio, zNear, zFar); //8
glViewport(0, 0, frame.size.width, frame.size.height); //9
//Make the OpenGL ModelView matrix the default.
glMatrixMode(GL_MODELVIEW); //10
}

Free download pdf