[ Pobierz całość w formacie PDF ]
glBegin(GL_POLYGON);
...
glEnd();
glBegin(GL_POLYGON);
...
glEnd();
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, BlueSurface);
glBegin(GL_POLYGON);
...
glEnd();
glBegin(GL_POLYGON);
...
glEnd();
These new calls make two of the cube faces red, two faces green, and two faces blue. The commands
set the ambient color for front and back of each face. However, the cube will still appear featureless until
the lighting model is enabled. To do this add the following command to the end of
CGLSample4View::OnSize:
glEnable(GL_LIGHTING);
Compile and run the program. You should see one of the blue faces of the cube. Rotate the cube with
your mouse. You will notice the cube looks very strange. Faces seem to appear and disappear at
random. This is because we are simply drawing the faces of the cube with no regard as to which is in
front. When we draw a face that is in back, it draws over any faces in front of it that have been drawn. The
solution to this problem is z-buffering.
The z-buffer holds a value for every pixel on the screen. This value represents how close that pixel is to
the eye point. Whenever OpenGL attempts to draw to a pixel, it checks the z-buffer to see if the new color
is closer to the eye point than the old color. If it is the pixel is set to the new color. If not, then the pixel
retains the old color. As you can guess, z-buffering can take up a large amount of memory and CPU time.
The cDepthBits parameter in the PIXELFORMATDESCRIPTOR we used in SetWindowPixelFormat
defines the number of bits in each z-buffer value. Enable z-buffering by adding the following command at
the end of OnSize:
glEnable(GL_DEPTH_TEST);
We also need to clear the z-buffer when we begin a new drawing. Change the glClear command in
RenderScene to the following:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Compile and run the program to see the results.
We now have a colorful cube that rotates in space and draws correctly, but it is very faint. Let's add a light
to the scene so that we can see the cube better. Add the following declaration to the beginning of
RenderScene:
GLfloat LightAmbient[] = { 0.1f, 0.1f, 0.1f, 0.1f };
GLfloat LightDiffuse[] = { 0.7f, 0.7f, 0.7f, 0.7f };
GLfloat LightSpecular[] = { 0.0f, 0.0f, 0.0f, 0.1f };
GLfloat LightPosition[] = { 5.0f, 5.0f, 5.0f, 0.0f };
These will serve as the property values for our light. Now add the following commands just after glClear in
RenderScene:
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpecular);
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
glEnable(GL_LIGHT0);
glLight defines properties for light sources. OpenGL's light sources are all created within the
implementation of OpenGL. Each light source has an identifier GL_LIGHTi where i is zero to
GL_MAX_LIGHTS. The above commands set the ambient, diffuse, and specular properties, as well as
the position, of light zero. glEnable(GL_LIGHT0) turns on the light.
The program is currently wasting time by drawing the interior faces of the cube with our colored surfaces.
To fix this, change the GL_FRONT_AND_BACK parameter in all of the glMaterialfv calls to GL_FRONT.
We also want to set the diffuse reflectivity of the cube faces now that we have a light source. To do this,
change the GL_AMBIENT parameter in the glMaterialfv calls to GL_AMBIENT_AND_DIFFUSE. Compile
and run the program.
You now have a program that displays a lighted, multi-colored cube in three dimensions that uses z-
buffering and double buffering. Go ahead and pat yourself on the back. You deserve it.
Conclusion
This concludes the construction of GLSample4 and this tutorial. You should now know how to set up an
OpenGL program in Windows, and should also understand some of the basic graphics commands. If you
wish to explore OpenGL further, I recommend studying the sample programs in the Microsoft Platform
SDK. If you would like to learn more about graphics in general, I recommend the following books. It really
is necessary to understand the basics of the material in either of these books if you want to do any
serious 3-D graphics.
1. Foley, J. D. and Dam, A. V. and Feiner, S. K. and Hughes., J. F. Computer Graphics, Principles
and Practice. Addison-Wesley Publishing Company: Reading, Massachusetts, 1990
2. Hill, F. S. Computer Graphics. MacMillian Publishing Company: New York, 1990.
You may also visit these sites to learm more about OpenGL programming:
" www.sgi.com/software/opengl/
" msdn.microsoft.com/library/default.asp?URL=/library/psdk/opengl/int01_2v58.htm
" Microsoft also offers the following OpenGL articles in msdn.microsoft.com :
Windows NT OpenGL: Getting Started
OpenGL I: Quick Start
OpenGL II: Windows Palettes in RGBA Mode
OpenGL III: Building an OpenGL C++ Class
OpenGL IV: Color Index Mode
OpenGL V: Translating Windows DIBs
Usenet Graphics Related FAQs
SAMPLE: MFCOGL a Generic MFC OpenGL Code Sample (Q127071)
I would appreciate any comments or suggestions for this tutorial. Please email me at
naoursla@bellsouth.net. Thanks!
[ Pobierz całość w formacie PDF ]
pobieranie ^ do ÂściÂągnięcia ^ pdf ^ download ^ ebook
Menu
- Home
- Visual_Basic_NET_Ksiega_eksperta_vbnetk
- WhiteFeather Sheri Na pewno mnie pokochasz
- Dick Philip K Transmigracja Timothyego Archera
- MacLean Alistair StraceśÂcy z Navarony
- Lovecraft_H.P._ _Szepczć cy w ciemnosci H. P. Lovecraft
- 6.Michael Moorcock Zemsta Róśźy
- Heinlein, Robert A Assignment in Eternity
- English_Idioms_and_Expressions_Tests
- 027 Bond Stephanie Nic gorszego sie nie zdarzy
- 274. Jordan Penny Ucieczka na SycylićÂ
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- monissiaaaa.pev.pl
Cytat
Fallite fallentes - okłamujcie kłamiących. Owidiusz
Diligentia comparat divitias - pilność zestawia bogactwa. Cyceron
Daj mi właściwe słowo i odpowiedni akcent, a poruszę świat. Joseph Conrad
I brak precedensu jest precedensem. Stanisław Jerzy Lec (pierw. de Tusch - Letz, 1909-1966)
Ex ante - z przed; zanim; oparte na wcześniejszych założeniach.