OpenGL中的绝大多数特效都与某些类型的(色彩)混合有关。混色的定义为,将某个象素的颜色和已绘制在屏幕上与其对应的象素颜色相互结合。至于如何结合这两个颜色则依赖于颜色的alpha通道的分量值,以及/或者所使用的混色函数。Alpha通常是位于颜色值末尾的第4个颜色组成分量。前面这些课我们都是用GL_RGB来指定颜色的三个分量。相应的GL_RGBA可以指定alpha分量的值。更进一步,我们可以使用glColor4f()来代替glColor3f()


   绝大多数人都认为Alpha分量代表材料的透明度。这就是说,alpha值为0.0时所代表的材料是完全透明的。alpha值为1.0时所代表的材料则是完全不透明的。

混合的基本原理是就将要分色的图像各象素的颜色以及背景颜色均按照RGB规则各自分离之后,根据图像的RGB颜色分量*alpha+背景的RGB颜色分量*(1-alpha) — 这样一个简单公式来混合之后,最后将混合得到的RGB分量重新合并。)公式如下:

(Rs Sr + Rd Dr, Gs Sg + Gd Dg, Bs Sb + Bd Db, As Sa + Ad Da)


   OpenGL按照上面的公式计算这两个象素的混色结果。小写的sr分别代表源象素和目标象素。大写的SD则是相应的混色因子。这些决定了您如何对这些象素混色。绝大多数情况下,各颜色通道的alpha混色值大小相同,这样对源象素就有(As, As, As, As),目标象素则有(1, 1, 1, 1) - (As, As, As, As)。上面的公式就成了下面的模样:

 

(Rs As + Rd (1 - As), Gs As + Gd (1 - As), Bs As + Bs (1 - As), As As + Ad (1 - As))

这个公式会生成透明/半透明的效果。

 

OpenGL中首先我们得打开混合, 然后设置混合式。在绘制透明物体的时候, 我们关闭深度缓存, 因为我们仍然希望在半透明物体后面的物体能被绘制。 这并不是使用混合的正确方式, 但是在情况简单的时候它会工作得很好。正确的混色过程应该是先绘制全部的场景之后再绘制透明的图形。并且要按照与深度缓存相反的次序来绘制(先画最远的物体)。考虑对两个多边形(12)进行alpha混合,不同的绘制次序会得到不同的结果。(这里假定多边形1离观察者最近,那么正确的过程应该先画多边形2,再画多边形1。正如您再现实中所见到的那样,从这两个透明的多边形背后照射来的光线总是先穿过多边形2,再穿过多边形1,最后才到达观察者的眼睛)。 在深度缓存启用时,您应该将透明图形按照深度进行排序,并在全部场景绘制完毕之后再绘制这些透明物体。否则您将得到不正确的结果。

ContractedBlock.gifExpandedBlockStart.gifdemo8
None.gif#include <windows.h>        // Header File For Windows
None.gif
#include <stdio.h>            // Header File For Standard Input/Output
None.gif
#include <gl\gl.h>            // Header File For The OpenGL32 Library
None.gif
#include <gl\glu.h>            // Header File For The GLu32 Library
None.gif
#include <gl\glaux.h>        // Header File For The Glaux Library
None.gif

None.gifHDC            hDC
=NULL;        // Private GDI Device Context
None.gif
HGLRC        hRC=NULL;        // Permanent Rendering Context
None.gif
HWND        hWnd=NULL;        // Holds Our Window Handle
None.gif
HINSTANCE    hInstance;        // Holds The Instance Of The Application
None.gif

None.gif
bool    keys[256];            // Array Used For The Keyboard Routine
None.gif
bool    active=TRUE;        // Window Active Flag Set To TRUE By Default
None.gif
bool    fullscreen=TRUE;    // Fullscreen Flag Set To Fullscreen Mode By Default
None.gif
bool    light;                // Lighting ON/OFF
None.gif
bool    blend;                // Blending OFF/ON? ( NEW )
None.gif
bool    lp;                    // L Pressed?
None.gif
bool    fp;                    // F Pressed?
None.gif
bool    bp;                    // B Pressed? ( NEW )
None.gif

