i
i
i
i
i
i
i
i
322 13. Programming 3D Graphics in Real Time
void initializeRender(HWND hWnd){
GLfloat d[]={1.0,1.0,1.0,1.0}; // diffuse light color
GLfloat a[]={0.1,0.1,0.1,1.0}; // ambient light color
GLfloat s[]={1.0,1.0,1.0,1.0}; // specular light color
GLfloat p[]={0.0,0.0,1.0,1.0}; // (x,y,z,w w=1 => positional light
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
glEnable(GL_LIGHT0); // enable the first (OGL has limited
// number of lights)
glLightfv(GL_LIGHT0,GL_DIFFUSE,d);// set the light parameters
glLightfv(GL_LIGHT0,GL_AMBIENT,a);
glLightfv(GL_LIGHT0,GL_SPECULAR,s);
glLightfv(GL_LIGHT0,GL_POSITION,p);
glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,0.15);
glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,0.05);
glClearDepth(1.0); // setup a Z buffer for hidden
// surface drawing
glEnable(GL_DEPTH_TEST); // a depth of 1.0 is fathest away
glClearColor(gbRed,gbGreen,gbBlue,1.0);
// The following code sets up the view point. It uses some UTILITY
// library functions
RECT oldrect;
GLfloat aspect;
glMatrixMode(GL_PROJECTION); // Going to define the viewing
// transformation
glLoadIdentity();
GetClientRect(hWnd, &oldrect);
aspect = (GLfloat) oldrect.right / (GLfloat) oldrect.bottom;
// A utility library function makes it easier to define a field of
// view 45 degrees,
// an aspect ratio that matches the window dimensions and two clipping planes.
// The clipping planes allow us to restrict which polygons are
// rendered to a volume
// space lying beyond the near_plane and on the viewpoint side of
// the far_plane.
gluPerspective( 45.0, aspect, near_plane, far_plane);
glMatrixMode( GL_MODELVIEW ); // Going to define the object transformation
glViewport(0, 0, oldrect.right,oldrect.bottom); // set to match the window
}
Listing 13.9. (continued).
initializeGL(..), that prepares the environment in which the model will
be viewed. It enables and specifies lighting. It defines the field of view and a
perspective projection with the viewpoint located at (0, 0, 0) and the viewer
looking along the −z-axis; the y-axis is vertical by default. OpenGL uses a
right-handed coordinate system as illustrated in Figure 13.10.
All 3D coordinates are subject to a transformation, which is called the
GL MODELVIEW transformation. This allows hierarchical models to be built