# OpenGL Animation in a MFC application and OPENGL (again, sorry)

This topic is 4128 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
Quote:
 Original post by riruiloOverwrite Idle function: There is an article about this in gamedev but I think (it is my opinion, nothing else) this approach is not elegant and intrusive.http://www.gamedev.net/reference/articles/article2204.asp

Yeah, it is intrusive, and a bit of a hack in truth [grin]

Thing is though, it's a quick and reliable way of continuously updating an MFC application. As you already mention, using a timer is a very poor solution, and just using the OnIdle and OnDraw functions on their own won't give you what you want.

Quote:
 Original post by riruiloAfter read some information I will suggest (me) this: (and maybe I´m wrong)Add to my engine a funtion like this:Render(fElapsedTime);So everything will be rendered depending of the value of fElapsedTime.If an animation has a 3 seconds animation, fElapsedTime will be from 0.0 to 3.0 in a variable frame rate (or fixed), but I prefer to draw all frames that my computer can able to do it.So when I will press PLAY, will occur this:starttime=QueryPerformanceCounter;and after this a thread will be launched with this code:while (true) { fElapsedTime=QueryPerformanceCounter-starttime; Render(fElapsedTime);}(Actually, until you press STOP)Is my approach elegant or very difficult? Will I have problems with MFC model? Will I block message waiting line?How can I add this on OnDraw function? or MFC view/doc paradigm.I think with my approach can give me the maximum amount of FPS of my system, not like SetTimer, which give me a constant FPS.

The other solution, as you suggest, is to use another thread to control rendering. This is a better (and arguably more elegant) way of doing your rendering, but you need to be aware of all the problems which can occur with a multi-threaded solution, such as the potential for deadlock and mutually-exclusive access to data which is shared among threads.

If you're using a different thread, you won't be blocking the message queue; the parent (main) thread will continue to run after launching the child (rendering) thread.

To prevent all of your views being updated every cycle by the child thread (which might really slow your app down), you can flag that a view is "dirty" (in need of redrawing) in the OnDraw function, and only redraw views in your Render function which have that dirty flag set. Alternatively, you could give the user the option to specify the update (5fps by default, say). 3ds max allows you to do this; basically, you just lock the frame-rate to the user-specified value (which would be a trivial change to your child thread).

Quote:
 Original post by riruiloWell, I will apreciate all opinions, I would just like to know WHICH is the best and easy way to do it.

The method outlined in my article of overriding the Run and OnIdle functions is a quick and relatively hacky way of getting an MFC application to update continuously. The better solution is to create a child thread which runs continuously, and frees your main thread to run the MFC app.

Quote:
 Original post by riruiloThank you very much for your time.

You're welcome [smile]

##### Share on other sites
Thanks a lot iNsAn1tY.

Right now I´m going to try to implement it using AfxBeginThread. I´m doing a COLLADA loader and I need that for animations.

By the way, is this a proper way to control animations? (time, I mean)

starttime=QueryPerformanceCounter;
and after this a thread will be launched with this code:

while (true) {
fElapsedTime=QueryPerformanceCounter-starttime;
Render(fElapsedTime);
}

Thanks!

##### Share on other sites
iNsAn1tY posts a lot of good information. I can't add much but I can tell you the experience I had trying to write an animation viewer with MFC & a render viewport. I've tried all kinds of techniques and the best solution I came up with was using the OnIdle() method. It simplifies the process greatly and isn't really a hack if you stop and think about how MFC applications work. Multi threading was really a hassle and made the application much more complicated than it needed to be.

Most applications are event driven - they wait until they receive a message to perform some task. In an animation viewer it is the same - play this animation, load this animation, start, stop. The only difference is that you want to continue to do something while waiting for messages. MFC provides that mechanism in the idle call. You can make it hacky if you don't code it up to make sense ie OnIdle() { DrawSomething(); }.

In our MFC framework there can be multiple viewports. Some of these are updated always and some of them are not.

Every view is derived from CRenderView. This class, on creation, registers itself with the RenderViewManager. It's automatic in the constructor so client code does not even know it's happenening.

