OpenGL programming based on MFC Part 7 Animation

Source: Internet
Author: User
Tags current time

In this paper, the solar system model of the 5th article will be modified to add some animation effects. You will also add code that displays the frame rate.

The easiest way to add an animation effect is to respond to a WM_TIMER message and change some parameter values in its message handler function, such as rotating a certain angle every few milliseconds, and redrawing the scene.

Frame Rate

Frame rate is no 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 are the elapsed time to render one frame. Thus we can calculate the frame rate for a given application.

1, we need to invoke the timeGetTime () function, so add in stdafx.h:

#include <mmsystem.h>    // for MM timers (you'll need WINMM.LIB)
并且Link—>Object/library modules中加入winmm.lib

2, in order to calculate the drawing, add the following variable in the CCY457OpenGLView.h:

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

and initialize in the constructor:

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

3, in order to calculate the frame rate, modify the OnCreate function, get the window title, remove the word "Untitled" from the caption, and start the timer;

4, also in order to calculate the frame rate, modify the OnDraw function as follows, in which the Renderscene function is wrapped with glpushmatrix and glpopmatrix to ensure that the animation will run correctly. After the swapbuffers call, we call Postrenderscene to display the frame rate information to the window caption.

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-&GT;GETSAFEHDC ());
Perform Post Display processing
Only update the title every redraws (this is about
Every 1/2 second)
Postrenderscene ();
The very last thing we are to save
The elapsed time, which is used with the
Next elapsed time to calculate the
Elapsed time since a render and the frame rate
M_previouselapsedtime = M_elapsedtime;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.