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

简介:
  本文中将对第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;
}
复制代码
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( string, 200, "%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.0, 0.0f, 1.0f, 0.0f);
    glTranslatef(4.0f,0.0f,0.0f);
    glPushMatrix();
        //Rotate the Planet in its orbit
        glRotatef((GLfloat)(360*HourOfDay)/24.0, 0.0f,1.0f,0.0f);
        //Draw the Planet
        glutWireSphere(0.2f,20,20);
    glPopMatrix();
    glRotatef((GLfloat) (360.0*12.5*DayOfYear)/365.0, 0.0f, 1.0f, 0.0f);
    glTranslatef(0.5f,0.0f,0.0f);
    //Draw the Moon
    glutWireSphere(0.01f,20,20);
}
复制代码


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/11/05/1327558.html,如需转载请自行联系原作者
目录
相关文章
|
Android开发 异构计算
Android OpenGL ES(八)----纹理编程框架(二)
Android OpenGL ES(八)----纹理编程框架(二)
161 0
Android OpenGL ES(八)----纹理编程框架(二)
|
存储 Java API
Android OpenGL ES(八)----纹理编程框架(一)
Android OpenGL ES(八)----纹理编程框架(一)
276 0
Android OpenGL ES(八)----纹理编程框架(一)
|
Android开发
Android OpenGL ES(三)----编程框架(二)
Android OpenGL ES(三)----编程框架(二)
101 0
Android OpenGL ES(三)----编程框架(二)
|
Java API Android开发
Android OpenGL ES(三)----编程框架(一)
Android OpenGL ES(三)----编程框架(一)
99 0
|
iOS开发 异构计算
了解 OpenGL ES实现自定义编程粒子效果 思路
本案例旨在于了解OpenGL ES中自定义编程粒子效果的整体实现思路。
165 0
了解 OpenGL ES实现自定义编程粒子效果 思路