码迷,mamicode.com
首页 > 其他好文 > 详细

outdated: 16.Cool Looking Fog

时间:2016-07-14 13:26:25      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

这一篇主要说的是雾气。雾气模型,颜色等。

GLuint fogMode[] = {GL_EXP, GL_EXP2, GL_LINEAR};
GLfloat fogColor[4] = {0.5, 0.5, 0.5, 1.0};

GL_LINEAR公式为:

技术分享

GL_EXP公式为:

技术分享

GL_EXP2公式为:

技术分享  

在InitGL()函数中,glFogi()glFogfv()glFogf()函数中各个参数在MSDN中都有详细的解释。

1 glFogi(GL_FOG_MODE, fogMode[fogfilter]);             // Fog mode
2 glFogfv(GL_FOG_COLOR, fogColor);                     // Set fog color
3 glFogf(GL_FOG_DENSITY, 0.35f);                       // How dense will the fog be
4 glHint(GL_FOG_HINT, GL_DONT_CARE);                   // Fog hint value
5 glFogf(GL_FOG_START, 1.0f);                          // Fog start depth
6 glFogf(GL_FOG_END, 5.0f);                            // Fog end depth
7 glEnable(GL_FOG);                                    // Enable GL_FOG

下面为代码,后面有截图,同样修改部分为双行星号内。

  1 #include <windows.h>
  2 #include <stdio.h>
  3 #include <math.h>
  4 #include <gl/glew.h>
  5 #include <gl/glut.h>
  6 #include <GL/GLUAX.H>
  7 #pragma comment(lib, "legacy_stdio_definitions.lib")
  8 /*
  9  *  Every OpenGL program is linked to a Rendering Context.
 10  *  A Rendering Context is what links OpenGL calls to the Device Context.
 11  *  In order for your program to draw to a Window you need to create a Device Context.
 12  *  The DC connects the Window to the GDI (Graphics Device Interface).
 13  */
 14 
 15 HGLRC     hRC = NULL;         // Permanent rendering context
 16 HDC       hDC = NULL;         // Private GDI device context
 17 HWND      hWnd = NULL;        // Holds our window handle
 18 HINSTANCE hInstance;          // Holds the instance of the application
 19 
 20 /*
 21  *  It‘s important to make this global so that each procedure knows if 
 22  *  the program is running in fullscreen mode or not.
 23  */
 24 
 25 bool keys[256];         // Array used for the keyboard routine
 26 bool active = TRUE;     // Window active flag set to TRUE by default
 27 bool fullscreen = TRUE; // Fullscreen flag set to fullscreen mode by default
 28 
 29 /******************************************************************************************************************************************/
 30 /******************************************************************************************************************************************/
 31 bool light;
 32 bool lp;
 33 bool fp;
 34 
 35 GLfloat xrot;
 36 GLfloat yrot;
 37 GLfloat xspeed;
 38 GLfloat yspeed;
 39 GLfloat z = -5.0f;
 40 
 41 GLfloat LightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
 42 GLfloat LightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
 43 GLfloat LightPosition[] = { 0.0f, 0.0f, 2.0f, 1.0f };
 44 
 45 GLuint texture[3];
 46 
 47 bool gp;
 48 GLuint filter;
 49 GLuint fogMode[] = {GL_EXP, GL_EXP2, GL_LINEAR};
 50 GLuint fogfilter = 0;
 51 GLfloat fogColor[4] = {0.5, 0.5, 0.5, 1.0};
 52 /******************************************************************************************************************************************/
 53 /******************************************************************************************************************************************/
 54 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration for WndProc
 55 
 56 AUX_RGBImageRec* LoadBMP(char* Filename)
 57 {
 58     FILE* File = NULL;
 59     
 60     if (!Filename) {
 61         return NULL;
 62     }
 63 
 64     File = fopen(Filename, "r");
 65     if (File) {
 66         fclose(File);
 67         return auxDIBImageLoad(Filename);
 68     }
 69 
 70     return NULL;
 71 }
 72 
 73 int LoadGLTextures()
 74 {
 75     int Status = FALSE;
 76 
 77     AUX_RGBImageRec* TextureImage[1];
 78     
 79     memset(TextureImage, 0, sizeof(void*) * 1);
 80 /******************************************************************************************************************************************/
 81 /******************************************************************************************************************************************/
 82     if (TextureImage[0] = LoadBMP("1.bmp")) {
 83         Status = TRUE;
 84         glGenTextures(3, &texture[0]);
 85 
 86         glBindTexture(GL_TEXTURE_2D, texture[0]);
 87         glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY,
 88             0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
 89         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 90         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 91 
 92 
 93         glBindTexture(GL_TEXTURE_2D, texture[1]);
 94         glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY,
 95             0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
 96         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 97         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 98 
 99         glBindTexture(GL_TEXTURE_2D, texture[2]);
100         gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY,
101             GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
102         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
103         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
104     }
105 /******************************************************************************************************************************************/
106 /******************************************************************************************************************************************/
107     if (TextureImage[0]) {
108         if (TextureImage[0]->data) {
109             free(TextureImage[0]->data);
110         }
111         free(TextureImage[0]);
112     }
113     return Status;
114 }
115 
116 GLvoid ReSizeGLScene(GLsizei width, GLsizei height)   // Resize and initialize the GL window
117 {
118     if (height == 0) {                                // Prevent a divide by zero by
119         height = 1;                                   // Making height equal one
120     }
121     
122     glViewport(0, 0, width, height);                  // Reset the current viewport
123 
124     /*
125      *  The following lines set the screen up for a perspective view. 
126      *  Meaning things in the distance get smaller. This creates a realistic looking scene. 
127      *  The perspective is calculated with a 45 degree viewing angle based on 
128      *  the windows width and height. The 0.1f, 100.0f is the starting point and 
129      *  ending point for how deep we can draw into the screen.
130      *
131      *  The projection matrix is responsible for adding perspective to our scene.
132      *  glLoadIdentity() restores the selected matrix to it‘s original state.
133      *  The modelview matrix is where our object information is stored.
134      *   Lastly we reset the modelview matrix.
135      */
136 
137     glMatrixMode(GL_PROJECTION);                      // Select the projection matrix
138     glLoadIdentity();                                 // Reset the projection matrix
139     
140                                                       // Calculate the aspect ratio of the window
141     gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
142 
143     glMatrixMode(GL_MODELVIEW);                       // Seclet the modelview matrix
144     glLoadIdentity();                                 // Reset the modelview matrix
145 }
146 /******************************************************************************************************************************************/
147 /******************************************************************************************************************************************/
148 int InitGL(GLvoid)                                    // All setup for OpenGL goes here
149 {
150     /*
151      *  Smooth shading blends colors nicely across a polygon, and smoothes out lighting.
152      */
153 
154     if (!LoadGLTextures()) {
155         return FALSE;
156     }
157 
158     glEnable(GL_TEXTURE_2D);
159     glShadeModel(GL_SMOOTH);                          // Enables smooth shading
160     glClearColor(0.5f, 0.5f, 0.5f, 1.0f);             // Black background
161 
162     glClearDepth(1.0f);                               // Depth buffer setup
163 
164     glDepthFunc(GL_LEQUAL);
165     glEnable(GL_DEPTH_TEST);
166 
167     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);   // Really nice perspective calculations
168     
169     glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
170     glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
171     glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
172     glEnable(GL_LIGHT1);
173 
174     glFogi(GL_FOG_MODE, fogMode[fogfilter]);             // Fog mode
175     glFogfv(GL_FOG_COLOR, fogColor);                     // Set fog color
176     glFogf(GL_FOG_DENSITY, 0.35f);                       // How dense will the fog be
177     glHint(GL_FOG_HINT, GL_DONT_CARE);                   // Fog hint value
178     glFogf(GL_FOG_START, 1.0f);                          // Fog start depth
179     glFogf(GL_FOG_END, 5.0f);                            // Fog end depth
180     glEnable(GL_FOG);                                    // Enable GL_FOG
181 
182     return TRUE;
183 }
184 /*
185  *  For now all we will do is clear the screen to the color we previously decided on,
186  *  clear the depth buffer and reset the scene. We wont draw anything yet.
187  */
188 int DrawGLScene(GLvoid)                                  // Here‘s where we do all the drawing
189 {
190     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  // Clear the screen and the depth buffer
191     glLoadIdentity();
192 
193     glTranslatef(0.0f, 0.0f, z);
194 
195     glRotatef(xrot, 1.0f, 0.0f, 0.0f);
196     glRotatef(yrot, 0.0f, 1.0f, 0.0f);
197 
198     glBindTexture(GL_TEXTURE_2D, texture[filter]);
199 
200     glBegin(GL_QUADS);
201         // Front Face
202         glNormal3f(0.0f, 0.0f, 1.0f);
203         glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
204         glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
205         glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
206         glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
207         // Back Face
208         glNormal3f(0.0f, 0.0f, -1.0f);
209         glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
210         glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
211         glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
212         glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);
213         // Top Face
214         glNormal3f(0.0f, 1.0f, 0.0f);
215         glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
216         glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
217         glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 1.0f);
218         glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
219         // Bottom Face
220         glNormal3f(0.0f, -1.0f, 0.0f);
221         glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
222         glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, -1.0f, -1.0f);
223         glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
224         glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
225         // Right face
226         glNormal3f(1.0f, 0.0f, 0.0f);
227         glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);
228         glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
229         glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
230         glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
231         // Left Face
232         glNormal3f(-1.0f, 0.0f, 0.0f);
233         glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
234         glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
235         glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
236         glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
237     glEnd();
238 
239     xrot += xspeed;
240     yrot += yspeed;
241     return TRUE;                                         // everthing went OK
242 }
243 /******************************************************************************************************************************************/
244 /******************************************************************************************************************************************/
245 /*
246  *  The job of KillGLWindow() is to release the Rendering Context, 
247  *  the Device Context and finally the Window Handle. 
248  */
249 
250 GLvoid KillGLWindow(GLvoid)                              // Properly kill the window
251 {
252     if (fullscreen) {                                    // Are we in fullscreen mode
253         
254         /*
255          *  We use ChangeDisplaySettings(NULL,0) to return us to our original desktop.
256          *  After we‘ve switched back to the desktop we make the cursor visible again.
257          */
258 
259         ChangeDisplaySettings(NULL, 0);                  // if so switch back to the desktop
260         ShowCursor(TRUE);                                // Show mouse pointer
261     }
262 
263     if (hRC) {                                           // Do we have a rendering context
264         if (!wglMakeCurrent(NULL, NULL)) {                // Are we able to release the DC and RC contexts
265             MessageBox(NULL, "Release of DC and RC failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
266         }
267 
268         if (!wglDeleteContext(hRC)) {                     // Are we able to delete the RC
269             MessageBox(NULL, "Release rendering context failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
270             hRC = NULL;                                  // Set RC to NULL
271         }
272 
273         if (hDC && !ReleaseDC(hWnd, hDC)) {              // Are we able to release the DC
274             MessageBox(NULL, "Release device context failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
275             hDC = NULL;                                  // Set DC to NULL
276         }
277         if (hWnd && !DestroyWindow(hWnd)) {              // Are we able to destroy the window
278             MessageBox(NULL, "Could not release hWnd.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
279             hWnd = NULL;                                 // Set hWnd to NULL
280         }
281 
282         if (!UnregisterClass("OpenGL", hInstance)) {     // Are we able to unregister class
283             MessageBox(NULL, "Could not register class.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
284             hInstance = NULL;                            // Set hInstance to NULL
285         }
286     }
287 /******************************************************************************************************************************************/
288 /******************************************************************************************************************************************/
289 /******************************************************************************************************************************************/
290 /******************************************************************************************************************************************/
291 }
292 
293 /*
294  * The next section of code creates our OpenGL Window.
295  */
296 
297 BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
298 {
299     /*
300      * Find  a pixel format that matches the one we want
301      */
302     GLuint PixelFormat;                                  // Holds the result after serching for a match
303     
304     /*
305      * Before you create a window, you MUST register a Class for the window
306      */
307     WNDCLASS wc;                                         // Windows class structure
308 
309     /*
310      *  dwExStyle and dwStyle will store the Extended and normal Window Style Information.
311     */
312     DWORD dwExStyle;                                     // Window extend style
313     DWORD dwStyle;                                       // Window style
314 
315     RECT WindowRect;                                     // Grabs rectangle upper left/lower right values
316     WindowRect.left = (long)0;                           // Set left value to 0
317     WindowRect.right = (long)width;                      // Set right value to requested width
318     WindowRect.top = (long)0;                            // Set top value to 0
319     WindowRect.bottom = (long)height;                    // Set bottom value to requested height
320 
321     fullscreen = fullscreenflag;                         // Set the global fullscreen flag
322 
323     /*
324      *  The style CS_HREDRAW and CS_VREDRAW force the Window to redraw whenever it is resized. 
325      *  CS_OWNDC creates a private DC for the Window. Meaning the DC is not shared across applications. 
326      *  WndProc is the procedure that watches for messages in our program. 
327      *  No extra Window data is used so we zero the two fields. Then we set the instance. 
328      *  Next we set hIcon to NULL meaning we don‘t want an ICON in the Window, 
329      *  and for a mouse pointer we use the standard arrow. The background color doesn‘t matter 
330      *  (we set that in GL). We don‘t want a menu in this Window so we set it to NULL, 
331      *  and the class name can be any name you want. I‘ll use "OpenGL" for simplicity.
332      */
333     hInstance = GetModuleHandle(NULL);                   // Grab an instance for our window
334     wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;       // Redraw on move, and own DC for window
335     wc.lpfnWndProc = (WNDPROC)WndProc;                   // WndProc handles message
336     wc.cbClsExtra = 0;                                   // No extra window date
337     wc.cbWndExtra = 0;                                   // No extra window date
338     wc.hInstance = hInstance;                            // set the instance
339     wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);              // Load the default icon
340     wc.hCursor = LoadCursor(NULL, IDC_ARROW);            // Load the arrow pointer
341     wc.hbrBackground = NULL;                             // No background requried for GL
342     wc.lpszMenuName = NULL;                              // We don‘t want a menu
343     wc.lpszClassName = "OpenGL";                         // set the class name
344 
345     if (!RegisterClass(&wc)) {                           // Attempt to register the window class
346         MessageBox(NULL, "Failed to register the window class.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
347         return FALSE;                                    // Exit and return false
348     }
349 
350     if (fullscreen) {                                    // attempt fullsreen model
351         
352         /*
353         T*  here are a few very important things you should keep in mind when switching to full screen mode.
354          *  Make sure the width and height that you use in fullscreen mode is the same as 
355          *  the width and height you plan to use for your window, and most importantly,
356          *  set fullscreen mode BEFORE you create your window.
357          */
358         DEVMODE dmScreenSettings;                        // Device mode
359         memset(&dmScreenSettings, 0, sizeof(dmScreenSettings)); // Make sure memory‘s cleared
360         dmScreenSettings.dmSize = sizeof(dmScreenSettings);     // Size of devmode structure
361         dmScreenSettings.dmPelsWidth = width;            // Select window width
362         dmScreenSettings.dmPelsHeight = height;          // Select window height
363         dmScreenSettings.dmBitsPerPel = bits;            // Select bits per pixel
364         dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
365         
366         /*
367          *  In the line below ChangeDisplaySettings tries to switch to a mode that matches 
368          *  what we stored in dmScreenSettings. I use the parameter CDS_FULLSCREEN when switching modes, 
369          *  because it‘s supposed to remove the start bar at the bottom of the screen, 
370          *  plus it doesn‘t move or resize the windows on your desktop when you switch to 
371          *  fullscreen mode and back.
372          */
373         //Try to set selected mode and get results. Note: CDS_FULLSCREEN gets rid of start bar
374         if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {
375             //If the mode fails, offer two options. Quit or run in a window
376             if (MessageBox(NULL, "The requested fullscreen mode is not supported by\n your video card. Use"
377                 "windowed mode instead?", "GL", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
378             {
379                 fullscreen = FALSE;                       // Select windowed mode (fullscreen=FLASE)
380             }
381             else {
382                 // Pop up a message box letting user know the programe is closing.
383                 MessageBox(NULL, "Program will now close.", "ERROR", MB_OK | MB_ICONSTOP);
384                 return FALSE;                             // Exit and return FALSE
385             }
386         }
387     }
388 
389     if (fullscreen) {                                     // Are we still in fullscreen mode
390         
391         /*
392          *  If we are still in fullscreen mode we‘ll set the extended style to WS_EX_APPWINDOW, 
393          *  which force a top level window down to the taskbar once our window is visible. 
394          *  For the window style we‘ll create a WS_POPUP window. 
395          *  This type of window has no border around it, making it perfect for fullscreen mode.
396 
397          *  Finally, we disable the mouse pointer. If your program is not interactive, 
398          *  it‘s usually nice to disable the mouse pointer when in fullscreen mode. It‘s up to you though.
399          */
400         dwExStyle = WS_EX_APPWINDOW;                      // Window extended style
401         dwStyle = WS_POPUP;                               // Window style
402         ShowCursor(FALSE);                                // Hide mosue pointer 
403     }
404     else {
405 
406         /*
407          *  If we‘re using a window instead of fullscreen mode, 
408          *  we‘ll add WS_EX_WINDOWEDGE to the extended style. This gives the window a more 3D look. 
409          *  For style we‘ll use WS_OVERLAPPEDWINDOW instead of WS_POPUP. 
410          *  WS_OVERLAPPEDWINDOW creates a window with a title bar, sizing border, 
411          *  window menu, and minimize / maximize buttons.
412          */
413         dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;   // Window extended style
414         dwStyle = WS_OVERLAPPEDWINDOW;                    // Window style
415     }
416 
417     /*
418      *  By using the AdjustWindowRectEx command none of our OpenGL scene will be covered up by the borders, 
419      *  instead, the window will be made larger to account for the pixels needed to draw the window border. 
420      *  In fullscreen mode, this command has no effect.
421      */
422     AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);  // Adjust window to true resqusted
423     
424     /*
425      *  WS_CLIPSIBLINGS and WS_CLIPCHILDREN are both REQUIRED for OpenGL to work properly. 
426      *  These styles prevent other windows from drawing over or into our OpenGL Window.
427      */
428     if (!(hWnd = CreateWindowEx(dwExStyle,                // Extended style for the window
429         "OpenGL",                                         // Class name
430         title,                                            // Window title
431         WS_CLIPSIBLINGS |                                 // Requried window style
432         WS_CLIPCHILDREN |                                 // Requried window style
433         dwStyle,                                          // Select window style
434         0, 0,                                             // Window position
435         WindowRect.right - WindowRect.left,               // Calculate adjusted window width
436         WindowRect.bottom - WindowRect.top,               // Calculate adjusted window height
437         NULL,                                             // No parent window
438         NULL,                                             // No menu
439         hInstance,                                        // Instance
440         NULL)))                                           // Don‘t pass anything to WM_CREATE
441     {
442         KillGLWindow();                                   //Reset the display
443         MessageBox(NULL, "Window creation error.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
444         return FALSE;                                     // Retrurn FALSE;
445     }
446 
447     /*
448      *  aside from the stencil buffer and the (slow) accumulation buffer
449      */
450     static PIXELFORMATDESCRIPTOR pfd =                    // pfd tells windows how we want things to be 
451     {
452         sizeof(PIXELFORMATDESCRIPTOR),                    // Size of this pixel format descriptor
453         1,                                                // Version number
454         PFD_DRAW_TO_WINDOW |                              // Format must support window
455         PFD_SUPPORT_OPENGL |                              // Format must support OpenGL
456         PFD_DOUBLEBUFFER,                                 // Must support double buffer
457         PFD_TYPE_RGBA,                                    // Request an RGBA format
458         bits,                                             // Select our color depth
459         0, 0, 0, 0, 0, 0,                                 // Color bits ignored
460         0,                                                // No alpha buffer
461         0,                                                // shift bit ignored
462         0,                                                // No accumulation buffer
463         0, 0, 0, 0,                                       // Accumulation bits ignored
464         16,                                               // 16Bits Z_Buffer (depth buffer)
465         0,                                                // No stencil buffer
466         0,                                                // No auxiliary buffer
467         PFD_MAIN_PLANE,                                   // Main drawing layer
468         0,                                                // Reserved
469         0, 0, 0                                           // Layer makes ignored
470     };
471 
472     if (!(hDC = GetDC(hWnd))) {                           // Did we get a device context
473         KillGLWindow();                                   // Reset the display
474         MessageBox(NULL, "Can‘t create a GL device context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
475         return FALSE;                                     // Return FALSE
476     }
477 
478     if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd))) {  // Did window find a matching pixel format
479         KillGLWindow();                                   // Reset the display
480         MessageBox(NULL, "Can‘t find a suitable pixelformat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
481         return FALSE;                                     // Return FALSE;
482     }
483 
484     if (!SetPixelFormat(hDC, PixelFormat, &pfd)) {        // Are we able to set the pixel format
485         KillGLWindow();                                   // Reset the display
486         MessageBox(NULL, "Can‘t set the pixelformat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
487         return FALSE;                                     // Return FALSE;
488     }
489 
490     if (!(hRC = wglCreateContext(hDC))) {                 // Are we able to rendering context
491         KillGLWindow();                                   // Reset the display
492         MessageBox(NULL, "Can‘t create a GL rendering context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
493         return FALSE;                                     // Return FASLE;
494     }
495 
496     if (!wglMakeCurrent(hDC, hRC)) {                      // Try to activate the rendering context
497         KillGLWindow();                                   // Reset the display
498         MessageBox(NULL, "Can‘t activate the GL rendering context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
499         return FALSE;                                     // Return FALSE    
500     }
501 
502     /*
503      *  ReSizeGLScene passing the screen width and height to set up our perspective OpenGL screen.
504      */
505     ShowWindow(hWnd, SW_SHOW);                            // Show the window
506     SetForegroundWindow(hWnd);                            // slightly higher priority
507     SetFocus(hWnd);                                       // Sets keyboard focus to the window
508     ReSizeGLScene(width, height);                         // Set up our perspective GL screen
509 
510 /*
511  *  we can set up lighting, textures, and anything else that needs to be setup in InitGL().
512  */
513 if (!InitGL()) {                                      // Initialize our newly created GL window
514     KillGLWindow();                                   // Reset the display
515     MessageBox(NULL, "Initialize Failed.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
516     return FALSE;                                     // Return FALSE
517 }
518 return TRUE;
519 }
520 
521 LRESULT CALLBACK WndProc(HWND hWnd,                       // Handle for this window
522     UINT uMsg,                                            // Message for this window
523     WPARAM wParam,                                        // Additional message information
524     LPARAM lParam)                                        // Additional message information
525 {
526     switch (uMsg) {                                       // Check for window message
527     case WM_ACTIVATE: {                               // Check minimization state
528         if (!HIWORD(wParam)) {
529             active = TRUE;                            // Program is active
530         }
531         else {
532             active = FALSE;                           // Program is no longer active
533         }
534         return 0;                                     // Return to the message loop
535     }
536     case WM_SYSCOMMAND: {                             // Intercept system commands
537         switch (wParam) {                             // Check system calls
538         case SC_SCREENSAVE:                       // Screensaver trying to start
539         case SC_MONITORPOWER:                     // Monitor trying to enter powersave
540             return 0;                                 // Prevent form happening
541         }
542         break;                                        // Exit
543     }
544     case WM_CLOSE: {                                  // Did we receive a close message
545         PostQuitMessage(0);                           // Send a quit message
546         return 0;
547     }
548     case WM_KEYDOWN: {                                // Is a key being held down
549         keys[wParam] = TRUE;                          // if so, mark it as TRUE
550         return 0;                                     // Jump back
551     }
552     case WM_KEYUP: {                                  // Has a key been released
553         keys[wParam] = FALSE;                         // if so, mark it as FALSE
554         return 0;                                     // Jump back
555     }
556     case WM_SIZE: {                                   // Resize the OpenGL window
557         ReSizeGLScene(LOWORD(lParam), HIWORD(lParam));   // LoWord = width HiWord = height
558         return 0;                                     // Jump back
559     }
560     }
561     return DefWindowProc(hWnd, uMsg, wParam, lParam);     // Pass all unhandled message to DefWindwProc
562 }
563 
564 int WINAPI WinMain(HINSTANCE hInstance,                   // Instance
565     HINSTANCE hPrevInstance,                              // Previous instance
566     LPSTR lpCmdLine,                                      // Command line parameters
567     int nCmdShow)                                         // Window show state
568 {
569     MSG msg;                                              // Window message structure
570     BOOL done = FALSE;                                    // Bool variable to exit loop
571                                                           // Ask the user which screen mode they prefer
572     if (MessageBox(NULL, "Would you like to run in fullscreen mode?",
573         "Start fullscreen?", MB_YESNO | MB_ICONQUESTION) == IDNO)
574     {
575         fullscreen = FALSE;                               // Window mode
576     }
577     // Create our OpenGL window
578     if (!CreateGLWindow("3D Shapes", 640, 480, 16, fullscreen)) {  // (Modified)
579         return 0;                                         // Quit if window was not create
580     }
581 
582     while (!done) {                                       // Loop that runs until donw = TRUE
583         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {   // Is there a message wating
584             if (msg.message == WM_QUIT) {                 // Havw we received a quit message
585                 done = TRUE;                              // if so done  = TRUE
586             }
587             else {                                        // If not, deal with window message
588                 TranslateMessage(&msg);                   // Translate message
589                 DispatchMessage(&msg);                    // Dispatch message
590             }
591         }
592         else {
593             // Draw the scene. Watch for ESC key and quit message from DrawGLScene()
594             if (active) {                                 // Program active
595                 if (keys[VK_ESCAPE]) {                    // Was ESC pressed
596                     done = TRUE;                          // ESC signalled a quit
597                 }
598                 else {                                    // Not time to quit, update screen
599                     DrawGLScene();                        // Draw scene
600                     SwapBuffers(hDC);                     // Swap buffers (double buffering)
601                 }
602             }
603 /******************************************************************************************************************************************/
604 /******************************************************************************************************************************************/
605             if (keys[L] && !lp) {
606                 lp = TRUE;
607                 light = !light;
608                 if (!light) {
609                     glDisable(GL_LIGHTING);
610                 }
611                 else {
612                     glEnable(GL_LIGHTING);
613                 }
614             }
615             if (!keys[L]) {
616                 lp = FALSE;
617             }
618 
619             if (keys[F] && !fp) {
620                 fp = TRUE;
621                 filter += 1;
622                 if (filter > 2) {
623                     filter = 0;
624                 }
625             }
626             if (!keys[F]) {
627                 fp = FALSE;
628             }
629 
630             if (keys[VK_SUBTRACT]) {
631                 z -= 0.005f;
632             }
633             if (keys[VK_ADD]) {
634                 z += 0.005f;
635             }
636             if (keys[VK_UP]) {
637                 xspeed -= 0.0005f;
638             }
639             if (keys[VK_DOWN]) {
640                 xspeed += 0.0005f;
641             }
642             if (keys[VK_LEFT]) {
643                 yspeed -= 0.0005f;
644             }
645             if (keys[VK_RIGHT]) {
646                 yspeed += 0.0005f;
647             }
648 
649             if (keys[G] && !gp) {
650                 gp = TRUE;
651                 fogfilter += 1;
652                 if (fogfilter > 2) {
653                     fogfilter = 0;
654                 }
655                 glFogi(GL_FOG_MODE, fogMode[fogfilter]);
656             }
657             if (keys[G]) {
658                 gp = FALSE;
659             }
660 /******************************************************************************************************************************************/
661 /******************************************************************************************************************************************/
662             /*
663             *  It allows us to press the F1 key to switch from fullscreen mode to
664             *  windowed mode or windowed mode to fullscreen mode.
665             */
666             if (keys[VK_F1]) {                            // Is F1 being pressed
667                 keys[VK_F1] = FALSE;                      // If so make key FASLE
668                 KillGLWindow();                           // Kill our current window
669                 fullscreen = !fullscreen;                 // Toggle fullscreen / window mode
670                 //Recreate our OpenGL window(modified)
671                 if (!CreateGLWindow("3D Shapes", 640, 480, 16, fullscreen)) {
672                     return 0;                             // Quit if window was not create
673                 }
674             }
675         }
676     }
677     // Shutdown
678     KillGLWindow();                                       // Kill the window
679     return (msg.wParam);                                  // Exit the program
680 }

技术分享

Thanks for Nehe‘s tutorials, this is his home.

outdated: 16.Cool Looking Fog

标签:

原文地址:http://www.cnblogs.com/clairvoyant/p/5670006.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!