Every application derives not from CWinApp but CRenderApp. This application base class in turn derives from CWinApp. What does it do? It contains the RenderViewManager and overloads OnIdle. Every time OnIdle is called, the time delta is determined and the list of CRenderView classes is iterated over, and the Render function is called with the time delta. We also have a virtual function in CRenderApp called OnFrameUpdate() which also gets the delta. This is called first so that objects are updated before they are rendered. The nice thing about this framework is that people can create MFC render applications without having to worry about the guts of the framework. Additionally, we can switch views between being live updated or not - our CRenderView() overrides the paint message and paints the screen if it is 'dirty update' instead of 'live update' mode. In a quad-view with three wireframe views and one live update view this works really well.

If you are trying to use MFC I would avoid multi-threading it; MFC provides the hooks you need to do the work. You'll be thankful you are working on the application's good points instead of working on critical sections and mutexes.

##### Share on other sites
Quote:
 Original post by riruiloBy the way, is this a proper way to control animations? (time, I mean)starttime=QueryPerformanceCounter;and after this a thread will be launched with this code:while (true) {fElapsedTime=QueryPerformanceCounter-starttime;Render(fElapsedTime);}

Yeah, that's how performance counters work in outline. You need to divide by the timer frequency, though:

// During initalization...QueryPerformanceFrequency( reinterpret_cast<LARGE_INTEGER *>( &miTimerFreq ) );QueryPerformanceCounter( reinterpret_cast<LARGE_INTEGER *>( &miStartTime ) );// When updating...__int64 liEndTime;float lfTime;QueryPerformanceCounter( reinterpret_cast<LARGE_INTEGER *>( &liEndTime ) );lfTime = static_cast<float>( liEndTime - miStartTime ) / static_cast<float>( miTimerFreq );miStartTime = liEndTime;return lfTime;

Also, every time your while loop executes, you need a non-blocking rendezvous with the parent thread, so that the parent can stop the child thread when it needs to.

Quote:
 Original post by SphetMost applications are event driven - they wait until they receive a message to perform some task. In an animation viewer it is the same - play this animation, load this animation, start, stop. The only difference is that you want to continue to do something while waiting for messages. MFC provides that mechanism in the idle call. You can make it hacky if you don't code it up to make sense ie OnIdle() { DrawSomething(); }.

True. This is the rationale the article works to; it's the simple modification of an application from one which is event-driven to on which is both event-driven and updates continuously. Did your animation viewer only use the OnIdle function? I found that I had to override Run as well to get the kind of reliable updating I expected.

##### Share on other sites
Hi!

Sorry for this post, but actually I dont know so much threads in MFC.
My first experiment was a failure.

I moved my code from OnDraw to my thread and my screen is black right now (black like my future)

while (true)
{
ts->_this->Render(ts->_this->m_pDC);
}
return 1;
}

And I add this code after opengl initialization, on OnCreate:
_param->m_pDC=m_pDC;
_param->_this=this;

But my screen is black.

Any idea? by the way StartThread is a static funtion.

##### Share on other sites
Quote:
 Original post by iNsAn1tYTrue. This is the rationale the article works to; it's the simple modification of an application from one which is event-driven to on which is both event-driven and updates continuously. Did your animation viewer only use the OnIdle function? I found that I had to override Run as well to get the kind of reliable updating I expected.

Only OnIdle I believe - I'll check when I am in the office - I think you just need to make sure your OnIdle() kick returns TRUE - this forces the application to keep firing OnIdle() until you return FALSE or there are messages to handle. Once the messages are handled it goes back to OnIdle() calls. It's in OnIdle() tha the time delta was calculated, so I don't know how widely variable the frame-times slices were but none of our engineers or artists complain about it. Maybe I'll put a profile graph in today to see. I do know that when a menu item is being selected, or a modal loop is entered, the screen no longer refreshes ( OnIdle() not called ) but what I did was make it that if WM_Paint is received and the last-render time is quite old ( > 50ms ) to redraw from the view invalidation - this has the effect of 'time stopping' while modal but still rendering what you need to.

What were you doing in Run()?

I justed checked and there's no call to Run at all.

[Edited by - Sphet on March 5, 2007 11:31:20 AM]

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631417
• Total Posts
2999970
×