首页 > 代码库 > outdated: 41.Volumetric Fog & IPicture Image Loading

outdated: 41.Volumetric Fog & IPicture Image Loading

雾气的实现在Cool Looking Fog中提到过,不过怎么实现雾气随距离的变化?

在Initialize()函数中首先设置fog的褪色,颜色,开始透明度和结尾透明度等等。如下,

glEnable(GL_FOG);glFogi(GL_FOG_MODE, GL_LINEAR);            // Fog fade is linearglFogfv(GL_FOG_COLOR, fogColor);           // Set the colorglFogf(GL_FOG_START, 0.0f);                // Set the fog start (least dense)glFogf(GL_FOG_END, 1.0f);                  // Set the fog end (most dense)glHint(GL_FOG_HINT, GL_NICEST);            // Per-pixel fog calculationglFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);   // Set fog based on vertice coordinate

glFogi()函数中的参数在Main文件头定义了,

#define GL_FOG_COORDINATE_SOURCE_EXT 0x8450#define GL_FOG_COORDINATE_EXT 0x8451

在画雾气时,也就是Draw()函数中,glFogCoordfEXT()函数是用来设置fog的,这个函数用wglGetProcAddress()函数来得到。具体如下,

glFogCoordfEXT = (PFNGLFOGCOORDFEXTPROC)wglGetProcAddress("glFogCoordfEXT");

在Main文件开头定义了PFNGLFOGCOORDFEXTPROC,

typedef void(APIENTRY* PFNGLFOGCOORDFEXTPROC) (GLfloat coord);   // Declare function prototypePFNGLFOGCOORDFEXTPROC glFogCoordfEXT = NULL;      // glFogCoordfEXT function

技术分享


代码如下,