None.gifGLfloat    xrot;                
// X Rotation
None.gif
GLfloat    yrot;                // Y Rotation
None.gif
GLfloat xspeed;                // X Rotation Speed
None.gif
GLfloat yspeed;                // Y Rotation Speed
None.gif
GLfloat    z=-5.0f;            // Depth Into The Screen
None.gif

ExpandedBlockStart.gifContractedBlock.gifGLfloat LightAmbient[]
=        dot.gif0.5f0.5f0.5f1.0f };
ExpandedBlockStart.gifContractedBlock.gifGLfloat LightDiffuse[]
=        dot.gif1.0f1.0f1.0f1.0f };
ExpandedBlockStart.gifContractedBlock.gifGLfloat LightPosition[]
=    dot.gif0.0f0.0f2.0f1.0f };
None.gif
None.gifGLuint    filter;                
// Which Filter To Use
None.gif
GLuint    texture[3];            // Storage For 3 Textures
None.gif

None.gifLRESULT    CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);    
// Declaration For WndProc
None.gif

None.gifAUX_RGBImageRec 
*LoadBMP(char *Filename)                // Loads A Bitmap Image
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    FILE 
*File=NULL;                                    // File Handle
InBlock.gif

InBlock.gif    
if (!Filename)                                        // Make Sure A Filename Was Given
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
return NULL;                                    // If Not Return NULL
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    File
=fopen(Filename,"r");                            // Check To See If The File Exists
InBlock.gif

InBlock.gif    
if (File)                                            // Does The File Exist?
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        fclose(File);                                    
// Close The Handle
InBlock.gif
        return auxDIBImageLoad(Filename);                // Load The Bitmap And Return A Pointer
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
return NULL;                                        // If Load Failed Return NULL
ExpandedBlockEnd.gif
}

None.gif
None.gif
int LoadGLTextures()                                    // Load Bitmaps And Convert To Textures
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
int Status=FALSE;                                    // Status Indicator
InBlock.gif

InBlock.gif    AUX_RGBImageRec 
*TextureImage[1];                    // Create Storage Space For The Texture
InBlock.gif

InBlock.gif    memset(TextureImage,
0,sizeof(void *)*1);               // Set The Pointer To NULL
InBlock.gif
InBlock.gif    
// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
InBlock.gif
    if (TextureImage[0]=LoadBMP("Data/glass.bmp"))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Status
=TRUE;                                    // Set The Status To TRUE
InBlock.gif

InBlock.gif        glGenTextures(
3&texture[0]);                    // Create Three Textures
InBlock.gif
InBlock.gif        
// Create Nearest Filtered Texture
InBlock.gif
        glBindTexture(GL_TEXTURE_2D, texture[0]);
