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->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;
}