《基于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;
}

时间: 2024-09-28 04:21:26

《基于MFC的OpenGL编程》Part 7 Animation的相关文章

《基于MFC的OpenGL编程》Part 19 Creating a Virtual Reality Walkthrough…

<基于MFC的OpenGL编程>Part 19 Creating a Virtual Reality Walkthrough Application 本文是整个系列文章的最后一篇,将创建一个完整的虚拟office应用程序(如图所示)来做为ending. 1,在CCY457OpenGLView类中加入下述变量,用来保存office内各个物体的显示列表 //Display List Names GLuint m_SceneList; GLuint m_ComputerList; GLuint m_

《基于MFC的OpenGL编程》Part 5 Transformations

<基于MFC的OpenGL编程>Part 5 Transformations - Rotations,Translations and Scaling Transformations - Translation, Rotation and Scaling Translation is nothing but moving along an arbitrary axis. Rotation is spinning about an arbitrary axis. Scaling is incre

《基于MFC的OpenGL编程》Part 2 Setting up OpenGL on Windows

WGL – Windows的 OpenGL扩展层 The WGL extension consists of a set of functions (wglCreateContext, wglDeleteContext etc.) and structures (such as PIXELFORMATDESCRIPTOR, GLYPHMETRICSFLOAT) etc. Thus every OpenGL implementation has a platform-specific portio

《基于MFC的OpenGL编程》Part 10 Texture Mapping

本文在第9篇文章的基础上,为立方体加入纹理映射的功能. Texture Mapping Texture Mapping in OpenGL is a fairly straightforward concept. Every texture is nothing but an image of some sort. Texture mapping is basically applying a texture to a surface. Textures can be 1D, 2D or eve

《基于MFC的OpenGL编程》Part 18 Reading objects from the OBJ File Format

本文将介绍如何从Obj文件格式中创建3D对象,我们使用的是Nate Miller的obj格式加载类. This would be very useful to create large Virtual Reality applications as we could make use of the readily available 3D model files or make use of modeling tools to create these models and load them

《基于MFC的OpenGL编程》Part 17 Shadows

Shadows Conceptually drawing a shadow is quite simple. A shadow is produced when an object keeps light from a source from striking some object or surface behind the object, casting the shadow. The area on the shadowed object's surface outlined by the

《基于MFC的OpenGL编程》Part 15 Selection

Selection Selection is a powerful feature of OpenGL that allows you click at some position of the OpenGL window using the mouse and determine which of your objects lie beneath it. The act of selecting a specific object is called Picking. With OpenGL'

《基于MFC的OpenGL编程》Part 14 Quadrics

本文在第11篇文章的基础上,为其加入显示各种二次曲面的代码: Quadrics Every quadric has a few settings associated with it. We have to create a quadric first and then customize its settings to render the shape we want. The gluNewQuadric function creates a state variable that descr

《基于MFC的OpenGL编程》Part 13 Creating 2D and 3D Text

wglUseFontBitmaps函数 The wglUseFontBitmaps() function creates a set of bitmap display lists based on the glyphs in the currently selected font in the current DC for use in the current OpenGL RC. It basically creates a series of sequential display list