![](http://images-20200215.ebookreading.net/11/4/4/9781568814773/9781568814773__a-hitchhikers-guide__9781568814773__bg148.png)
i
i
i
i
i
i
i
i
13.1. Visualizing a Virtual World Using OpenGL 311
device driver, which actually implements the OpenGL functionality. With
this component architecture, taking advantage of any new functionality pro-
vided by graphics card vendors is just a matter of replacing the driver with
another version.
Now that we have a fair understanding of how OpenGL works, we can
get down to some detail of how to use it to visualize a virtual environment.
13.1.2 Rendering OpenGL in a Windows Window
The template code in Appendix D shows that to render within a Windows
window, an application program must pro vide a handler for the
WM PAINT
message, obtain a drawing device context for the client area and use it to
render the lines, bitmaps or whatever. In platform-independent OpenGL,
there is no such thing as a device context. To draw something, one simply
issues calls to the required OpenGL functions, as in:
glClear(GL_BUFFER); // clear the screen
glBegin(GL_TRIANGLES); // Draw a single triangle - just as an example,
glVertex3f(0.0.0,-1.0); // it may not be visible until we describe
glVertex3f(0.0.0,-1.0); // the view point correctly.
glVertex3f(0.0.0,-1.0); // 3 calls to glVertex... make up a triangle
glEnd(); // match the call to glBegin()
glFlush(); // flush the OpenGL buffers
glFinish(); // finish drawing and make it visible
Drawing directly to the screen like this can give an unsatisfactory result.
Each time we draw, we start by clearing the screen and then render one poly-
gon after another until the picture is complete. This will give the screen the
appearance of flickering, and you will see the polygons appearing one by one.
This is not what we want; a 3D solid object does not materialize bit by bit. To
solve the problem, we use the technique of double buffering. Two copies of
the frame buffer exist; one is visible (appears on the monitor) and one is hid-
den. When we render with OpenGL, we render into the hidden buffer, and
once the drawing is complete, we swap them over and start drawing again
in the hidden buffer. The swap can take place imperceptibly to the viewer
because it is just a matter of swapping a pointer during the display’s vertical
blanking interval. All graphics hardware is capable of this.
Under Windows, there is a very small subset of platform-specific OpenGL
functions that have names beginning
wgl. These allow us to make an implicit
connection between the Windows window handle hWnd and OpenGL’s draw-