InBlock.gif        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
InBlock.gif        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
InBlock.gif        glTexImage2D(GL_TEXTURE_2D, 
03, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
InBlock.gif
InBlock.gif        
// Create Linear Filtered Texture
InBlock.gif
        glBindTexture(GL_TEXTURE_2D, texture[1]);
InBlock.gif        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
InBlock.gif        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
InBlock.gif        glTexImage2D(GL_TEXTURE_2D, 
03, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
InBlock.gif
InBlock.gif        
// Create MipMapped Texture
InBlock.gif
        glBindTexture(GL_TEXTURE_2D, texture[2]);
InBlock.gif        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
InBlock.gif        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
InBlock.gif        gluBuild2DMipmaps(GL_TEXTURE_2D, 
3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
if (TextureImage[0])                                // If Texture Exists
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
if (TextureImage[0]->data)                        // If Texture Image Exists
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            free(TextureImage[
0]->data);                // Free The Texture Image Memory
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        free(TextureImage[
0]);                            // Free The Image Structure
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
return Status;                                        // Return The Status
ExpandedBlockEnd.gif
}

None.gif
None.gifGLvoid ReSizeGLScene(GLsizei width, GLsizei height)        
// Resize And Initialize The GL Window
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
if (height==0)                                        // Prevent A Divide By Zero By
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        height
=1;                                        // Making Height Equal One
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    glViewport(
0,0,width,height);                        // Reset The Current Viewport
InBlock.gif

InBlock.gif    glMatrixMode(GL_PROJECTION);                        
// Select The Projection Matrix
InBlock.gif
    glLoadIdentity();                                    // Reset The Projection Matrix
InBlock.gif
InBlock.gif    
// Calculate The Aspect Ratio Of The Window
InBlock.gif
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
InBlock.gif
InBlock.gif    glMatrixMode(GL_MODELVIEW);                            
// Select The Modelview Matrix
InBlock.gif
    glLoadIdentity();                                    // Reset The Modelview Matrix
ExpandedBlockEnd.gif
}

None.gif
None.gif
int InitGL(GLvoid)                                        // All Setup For OpenGL Goes Here
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
if (!LoadGLTextures())                                // Jump To Texture Loading Routine
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
return FALSE;                                    // If Texture Didn't Load Return FALSE
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    glEnable(GL_TEXTURE_2D);                            
// Enable Texture Mapping
InBlock.gif
    glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
InBlock.gif
    glClearColor(0.0f0.0f0.0f0.5f);                // Black Background
InBlock.gif
    glClearDepth(1.0f);                                    // Depth Buffer Setup
InBlock.gif
    glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
InBlock.gif
    glDepthFunc(GL_LEQUAL);                                // The Type Of Depth Testing To Do
InBlock.gif
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);    // Really Nice Perspective Calculations
InBlock.gif

InBlock.gif    glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);        
// Setup The Ambient Light
InBlock.gif
    glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);        // Setup The Diffuse Light
InBlock.gif
    glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);    // Position The Light
InBlock.gif
    glEnable(GL_LIGHT1);                                // Enable Light One
InBlock.gif

InBlock.gif    glColor4f(
1.0f1.0f1.0f0.5);                    // Full Brightness.  50% Alpha
InBlock.gif
    glBlendFunc(GL_SRC_ALPHA,GL_ONE);                    // Set The Blending Function For Translucency
InBlock.gif

InBlock.gif    
return TRUE;                                        // Initialization Went OK
ExpandedBlockEnd.gif
}

None.gif
None.gif
int DrawGLScene(GLvoid)                                    // Here's Where We Do All The Drawing
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    glClear(GL_COLOR_BUFFER_BIT 
| GL_DEPTH_BUFFER_BIT);    // Clear The Screen And The Depth Buffer
InBlock.gif
    glLoadIdentity();                                    // Reset The View
InBlock.gif
    glTranslatef(0.0f,0.0f,z);
InBlock.gif
InBlock.gif    glRotatef(xrot,
1.0f,0.0f,0.0f);
InBlock.gif    glRotatef(yrot,
0.0f,1.0f,0.0f);
InBlock.gif
InBlock.gif    glBindTexture(GL_TEXTURE_2D, texture[filter]);
InBlock.gif
InBlock.gif    glBegin(GL_QUADS);
InBlock.gif        
// Front Face
InBlock.gif
        glNormal3f( 0.0f0.0f1.0f);
