【正文】
break。}InvalidateRect(NULL,FALSE)。CView::OnKeyDown(nChar,nRepCnt,nFlags)。}voidCOpenGLView::OnLButtonDown(UINTnFlags,CPointpoint){//TODO:Addyourmessagehandlercodehereand/orcalldefaultm_MouseDownPoint=point。SetCapture()。CView::OnLButtonDown(nFlags,point)。}voidCOpenGLView::OnLButtonUp(UINTnFlags,CPointpoint){//TODO:Addyourmessagehandlercodehereand/orcalldefaultm_MouseDownPoint=CPoint(0,0)。ReleaseCapture()。CView::OnLButtonUp(nFlags,point)。}voidCOpenGLView::OnMouseMove(UINTnFlags,CPointpoint){//TODO:Addyourmessagehandlercodehereand/orcalldefault//Checkifwehavecapturedthemouseif(GetCapture()==this){//Incrementtheobjectrotationanglesm_xAngle+=()/。m_yAngle+=()/。//RedrawtheviewInvalidateRect(NULL,FALSE)。//Setthemousepointm_MouseDownPoint=point。}。CView::OnMouseMove(nFlags,point)。}0 0 0 (請(qǐng)您對(duì)文章做出評(píng)價(jià))光榮在于平淡,艱巨因?yàn)槁L(zhǎng)《基于MFC的OpenGL編程》Part 7 Animation 本文中將對(duì)第5篇文章的太陽(yáng)系模型進(jìn)行修改,加入一些動(dòng)畫效果。此外還會(huì)加入顯示幀速率的代碼。 加入動(dòng)畫效果最容易的方法是響應(yīng)WM_TIMER消息,在其消息處理函數(shù)中改變一些參數(shù)值,比如每過(guò)多少毫秒就旋轉(zhuǎn)一定的角度,并且重繪場(chǎng)景。Frame RateFrame rate is nothing but the number of frames that can be rendered per second. The higher this rate, the smoother the animation. In order to calculate the frame rate we retrieve the system time (using the Windows multimedia API function timeGetTime()) before the rendering is performed and after the buffer is swapped. The difference between the two values is the elapsed time to render one frame. Thus we can calculate the frame rate for a given application.1,我們需要調(diào)用timeGetTime()函數(shù),:include//forMMtimers(you39。llneed)并且Link—Object/library 2,為了計(jì)算繪制用時(shí),://ForelapsedtimingcalculationsDWORDm_StartTime,m_ElapsedTime,m_previousElapsedTime。CStringm_WindowTitle。//WindowTitleintDayOfYear。intHourOfDay。并在構(gòu)造函數(shù)中進(jìn)行初始化:CCY457OpenGLView::CCY457OpenGLView(){DayOfYear=1。HourOfDay=1。}3,為了計(jì)算幀速率,修改OnCreate函數(shù),在其中獲取窗口標(biāo)題,從標(biāo)題中去掉”Untitled”字樣,并啟動(dòng)定時(shí)器;4,同樣為了計(jì)算幀速率,修改OnDraw函數(shù)如下,在其中用glPushMatrix 和 glPopMatrix將RenderScene函數(shù)包裹起來(lái),從而確保動(dòng)畫會(huì)正確運(yùn)行。在SwapBuffers調(diào)用后我們調(diào)用PostRenderScene來(lái)顯示幀速率信息到窗口標(biāo)題。voidCCY457OpenGLView::OnDraw(CDC*pDC){CCY457OpenGLDoc*pDoc=GetDocument()。ASSERT_VALID(pDoc)。//Getthesystemtime,inmilliseconds.m_ElapsedTime=::timeGetTime()。//getcurrenttimeif(ElapsedTimeinMSSinceLastRender()30)return//Clearoutthecoloramp。depthbuffers::glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)。glPushMatrix()。RenderScene()。glPopMatrix()。//TellOpenGLtoflushitspipeline::glFinish()。//NowSwapthebuffers::SwapBuffers(m_pDCGetSafeHdc())。//PerformPostDisplayProcessing//Onlyupdatethetitleevery15redraws(thisisabout//every1/2second)PostRenderScene()。//theverylastthingwedoistosave//theelapsedtime,thisisusedwiththe//nextelapsedtimetocalculatethe//elapsedtimesincearenderandtheframeratem_previousElapsedTime=m_ElapsedTime。}4,在CCY457OpenGLView類中加入下述成員函數(shù),用來(lái)顯示幀速率信息到窗口標(biāo)題////////////////////////////////////////////////////////////////////////////////PostRenderScene//performpostdisplayprocessing//ThedefaultPostRenderSceneplacestheframerateinthe//view39。stitle.Replacethiswithyourowntitleifyoulike.voidCCY457OpenGLView::PostRenderScene(void){//Onlyupdatethetitleevery15redraws(thisisabout//every1/2second)staticintupdateFrame=15。if(16++updateFrame)return。updateFrame=0。charstring[256]。_snprintf(string,200,%s(%dFrames/sec),(constchar*)m_WindowTitle,FramesPerSecond())。GetParentFrame()SetWindowText(string)。}////////////////////////////////////////////////////////////////////////////////FramesPerSecond//fetchframeratecalculationsintCCY457OpenGLView::FramesPerSecond(void){doubleeTime=ElapsedTimeinMSSinceLastRender()。if(0==(int)eTime)return0。return(int)(1000/(int)eTime)。}DWORDElapsedTimeinMSSinceLastStartup(){return(m_ElapsedTimem_StartTime)。}DWORDElapsedTimeinMSSinceLastRender(){return(m_ElapsedTimem_previousElapsedTime)。}5,在OnTimer函數(shù)中,通過(guò)增加變量DayOfYear 和 HourOfDay的值來(lái)控制地球和月球的位置,并且調(diào)用InvalidateRect來(lái)刷新界面。voidCCY457OpenGLView::OnTimer(UINTnIDEvent){if(DayOfYear365)DayOfYear++。elseDayOfYear=1。if(HourOfDay365)HourOfDay++。elseHourOfDay=1。InvalidateRect(NULL,FALSE)。CView::OnTimer(nIDEvent)。}6,在RenderScene中加入繪制代碼:voidCCY457OpenGLView::RenderScene(){//繪制函數(shù)glTranslatef(,)。//DrawtheSunglutWireSphere(,20,20)。//RotatethePlanetinitsorbitglRotatef((GLfloat)(*DayOfYear)/,,,)。glTranslatef(,)。glPushMatrix()。//RotatethePlanetinitsorbitglRotatef((GLfloat)(360*HourOfDay)/,,)。//DrawthePlanetglutWireSphere(,20,20)。glPopMatrix()。glRotatef((GLfloat)(**DayOfYear)/,,,)。glTranslatef(,)。//DrawtheMoonglutWireSphere(,20,20)。}光榮在于平淡,艱巨因?yàn)槁L(zhǎng)《基于MFC的OpenGL編程》Part 8 Colors OpenGL支持兩種顏色模式:RGBA和顏色索引模式,本文關(guān)注于前者。 Smooth Shading and Flat ShadingWhen Smooth Shading is specified, the color values are interpolated between vertices. If Flat Shading is specified, one vertex is selected as being representative of all the vertices, thus the entire primitive is displayed using one single color.一個(gè)簡(jiǎn)單Demo1,:GLfloatm_xRot,m_yRot。//繞x,y軸旋轉(zhuǎn)的角度,隨時(shí)間不斷變化并在構(gòu)造函數(shù)中初始化:CCY457OpenGLView::CCY457OpenGLView(){m_xRot=m_yRot=}2,在OnTimer函數(shù)中,修改繞x,y軸旋轉(zhuǎn)的角度值voidCCY457OpenGLView::OnTimer(UINTnIDEvent){m_xRot=m_xRot+m_yRot=m_yRot+InvalidateRect(NULL,FALSE)。CView::OnTimer(nIDEvent)。}3,加入兩個(gè)菜單項(xiàng),控制OpenGL的渲染模式voidCCY457OpenGLView::OnShadingmodelSmooth(){glShadeModel(GL_