版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
《計算機圖形學》課程設計系專:班級:姓名:學號:題目:1、畫直線2、貼紋理指導老師:Bresenham算法畫直線目錄一、實驗目的----------------------3二、設計思路----------------------3三、程序代碼----------------------3四、運行結果---------------------19一、實驗目的運用Bresenham算法畫出斜率各不同的直線,包括大于-1小于1、等于1,大于1,小于-1,無窮大。設計思路假定直線的起點、終點分別為:(x1,y1),(x2,y2),且都為整數(shù)。則直線方程為y=mx+b,其中b=y1-mx1,m=dy/dx,當x每遞增1,y遞增k(即直線斜率)取yi+1,yi由d1,d2的大小決定y=m(xi+1)+b,d1=y-yi,d2=yi+1–y,當d1-d2>0,;當d1-d2<0,;d1-d2=2y-2yi-1=2dy/dx(xi+1)+2b-2yi-1,dx(d1-d2)=2dy(xi+1)+2bdx-2yidx-dx,設pi=dx(d1-d2),則pi=2xidy-2yidx+2dy+2bdx-dx(1)則pi+1=2(xi+1)dy-2yi+1dx+2dy+2bdx-dxpi+1=2dy-2(yi+1-yi)dx+pi;當pi>0時yi+1=yi+1,pi+1=2dy-2dx+pi當pi<0時yi+1=yi,pi+1=2dy+pi初值p1=2x1dy-2y1dx+2dy+2bdx-dx因為y1dx=x1dy+bdx;所以p1=2x1dy-2(x1dy+bdx)+2dy+2bdx-dx=2dy-dx總結:p1=2dy-dxdx=x2-x1,dy=y2-y1當p1>0時p2=p1+2dy-2dx當p1<0時p2=p1+2dy,由此得出算法。程序代碼#include<windows.h> //HeaderFileForWindows#include<gl\gl.h> //HeaderFileForTheOpenGL32Library#include<gl\glu.h> //HeaderFileForTheGLu32Library#include<gl\glaux.h> //HeaderFileForTheGlauxLibrary#include<stdio.h>#include<math.h>HDC hDC=NULL; //PrivateGDIDeviceContextHGLRC hRC=NULL; //PermanentRenderingContextHWND hWnd=NULL; //HoldsOurWindowHandleHINSTANCE hInstance; //HoldsTheInstanceOfTheApplicationbool keys[256]; //ArrayUsedForTheKeyboardRoutinebool active=TRUE; //WindowActiveFlagSetToTRUEByDefaultbool fullscreen=TRUE; //FullscreenFlagSetToFullscreenModeByDefaultboolpt=true;boolfc=true;floatx=0.0,y=0.0;LRESULT CALLBACKWndProc(HWND,UINT,WPARAM,LPARAM); //DeclarationForWndProcGLvoidReSizeGLScene(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}voidDrawLine(){ intx1=30,y1=-60,x2=0,y2=0;intdx=x2-x1,dy=y2-y1,y=y1,eps=0; floatm=(float)dy/dx; if(m>=0) { if(m<=1) { for(intx=x1;x<=x2;x++) { glBegin(GL_POINTS); glColor3f(1.0f,1.0f,1.0f); glVertex3f((float)x,(float)y,0.0f); glEnd(); if(pt==true) { printf("%d,%d,\n",x,y); } eps+=dy; if((eps<<1)>=dx) { y++; eps-=dx; } } } else { intdx=x2-x1,dy=y2-y1,x=x1,eps=0;for(inty=y1;y<=y2;y++) { glBegin(GL_POINTS); glColor3f(1.0f,1.0f,1.0f); glVertex3f((float)x,(float)y,0.0f); glEnd(); if(pt==true) { printf("%d,%d,\n",x,y); } eps+=dx; if((eps<<1)>=dy) { x++; eps-=dy; } } } } else { if(m>=(-1)) { intdx=x2-x1,dy=y2-y1,y=y1,eps=0;for(intx=x1;x>=x2;x--) { glBegin(GL_POINTS); glColor3f(1.0f,1.0f,1.0f); glVertex3f((float)x,(float)y,0.0f); glEnd(); if(pt==true) { printf("%d,%d,\n",x,y); } eps+=dy; if((eps<<1)>=abs(dx)) { y++; eps-=abs(dx); } } } else { intdx=x2-x1,dy=y2-y1,x=x1,eps=0;for(inty=y1;y<=y2;y++) { glBegin(GL_POINTS); glColor3f(1.0f,1.0f,1.0f); glVertex3f((float)x,(float)y,0.0f); glEnd(); if(pt==true) { printf("%d,%d,\n",x,y); } eps+=dx; if((eps<<1)<=-dy) { x--; eps+=dy; } } } } pt=false;}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 glEnable(GL_POINT_SMOOTH); AllocConsole(); freopen("CONOUT$","w",stdout); returnTRUE; //InitializationWentOK} intDrawGLScene(GLvoid) //Here'sWhereWeDoAllTheDrawing{ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //ClearScreenAndDepthBuffer glLoadIdentity();//ResetTheCurrentModelviewMatrix glTranslatef(0.0f,0.0f,-100.0f);DrawLine(); returnTRUE; //EverythingWentOK}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){ GLuint PixelFormat; //HoldsTheResultsAfterSearchingForAMatch WNDCLASS wc; //WindowsClassStructure DWORD dwExStyle; //WindowExtendedStyle DWORD dwStyle; //WindowStyle RECT WindowRect; //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; //DeviceMode memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); //MakesSureMemory'sCleared dmScreenSettings.dmSize=sizeof(dmScreenSettings); //SizeOfTheDevmodeStructure dmScreenSettings.dmPelsWidth =width; //SelectedScreenWidth dmScreenSettings.dmPelsHeight =height; //SelectedScreenHeight dmScreenSettings.dmBitsPerPel =bits; //SelectedBitsPerPixel dmScreenSettings.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; //WindowExtendedStyle dwStyle=WS_POPUP; //WindowsStyle ShowCursor(FALSE); //HideMousePointer } else { dwExStyle=WS_EX_APPWINDOW|WS_EX_WINDOWEDGE; //WindowExtendedStyle dwStyle=WS_OVERLAPPEDWINDOW; //WindowsStyle } AdjustWindowRectEx(&WindowRect,dwStyle,FALSE,dwExStyle); //AdjustWindowToTrueRequestedSize //CreateTheWindow if(!(hWnd=CreateWindowEx( dwExStyle, //ExtendedStyleForTheWindow "OpenGL", //ClassName title, //WindowTitle dwStyle| //DefinedWindowStyle WS_CLIPSIBLINGS| //RequiredWindowStyle WS_CLIPCHILDREN, //RequiredWindowStyle 0,0, //WindowPosition WindowRect.right-WindowRect.left, //CalculateWindowWidth WindowRect.bottom-WindowRect.top, //CalculateWindowHeight NULL, //NoParentWindow NULL, //NoMenu hInstance, //Instance NULL))) //DontPassAnythingToWM_CREATE { KillGLWindow(); //ResetTheDisplay MessageBox(NULL,"WindowCreationError.","ERROR",MB_OK|MB_ICONEXCLAMATION); returnFALSE; //ReturnFALSE } static PIXELFORMATDESCRIPTORpfd= //pfdTellsWindowsHowWeWantThingsToBe { sizeof(PIXELFORMATDESCRIPTOR), //SizeOfThisPixelFormatDescriptor 1, //VersionNumber PFD_DRAW_TO_WINDOW| //FormatMustSupportWindow PFD_SUPPORT_OPENGL| //FormatMustSupportOpenGL PFD_DOUBLEBUFFER, //MustSupportDoubleBuffering PFD_TYPE_RGBA, //RequestAnRGBAFormat bits, //SelectOurColorDepth 0,0,0,0,0,0, //ColorBitsIgnored 0, //NoAlphaBuffer 0, //ShiftBitIgnored 0, //NoAccumulationBuffer 0,0,0,0, //AccumulationBitsIgnored 16, //16BitZ-Buffer(DepthBuffer) 0, //NoStencilBuffer 0, //NoAuxiliaryBuffer PFD_MAIN_PLANE, //MainDrawingLayer 0, //Reserved 0,0,0 //LayerMasksIgnored }; if(!(hDC=GetDC(hWnd))) //DidWeGetADeviceContext? { KillGLWindow(); //ResetTheDisplay MessageBox(NULL,"Can'tCreateAGLDeviceContext.","ERROR",MB_OK|MB_ICONEXCLAMATION); returnFALSE; //ReturnFALSE } if(!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) //DidWindowsFindAMatchingPixelFormat? { KillGLWindow(); //ResetTheDisplay MessageBox(NULL,"Can'tFindASuitablePixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); returnFALSE; //ReturnFALSE } if(!SetPixelFormat(hDC,PixelFormat,&pfd)) //AreWeAbleToSetThePixelFormat? { KillGLWindow(); //ResetTheDisplay MessageBox(NULL,"Can'tSetThePixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); returnFALSE; //ReturnFALSE } if(!(hRC=wglCreateContext(hDC))) //AreWeAbleToGetARenderingContext? { KillGLWindow(); //ResetTheDisplay MessageBox(NULL,"Can'tCreateAGLRenderingContext.","ERROR",MB_OK|MB_ICONEXCLAMATION); returnFALSE; //ReturnFALSE } if(!wglMakeCurrent(hDC,hRC)) //TryToActivateTheRenderingContext { KillGLWindow(); //ResetTheDisplay MessageBox(NULL,"Can'tActivateTheGLRenderingContext.","ERROR",MB_OK|MB_ICONEXCLAMATION); returnFALSE; //ReturnFALSE } ShowWindow(hWnd,SW_SHOW); //ShowTheWindow SetForegroundWindow(hWnd); //SlightlyHigherPriority SetFocus(hWnd); //SetsKeyboardFocusToTheWindow ReSizeGLScene(width,height); //SetUpOurPerspectiveGLScreen if(!InitGL()) //InitializeOurNewlyCreatedGLWindow { KillGLWindow(); //ResetTheDisplay MessageBox(NULL,"InitializationFailed.","ERROR",MB_OK|MB_ICONEXCLAMATION); returnFALSE; //ReturnFALSE } returnTRUE; //Success}LRESULTCALLBACKWndProc( HWND hWnd, //HandleForThisWindow UINT uMsg, //MessageForThisWindow WPARAM wParam, //AdditionalMessageInformation LPARAM lParam) //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); //SendAQuitMessage return0; //JumpBack } caseWM_KEYDOWN: //IsAKeyBeingHeldDown? { keys[wParam]=TRUE; //IfSo,MarkItAsTRUE return0; //JumpBack } caseWM_KEYUP: //HasAKeyBeenReleased? { keys[wParam]=FALSE; //IfSo,MarkItAsFALSE return0; //JumpBack } caseWM_SIZE: //ResizeTheOpenGLWindow { ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));//LoWord=Width,HiWord=Height return0; //JumpBack } } //PassAllUnhandledMessagesToDefWindowProc returnDefWindowProc(hWnd,uMsg,wParam,lParam);}intWINAPIWinMain( HINSTANCE hInstance, //Instance HINSTANCE hPrevInstance, //PreviousInstance LPSTR lpCmdLine, //CommandLineParameters int nCmdShow) //WindowShowState{ MSG msg; //WindowsMessageStructure BOOL done=FALSE; //BoolVariableToExitLoop //AskTheUserWhichScreenModeTheyPrefer if(MessageBox(NULL,"WouldYouLikeToRunInFullscreenMode?","StartFullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO) { fullscreen=FALSE; //WindowedMode } //CreateOurOpenGLWindow if(!CreateGLWindow("OpenGLFramework",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); //TranslateTheMessage DispatchMessage(&msg); //DispatchTheMessage } } else //IfThereAreNoMessages { //DrawTheScene.WatchForESCKeyAndQuitMessagesFromDrawGLScene() if(active) //ProgramActive? { if(keys[VK_ESCAPE]) //WasESCPressed? { done=TRUE; //ESCSignalledAQuit } else //NotTimeToQuit,UpdateScreen { DrawGLScene(); //DrawTheScene SwapBuffers(hDC); //SwapBuffers(DoubleBuffering) } } if(keys[VK_F1]) //IsF1BeingPressed? { keys[VK_F1]=FALSE; //IfSoMakeKeyFALSE KillGLWindow(); //KillOurCurrentWindow fullscreen=!fullscreen; //ToggleFullscreen/WindowedMode //RecreateOurOpenGLWindow if(!CreateGLWindow("OpenGLFramework",640,480,16,fullscreen)) { return0; //QuitIfWindowWasNotCreated } } } } //Shutdown KillGLWindow(); //KillTheWindow return(msg.wParam); //ExitTheProgram}運行結果0<m<1:m>10>m>-1m<-1貼紋理目錄一、實驗目的----------------------3二、實驗內容----------------------3三、程序代碼----------------------3四、運行結果---------------------19實驗目的給一個正方體的表面貼上紋理圖案。實驗內容運用opengl圖形軟件包給正方體添上圖案。程序代碼/** ThisCodeWasCreatedByJeffMolofee2000* AHUGEThanksToFredricEcholsForCleaningUp* AndOptimizingTheBaseCode,MakingItMoreFlexible!* IfYou'veFoundThisCodeUseful,PleaseLetMeKnow.* VisitMySiteAt*/#include<windows.h> //HeaderFileForWindows#include<stdio.h> //HeaderFileForStandardInput/Output#include<gl\gl.h> //HeaderFileForTheOpenGL32Library#include<gl\glu.h> //HeaderFileForTheGLu32Library#include<gl\glaux.h> //HeaderFileForTheGlauxLibraryHDC hDC=NULL; //PrivateGDIDeviceContextHGLRC hRC=NULL; //PermanentRenderingContextHWND hWnd=NULL; //HoldsOurWindowHandleHINSTANCE hInstance; //HoldsTheInstanceOfTheApplicationbool keys[256]; //ArrayUsedForTheKeyboardRoutinebool active=TRUE; //WindowActiveFlagSetToTRUEByDefaultbool fullscreen=TRUE; //FullscreenFlagSetToFullscreenModeByDefaultGLfloat xrot; //XRotation(NEW)GLfloat yrot; //YRotation(NEW)GLfloat zrot; //ZRotation(NEW)GLuint texture[1]; //StorageForOneTexture(NEW)LRESULT CALLBACKWndProc(HWND,UINT,WPARAM,LPARAM); //DeclarationForWndProcAUX_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("banma.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 returnTRUE; //InitializationWentOK}intDrawGLScene(GLvoid) //Here'sWhereWeDoAllTheDrawing{ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //ClearTheScreenAndTheDepthBuffer glLoadIdentity(); //ResetTheView glTranslatef(0.0f,0.0f,-5.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); //FrontFace glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,-1.0f,1.0f); glTexCoord2f(1.0f,0.0f);glVertex3f(1.0f,-1.0f,1.0f); glTexCoord2f(1.0f,1.0f);glVertex3f(1.0f,1.0f,1.0f); glTexCoord2f(0.0f,1.0f);glVertex3f(-1.0f,1.0f,1.0f); //BackFace glTexCoord2f(1.0f,0.0f);glVertex3f(-1.0f,-1.0f,-1.0f); glTexCoord2f(1.0f,1.0f);glVertex3f(-1.0f,1.0f,-1.0f); glTexCoord2f(0.0f,1.0f);glVertex3f(1.0f,1.0f,-1.0f); glTexCoord2f(0.0f,0.0f);glVertex3f(1.0f,-1.0f,-1.0f); //TopFace glTexCoord2f(0.0f,1.0f);glVertex3f(-1.0f,1.0f,-1.0f); glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,1.0f,1.0f); glTexCoord2f(1.0f,0.0f);glVertex3f(1.0f,1.0f,1.0f); glTexCoord2f(1.0f,1.0f);glVertex3f(1.0f,1.0f,-1.0f); //BottomFace glTexCoord2f(1.0f,1.0f);glVertex3f(-1.0f,-1.0f,-1.0f); glTexCoord2f(0.0f,1.0f);glVertex3f(1.0f,-1.0f,-1.0f); glTexCoord2f(0.0f,0.0f);glVertex3f(1.0f,-1.0f,1.0f); glTexCoord2f(1.0f,0.0f);glVertex3f(-1.0f,-1.0f,1.0f); //Rightface glTexCoord2f(1.0f,0.0f);glVertex3f(1.0f,-1.0f,-1.0f); glTexCoord2f(1.0f,1.0f);glVertex3f(1.0f,1.0f,-1.0f); glTexCoord2f(0.0f,1.0f);glVertex3f(1.0f,1.0f,1.0f); glTexCoord2f(0.0f,0.0f);glVertex3f(1.0f,-1.0f,1.0f); //LeftFace glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,-1.0f,-1.0f); glTexCoord2f(1.0f,0.0f);glVertex3f(-1.0f,-1.0f,1.0f); glTexCoord2f(1.0f,1.0f);glVertex3f(-1.0f,1.0f,1.0f); glTexCoord2f(0.0f,1.0f);glVertex3f(-1.0f,1.0f,-1.0f); glEnd(); xrot+=0.1f; yrot+=0.1f; zrot+=0.05f; 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){ GLuint PixelFormat; //HoldsTheResultsAfterSearchingForAMatch WNDCLASS wc; //WindowsClassStructure DWORD dwExStyle; //WindowExtendedStyle DWORD dwStyle; //WindowStyle RECT WindowRect; //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; //DeviceMode memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); /
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年特許經(jīng)營許可協(xié)議3篇
- 二零二五年度跨境貿易貸款抵押合同規(guī)范3篇
- 二零二五年度貨物采購合同商品規(guī)格及交貨期限2篇
- 二零二五年食堂食材質量保證合同3篇
- 二零二五年度降水施工臨時設施建設合同3篇
- 學校管道清洗合同(2篇)
- 二零二五年度高校校園班車運營管理合同2篇
- 二零二五年度股權轉讓合同涉及的資產(chǎn)與權益3篇
- 二零二五年度道路施工工程承包合同2篇
- 二零二五年度綜合交通工程設計合同終止與交通組織協(xié)議3篇
- 直播電商年終總結
- PAS 2050:2011-商品和服務在生命周期內的溫室氣體排放評價規(guī)范(英文)
- 空調供貨方案
- 幕墻作業(yè)安全技術交底
- 《建筑工程設計文件編制深度規(guī)定》(2022年版)
- 線下結算傭金合同模板
- 疫情物品采購合同模板
- 老年病科專業(yè)知識考核試卷
- 《邊緣計算與人工智能應用開發(fā)技術》全套教學課件
- 病例報告表(CRF)模板
- 埃森哲流程制造-智能工廠規(guī)劃設計相關兩份資料
評論
0/150
提交評論