InBlock.gif        glTexCoord2f(
0.0f0.0f); glVertex3f(-1.0f-1.0f,  1.0f);
InBlock.gif        glTexCoord2f(
1.0f0.0f); glVertex3f( 1.0f-1.0f,  1.0f);
InBlock.gif        glTexCoord2f(
1.0f1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
InBlock.gif        glTexCoord2f(
0.0f1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
InBlock.gif        
// Back Face
InBlock.gif
        glNormal3f( 0.0f0.0f,-1.0f);
InBlock.gif        glTexCoord2f(
1.0f0.0f); glVertex3f(-1.0f-1.0f-1.0f);
InBlock.gif        glTexCoord2f(
1.0f1.0f); glVertex3f(-1.0f,  1.0f-1.0f);
InBlock.gif        glTexCoord2f(
0.0f1.0f); glVertex3f( 1.0f,  1.0f-1.0f);
InBlock.gif        glTexCoord2f(
0.0f0.0f); glVertex3f( 1.0f-1.0f-1.0f);
InBlock.gif        
// Top Face
InBlock.gif
        glNormal3f( 0.0f1.0f0.0f);
InBlock.gif        glTexCoord2f(
0.0f1.0f); glVertex3f(-1.0f,  1.0f-1.0f);
InBlock.gif        glTexCoord2f(
0.0f0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
InBlock.gif        glTexCoord2f(
1.0f0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
InBlock.gif        glTexCoord2f(
1.0f1.0f); glVertex3f( 1.0f,  1.0f-1.0f);
InBlock.gif        
// Bottom Face
InBlock.gif
        glNormal3f( 0.0f,-1.0f0.0f);
InBlock.gif        glTexCoord2f(
1.0f1.0f); glVertex3f(-1.0f-1.0f-1.0f);
InBlock.gif        glTexCoord2f(
0.0f1.0f); glVertex3f( 1.0f-1.0f-1.0f);
InBlock.gif        glTexCoord2f(
0.0f0.0f); glVertex3f( 1.0f-1.0f,  1.0f);
InBlock.gif        glTexCoord2f(
1.0f0.0f); glVertex3f(-1.0f-1.0f,  1.0f);
InBlock.gif        
// Right face
InBlock.gif
        glNormal3f( 1.0f0.0f0.0f);
InBlock.gif        glTexCoord2f(
1.0f0.0f); glVertex3f( 1.0f-1.0f-1.0f);
InBlock.gif        glTexCoord2f(
1.0f1.0f); glVertex3f( 1.0f,  1.0f-1.0f);
InBlock.gif        glTexCoord2f(
0.0f1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
InBlock.gif        glTexCoord2f(
0.0f0.0f); glVertex3f( 1.0f-1.0f,  1.0f);
InBlock.gif        
// Left Face
InBlock.gif
        glNormal3f(-1.0f0.0f0.0f);
InBlock.gif        glTexCoord2f(
0.0f0.0f); glVertex3f(-1.0f-1.0f-1.0f);
InBlock.gif        glTexCoord2f(
1.0f0.0f); glVertex3f(-1.0f-1.0f,  1.0f);
InBlock.gif        glTexCoord2f(
1.0f1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
InBlock.gif        glTexCoord2f(
0.0f1.0f); glVertex3f(-1.0f,  1.0f-1.0f);
InBlock.gif    glEnd();
InBlock.gif
InBlock.gif    xrot
+=xspeed;
InBlock.gif    yrot
+=yspeed;
InBlock.gif    
return TRUE;                                        // Keep Going
ExpandedBlockEnd.gif
}

None.gif
None.gifGLvoid KillGLWindow(GLvoid)                                
// Properly Kill The Window
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
if (fullscreen)                                        // Are We In Fullscreen Mode?
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        ChangeDisplaySettings(NULL,
0);                    // If So Switch Back To The Desktop
InBlock.gif
        ShowCursor(TRUE);                                // Show Mouse Pointer
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
if (hRC)                                            // Do We Have A Rendering Context?
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
if (!wglMakeCurrent(NULL,NULL))                    // Are We Able To Release The DC And RC Contexts?
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            MessageBox(NULL,
"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
if (!wglDeleteContext(hRC))                        // Are We Able To Delete The RC?
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            MessageBox(NULL,
"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
ExpandedSubBlockEnd.gif        }

InBlock.gif        hRC
=NULL;                                        // Set RC To NULL
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
if (hDC && !ReleaseDC(hWnd,hDC))                    // Are We Able To Release The DC
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        MessageBox(NULL,
"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
InBlock.gif        hDC
=NULL;                                        // Set DC To NULL
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
if (hWnd && !DestroyWindow(hWnd))                    // Are We Able To Destroy The Window?
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        MessageBox(NULL,
"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
InBlock.gif        hWnd
=NULL;                                        // Set hWnd To NULL
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
if (!UnregisterClass("OpenGL",hInstance))            // Are We Able To Unregister Class
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        MessageBox(NULL,
"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
InBlock.gif        hInstance
=NULL;                                    // Set hInstance To NULL
ExpandedSubBlockEnd.gif
    }

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//*    This Code Creates Our OpenGL Window.  Parameters Are:                    *
InBlock.gif *    title            - Title To Appear At The Top Of The Window                *
InBlock.gif *    width            - Width Of The GL Window Or Fullscreen Mode                *
InBlock.gif *    height            - Height Of The GL Window Or Fullscreen Mode            *
InBlock.gif *    bits            - Number Of Bits To Use For Color (8/16/24/32)            *
ExpandedBlockEnd.gif *    fullscreenflag    - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE)    
*/

None.gif 
None.gifBOOL CreateGLWindow(
char* title, int width, int height, int bits, bool fullscreenflag)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    GLuint        PixelFormat;            
// Holds The Results After Searching For A Match
InBlock.gif
    WNDCLASS    wc;                        // Windows Class Structure
InBlock.gif
    DWORD        dwExStyle;                // Window Extended Style
InBlock.gif
    DWORD        dwStyle;                // Window Style
InBlock.gif
    RECT        WindowRect;                // Grabs Rectangle Upper Left / Lower Right Values
InBlock.gif
    WindowRect.left=(long)0;            // Set Left Value To 0
InBlock.gif
    WindowRect.right=(long)width;        // Set Right Value To Requested Width
InBlock.gif
    WindowRect.top=(long)0;                // Set Top Value To 0
InBlock.gif
    WindowRect.bottom=(long)height;        // Set Bottom Value To Requested Height
InBlock.gif

InBlock.gif    fullscreen
=fullscreenflag;            // Set The Global Fullscreen Flag
InBlock.gif

InBlock.gif    hInstance            
= GetModuleHandle(NULL);                // Grab An Instance For Our Window
InBlock.gif
    wc.style            = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;    // Redraw On Size, And Own DC For Window.
InBlock.gif
    wc.lpfnWndProc        = (WNDPROC) WndProc;                    // WndProc Handles Messages
InBlock.gif
    wc.cbClsExtra        = 0;                                    // No Extra Window Data
InBlock.gif
    wc.cbWndExtra        = 0;                                    // No Extra Window Data
InBlock.gif
    wc.hInstance        = hInstance;                            // Set The Instance
InBlock.gif
    wc.hIcon            = LoadIcon(NULL, IDI_WINLOGO);            // Load The Default Icon
InBlock.gif
    wc.hCursor            = LoadCursor(NULL, IDC_ARROW);            // Load The Arrow Pointer
InBlock.gif
    wc.hbrBackground    = NULL;                                    // No Background Required For GL
InBlock.gif
    wc.lpszMenuName        = NULL;                                    // We Don't Want A Menu
InBlock.gif
    wc.lpszClassName    = "OpenGL";                                // Set The Class Name
InBlock.gif

InBlock.gif    
if (!RegisterClass(&wc))                                    // Attempt To Register The Window Class
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        MessageBox(NULL,
"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif        
return FALSE;                                            // Return FALSE
ExpandedSubBlockEnd.gif
    }

InBlock.gif    
InBlock.gif    
if (fullscreen)                                                // Attempt Fullscreen Mode?
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        DEVMODE dmScreenSettings;                                
// Device Mode
InBlock.gif
        memset(&dmScreenSettings,0,sizeof(dmScreenSettings));    // Makes Sure Memory's Cleared
InBlock.gif
        dmScreenSettings.dmSize=sizeof(dmScreenSettings);        // Size Of The Devmode Structure
InBlock.gif
        dmScreenSettings.dmPelsWidth    = width;                // Selected Screen Width
InBlock.gif
        dmScreenSettings.dmPelsHeight    = height;                // Selected Screen Height
InBlock.gif
        dmScreenSettings.dmBitsPerPel    = bits;                    // Selected Bits Per Pixel
InBlock.gif
        dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
InBlock.gif
InBlock.gif        
// Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
InBlock.gif
        if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// If The Mode Fails, Offer Two Options.  Quit Or Use Windowed Mode.
InBlock.gif
            if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                fullscreen
=FALSE;        // Windowed Mode Selected.  Fullscreen = FALSE
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Pop Up A Message Box Letting User Know The Program Is Closing.
InBlock.gif
                MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
InBlock.gif                
return FALSE;                                    // Return FALSE
ExpandedSubBlockEnd.gif
            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
if (fullscreen)                                                // Are We Still In Fullscreen Mode?
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        dwExStyle
=WS_EX_APPWINDOW;                                // Window Extended Style
InBlock.gif
        dwStyle=WS_POPUP;                                        // Windows Style
InBlock.gif
        ShowCursor(FALSE);                                        // Hide Mouse Pointer
ExpandedSubBlockEnd.gif
    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        dwExStyle
=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;            // Window Extended Style
InBlock.gif
        dwStyle=WS_OVERLAPPEDWINDOW;                            // Windows Style
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    AdjustWindowRectEx(
&WindowRect, dwStyle, FALSE, dwExStyle);        // Adjust Window To True Requested Size
InBlock.gif
InBlock.gif    
// Create The Window
InBlock.gif
    if (!(hWnd=CreateWindowEx(    dwExStyle,                            // Extended Style For The Window
InBlock.gif
                                "OpenGL",                            // Class Name
InBlock.gif
                                title,                                // Window Title
InBlock.gif
                                dwStyle |                            // Defined Window Style
InBlock.gif
                                WS_CLIPSIBLINGS |                    // Required Window Style
InBlock.gif
                                WS_CLIPCHILDREN,                    // Required Window Style
InBlock.gif
                                00,                                // Window Position
InBlock.gif
                                WindowRect.right-WindowRect.left,    // Calculate Window Width
InBlock.gif
                                WindowRect.bottom-WindowRect.top,    // Calculate Window Height
InBlock.gif
                                NULL,                                // No Parent Window
InBlock.gif
                                NULL,                                // No Menu
InBlock.gif
                                hInstance,                            // Instance
InBlock.gif
                                NULL)))                                // Dont Pass Anything To WM_CREATE
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        KillGLWindow();                                
// Reset The Display
InBlock.gif
        MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif        
return FALSE;                                // Return FALSE
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
static    PIXELFORMATDESCRIPTOR pfd=                // pfd Tells Windows How We Want Things To Be
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
sizeof(PIXELFORMATDESCRIPTOR),                // Size Of This Pixel Format Descriptor
InBlock.gif
        1,                                            // Version Number
InBlock.gif
        PFD_DRAW_TO_WINDOW |                        // Format Must Support Window
InBlock.gif
        PFD_SUPPORT_OPENGL |                        // Format Must Support OpenGL
InBlock.gif
        PFD_DOUBLEBUFFER,                            // Must Support Double Buffering
InBlock.gif
        PFD_TYPE_RGBA,                                // Request An RGBA Format
InBlock.gif
        bits,                                        // Select Our Color Depth
InBlock.gif
        000000,                            // Color Bits Ignored
InBlock.gif
        0,                                            // No Alpha Buffer
InBlock.gif
        0,                                            // Shift Bit Ignored
InBlock.gif
        0,                                            // No Accumulation Buffer
InBlock.gif
        0000,                                    // Accumulation Bits Ignored
InBlock.gif
        16,                                            // 16Bit Z-Buffer (Depth Buffer)  
InBlock.gif
        0,                                            // No Stencil Buffer
InBlock.gif
        0,                                            // No Auxiliary Buffer
InBlock.gif
        PFD_MAIN_PLANE,                                // Main Drawing Layer
InBlock.gif
        0,                                            // Reserved
InBlock.gif
        000                                        // Layer Masks Ignored
ExpandedSubBlockEnd.gif
    }
;
InBlock.gif    
InBlock.gif    
if (!(hDC=GetDC(hWnd)))                            // Did We Get A Device Context?
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        KillGLWindow();                                
// Reset The Display
InBlock.gif
        MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif        
return FALSE;                                // Return FALSE
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))    // Did Windows Find A Matching Pixel Format?
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        KillGLWindow();                                
// Reset The Display
InBlock.gif
        MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif        
return FALSE;                                // Return FALSE
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
if(!SetPixelFormat(hDC,PixelFormat,&pfd))        // Are We Able To Set The Pixel Format?
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        KillGLWindow();                                
// Reset The Display
InBlock.gif
        MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif        
return FALSE;                                // Return FALSE
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
if (!(hRC=wglCreateContext(hDC)))                // Are We Able To Get A Rendering Context?
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        KillGLWindow();                                
// Reset The Display
InBlock.gif
        MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif        
return FALSE;                                // Return FALSE
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
if(!wglMakeCurrent(hDC,hRC))                    // Try To Activate The Rendering Context
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        KillGLWindow();                                
// Reset The Display
InBlock.gif
        MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif        
return FALSE;                                // Return FALSE
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    ShowWindow(hWnd,SW_SHOW);                        
// Show The Window
InBlock.gif
    SetForegroundWindow(hWnd);                        // Slightly Higher Priority
InBlock.gif
    SetFocus(hWnd);                                    // Sets Keyboard Focus To The Window
InBlock.gif
    ReSizeGLScene(width, height);                    // Set Up Our Perspective GL Screen
InBlock.gif

InBlock.gif    
if (!InitGL())                                    // Initialize Our Newly Created GL Window
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        KillGLWindow();                                
// Reset The Display
InBlock.gif
        MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif        
return FALSE;                                // Return FALSE
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
return TRUE;                                    // Success
ExpandedBlockEnd.gif
}

None.gif
None.gifLRESULT CALLBACK WndProc(    HWND    hWnd,            
// Handle For This Window
None.gif
                            UINT    uMsg,            // Message For This Window
None.gif
                            WPARAM    wParam,            // Additional Message Information
None.gif
                            LPARAM    lParam)            // Additional Message Information
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
switch (uMsg)                                    // Check For Windows Messages
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
case WM_ACTIVATE:                            // Watch For Window Activate Message
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
if (!HIWORD(wParam))                    // Check Minimization State
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                active
=TRUE;                        // Program Is Active
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                active
=FALSE;                        // Program Is No Longer Active
ExpandedSubBlockEnd.gif
            }

InBlock.gif
InBlock.gif            
return 0;                                // Return To The Message Loop
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
case WM_SYSCOMMAND:                            // Intercept System Commands
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
switch (wParam)                            // Check System Calls
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                
case SC_SCREENSAVE:                    // Screensaver Trying To Start?
InBlock.gif
                case SC_MONITORPOWER:                // Monitor Trying To Enter Powersave?
InBlock.gif
                return 0;                            // Prevent From Happening
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
break;                                    // Exit
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
case WM_CLOSE:                                // Did We Receive A Close Message?
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            PostQuitMessage(
0);                        // Send A Quit Message
InBlock.gif
            return 0;                                // Jump Back
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
case WM_KEYDOWN:                            // Is A Key Being Held Down?
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            keys[wParam] 
= TRUE;                    // If So, Mark It As TRUE
InBlock.gif
            return 0;                                // Jump Back
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
case WM_KEYUP:                                // Has A Key Been Released?
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            keys[wParam] 
= FALSE;                    // If So, Mark It As FALSE
InBlock.gif
            return 0;                                // Jump Back
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
case WM_SIZE:                                // Resize The OpenGL Window
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));  
// LoWord=Width, HiWord=Height
InBlock.gif
            return 0;                                // Jump Back
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Pass All Unhandled Messages To DefWindowProc
InBlock.gif
    return DefWindowProc(hWnd,uMsg,wParam,lParam);
