• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Shuffy

            不斷的學(xué)習(xí),不斷的思考,才能不斷的進(jìn)步.Let's do better together!
            posts - 102, comments - 43, trackbacks - 0, articles - 19
                  本文中將對(duì)5篇文章的太陽(yáng)系模型進(jìn)行修改,加入一些動(dòng)畫效果。此外還會(huì)加入顯示幀速率的代碼。

                  加入動(dòng)畫效果最容易的方法是響應(yīng)WM_TIMER消息,在其消息處理函數(shù)中改變一些參數(shù)值,比如每過(guò)多少毫秒就旋轉(zhuǎn)一定的角度,并且重繪場(chǎng)景。

            Frame Rate

            Frame 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ù),因此在stdafx.h中加入:

            #include <mmsystem.h>        // for MM timers (you'll need WINMM.LIB)

            并且Link—>Object/library modules中加入winmm.lib

            2,為了計(jì)算繪制用時(shí),在CCY457OpenGLView.h中加入如下變量:

                //For elapsed timing calculations
                DWORD m_StartTime, m_ElapsedTime, m_previousElapsedTime;    
                CString m_WindowTitle;    
            //Window Title
                int DayOfYear;
                
            int HourOfDay;

            并在構(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 glPopMatrixRenderScene函數(shù)包裹起來(lái),從而確保動(dòng)畫會(huì)正確運(yùn)行。在SwapBuffers調(diào)用后我們調(diào)用PostRenderScene來(lái)顯示幀速率信息到窗口標(biāo)題。

            void CCY457OpenGLView::OnDraw(CDC* pDC)
            {
                CCY457OpenGLDoc
            * pDoc = GetDocument();
                ASSERT_VALID(pDoc);
                
            // Get the system time, in milliseconds.
                m_ElapsedTime = ::timeGetTime(); // get current time
                if ( ElapsedTimeinMSSinceLastRender() < 30 )
                    
            return
                
            // Clear out the color & depth buffers
                ::glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
                glPushMatrix();
                    RenderScene();
                glPopMatrix();
                
            // Tell OpenGL to flush its pipeline
                ::glFinish();
                
            // Now Swap the buffers
                ::SwapBuffers( m_pDC->GetSafeHdc() );
                
            //Perform Post Display Processing
                
            // Only update the title every 15 redraws (this is about
                
            // every 1/2 second)
                PostRenderScene();
                
            // the very last thing we do is to save
                
            // the elapsed time, this is used with the
                
            // next elapsed time to calculate the
                
            // elapsed time since a render and the frame rate
                m_previousElapsedTime = m_ElapsedTime;
            }

            4,在CCY457OpenGLView類中加入下述成員函數(shù),用來(lái)顯示幀速率信息到窗口標(biāo)題

            //////////////////////////////////////////////////////////////////////////////
            // PostRenderScene
            // perform post display processing
            // The default PostRenderScene places the framerate in the
            // view's title. Replace this with your own title if you like.
            void CCY457OpenGLView::PostRenderScene( void )
            {
                
            // Only update the title every 15 redraws (this is about
                
            // every 1/2 second)
                static int updateFrame = 15;
                
            if (16 > ++updateFrame )
                    
            return;
                updateFrame 
            = 0;
                
            char string[256];
                _snprintf( 
            string200"%s ( %d Frames/sec )",
                    (
            const char*)m_WindowTitle, FramesPerSecond() );
                GetParentFrame()
            ->SetWindowText( string );
            }
            //////////////////////////////////////////////////////////////////////////////
            // FramesPerSecond
            // fetch frame rate calculations
            int CCY457OpenGLView::FramesPerSecond( void )
            {
                
            double eTime = ElapsedTimeinMSSinceLastRender();
                
            if ( 0 == (int)eTime )
                    
            return 0;
                
            return (int)(1000/(int)eTime);
            }
            DWORD ElapsedTimeinMSSinceLastStartup()
            {
                
            return(m_ElapsedTime - m_StartTime);
            }
            DWORD ElapsedTimeinMSSinceLastRender()
            {
                
            return(m_ElapsedTime - m_previousElapsedTime);
            }

            5,在OnTimer函數(shù)中,通過(guò)增加變量DayOfYear HourOfDay的值來(lái)控制地球和月球的位置,并且調(diào)用InvalidateRect來(lái)刷新界面。

            void CCY457OpenGLView::OnTimer(UINT nIDEvent) 
            {
                
            if(DayOfYear < 365)
                    DayOfYear
            ++;
                
            else
                    DayOfYear 
            = 1;
                
            if(HourOfDay < 365)
                    HourOfDay
            ++;
                
            else
                    HourOfDay 
            = 1;
                InvalidateRect(NULL, FALSE);    
                CView::OnTimer(nIDEvent);
            }

            6,在RenderScene中加入繪制代碼:

            void CCY457OpenGLView::RenderScene ()
            {
            //繪制函數(shù)
                glTranslatef(0.0f,0.0f,-5.0f);
                
            //Draw the Sun
                glutWireSphere(1.0f,20,20);
                
            //Rotate the Planet in its orbit
                glRotatef((GLfloat) (360.0*DayOfYear)/365.00.0f1.0f0.0f);
                glTranslatef(
            4.0f,0.0f,0.0f);
                glPushMatrix();
                    
            //Rotate the Planet in its orbit
                    glRotatef((GLfloat)(360*HourOfDay)/24.00.0f,1.0f,0.0f);
                    
            //Draw the Planet
                    glutWireSphere(0.2f,20,20);
                glPopMatrix();
                glRotatef((GLfloat) (
            360.0*12.5*DayOfYear)/365.00.0f1.0f0.0f);
                glTranslatef(
            0.5f,0.0f,0.0f);
                
            //Draw the Moon
                glutWireSphere(0.01f,20,20);
            }

             

            作者:洞庭散人

            出處:http://phinecos.cnblogs.com/    

            本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
            原文鏈接:http://www.cnblogs.com/phinecos/archive/2008/11/05/1327558.html
            久久精品中文字幕第23页| 亚洲欧美一级久久精品| 99国产欧美久久久精品蜜芽| 久久99精品国产麻豆| 久久精品国产91久久综合麻豆自制 | 久久se这里只有精品| 亚洲&#228;v永久无码精品天堂久久| 波多野结衣久久精品| 久久精品国产亚洲AV高清热 | 久久久久久曰本AV免费免费| 久久香蕉国产线看观看精品yw| 国产精品综合久久第一页| 亚洲一区精品伊人久久伊人| 99久久无色码中文字幕| 国色天香久久久久久久小说| 天天久久狠狠色综合| 日韩乱码人妻无码中文字幕久久| 久久久久99精品成人片三人毛片| 热re99久久精品国99热| 怡红院日本一道日本久久| 2021国内精品久久久久久影院| 欧美激情精品久久久久| 精品国产乱码久久久久久郑州公司 | 日批日出水久久亚洲精品tv| 99久久精品国产免看国产一区| 久久人人爽人人人人爽AV| 久久综合九色综合精品| 久久国产亚洲高清观看| 亚洲中文字幕无码久久精品1| 四虎久久影院| 久久青青草原精品国产软件| 久久久国产精品网站| 精品国产91久久久久久久| 97久久精品人妻人人搡人人玩| 无码专区久久综合久中文字幕 | 国产精品毛片久久久久久久 | 一本色道久久综合狠狠躁| 日韩AV毛片精品久久久| 一本大道久久香蕉成人网| 久久久久久综合网天天| 久久国产精品成人片免费|