飘动的风景opengl程序.docx
《飘动的风景opengl程序.docx》由会员分享,可在线阅读,更多相关《飘动的风景opengl程序.docx(20页珍藏版)》请在冰豆网上搜索。
飘动的风景opengl程序
/*
*ThisCodeWasCreatedBybosco/JeffMolofee2000
*AHUGEThanksToFredricEcholsForCleaningUp
*AndOptimizingTheBaseCode,MakingItMoreFlexible!
*IfYou'veFoundThisCodeUseful,PleaseLetMeKnow.
*VisitMySiteAt
*/
#include//HeaderFileForWindows
#include//HeaderFileForStandardInput/Output
#include//HeaderFileForTheMathLibrary
#include//HeaderFileForTheOpenGL32Library
#include//HeaderFileForTheGLu32Library
#include//HeaderFileForTheGlauxLibrary
HDChDC=NULL;//PrivateGDIDeviceContext
HGLRChRC=NULL;//PermanentRenderingContext
HWNDhWnd=NULL;//HoldsOurWindowHandle
HINSTANCEhInstance;//HoldsTheInstanceOfTheApplication
boolkeys[256];//ArrayUsedForTheKeyboardRoutine
boolactive=TRUE;//WindowActiveFlagSetToTRUEByDefault
boolfullscreen=TRUE;//FullscreenFlagSetToFullscreenModeByDefault
floatpoints[45][45][3];//TheArrayForThePointsOnTheGridOfOur"Wave"
intwiggle_count=0;//CounterUsedToControlHowFastFlagWaves
GLfloatxrot;//XRotation(NEW)
GLfloatyrot;//YRotation(NEW)
GLfloatzrot;//ZRotation(NEW)
GLfloathold;//TemporarilyHoldsAFloatingPointValue
GLuinttexture[1];//StorageForOneTexture(NEW)
LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);//DeclarationForWndProc
AUX_RGBImageRec*LoadBMP(char*Filename)//LoadsABitmapImage
{
FILE*File=NULL;//FileHandle
if(!
Filename)//MakeSureAFilenameWasGiven
{
returnNULL;//IfNotReturnNULL
}
File=fopen(Filename,"r");//CheckToSeeIfTheFileExists
if(File)//DoesTheFileExist?
{
fclose(File);//CloseTheHandle
returnauxDIBImageLoad(Filename);//LoadTheBitmapAndReturnAPointer
}
returnNULL;//IfLoadFailedReturnNULL
}
intLoadGLTextures()//LoadBitmapsAndConvertToTextures
{
intStatus=FALSE;//StatusIndicator
AUX_RGBImageRec*TextureImage[1];//CreateStorageSpaceForTheTexture
memset(TextureImage,0,sizeof(void*)*1);//SetThePointerToNULL
//LoadTheBitmap,CheckForErrors,IfBitmap'sNotFoundQuit
if(TextureImage[0]=LoadBMP("Data/Tim.bmp"))
{
Status=TRUE;//SetTheStatusToTRUE
glGenTextures(1,&texture[0]);//CreateTheTexture
//TypicalTextureGenerationUsingDataFromTheBitmap
glBindTexture(GL_TEXTURE_2D,texture[0]);
glTexImage2D(GL_TEXTURE_2D,0,3,TextureImage[0]->sizeX,TextureImage[0]->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
if(TextureImage[0])//IfTextureExists
{
if(TextureImage[0]->data)//IfTextureImageExists
{
free(TextureImage[0]->data);//FreeTheTextureImageMemory
}
free(TextureImage[0]);//FreeTheImageStructure
}
returnStatus;//ReturnTheStatus
}
GLvoidReSizeGLScene(GLsizeiwidth,GLsizeiheight)//ResizeAndInitializeTheGLWindow
{
if(height==0)//PreventADivideByZeroBy
{
height=1;//MakingHeightEqualOne
}
glViewport(0,0,width,height);//ResetTheCurrentViewport
glMatrixMode(GL_PROJECTION);//SelectTheProjectionMatrix
glLoadIdentity();//ResetTheProjectionMatrix
//CalculateTheAspectRatioOfTheWindow
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);//SelectTheModelviewMatrix
glLoadIdentity();//ResetTheModelviewMatrix
}
intInitGL(GLvoid)//AllSetupForOpenGLGoesHere
{
if(!
LoadGLTextures())//JumpToTextureLoadingRoutine(NEW)
{
returnFALSE;//IfTextureDidn'tLoadReturnFALSE
}
glEnable(GL_TEXTURE_2D);//EnableTextureMapping(NEW)
glShadeModel(GL_SMOOTH);//EnableSmoothShading
glClearColor(0.0f,0.0f,0.0f,0.5f);//BlackBackground
glClearDepth(1.0f);//DepthBufferSetup
glEnable(GL_DEPTH_TEST);//EnablesDepthTesting
glDepthFunc(GL_LEQUAL);//TheTypeOfDepthTestingToDo
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//ReallyNicePerspectiveCalculations
glPolygonMode(GL_BACK,GL_FILL);//BackFaceIsSolid
glPolygonMode(GL_FRONT,GL_LINE);//FrontFaceIsMadeOfLines
for(intx=0;x<45;x++)
{
for(inty=0;y<45;y++)
{
points[x][y][0]=float((x/5.0f)-4.5f);
points[x][y][1]=float((y/5.0f)-4.5f);
points[x][y][2]=float(sin((((x/5.0f)*40.0f)/360.0f)*3.141592654*2.0f));
}
}
returnTRUE;//InitializationWentOK
}
intDrawGLScene(GLvoid)//Here'sWhereWeDoAllTheDrawing
{
intx,y;
floatfloat_x,float_y,float_xb,float_yb;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//ClearTheScreenAndTheDepthBuffer
glLoadIdentity();//ResetTheView
glTranslatef(0.0f,0.0f,-12.0f);
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
glRotatef(zrot,0.0f,0.0f,1.0f);
glBindTexture(GL_TEXTURE_2D,texture[0]);
glBegin(GL_QUADS);
for(x=0;x<44;x++)
{
for(y=0;y<44;y++)
{
float_x=float(x)/44.0f;
float_y=float(y)/44.0f;
float_xb=float(x+1)/44.0f;
float_yb=float(y+1)/44.0f;
glTexCoord2f(float_x,float_y);
glVertex3f(points[x][y][0],points[x][y][1],points[x][y][2]);
glTexCoord2f(float_x,float_yb);
glVertex3f(points[x][y+1][0],points[x][y+1][1],points[x][y+1][2]);
glTexCoord2f(float_xb,float_yb);
glVertex3f(points[x+1][y+1][0],points[x+1][y+1][1],points[x+1][y+1][2]);
glTexCoord2f(float_xb,float_y);
glVertex3f(points[x+1][y][0],points[x+1][y][1],points[x+1][y][2]);
}
}
glEnd();
if(wiggle_count==2)
{
for(y=0;y<45;y++)
{
hold=points[0][y][2];
for(x=0;x<44;x++)
{
points[x][y][2]=points[x+1][y][2];
}
points[44][y][2]=hold;
}
wiggle_count=0;
}
wiggle_count++;
xrot+=0.3f;
yrot+=0.2f;
zrot+=0.4f;
returnTRUE;//KeepGoing
}
GLvoidKillGLWindow(GLvoid)//ProperlyKillTheWindow
{
if(fullscreen)//AreWeInFullscreenMode?
{
ChangeDisplaySettings(NULL,0);//IfSoSwitchBackToTheDesktop
ShowCursor(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;//HoldsTheResultsAfterSearchingForAMatch
WNDCLASSwc;//WindowsClassStructure
DWORDdwExStyle;//WindowExtendedStyle
DWORDdwStyle;//WindowStyle
RECTWindowRect;//GrabsRectangleUpperLeft/LowerRightValues
WindowRect.left=(long)0;//SetLeftValueTo0
WindowRect.right=(long)width;//SetRightValueToRequestedWidth
WindowRect.top=(long)0;//SetTopValueTo0
WindowRect.bottom=(long)height;//SetBottomValueToRequestedHeight
fullscreen=fullscreenflag;//SetTheGlobalFullscreenFlag
hInstance=GetModuleHandle(NULL);//GrabAnInstanceForOurWindow
wc.style=CS_HREDRAW|CS_VREDRAW|CS_OWNDC;//RedrawOnSize,AndOwnDCForWindow.
wc.lpfnWndProc=(WNDPROC)WndProc;//WndProcHandlesMessages
wc.cbClsExtra=0;//NoExtraWindowData
wc.cbWndExtra=0;//NoExtraWindowData
wc.hInstance=hInstance;//SetTheInstance
wc.hIcon=LoadIcon(NULL,IDI_WINLOGO);//LoadTheDefaultIcon
wc.hCursor=LoadCursor(NULL,IDC_ARROW);//LoadTheArrowPointer
wc.hbrBackground=NULL;//NoBackgroundRequiredForGL
wc.lpszMenuName=NULL;//WeDon'tWantAMenu
wc.lpszClassName="OpenGL";//SetTheClassName
if(!
RegisterClass(&wc))//AttemptToRegisterTheWindowClass
{
MessageBox(NULL,"FailedToRegisterTheWindowClass.","ERROR",MB_OK|MB_ICONEXCLAMATION);
returnFALSE;//ReturnFALSE
}
if(fullscreen)//AttemptFullscreenMode?
{
DEVMODEdmScreenSettings;