ExpandedBlockEnd.gif}

None.gif
None.gif
int WINAPI WinMain(    HINSTANCE    hInstance,            // Instance
None.gif
                    HINSTANCE    hPrevInstance,        // Previous Instance
None.gif
                    LPSTR        lpCmdLine,            // Command Line Parameters
None.gif
                    int            nCmdShow)            // Window Show State
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    MSG        msg;                                    
// Windows Message Structure
InBlock.gif
    BOOL    done=FALSE;                                // Bool Variable To Exit Loop
InBlock.gif
InBlock.gif    
// Ask The User Which Screen Mode They Prefer
InBlock.gif
    if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?""Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        fullscreen
=FALSE;                            // Windowed Mode
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
// Create Our OpenGL Window
InBlock.gif
    if (!CreateGLWindow("Tom Stanis & NeHe's Blending Tutorial",640,480,16,fullscreen))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return 0;                                    // Quit If Window Was Not Created
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
while(!done)                                    // Loop That Runs While done=FALSE
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))    // Is There A Message Waiting?
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
if (msg.message==WM_QUIT)                // Have We Received A Quit Message?
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                done
=TRUE;                            // If So done=TRUE
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
else                                    // If Not, Deal With Window Messages
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                TranslateMessage(
&msg);                // Translate The Message
InBlock.gif
                DispatchMessage(&msg);                // Dispatch The Message