技术分享
#ifndef GL_FRAMEWORK_INCLUDED#define GL_FRAMEWORK_INCLUDED#include <windows.h>typedef struct {                                   // Structure for keyboard stuff    BOOL keyDown[256];} Keys;typedef struct {                                   // Contains information vital to applications     HMODULE hInstance;                             // Application Instance    const char* className;} Application;typedef struct {                                   // Window creation info    Application* application;    char* title;    int width;    int height;    int bitsPerPixel;    BOOL isFullScreen;} GL_WindowInit;typedef struct {                                   // Contains information vital to a window    Keys* keys;    HWND hWnd;                                     // Windows handle    HDC hDC;                                       // Device context    HGLRC hRC;                                     // Rendering context    GL_WindowInit init;    BOOL isVisible;                                // Window visiable?    DWORD lastTickCount;                           // Tick counter} GL_Window;void TerminateApplication(GL_Window* window);      // Terminate the applicationvoid ToggleFullscreen(GL_Window* window);          // Toggle fullscreen / Windowed modeBOOL Initialize(GL_Window* window, Keys* keys);void Deinitialize(void);void Update(DWORD milliseconds);void Draw(void);#endif
Previous.h
技术分享
#include <Windows.h>#include <GL\glew.h>#include <GL\glut.h>#include "Previous.h"#define WM_TOGGLEFULLSCREEN (WM_USER+1)                   // Application define message for toggling // between fulscreen / windowed modestatic BOOL g_isProgramLooping;                           // Window creation loop, for fullscreen / windowed modestatic BOOL g_createFullScreen;                           // If true, then create windowvoid TerminateApplication(GL_Window* window)              // Terminate the application{    PostMessage(window->hWnd, WM_QUIT, 0, 0);             // Send a WM_QUIT message    g_isProgramLooping = FALSE;                           // Stop looping of the program}void ToggleFullscreen(GL_Window* window)                  // Toggle fullscreen /windowed mode{    PostMessage(window->hWnd, WM_TOGGLEFULLSCREEN, 0, 0); // Send a WM_TOGGLEFULLSCREEN message}void ReshapeGL(int width, int height)                     // Reshape the window  when it‘s moved or resized{    glViewport(0, 0, (GLsizei)(width), (GLsizei)(height)); // Reset the current viewport    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    // Calcutate the aspect ratio of the window    gluPerspective(45.0f, (GLfloat)(width) / (GLfloat)(height), 1.0, 100.0f);    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();}BOOL ChangeScreenResolution(int width, int height, int bitsPerPixel)     // Change the screen resolution{    DEVMODE dmScreenSettings;                              // Device mode    ZeroMemory(&dmScreenSettings, sizeof(DEVMODE));        // Make sure memory is cleared    dmScreenSettings.dmSize = sizeof(DEVMODE);             // Size of the devmode structure    dmScreenSettings.dmPelsWidth = width;    dmScreenSettings.dmPelsHeight = height;    dmScreenSettings.dmBitsPerPel = bitsPerPixel;    dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;    if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {        return FALSE;                                      // Display change failed    }    return TRUE;}BOOL CreateWindowGL(GL_Window* window){    DWORD windowStyle = WS_OVERLAPPEDWINDOW;                // Define window style    DWORD windowExtendedStyle = WS_EX_APPWINDOW;            // Define the window‘s extended style    PIXELFORMATDESCRIPTOR pdf = {        sizeof(PIXELFORMATDESCRIPTOR),                      // Size of this pixel format descriptor        1,                                                  // Version Number        PFD_DRAW_TO_WINDOW |                                // Format must support window        PFD_SUPPORT_OPENGL |                                // Format must support openGL        PFD_DOUBLEBUFFER,                                   // Must support double buffering        PFD_TYPE_RGBA,                                      // Request an RGBA format        window->init.bitsPerPixel,                          // Select color depth        0, 0, 0, 0, 0, 0,                                   // Color bits ignored        0,                                                  // No alpha buffer        0,                                                  // Shift bit ignored        0,                                                  // No accumulation buffer        0, 0, 0, 0,                                         // Accumulation bits ignored        16,                                                 // 16bits Z-buffer (depth buffer)        0,                                                  // No stencil buffer        0,                                                  // No auxiliary buffer        PFD_MAIN_PLANE,                                     // Main drawing layer        0,                                                  // Reserved        0, 0, 0                                             // Layer masks ignored    };    RECT windowRect = { 0, 0, window->init.width, window->init.height };   // Window coordiantes    GLuint PixelFormat;    if (window->init.isFullScreen == TRUE) {        if (ChangeScreenResolution(window->init.width, window->init.height, window->init.bitsPerPixel) == FALSE)        {            // Fullscreen mode failed, run in windowed mode instead            MessageBox(HWND_DESKTOP, "Mode Switch Failed.\nRuning In Windowed Mode.",                "Error", MB_OK | MB_ICONEXCLAMATION);            window->init.isFullScreen = FALSE;        }        else {            ShowCursor(FALSE);            windowStyle = WS_POPUP;                         // Popup window            windowExtendedStyle |= WS_EX_TOPMOST;        }    }    else {        // Adjust window, account for window borders        AdjustWindowRectEx(&windowRect, windowStyle, 0, windowExtendedStyle);    }    // Create Opengl window    window->hWnd = CreateWindowEx(windowExtendedStyle,      // Extended style        window->init.application->className,                // Class name        window->init.title,                                 // Window title        windowStyle,                                        // Window style        0, 0,                                               // Window X,Y position        windowRect.right - windowRect.left,                 // Window width        windowRect.bottom - windowRect.top,                 // Window height        HWND_DESKTOP,                                       // Desktop is window‘s parent        0,                                                  // No menu        window->init.application->hInstance,                // Pass the window instance        window);    if (window->hWnd == 0) {                                // Was window creation a success?        return FALSE;    }    window->hDC = GetDC(window->hWnd);    if (window->hDC == 0) {        DestroyWindow(window->hWnd);        window->hWnd = 0;        return FALSE;    }    PixelFormat = ChoosePixelFormat(window->hDC, &pdf);     // Find a compatible pixel format    if (PixelFormat == 0) {        ReleaseDC(window->hWnd, window->hDC);               // Release device context        window->hDC = 0;        DestroyWindow(window->hWnd);        window->hWnd = 0;        return FALSE;    }    if (SetPixelFormat(window->hDC, PixelFormat, &pdf) == FALSE) {   // Try to set the pixel format        ReleaseDC(window->hWnd, window->hDC);        window->hDC = 0;        DestroyWindow(window->hWnd);        window->hWnd = 0;        return FALSE;    }    window->hRC = wglCreateContext(window->hDC);            // Try to get a rendering context    if (window->hRC == 0) {        ReleaseDC(window->hWnd, window->hDC);        window