#include<windows.h>//HeaderFileForWindows#include<gl/gl.h>//HeaderFileForTheOpenGL32Library#include<gl/glu.h>//HeaderFileForTheGLu32Library#include<gl/glaux.h>//HeaderFileForTheGlauxLibraryHDChDC=NULL;//PrivateGDIDeviceContextHGLRChRC=NULL;//PermanentRenderingContextHWNDhWnd=NULL;//HoldsOurWindowHandleHINSTANCEhInstance;//HoldsTheInstanceOfTheApplicationboolkeys[256];//ArrayUsedForTheKeyboardRoutineboolactive=TRUE;//WindowActiveFlagSetToTRUEByDefaultboolfullscreen=TRUE;//FullscreenFlagSetToFullscreenModeByDefaultLRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);//DeclarationForWndProcGLvoidReSizeGLScene(GLsizeiwidth,GLsizeiheight)//ResizeAndInitializeTheGLWindow{if(height==0)//PreventADivideByZeroBy{height=1;//MakingHeightEqualOne}glViewport(0,0,width,height);//ResetTheCurrentViewportglMatrixMode(GL_PROJECTION);//SelectTheProjectionMatrixglLoadIdentity();//ResetTheProjectionMatrix//CalculateTheAspectRatioOfTheWindowgluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);glMatrixMode(GL_MODELVIEW);//SelectTheModelviewMatrixglLoadIdentity();//ResetTheModelviewMatrix}intInitGL(GLvoid)//AllSetupForOpenGLGoesHere{glShadeModel(GL_SMOOTH);//EnableSmoothShadingglClearColor(0.0f,0.0f,0.0f,0.5f);//BlackBackgroundglClearDepth(1.0f);//DepthBufferSetupglEnable(GL_DEPTH_TEST);//EnablesDepthTestingglDepthFunc(GL_LEQUAL);//TheTypeOfDepthTestingToDoglHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//ReallyNicePerspectiveCalculationsreturnTRUE;//InitializationWentOK}intDrawGLScene(GLvoid)//Here'sWhereWeDoAllTheDrawing{glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//ClearScreenAndDepthBufferglLoadIdentity();//ResetTheCurrentModelviewMatrixreturnTRUE;//EverythingWentOK}GLvoidKillGLWindow(GLvoid)//ProperlyKillTheWindow{if(fullscreen)//AreWeInFullscreenMode?{ChangeDisplaySettings(NULL,0);//IfSoSwitchBackToTheDesktopShowCursor(TRUE);//ShowMousePointer}if(hRC)//DoWeHaveARenderingContext?{if(!wglMakeCurrent(NULL,NULL))//AreWeAbleToReleaseTheDCAndRCContexts?{MessageBox(NULL,"ReleaseOfDCAndRCFailed.","SHUTDOWNERROR",MB_OK|MB_ICONINFORMATION);}if(!wglDeleteContext(hRC))//AreWeAbleToDeleteTheRC?{MessageBox(NULL,"ReleaseRenderingContextFailed.","SHUTDOWNERROR",MB_OK|MB_ICONINFORMATION);}hRC=NULL;//SetRCToNULL}if(hDC&&!ReleaseDC(hWnd,hDC))//AreWeAbleToReleaseTheDC{MessageBox(NULL,"ReleaseDeviceContextFailed.","SHUTDOWNERROR",MB_OK|MB_ICONINFORMATION);hDC=NULL;//SetDCToNULL}if(hWnd&&!DestroyWindow(hWnd))//AreWeAbleToDestroyTheWindow?{MessageBox(NULL,"CouldNotReleasehWnd.","SHUTDOWNERROR",MB_OK|MB_ICONINFORMATION);hWnd=NULL;//SethWndToNULL}if(!UnregisterClass("OpenGL",hInstance))//AreWeAbleToUnregisterClass{MessageBox(NULL,"CouldNotUnregisterClass.","SHUTDOWNERROR",MB_OK|MB_ICONINFORMATION);hInstance=NULL;//SethInstanceToNULL}}/**//*ThisCodeCreatesOurOpenGLWindow.ParametersAre:**title-TitleToAppearAtTheTopOfTheWindow**width-WidthOfTheGLWindowOrFullscreenMode**height-HeightOfTheGLWindowOrFullscreenMode**bits-NumberOfBitsToUseForColor(8/16/24/32)**fullscreenflag-UseFullscreenMode(TRUE)OrWindowedMode(FALSE)*/BOOLCreateGLWindow(char*title,intwidth,intheight,intbits,boolfullscreenflag){GLuintPixelFormat;//HoldsTheResultsAfterSearchingForAMatchWNDCLASSwc;//WindowsClassStructureDWORDdwExStyle;//WindowExtendedStyleDWORDdwStyle;//WindowStyleRECTWindowRect;//GrabsRectangleUpperLeft/LowerRightValuesWindowRect.left=(long)0;//SetLeftValueTo0WindowRect.right=(long)width;//SetRightValueToRequestedWidthWindowRect.top=(long)0;//SetTopValueTo0WindowRect.bottom=(long)height;//SetBottomValueToRequestedHeightfullscreen=fullscreenflag;//SetTheGlobalFullscreenFlaghInstance=GetModuleHandle(NULL);//GrabAnInstanceForOurWindowwc.style=CS_HREDRAW|CS_VREDRAW|CS_OWNDC;//RedrawOnSize,AndOwnDCForWindow.wc.lpfnWndProc=(WNDPROC)WndProc;//WndProcHandlesMessageswc.cbClsExtra=0;//NoExtraWindowDatawc.cbWndExtra=0;//NoExtraWindowDatawc.hInstance=hInstance;//SetTheInstancewc.hIcon=LoadIcon(NULL,IDI_WINLOGO);//LoadTheDefaultIconwc.hCursor=LoadCursor(NULL,IDC_ARROW);//LoadTheArrowPointerwc.hbrBackground=NULL;//NoBackgroundRequiredForGLwc.lpszMenuName=NULL;//WeDon'tWantAMenuwc.lpszClassName="OpenGL";//SetTheClassNameif(!RegisterClass(&wc))//AttemptToRegisterTheWindowClass{MessageBox(NULL,"FailedToRegisterTheWindowClass.","ERROR",MB_OK|MB_ICONEXCLAMATION);returnFALSE;//ReturnFALSE}if(fullscreen)//AttemptFullscreenMode?{DEVMODEdmScreenSettings;//DeviceModememset(&dmScreenSettings,0,sizeof(dmScreenSettings));//MakesSureMemory'sCleareddmScreenSettings.dmSize=sizeof(dmScreenSettings);//SizeOfTheDevmodeStructuredmScreenSettings.dmPelsWidth=width;//SelectedScreenWidthdmScreenSettings.dmPelsHeight=height;//SelectedScreenHeightdmScreenSettings.dmBitsPerPel=bits;//SelectedBitsPerPixeldmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;//TryToSetSelectedModeAndGetResults.NOTE:CDS_FULLSCREENGetsRidOfStartBar.if(ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL){//IfTheModeFails,OfferTwoOptions.QuitOrUseWindowedMode.if(MessageBox(NULL,"TheRequestedFullscreenModeIsNotSupportedBy/nYourVideoCard.UseWindowedModeInstead?","NeHeGL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES){fullscreen=FALSE;//WindowedModeSelected.Fullscreen=FALSE}else{//PopUpAMessageBoxLettingUserKnowTheProgramIsClosing.MessageBox(NULL,"ProgramWillNowClose.","ERROR",MB_OK|MB_ICONSTOP);returnFALSE;//ReturnFALSE}}}if(fullscreen)//AreWeStillInFullscreenMode?{dwExStyle=WS_EX_APPWINDOW;//WindowExtendedStyledwStyle=WS_POPUP;//WindowsStyleShowCursor(FALSE);//HideMousePointer}else{dwExStyle=WS_EX_APPWINDOW|WS_EX_WINDOWEDGE;//WindowExtendedStyledwStyle=WS_OVERLAPPEDWINDOW;//WindowsStyle}AdjustWindowRectEx(&WindowRect,dwStyle,FALSE,dwExStyle);//AdjustWindowToTrueRequestedSize//CreateTheWindowif(!(hWnd=CreateWindowEx(dwExStyle,//ExtendedStyleForTheWindow"OpenGL",//ClassNametitle,//WindowTitledwStyle|//DefinedWindowStyleWS_CLIPSIBLINGS|//RequiredWindowStyleWS_CLIPCHILDREN,//RequiredWindowStyle0,0,//WindowPositionWindowRect.right-WindowRect.left,//CalculateWindowWidthWindowRect.bottom-WindowRect.top,//CalculateWindowHeightNULL,//NoParentWindowNULL,//NoMenuhInstance,//InstanceNULL)))//DontPassAnythingToWM_CREATE{KillGLWindow();//ResetTheDisplayMessageBox(NULL,"WindowCreationError.","ERROR",MB_OK|MB_ICONEXCLAMATION);returnFALSE;//ReturnFALSE}staticPIXELFORMATDESCRIPTORpfd=//pfdTellsWindowsHowWeWantThingsToBe{sizeof(PIXELFORMATDESCRIPTOR),//SizeOfThisPixelFormatDescriptor1,//VersionNumberPFD_DRAW_TO_WINDOW|//FormatMustSupportWindowPFD_SUPPORT_OPENGL|//FormatMustSupportOpenGLPFD_DOUBLEBUFFER,//MustSupportDoubleBufferingPFD_TYPE_RGBA,//RequestAnRGBAFormatbits,//SelectOurColorDepth0,0,0,0,0,0,//ColorBitsIgnored0,//NoAlphaBuffer0,//ShiftBitIgnored0,//NoAccumulationBuffer0,0,0,0,//AccumulationBitsIgnored16,//16BitZ-Buffer(DepthBuffer)0,//NoStencilBuffer0,//NoAuxiliaryBufferPFD_MAIN_PLANE,//MainDrawingLayer0,//Reserved0,0,0//LayerMasksIgnored};if(!(hDC=GetDC(hWnd)))//DidWeGetADeviceContext?{KillGLWindow();//ResetTheDisplayMessageBox(NULL,"Can'tCreateAGLDeviceContext.","ERROR",MB_OK|MB_ICONEXCLAMATION);returnFALSE;//ReturnFALSE}if(!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))//DidWindowsFindAMatchingPixelFormat?{KillGLWindow();//ResetTheDisplayMessageBox(NULL,"Can'tFindASuitablePixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);returnFALSE;//ReturnFALSE}if(!SetPixelFormat(hDC,PixelFormat,&pfd))//AreWeAbleToSetThePixelFormat?{KillGLWindow();//ResetTheDisplayMessageBox(NULL,"Can'tSetThePixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);returnFALSE;//ReturnFALSE}if(!(hRC=wglCreateContext(hDC)))//AreWeAbleToGetARenderingContext?{KillGLWindow();//ResetTheDisplayMessageBox(NULL,"Can'tCreateAGLRenderingContext.","ERROR",MB_OK|MB_ICONEXCLAMATION);returnFALSE;//ReturnFALSE}if(!wglMakeCurrent(hDC,hRC))//TryToActivateTheRenderingContext{KillGLWindow();//ResetTheDisplayMessageBox(NULL,"Can'tActivateTheGLRenderingContext.","ERROR",MB_OK|MB_ICONEXCLAMATION);returnFALSE;//ReturnFALSE}ShowWindow(hWnd,SW_SHOW);//ShowTheWindowSetForegroundWindow(hWnd);//SlightlyHigherPrioritySetFocus(hWnd);//SetsKeyboardFocusToTheWindowReSizeGLScene(width,height);//SetUpOurPerspectiveGLScreenif(!InitGL())//InitializeOurNewlyCreatedGLWindow{KillGLWindow();//ResetTheDisplayMessageBox(NULL,"InitializationFailed.","ERROR",MB_OK|MB_ICONEXCLAMATION);returnFALSE;//ReturnFALSE}returnTRUE;//Success}LRESULTCALLBACKWndProc(HWNDhWnd,//HandleForThisWindowUINTuMsg,//MessageForThisWindowWPARAMwParam,//AdditionalMessageInformationLPARAMlParam)//AdditionalMessageInformation{switch(uMsg)//CheckForWindowsMessages{caseWM_ACTIVATE://WatchForWindowActivateMessage{if(!HIWORD(wParam))//CheckMinimizationState{active=TRUE;//ProgramIsActive}else{active=FALSE;//ProgramIsNoLongerActive}return0;//ReturnToTheMessageLoop}caseWM_SYSCOMMAND://InterceptSystemCommands{switch(wParam)//CheckSystemCalls{caseSC_SCREENSAVE://ScreensaverTryingToStart?caseSC_MONITORPOWER://MonitorTryingToEnterPowersave?return0;//PreventFromHappening}break;//Exit}caseWM_CLOSE://DidWeReceiveACloseMessage?{PostQuitMessage(0);//SendAQuitMessagereturn0;//JumpBack}caseWM_KEYDOWN://IsAKeyBeingHeldDown?{keys[wParam]=TRUE;//IfSo,MarkItAsTRUEreturn0;//JumpBack}caseWM_KEYUP://HasAKeyBeenReleased?{keys[wParam]=FALSE;//IfSo,MarkItAsFALSEreturn0;//JumpBack}caseWM_SIZE://ResizeTheOpenGLWindow{ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));//LoWord=Width,HiWord=Heightreturn0;//JumpBack}}//PassAllUnhandledMessagesToDefWindowProcreturnDefWindowProc(hWnd,uMsg,wParam,lParam);}intWINAPIWinMain(HINSTANCEhInstance,//InstanceHINSTANCEhPrevInstance,//PreviousInstanceLPSTRlpCmdLine,//CommandLineParametersintnCmdShow)//WindowShowState{MSGmsg;//WindowsMessageStructureBOOLdone=FALSE;//BoolVariableToExitLoop//AskTheUserWhichScreenModeTheyPreferif(MessageBox(NULL,"WouldYouLikeToRunInFullscreenMode?","StartFullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO){fullscreen=FALSE;//WindowedMode}//CreateOurOpenGLWindowif(!CreateGLWindow("NeHe'sOpenGLFramework",640,480,16,fullscreen)){return0;//QuitIfWindowWasNotCreated}while(!done)//LoopThatRunsWhiledone=FALSE{if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))//IsThereAMessageWaiting?{if(msg.message==WM_QUIT)//HaveWeReceivedAQuitMessage?{done=TRUE;//IfSodone=TRUE}else//IfNot,DealWithWindowMessages{TranslateMessage(&msg);//TranslateTheMessageDispatchMessage(&msg);//DispatchTheMessage}}else//IfThereAreNoMessages{//DrawTheScene.WatchForESCKeyAndQuitMessagesFromDrawGLScene()if(active)//ProgramActive?{if(keys[VK_ESCAPE])//WasESCPressed?{done=TRUE;//ESCSignalledAQuit}else//NotTimeToQuit,UpdateScreen{DrawGLScene();//DrawTheSceneSwapBuffers(hDC);//SwapBuffers(DoubleBuffering)}}if(keys[VK_F1])//IsF1BeingPressed?{keys[VK_F1]=FALSE;//IfSoMakeKeyFALSEKillGLWindow();//KillOurCurrentWindowfullscreen=!fullscreen;//ToggleFullscreen/WindowedMode//RecreateOurOpenGLWindowif(!CreateGLWindow("NeHe'sOpenGLFramework",640,480,16,fullscreen)){return0;//QuitIfWindowWasNotCreated}}}}//ShutdownKillGLWindow();//KillTheWindowreturn(msg.wParam);//ExitTheProgram}