三维物体转动opengl程序.docx
《三维物体转动opengl程序.docx》由会员分享,可在线阅读,更多相关《三维物体转动opengl程序.docx(15页珍藏版)》请在冰豆网上搜索。
三维物体转动opengl程序
/*
*ThisCodeWasCreatedByJeffMolofee2000
*AHUGEThanksToFredricEcholsForCleaningUp
*AndOptimizingTheBaseCode,MakingItMoreFlexible!
*IfYou'veFoundThisCodeUseful,PleaseLetMeKnow.
*VisitMySiteAt
*/
#include//HeaderFileForWindows
#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
GLfloatrtri;//AngleForTheTriangle(NEW)
GLfloatrquad;//AngleForTheQuad(NEW)
LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);//DeclarationForWndProc
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
{
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
returnTRUE;//InitializationWentOK
}
intDrawGLScene(GLvoid)//Here'sWhereWeDoAllTheDrawing
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//ClearScreenAndDepthBuffer
glLoadIdentity();//ResetTheCurrentModelviewMatrix
glTranslatef(-1.5f,0.0f,-6.0f);//MoveLeft1.5UnitsAndIntoTheScreen6.0
glRotatef(rtri,0.0f,1.0f,0.0f);//RotateTheTriangleOnTheYaxis(NEW)
glBegin(GL_TRIANGLES);//StartDrawingATriangle
glColor3f(1.0f,0.0f,0.0f);//Red
glVertex3f(0.0f,1.0f,0.0f);//TopOfTriangle(Front)
glColor3f(0.0f,1.0f,0.0f);//Green
glVertex3f(-1.0f,-1.0f,1.0f);//LeftOfTriangle(Front)
glColor3f(0.0f,0.0f,1.0f);//Blue
glVertex3f(1.0f,-1.0f,1.0f);//RightOfTriangle(Front)
glColor3f(1.0f,0.0f,0.0f);//Red
glVertex3f(0.0f,1.0f,0.0f);//TopOfTriangle(Right)
glColor3f(0.0f,0.0f,1.0f);//Blue
glVertex3f(1.0f,-1.0f,1.0f);//LeftOfTriangle(Right)
glColor3f(0.0f,1.0f,0.0f);//Green
glVertex3f(1.0f,-1.0f,-1.0f);//RightOfTriangle(Right)
glColor3f(1.0f,0.0f,0.0f);//Red
glVertex3f(0.0f,1.0f,0.0f);//TopOfTriangle(Back)
glColor3f(0.0f,1.0f,0.0f);//Green
glVertex3f(1.0f,-1.0f,-1.0f);//LeftOfTriangle(Back)
glColor3f(0.0f,0.0f,1.0f);//Blue
glVertex3f(-1.0f,-1.0f,-1.0f);//RightOfTriangle(Back)
glColor3f(1.0f,0.0f,0.0f);//Red
glVertex3f(0.0f,1.0f,0.0f);//TopOfTriangle(Left)
glColor3f(0.0f,0.0f,1.0f);//Blue
glVertex3f(-1.0f,-1.0f,-1.0f);//LeftOfTriangle(Left)
glColor3f(0.0f,1.0f,0.0f);//Green
glVertex3f(-1.0f,-1.0f,1.0f);//RightOfTriangle(Left)
glEnd();//DoneDrawingThePyramid
glLoadIdentity();//ResetTheCurrentModelviewMatrix
glTranslatef(1.5f,0.0f,-7.0f);//MoveRight1.5UnitsAndIntoTheScreen7.0
glRotatef(rquad,1.0f,1.0f,1.0f);//RotateTheQuadOnTheXaxis(NEW)
glBegin(GL_QUADS);//DrawAQuad
glColor3f(0.0f,1.0f,0.0f);//SetTheColorToGreen
glVertex3f(1.0f,1.0f,-1.0f);//TopRightOfTheQuad(Top)
glVertex3f(-1.0f,1.0f,-1.0f);//TopLeftOfTheQuad(Top)
glVertex3f(-1.0f,1.0f,1.0f);//BottomLeftOfTheQuad(Top)
glVertex3f(1.0f,1.0f,1.0f);//BottomRightOfTheQuad(Top)
glColor3f(1.0f,0.5f,0.0f);//SetTheColorToOrange
glVertex3f(1.0f,-1.0f,1.0f);//TopRightOfTheQuad(Bottom)
glVertex3f(-1.0f,-1.0f,1.0f);//TopLeftOfTheQuad(Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f);//BottomLeftOfTheQuad(Bottom)
glVertex3f(1.0f,-1.0f,-1.0f);//BottomRightOfTheQuad(Bottom)
glColor3f(1.0f,0.0f,0.0f);//SetTheColorToRed
glVertex3f(1.0f,1.0f,1.0f);//TopRightOfTheQuad(Front)
glVertex3f(-1.0f,1.0f,1.0f);//TopLeftOfTheQuad(Front)
glVertex3f(-1.0f,-1.0f,1.0f);//BottomLeftOfTheQuad(Front)
glVertex3f(1.0f,-1.0f,1.0f);//BottomRightOfTheQuad(Front)
glColor3f(1.0f,1.0f,0.0f);//SetTheColorToYellow
glVertex3f(1.0f,-1.0f,-1.0f);//TopRightOfTheQuad(Back)
glVertex3f(-1.0f,-1.0f,-1.0f);//TopLeftOfTheQuad(Back)
glVertex3f(-1.0f,1.0f,-1.0f);//BottomLeftOfTheQuad(Back)
glVertex3f(1.0f,1.0f,-1.0f);//BottomRightOfTheQuad(Back)
glColor3f(0.0f,0.0f,1.0f);//SetTheColorToBlue
glVertex3f(-1.0f,1.0f,1.0f);//TopRightOfTheQuad(Left)
glVertex3f(-1.0f,1.0f,-1.0f);//TopLeftOfTheQuad(Left)
glVertex3f(-1.0f,-1.0f,-1.0f);//BottomLeftOfTheQuad(Left)
glVertex3f(-1.0f,-1.0f,1.0f);//BottomRightOfTheQuad(Left)
glColor3f(1.0f,0.0f,1.0f);//SetTheColorToViolet
glVertex3f(1.0f,1.0f,-1.0f);//TopRightOfTheQuad(Right)
glVertex3f(1.0f,1.0f,1.0f);//TopLeftOfTheQuad(Right)
glVertex3f(1.0f,-1.0f,1.0f);//BottomLeftOfTheQuad(Right)
glVertex3f(1.0f,-1.0f,-1.0f);//BottomRightOfTheQuad(Right)
glEnd();//DoneDrawingTheQuad
rtri+=0.2f;//IncreaseTheRotationVariableForTheTriangle(NEW)
rquad-=0.15f;//DecreaseTheRotationVariableForTheQuad(NEW)
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(NUL