您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > 基于MFC的Opengl编程
3D图形学基本概念PerspectivePerspectivereferstotheanglesbetweenthelinesthatlendtheillusionofthreedimensions.ColorsandShadingMovingbeyondlinedrawing,weneedtoaddcolortocreateasolidobject.Shadingreferstothewaythecolorisappliedtothepolygon.ShadingcanbeoftwotypesinOpenGL-FlatorSmooth.LightsandShadowsPlainsolidcolordoesn’tofferenoughrealism.ByapplyingLightingeffectswecanmakeobjectsappearastheywouldinrealitydependingontheirmaterialpropertiesandthelightingparameters.Addingashadowfurtherincreasesrealism.TextureMappingWithTextureMappingwecanhavewoodgrains,clothtextures,brickliketexturesetcinsteadofplainmaterials.ThistechniqueofapplyinganimagetothesurfaceofapolygoniscalledTextureMapping.TheimageweuseiscalledtheTextureandtheindividualelementsofthetexturearecalledTexels.FogFogisanatmosphericeffectthataddshazinesstoobjectsinascenedependingonhowfartheobjectsarefromtheviewer.BlendingandTransparencyBlendingisthecombinationofcolorsofobjectsonthescreen.Thiseffectcanbeusedforavarietyofpurposes.Byvaryingtheamounteachobjectisblendedwiththescenewecanmakeobjectslooktransparent.Anti-AliasingAliasingisaneffectthatisvisibleonscreenduetothefactthatanimageconsistsofdiscretepixels.Bycarefullyblendingthelineswiththebackgroundcolorwecaneliminatejaggededgesandgivethemasmoothappearance.Thisblendingtechniqueiscalledanti-aliasing.第一个OpenGL程序复制代码//Simple.cpp-FirstOpenGLProgram#includewindows.h//RequiredforeveryWindowsProgram#includegl\glut.h//RequiredforusingtheGLUTlibrary//PerformOpenGLInitializationherevoidSetupRC(){//SetthebackgroundclearingcolortoblueglClearColor(0.0f,0.0f,1.0f,1.0f);//设置背景色为蓝色}//ThedrawingcallbackfunctionvoidRenderScene(){//ClearthecolorbufferglClear(GL_COLOR_BUFFER_BIT);//FlushtherenderingpipelineglFlush();}voidmain(){//ChoosethedisplaymodesettingsglutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);//初始化显示模式(单缓冲,RGB)//CreatetheWindowglutCreateWindow(Simple);//创建窗口//SettheRenderScsnefunctionasthedisplaycallbackglutDisplayFunc(RenderScene);//绘制回调函数,当窗口需要绘制时,GLUT会调用此函数//InitializeOpenGLSetupRC();//初始化OpenGL//StarttheGLUTframeworkglutMainLoop();//开始消息循环}WGL–Windows的OpenGL扩展层TheWGLextensionconsistsofasetoffunctions(wglCreateContext,wglDeleteContextetc.)andstructures(suchasPIXELFORMATDESCRIPTOR,GLYPHMETRICSFLOAT)etc.ThuseveryOpenGLimplementationhasaplatform-specificportionwhichhastobesetupandusedaccordingtotheparticularplatform.设备上下文TheWindowsGraphicalDeviceInterface(GDI)iscapableofdrawingtoscreen,tomemory,toprintersortoanyotherdevicethatprovidesaGDIinterfacelayerandthatcanprocessGDIcalls.GDIaccomplishesthisbyarenderinghandletothecurrentlyselecteddevice,whichiscalledthedevicecontext,orDC.绘制上下文ArenderingcontextistheOpenGLequivalentoftheGDIDC.AllOpenGLcallsarerenderedtothedevicethroughaRC.TherenderingcontextmaintainsOpenGLstatevariablessuchascurrentbackgroundcolor,currentcoloretc.justastheDCmaintainsGDIstatevariablessuchascurrentpen,currentbrushetc.像素格式PixelformatsarethetranslationlayerbetweenOpenGLcallsandtherenderingoperationthatWindowsperforms.举个例子,若像素格式只支持很少一部分颜色值,则OpenGL在用RGB值(128,120,135)绘制一个像素时,就可能使用转换后的值(128,128,128)来绘制.Thepixelformatselectedessentiallydescribessuchthingsashowcolorsaredisplayed,depthoffieldresolutionandwhatadditionalcapabilitiesaresupportedbytherenderingcontextcreated.第一个基于MFC的OpenGL应用程开发环境:VC6.01,首先下载需要的GLUT头文件,DLL和Lib文件,下载链接:glutdlls37beta.zip(149kilobytes),解压缩后把gltu.h放到VC98/Include/GL下,把glut.lib和glut32.lib放到VC9/Lib下,glut32.dll和glut.dll放到你创建的应用程序的运行目录下2,创建一个MFCSDI应用程序,在项目属性中加入所需要链接的库文件1,在stdafx.h中加入下列语句://OpenGLHeaders#includegl/gl.h#includegl/glu.h#includegl/glut.h#includegl/glaux.h2,打开ClassWizard,选择CCY457OpenGLView类,为下述消息加入消息处理函数:WM_CREATE(forOnCreate),WM_DESTROY(forOnDestroy),WM_SIZE(forOnSize),WM_ERASEBACKGROUND(forOnEraseBkground).3,在窗口创建之前我们必须设置窗口风格包含WS_CLIPCHILDREN和WS_CLIPSIBLINGS,从而避免OpenGL绘制到其他窗口中去。这些应该放在PreCreateWindow()中。BOOLCCY457OpenGLView::PreCreateWindow(CREATESTRUCT&cs){//TODO:ModifytheWindowclassorstylesherebymodifying//theCREATESTRUCTcs//AnOpenGLWindowmustbecreatedwiththefollowingflagscs.style|=WS_CLIPSIBLINGS|WS_CLIPCHILDREN;returnCView::PreCreateWindow(cs);}4,在CCY457OpenGLView.h中加入如下语句:HGLRCm_hRC;//RenderingContextCDC*m_pDC;//DeviceContextBOOLInitializeOpenGL();//InitializeOpenGLBOOLSetupPixelFormat();//SetupthePixelFormatvoidRenderScene();//RendertheScene5,在OnCreate中我们将通过建立像素格式和绘制上下文来初始化OpenGL.在InitializeOpenGL()中会创建一个设备上下文(DC),为这个DC选择一个像素格式,创建和这个DC相关的绘制上下文(RC),然后选择这个RC.这个函数会调用SetupPixelFormat()来建立像素格式。intCCY457OpenGLView::OnCreate(LPCREATESTRUCTlpCreateStruct){if(CView::OnCreate(lpCreateStruct)==-1)return-1;//InitializeOpenGLHereInitializeOpenGL();return0;}BOOLCCY457OpenGLView::InitializeOpenGL(){//GetaDCfortheClientAream_pDC=newCClientDC(this);//FailuretoGetDCif(m_pDC==NULL){MessageBox(ErrorObtainingDC);returnFALSE;}//Failuretosetthepixelformatif(!SetupPixelFormat()){returnFALSE;}//CreateRenderingContextm_hRC=::wglCreateContext(m_pDC-GetSafeHdc());//FailuretoCreateRenderingContextif(m_hRC==0){MessageBox(ErrorCreatingRC);returnFALSE;}//MaketheRCCurrentif(::wglMakeCurrent(m_pDC-GetSafe
本文标题:基于MFC的Opengl编程
链接地址:https://www.777doc.com/doc-5857383 .html