ExpandedSubBlockEnd.gif
            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
else                                        // If There Are No Messages
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
// Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
InBlock.gif
            if ((active && !DrawGLScene()) || keys[VK_ESCAPE])    // Active?  Was There A Quit Received?
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                done
=TRUE;                            // ESC or DrawGLScene Signalled A Quit
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
else                                    // Not Time To Quit, Update Screen
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                SwapBuffers(hDC);                    
// Swap Buffers (Double Buffering)
InBlock.gif
                if (keys['L'&& !lp)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    lp
=TRUE;
InBlock.gif                    light
=!light;
InBlock.gif                    
if (!light)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        glDisable(GL_LIGHTING);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        glEnable(GL_LIGHTING);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (!keys['L'])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    lp
=FALSE;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (keys['F'&& !fp)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    fp
=TRUE;
InBlock.gif                    filter
+=1;
InBlock.gif                    
if (filter>2)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        filter
=0;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (!keys['F'])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    fp
=FALSE;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (keys[VK_PRIOR])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    z
-=0.02f;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (keys[VK_NEXT])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    z
+=0.02f;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (keys[VK_UP])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    xspeed
-=0.01f;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (keys[VK_DOWN])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    xspeed
+=0.01f;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (keys[VK_RIGHT])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    yspeed
+=0.01f;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (keys[VK_LEFT])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    yspeed
-=0.01f;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
// Blending Code Starts Here
InBlock.gif
                if (keys['B'&& !bp)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    bp
=TRUE;
InBlock.gif                    blend 
= !blend;
InBlock.gif                    
if(blend)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        glEnable(GL_BLEND);            
// Turn Blending On
InBlock.gif
                        glDisable(GL_DEPTH_TEST);    // Turn Depth Testing Off
ExpandedSubBlockEnd.gif
                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        glDisable(GL_BLEND);        
// Turn Blending Off
InBlock.gif
                        glEnable(GL_DEPTH_TEST);    // Turn Depth Testing On
ExpandedSubBlockEnd.gif
                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (!keys['B'])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    bp
=FALSE;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
// Blending Code Ends Here
InBlock.gif

InBlock.gif                
if (keys[VK_F1])                        // Is F1 Being Pressed?
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    keys[VK_F1]
=FALSE;                    // If So Make Key FALSE
InBlock.gif
                    KillGLWindow();                        // Kill Our Current Window
InBlock.gif
                    fullscreen=!fullscreen;                // Toggle Fullscreen / Windowed Mode
InBlock.gif                    
// Recreate Our OpenGL Window
InBlock.gif
                    if (!CreateGLWindow("Tom Stanis & NeHe's Blending Tutorial",640,480,16,fullscreen))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return 0;                        // Quit If Window Was Not Created
ExpandedSubBlockEnd.gif
                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Shutdown
InBlock.gif
    KillGLWindow();                                    // Kill The Window
InBlock.gif
    return (msg.wParam);                            // Exit The Program
ExpandedBlockEnd.gif
}

None.gif
None.gif