其实纹理滤波是什么概念我也还不了解,图形学就是这样,理论和实践都不可偏废,可究竟从何处入手也是个两难的问题,有人说图形血到最后就是数学,对我这样的新手来说,快速有个概念也许不失为一个笨办法吧。

 

 

Nehe在第6课里已经用了线性滤波了,并且还推荐我们把gl_nearest(不光滑)滤波方式和线性滤波结合起来使用,在近处时用线性滤波,在远处时用gl_nearest(不光滑)滤波,因为线性滤波对运行机器的要求高些。

 

 

ContractedBlock.gifExpandedBlockStart.gifDemo7
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.gifHGLRC        hRC=NULL;        // Permanent Rendering Context
None.gifHWND        hWnd=NULL;        // Holds Our Window Handle
None.gifHINSTANCE    hInstance;        // Holds The Instance Of The Application
None.gif
None.gifbool    keys[256];            // Array Used For The Keyboard Routine
None.gifbool    active=TRUE;        // Window Active Flag Set To TRUE By Default
None.gifbool    fullscreen=TRUE;    // Fullscreen Flag Set To Fullscreen Mode By Default
None.gifbool    light;                // Lighting ON/OFF ( NEW )
None.gifbool    lp;                    // L Pressed? ( NEW )
None.gifbool    fp;                    // F Pressed? ( NEW )
None.gif
None.gifGLfloat    xrot;                // X Rotation
None.gifGLfloat    yrot;                // Y Rotation
None.gifGLfloat xspeed;                // X Rotation Speed
None.gifGLfloat yspeed;                // Y Rotation Speed
None.gifGLfloat    z=-5.0f;            // Depth Into The Screen
None.gif
ExpandedBlockStart.gifContractedBlock.gifGLfloat LightAmbient[]=        dot.gif{ 0.5f, 0.5f, 0.5f, 1.0f };
ExpandedBlockStart.gifContractedBlock.gifGLfloat LightDiffuse[]=        dot.gif{ 1.0f, 1.0f, 1.0f, 1.0f };
ExpandedBlockStart.gifContractedBlock.gifGLfloat LightPosition[]=    dot.gif{ 0.0f, 0.0f, 2.0f, 1.0f };
None.gif
None.gifGLuint    filter;                // Which Filter To Use
None.gifGLuint    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.gifdot.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.gifint LoadGLTextures()                                    // Load Bitmaps And Convert To Textures
ExpandedBlockStart.gifContractedBlock.gifdot.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/Crate.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, 0, 3, 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, 0, 3, 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.gifdot.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.gifint InitGL(GLvoid)                                        // All Setup For OpenGL Goes Here
ExpandedBlockStart.gifContractedBlock.gifdot.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.0f, 0.0f, 0.0f, 0.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    return TRUE;                                        // Initialization Went OK
ExpandedBlockEnd.gif}
None.gif
None.gifint DrawGLScene(GLvoid)                                    // Here's Where We Do All The Drawing
ExpandedBlockStart.gifContractedBlock.gifdot.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.0f, 0.0f, 1.0f);
InBlock.gif        glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
InBlock.gif        glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
InBlock.gif        glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
InBlock.gif        glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
InBlock.gif        // Back Face
InBlock.gif        glNormal3f( 0.0f, 0.0f,-1.0f);
InBlock.gif        glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
InBlock.gif        glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
InBlock.gif        glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
InBlock.gif        glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
InBlock.gif        // Top Face
InBlock.gif        glNormal3f( 0.0f, 1.0f, 0.0f);
InBlock.gif        glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
InBlock.gif        glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
InBlock.gif        glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
InBlock.gif        glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
InBlock.gif        // Bottom Face
InBlock.gif        glNormal3f( 0.0f,-1.0f, 0.0f);
InBlock.gif        glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
InBlock.gif        glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
InBlock.gif        glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
InBlock.gif        glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
InBlock.gif        // Right face
InBlock.gif        glNormal3f( 1.0f, 0.0f, 0.0f);
InBlock.gif        glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
InBlock.gif        glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
InBlock.gif        glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
InBlock.gif        glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
InBlock.gif        // Left Face
InBlock.gif        glNormal3f(-1.0f, 0.0f, 0.0f);
InBlock.gif        glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
InBlock.gif        glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
InBlock.gif        glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
InBlock.gif        glTexCoord2f(0.0f, 1.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.gifdot.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.gifdot.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                                0, 0,                                // 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        0, 0, 0, 0, 0, 0,                            // Color Bits Ignored
InBlock.gif        0,                                            // No Alpha Buffer
InBlock.gif        0,                                            // Shift Bit Ignored
InBlock.gif        0,                                            // No Accumulation Buffer
InBlock.gif        0, 0, 0, 0,                                    // 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        0, 0, 0                                        // 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.gifdot.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.gifint 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.gifdot.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("NeHe's Textures, Lighting & Keyboard 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
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("NeHe's Textures, Lighting & Keyboard 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

 

这一课里还讲解了如何使用光照。他使用了两种不同的光。第一种称为环境光。环境光来自于四面八方。所有场景中的对象都处于环境光的照射中。第二种类型的光源叫做漫射光。漫射光由特定的光源产生,并在您的场景中的对象表面上产生反射。处于漫射光直接照射下的任何对象表面都变得很亮,而几乎未被照射到的区域就显得要暗一些。创建光源的过程和颜色的创建完全一致,先设置用来创建光源的数组。前三个参数分别是RGB三色分量,最后一个是alpha通道参数。

 

 

GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };     // 环境光参数

 

GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };     // 漫射光参数

 

 

