Graphics Reference
In-Depth Information
1
2
3
4
5
6
7
8
9
10
11
12
// Prepare to specify the PROJECTION matrix.
glMatrixMode( GL_PROJECTION );
// Reset the PROJ matrix to ignore any previous value.
glLoadIdentity();
// Generate a perspective-proj matrix,
// append the result to the PROJ matrix.
gluPerspective(
45, // y-axis field of view
(640.0/480.0), // ratio of FOV(x) to FOV(y)
0.02, // distance to near clip plane
1000 ); // distance to far clip plane
Note that each OpenGL or GLU matrix-calculation function performs two
actions: (1) calculates the matrix that meets the specification given by the param-
eters, and (2) “appends” (via matrix multiplication) it to the current value of the
matrix that is currently being specified. This is why the call to glLoadIdentity is
important; without it, the calculated perspective matrix would be combined with,
instead of replacing, the current value of the projection matrix.
Now, let's set up the viewing transformation, typically one of the first opera-
tions in the preparation for rendering each frame of the animation. We describe the
camera's position and orientation using a GLU convenience function, providing
parameters equivalent to those used for WPF camera specification:
1
2
3
4
5
6
7
8
9
// Prepare to specify the MODELVIEW matrix.
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// Generate a viewing matrix and append result
// to the MODELVIEW matrix.
gluLookAt( 57,41,247, / * camera position in world coordinates * /
0,0,0,
/ * the point at which camera is aimed * /
0,1,0 );
/ * the "up vector" * /
16.2.6 Drawing Primitives
OpenGL provides several mesh-specification strategies, including the efficient
triangle-strip and triangle-fan techniques described in Chapter 14.
The application is responsible for representing curved surfaces via tessella-
tion into triangles; additionally, the application is expected to provide the vertex
normal for each vertex. Since complex models are typically either computed via
mathematics (in which case the normal is easily derived as part of that algorithm)
or imported by loading a model (which often includes precomputed normal val-
ues), this requirement is rarely inconvenient.
The application can choose from several strategies for transmitting the mesh
specification to the platform, including techniques for managing and using GPU
hardware RAM (e.g., vertex buffers or VBOs, described in Section 15.7.2). In this
example, for simplicity, we use the Compatibility Profile's per-vertex function
calls which are inefficient (and no longer present in the Core API) but popular
in demos and “hello world” programs. To use this strategy as demonstrated in
the code below, set the current material, initiate mesh-specification mode, enu-
merate each vertex one at a time (interleaved with control of the current-vertex-
normal state variable), then end the specification mode. Let's specify the same
solid-yellow pyramid that we constructed in Section 6.2:
 
 
 
Search WWH ::




Custom Search