• <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

            不斷的學習,不斷的思考,才能不斷的進步.Let's do better together!
            posts - 102, comments - 43, trackbacks - 0, articles - 19

            《基于MFC的OpenGL編程》Part 7 Animation

            Posted on 2010-03-31 20:34 Shuffy 閱讀(326) 評論(0)  編輯 收藏 引用 所屬分類: MFC,OpenGL
                  本文中將對5篇文章的太陽系模型進行修改,加入一些動畫效果。此外還會加入顯示幀速率的代碼。

                  加入動畫效果最容易的方法是響應WM_TIMER消息,在其消息處理函數中改變一些參數值,比如每過多少毫秒就旋轉一定的角度,并且重繪場景。

            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,我們需要調用timeGetTime()函數,因此在stdafx.h中加入:

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

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

            2,為了計算繪制用時,在CCY457OpenGLView.h中加入如下變量:

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

            并在構造函數中進行初始化:

            CCY457OpenGLView::CCY457OpenGLView()
            {
                DayOfYear 
            = 1;
                HourOfDay 
            = 1;
            }

            3,為了計算幀速率,修改OnCreate函數,在其中獲取窗口標題,從標題中去掉”Untitled”字樣,并啟動定時器;

            4,同樣為了計算幀速率,修改OnDraw函數如下,在其中用glPushMatrix glPopMatrixRenderScene函數包裹起來,從而確保動畫會正確運行。在SwapBuffers調用后我們調用PostRenderScene來顯示幀速率信息到窗口標題。

            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類中加入下述成員函數,用來顯示幀速率信息到窗口標題

            //////////////////////////////////////////////////////////////////////////////
            // 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函數中,通過增加變量DayOfYear HourOfDay的值來控制地球和月球的位置,并且調用InvalidateRect來刷新界面。

            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 ()
            {
            //繪制函數
                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/    

            本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
            原文鏈接:http://www.cnblogs.com/phinecos/archive/2008/11/05/1327558.html
            久久永久免费人妻精品下载| 国产综合免费精品久久久| 中文成人久久久久影院免费观看| 亚洲午夜精品久久久久久浪潮| 欧美久久久久久午夜精品| 久久久久亚洲精品日久生情| 天堂久久天堂AV色综合| 97精品伊人久久大香线蕉app| 亚洲国产精品久久久久网站 | 久久久国产视频| 国产V亚洲V天堂无码久久久| 国产精品99久久久久久董美香| 亚洲欧美一区二区三区久久| 国产产无码乱码精品久久鸭| 四虎久久影院| 99久久精品无码一区二区毛片| 伊人久久成人成综合网222| 潮喷大喷水系列无码久久精品| 性做久久久久久久久| 久久精品国产精品青草app| 国产精品久久久久a影院| 色综合久久综合网观看| 97久久婷婷五月综合色d啪蜜芽 | 99久久精品午夜一区二区| 国产AⅤ精品一区二区三区久久| 亚洲AV无码1区2区久久| 久久综合一区二区无码| 久久国产精品免费一区二区三区| 久久精品午夜一区二区福利| 亚洲欧美日韩久久精品第一区| 久久精品国产亚洲精品| 久久高清一级毛片| 国产精品欧美久久久久无广告| 国产精品视频久久| 精品久久久噜噜噜久久久| 日本欧美久久久久免费播放网| 久久综合亚洲色一区二区三区| 久久久久亚洲AV无码专区网站 | 久久综合九色综合欧美狠狠| av无码久久久久不卡免费网站| 久久无码人妻一区二区三区午夜 |