最后保存光源的位置。前三个参数和glTranslate中的一样。依次分别是XYZ轴上的位移。最后一个参数取为1.0f。这将告诉OpenGL这里指定的坐标就是光源的位置。

 

 

GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };    // 光源位置

 

 

教程里使用三种纹理滤波方式,第一种纹理(texture 0)使用gl_nearest(不光滑)滤波方式构建。第二种纹理(texture 1)使用gl_linear(线性滤波)方式,离屏幕越近的图像看起来就越光滑。第三种纹理 (texture 2)使用mipmapped滤波方式,这将创建一个外观十分优秀的纹理。

 

 

下面是创建纹理的新方法:Mip-mapping(纹理细化)。您可能会注意到当图像在屏幕上变得很小的时候,很多细节将会丢失。刚才还很不错的图案变得很难看。当您告诉OpenGL创建一个mipmapped的纹理后,OpenGL将尝试创建不同尺寸的高质量纹理。当您向屏幕绘制一个mipmapped纹理的时候,OpenGL将选择它已经创建的外观最佳的纹理(带有更多细节)来绘制,而不仅仅是缩放原先的图像(这将导致细节丢失)。

 

 

要绕过OpenGL对纹理宽度和高度所加的限制 (64128256等等)。办法就是 gluBuild2DMipmaps,我们可以使用任意的位图来创建纹理。OpenGL将自动将它缩放到正常的大小。

 

 

// 创建 MipMapped 纹理
glBindTexture(GL_TEXTURE_2D, texture[2]);
           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);

 

 

下面生成mipmapped纹理。我们使用三种颜色(红,绿,蓝)来生成一个2D纹理。TextureImage[0]->sizeX 是位图宽度,extureImage[0]->sizeY是位图高度,GL_RGB意味着我们依次使用RGB色彩。GL_UNSIGNED_BYTE意味着纹理数据的单位是字节。TextureImage[0]->data指向我们创建纹理所用的位图。

    gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
    TextureImage[0]->sizeX, TextureImage[0]->sizeY,
    GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
 

 

 

现在开始设置光源。下面一行设置环境光的发光量,光源light1开始发光:

 

 

  glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);  // 设置环境光

 

设置漫射光的发光量:

 

glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);  // 设置漫射光

 

然后设置光源的位置:

 

glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);  // 光源位置

 

 

最后,我们启用一号光源,没有启用GL_LIGHTING,所以您看不见任何光线(只对光源进行设置、定位、甚至启用,光源都不会工作。除非我们启用GL_LIGHTING)。

 

 

glEnable(GL_LIGHT1);                // 启用一号光源

 

 

课程中还学习了一个新的函数glNormal3fNormal就是法线的意思,所谓法线是指经过面(多边形)上的一点且垂直于这个面(多边形)的直线。使用光源的时候必须指定一条法线。法线告诉OpenGL这个多边形的朝向,并指明多边形的正面和背面。如果没有指定法线,什么怪事情都可能发生。