




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
计算机图形学实验日志指导教师杜欢实验时间:2010年10月28日学院计算机科学与技术专业计算机科学与技术班级0410805学号08100523姓名孙黎黎实验室s401实验题目:新手上路实验目的:1,熟悉编程环境;2,了解计算机绘图的特点;3,利用VC+OpenGL作为开发平台设计程序。4,掌握如何构造几何图元。实验要求:1,了解和使用VC的开发环境,理解简单的OpenGL程序结构。对VC6中的OPENGL开发环境进行简单配置。2,以交互的方式完成几何图形正方形的生成3,实验的核心算法(C参考程序)及实验结果4,VC6.0中OPENGL开发环境配置Windows环境下安装GLUT的步骤实验主要步骤:开发环境OpenGL的配置。<1>在windows环境下安装GLUT;<2>将glut.h,glut.lib,glut.dll分别放入到指定的文件夹内设置动态链接库;<3>在VC6.0中创建工程,并链接OpenGLlibraries(方法是:在VisualC++中先单击Project,再单击Settings,再找到Link单击,最后在Object/librarymodules的最前面加上opengl32.libGlut32.libGlaux.libglu32.lib。)<4>单击ProjectSettings中的C/C++标签,将Preprocessordefinitions中的_CONSOLE改为__WINDOWS。最后单击OK。算法为:#include<windows.h>#include<gl/gl.h>#include<gl/glaux.h>voidmain(charargv[]){auxInitDisplayMode(AUX_SINGLE|AUX_RGBA);//窗口的初始化auxInitPosition(0,0,500,500);auxInitWindow(argv[0]);glClearColor(0.0,0.0,0.0,0.0);//设置背景色为黑色glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0,1.0,1.0);//设置前景色为白色glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);glBegin(GL_POLYGON);//构造几何元,开始。glVertex2f(-0.5,-0.5);glVertex2f(-0.5,0.5);glVertex2f(0.5,0.5);glVertex2f(0.5,-0.5);glEnd();//构造几何元,结束glFlush();Sleep(3000);}实验结果:心得体会:第一次做图形学实验,对图形学有很大兴趣,开始按照老师所说把代码拷进VC里面,由于开始没有调节OpenGL的配置,所以编译没通过。按照实验指导书上的步骤改了设置,调了几次,最终通过了。当看到运行出来的图形时,觉得很神奇,也挺有成就感的。于是觉得好好去探索图形学实验。接下来好好努力。计算机图形学实验日志指导教师邹洋实验时间:2010年11月11日学院计算机科学与技术专业计算机科学与技术班级0410805学号08100523姓名孙黎黎实验室s401实验题目:给图形着色实验目的:掌握OpenGL中一些常用函数;掌握两种不同的着色方法:Flatcoloring(单调着色),Smoothcoloring(平滑着色);实验要求:完成一个三角形的平滑着色和一个正方形的单调着色的显示算法;体会二者的区别,给出显示结果;实验主要步骤:同实验一一样,先进行开发环境的配置及创建工程,方法同实验一;实验核心代码如下:#include"StdAfx.h"//HeaderFileForWindows #include<stdio.h> //HeaderFileForStandardInput/Output#include<math.h> //HeaderFileForTheMathLibrary#include<gl\gl.h> //HeaderFileForTheOpenGL32Library#include<gl\glu.h> //HeaderFileForTheGLu32Library#include<gl\glaux.h> //HeaderFileForTheGlauxLibrary#pragmacomment(lib,"GLAUX.LIB")#pragmacomment(lib,"GLU32.LIB")#pragmacomment(lib,"OPENGL32.LIB")HDC hDC=NULL; //PrivateGDIDeviceContextHGLRC hRC=NULL; //PermanentRenderingContextHWND hWnd=NULL; //HoldsOurWindowHandleHINSTANCE hInstance; //HoldsTheInstanceOfTheApplicationbool keys[256]; //ArrayUsedForTheKeyboardRoutinebool active=TRUE; //WindowActiveFlagSetToTRUEByDefaultbool fullscreen=TRUE; //FullscreenFlagSetToFullscreenModeByDefaultfloatpoints[45][45][3];//TheArrayForThePointsOnTheGridOfOur"Wave"intwiggle_count=0; //CounterUsedToControlHowFastFlagWavesGLfloat xrot; //XRotation(NEW)GLfloat yrot; //YRotation(NEW)GLfloat zrot; //ZRotation(NEW)GLfloathold; //TemporarilyHoldsAFloatingPointValueGLuint texture[1]; //StorageForOneTexture(NEW)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}intInitGL(GLvoid) //AllSetupForOpenGLGoesHere{glShadeModel(GL_SMOOTH);//启用阴影平滑glClearDepth(1.0f);//设置深度缓存glEnable(GL_DEPTH_TEST);//启用深度测试glDepthFunc(GL_LEQUAL);//所作深度测试的类型glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//真正精细的透视修正returnTRUE; //InitializationWentOK}intDrawGLScene(GLvoid)//此过程中包括所有的绘制代码{glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//清除屏幕及深度缓存glLoadIdentity();//重置模型观察矩阵glTranslatef(-1.5f,0.0f,-6.0f);//左移1.5单位,并移入屏幕6.0glBegin(GL_TRIANGLES);//绘制三角形glColor3f(1.0f,0.0f,0.0f);//设置当前色为红色glVertex3f(0.0f,1.0f,0.0f);//上顶点glColor3f(0.0f,1.0f,0.0f);//设置当前色为绿色glVertex3f(-1.0f,-1.0f,0.0f);//左下glColor3f(0.0f,0.0f,1.0f);//设置当前色为蓝色glVertex3f(1.0f,-1.0f,0.0f);//右下glEnd();//三角形绘制结束glTranslatef(3.0f,0.0f,0.0f);//右移3单位glColor3f(0.5f,0.5f,1.0f);//一次性将当前色设置为蓝色glBegin(GL_QUADS);//绘制正方形glVertex3f(-1.0f,1.0f,0.0f);//左上glVertex3f(1.0f,1.0f,0.0f);//右上glVertex3f(1.0f,-1.0f,0.0f);//左下glVertex3f(-1.0f,-1.0f,0.0f);//右下glEnd();//正方形绘制结束returnTRUE;//继续运行}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 }}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 } if(!CreateGLWindow("bosco&NeHe'sWavingTextureTutorial",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&&!DrawGLScene())||keys[VK_ESCAPE]) //Active?WasThereAQuitReceived? { done=TRUE; //ESCorDrawGLSceneSignalledAQuit } else //NotTimeToQuit,UpdateScreen { SwapBuffers(hDC); //SwapBuffers(DoubleBuffering) } if(keys[VK_F1]) //IsF1BeingPressed? { keys[VK_F1]=FALSE; //IfSoMakeKeyFALSE KillGLWindow(); //KillOurCurrentWindow fullscreen=!fullscreen; //ToggleFullscreen/WindowedMode //RecreateOurOpenGLWindow if(!CreateGLWindow("bosco&NeHe'sWavingTextureTutorial",640,480,16,fullscreen)) { return0; //QuitIfWindowWasNotCreated } } } }实验结果:心得体会:本次实验,要更改同文件名,将#include"windows.h"改为#include"StdAfx.h"就对了,图形学实验虽然结果很令人兴奋,利用前面的知识和本次实验新学习的方法实现了这个功能,取得非常大的作用。取得了非常好的效果。计算机图形学实验日志指导教师邹洋实验时间:2010年11月25日学院计算机科学与技术专业计算机科学与技术班级0410805学号08100523姓名孙黎黎实验室s401实验题目:图形的转动实验目的:掌握图形旋转几何变换的算法,并利用这些算法对实验二给出的彩色图形进行几何换。实验要求:1,对实验二中的三角形和正方形的程序代码进行调整,使三角形绕Y轴左右旋转,正方形绕X轴上下旋转。2,思考若让对象绕着Z轴、X+Y轴或者所有三个轴来转动该怎么做。3,动态显示给定三角形与正方形的变化过程。实验主要步骤:同实验一一样,先进行开发环境的配置及创建工程,方法同实验一;实验核心代码如下:,#include"StdAfx.h"//HeaderFileForWindows#include<stdio.h> //HeaderFileForStandardInput/Output#include<math.h> //HeaderFileForTheMathLibrary#include<gl\gl.h> //HeaderFileForTheOpenGL32Library#include<gl\glu.h> //HeaderFileForTheGLu32Library#include<gl\glaux.h> //HeaderFileForTheGlauxLibrary#pragmacomment(lib,"GLAUX.LIB")#pragmacomment(lib,"GLU32.LIB")#pragmacomment(lib,"OPENGL32.LIB")HDC hDC=NULL; //PrivateGDIDeviceContextHGLRC hRC=NULL; //PermanentRenderingContextHWND hWnd=NULL; //HoldsOurWindowHandleHINSTANCE hInstance; //HoldsTheInstanceOfTheApplicationbool keys[256]; //ArrayUsedForTheKeyboardRoutinebool active=TRUE; //WindowActiveFlagSetToTRUEByDefaultbool fullscreen=TRUE; //FullscreenFlagSetToFullscreenModeByDefaultGLfloatrtri;//用于三角形的角度(新增)GLfloatrquad;//用于四边形的角度(新增)floatpoints[45][45][3];//TheArrayForThePointsOnTheGridOfOur"Wave"intwiggle_count=0; //CounterUsedToControlHowFastFlagWavesGLfloat xrot; //XRotation(NEW)GLfloat yrot; //YRotation(NEW)GLfloat zrot; //ZRotation(NEW)GLfloathold; //TemporarilyHoldsAFloatingPointValueGLuint texture[1]; //StorageForOneTexture(NEW)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}intInitGL(GLvoid) //AllSetupForOpenGLGoesHere{glShadeModel(GL_SMOOTH);//启用阴影平滑glClearDepth(1.0f);//设置深度缓存glEnable(GL_DEPTH_TEST);//启用深度测试glDepthFunc(GL_LEQUAL);//所作深度测试的类型glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//真正精细的透视修正returnTRUE; //InitializationWentOK}intDrawGLScene(GLvoid)//此过程中包括所有的绘制代码{glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//清除屏幕及深度缓存glLoadIdentity();//重置模型观察矩阵glTranslatef(-1.5f,0.0f,-6.0f);//左移1.5单位,并移入屏幕6.0glRotatef(rtri,0.0f,1.0f,0.0f);//绕Y轴旋转三角形(新增)glBegin(GL_TRIANGLES);//绘制三角形glColor3f(1.0f,0.0f,0.0f);//设置当前色为红色glVertex3f(0.0f,1.0f,0.0f);//上顶点glColor3f(0.0f,1.0f,0.0f);//设置当前色为绿色glVertex3f(-1.0f,-1.0f,0.0f);//左下glColor3f(0.0f,0.0f,1.0f);//设置当前色为蓝色glVertex3f(1.0f,-1.0f,0.0f);//右下glEnd();//三角形绘制结束glLoadIdentity();//重置模型观察矩阵glTranslatef(1.5f,0.0f,-6.0f);//右移1.5单位,并移入屏幕6.0glRotatef(rquad,1.0f,0.0f,0.0f);//绕X轴旋转四边形(新增)glColor3f(0.5f,0.5f,1.0f);//一次性将当前色设置为蓝色glBegin(GL_QUADS);//绘制正方形glVertex3f(-1.0f,1.0f,0.0f);//左上glVertex3f(1.0f,1.0f,0.0f);//右上glVertex3f(1.0f,-1.0f,0.0f);//左下glVertex3f(-1.0f,-1.0f,0.0f);//右下glEnd();//正方形绘制结束rtri+=0.2f;//增加三角形的旋转变量(新增)rquad-=0.15f;//减少四边形的旋转变量(新增)returnTRUE;//继续运行}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();
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论