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

來源:互聯網
上載者:User

本文中將對第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 和 glPopMatrix將RenderScene函數包裹起來,從而確保動畫會正確運行。在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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.