source: OGRE/trunk/ogre_changes/Ogre1.2/OgreMain/src/OgreRenderTarget.cpp @ 768

Revision 768, 14.5 KB checked in by szirmay, 18 years ago (diff)
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4(Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#include "OgreStableHeaders.h"
26#include "OgreRenderTarget.h"
27#include "OgreStringConverter.h"
28
29#include "OgreViewport.h"
30#include "OgreException.h"
31#include "OgreLogManager.h"
32#include "OgreRenderTargetListener.h"
33#include "OgrePlatformManager.h"
34#include "OgreRoot.h"
35
36namespace Ogre {
37
38    RenderTarget::RenderTarget()
39                :mPriority(OGRE_DEFAULT_RT_GROUP),
40                mActive(true),
41                mAutoUpdate(true)
42                #ifdef GAMETOOLS_ILLUMINATION_MODULE
43                , lastFrame(0)
44                #endif
45    {
46        mTimer = Root::getSingleton().getTimer();
47        resetStatistics();
48    }
49
50    RenderTarget::~RenderTarget()
51    {
52        // Delete viewports
53        for (ViewportList::iterator i = mViewportList.begin();
54            i != mViewportList.end(); ++i)
55        {
56            delete (*i).second;
57        }
58
59
60        // Write closing message
61                StringUtil::StrStreamType msg;
62                msg << "Render Target '" << mName << "' "
63                        << "Average FPS: " << mStats.avgFPS << " "
64                        << "Best FPS: " << mStats.bestFPS << " "
65                        << "Worst FPS: " << mStats.worstFPS;
66        LogManager::getSingleton().logMessage(msg.str());
67
68    }
69
70    const String& RenderTarget::getName(void) const
71    {
72        return mName;
73    }
74
75
76    void RenderTarget::getMetrics(unsigned int& width, unsigned int& height, unsigned int& colourDepth)
77    {
78        width = mWidth;
79        height = mHeight;
80        colourDepth = mColourDepth;
81    }
82
83    unsigned int RenderTarget::getWidth(void) const
84    {
85        return mWidth;
86    }
87    unsigned int RenderTarget::getHeight(void) const
88    {
89        return mHeight;
90    }
91    unsigned int RenderTarget::getColourDepth(void) const
92    {
93        return mColourDepth;
94    }
95
96    void RenderTarget::update(void)
97    {
98
99        // notify listeners (pre)
100        firePreUpdate();
101
102        mStats.triangleCount = 0;
103        // Go through viewports in Z-order
104        // Tell each to refresh
105        ViewportList::iterator it = mViewportList.begin();
106        while (it != mViewportList.end())
107        {
108            fireViewportPreUpdate((*it).second);
109            (*it).second->update();
110            mStats.triangleCount += (*it).second->_getNumRenderedFaces();
111            fireViewportPostUpdate((*it).second);
112            ++it;
113        }
114
115        // notify listeners (post)
116        firePostUpdate();
117
118        // Update statistics (always on top)
119        updateStats();
120
121
122    }
123
124    Viewport* RenderTarget::addViewport(Camera* cam, int ZOrder, float left, float top ,
125        float width , float height)
126    {
127        // Check no existing viewport with this Z-order
128        ViewportList::iterator it = mViewportList.find(ZOrder);
129
130        if (it != mViewportList.end())
131        {
132                        StringUtil::StrStreamType str;
133                        str << "Can't create another viewport for "
134                                << mName << " with Z-Order " << ZOrder
135                                << " because a viewport exists with this Z-Order already.";
136            OGRE_EXCEPT(9999, str.str(), "RenderTarget::addViewport");
137        }
138        // Add viewport to list
139        // Order based on Z-Order
140        Viewport* vp = new Viewport(cam, this, left, top, width, height, ZOrder);
141
142        mViewportList.insert(ViewportList::value_type(ZOrder, vp));
143
144                fireViewportAdded(vp);
145
146        return vp;
147    }
148        //-----------------------------------------------------------------------
149    void RenderTarget::removeViewport(int ZOrder)
150    {
151        ViewportList::iterator it = mViewportList.find(ZOrder);
152
153        if (it != mViewportList.end())
154        {
155                        fireViewportRemoved((*it).second);
156            delete (*it).second;
157            mViewportList.erase(ZOrder);
158        }
159    }
160
161    void RenderTarget::removeAllViewports(void)
162    {
163
164
165        for (ViewportList::iterator it = mViewportList.begin(); it != mViewportList.end(); ++it)
166        {
167            delete (*it).second;
168        }
169
170        mViewportList.clear();
171
172    }
173
174    void RenderTarget::getStatistics(float& lastFPS, float& avgFPS,
175        float& bestFPS, float& worstFPS) const
176    {
177
178        // Note - the will have been updated by the last render
179        lastFPS = mStats.lastFPS;
180        avgFPS = mStats.avgFPS;
181        bestFPS = mStats.bestFPS;
182        worstFPS = mStats.worstFPS;
183
184
185    }
186
187    const RenderTarget::FrameStats& RenderTarget::getStatistics(void) const
188    {
189        return mStats;
190    }
191
192    float RenderTarget::getLastFPS() const
193    {
194        return mStats.lastFPS;
195    }
196    float RenderTarget::getAverageFPS() const
197    {
198        return mStats.avgFPS;
199    }
200    float RenderTarget::getBestFPS() const
201    {
202        return mStats.bestFPS;
203    }
204    float RenderTarget::getWorstFPS() const
205    {
206        return mStats.worstFPS;
207    }
208
209    size_t RenderTarget::getTriangleCount(void) const
210    {
211        return mStats.triangleCount;
212    }
213
214    float RenderTarget::getBestFrameTime() const
215    {
216        return mStats.bestFrameTime;
217    }
218
219    float RenderTarget::getWorstFrameTime() const
220    {
221        return mStats.worstFrameTime;
222    }
223
224    void RenderTarget::resetStatistics(void)
225    {
226        mStats.avgFPS = 0.0;
227        mStats.bestFPS = 0.0;
228        mStats.lastFPS = 0.0;
229        mStats.worstFPS = 999.0;
230        mStats.triangleCount = 0;
231        mStats.bestFrameTime = 999999;
232        mStats.worstFrameTime = 0;
233
234        mLastTime = mTimer->getMilliseconds();
235        mLastSecond = mLastTime;
236        mFrameCount = 0;
237    }
238
239    void RenderTarget::updateStats(void)
240    {
241                #ifdef GAMETOOLS_ILLUMINATION_MODULE
242                 lastFrame = Root::getSingleton().getCurrentFrameNumber();
243                #endif
244
245        ++mFrameCount;
246        unsigned long thisTime = mTimer->getMilliseconds();
247
248        // check frame time
249        unsigned long frameTime = thisTime - mLastTime ;
250        mLastTime = thisTime ;
251
252        mStats.bestFrameTime = std::min(mStats.bestFrameTime, frameTime);
253        mStats.worstFrameTime = std::max(mStats.worstFrameTime, frameTime);
254
255        // check if new second (update only once per second)
256        if (thisTime - mLastSecond > 1000)
257        {
258            // new second - not 100% precise
259            mStats.lastFPS = (float)mFrameCount / (float)(thisTime - mLastSecond) * 1000.0;
260
261            if (mStats.avgFPS == 0)
262                mStats.avgFPS = mStats.lastFPS;
263            else
264                mStats.avgFPS = (mStats.avgFPS + mStats.lastFPS) / 2; // not strictly correct, but good enough
265
266            mStats.bestFPS = std::max(mStats.bestFPS, mStats.lastFPS);
267            mStats.worstFPS = std::min(mStats.worstFPS, mStats.lastFPS);
268
269            mLastSecond = thisTime ;
270            mFrameCount  = 0;
271
272        }
273
274    }
275
276    void RenderTarget::getCustomAttribute(const String& name, void* pData)
277    {
278        OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Attribute not found.", "RenderTarget::getCustomAttribute");
279    }
280    //-----------------------------------------------------------------------
281    void RenderTarget::setDebugText(const String& text)
282    {
283        mDebugText = text;
284    }
285    //-----------------------------------------------------------------------
286    const String & RenderTarget::getDebugText() const
287    {
288        return mDebugText;
289    }
290    //-----------------------------------------------------------------------
291    void RenderTarget::addListener(RenderTargetListener* listener)
292    {
293        mListeners.push_back(listener);
294    }
295    //-----------------------------------------------------------------------
296    void RenderTarget::removeListener(RenderTargetListener* listener)
297    {
298        RenderTargetListenerList::iterator i;
299        for (i = mListeners.begin(); i != mListeners.end(); ++i)
300        {
301            if (*i == listener)
302            {
303                mListeners.erase(i);
304                break;
305            }
306        }
307
308    }
309    //-----------------------------------------------------------------------
310    void RenderTarget::removeAllListeners(void)
311    {
312        mListeners.clear();
313    }
314    //-----------------------------------------------------------------------
315    void RenderTarget::firePreUpdate(void)
316    {
317        RenderTargetEvent evt;
318        evt.source = this;
319
320        RenderTargetListenerList::iterator i, iend;
321        i = mListeners.begin();
322        iend = mListeners.end();
323        for(; i != iend; ++i)
324        {
325            (*i)->preRenderTargetUpdate(evt);
326        }
327
328
329    }
330    //-----------------------------------------------------------------------
331    void RenderTarget::firePostUpdate(void)
332    {
333        RenderTargetEvent evt;
334        evt.source = this;
335
336        RenderTargetListenerList::iterator i, iend;
337        i = mListeners.begin();
338        iend = mListeners.end();
339        for(; i != iend; ++i)
340        {
341            (*i)->postRenderTargetUpdate(evt);
342        }
343    }
344    //-----------------------------------------------------------------------
345    unsigned short RenderTarget::getNumViewports(void) const
346    {
347        return (unsigned short)mViewportList.size();
348
349    }
350    //-----------------------------------------------------------------------
351    Viewport* RenderTarget::getViewport(unsigned short index)
352    {
353        assert (index < mViewportList.size() && "Index out of bounds");
354
355        ViewportList::iterator i = mViewportList.begin();
356        while (index--)
357            ++i;
358        return i->second;
359    }
360    //-----------------------------------------------------------------------
361    bool RenderTarget::isActive() const
362    {
363        return mActive;
364    }
365    //-----------------------------------------------------------------------
366    void RenderTarget::setActive( bool state )
367    {
368        mActive = state;
369    }
370    //-----------------------------------------------------------------------
371    void RenderTarget::fireViewportPreUpdate(Viewport* vp)
372    {
373        RenderTargetViewportEvent evt;
374        evt.source = vp;
375
376        RenderTargetListenerList::iterator i, iend;
377        i = mListeners.begin();
378        iend = mListeners.end();
379        for(; i != iend; ++i)
380        {
381            (*i)->preViewportUpdate(evt);
382        }
383    }
384    //-----------------------------------------------------------------------
385    void RenderTarget::fireViewportPostUpdate(Viewport* vp)
386    {
387        RenderTargetViewportEvent evt;
388        evt.source = vp;
389
390        RenderTargetListenerList::iterator i, iend;
391        i = mListeners.begin();
392        iend = mListeners.end();
393        for(; i != iend; ++i)
394        {
395            (*i)->postViewportUpdate(evt);
396        }
397    }
398        //-----------------------------------------------------------------------
399        void RenderTarget::fireViewportAdded(Viewport* vp)
400        {
401                RenderTargetViewportEvent evt;
402                evt.source = vp;
403
404                RenderTargetListenerList::iterator i, iend;
405                i = mListeners.begin();
406                iend = mListeners.end();
407                for(; i != iend; ++i)
408                {
409                        (*i)->viewportAdded(evt);
410                }
411        }
412        //-----------------------------------------------------------------------
413        void RenderTarget::fireViewportRemoved(Viewport* vp)
414        {
415                RenderTargetViewportEvent evt;
416                evt.source = vp;
417
418                RenderTargetListenerList::iterator i, iend;
419                i = mListeners.begin();
420                iend = mListeners.end();
421                for(; i != iend; ++i)
422                {
423                        (*i)->viewportRemoved(evt);
424                }
425        }
426    //-----------------------------------------------------------------------
427    String RenderTarget::writeContentsToTimestampedFile(const String& filenamePrefix, const String& filenameSuffix)
428    {
429        struct tm *pTime;
430        time_t ctTime; time(&ctTime);
431        pTime = localtime( &ctTime );
432        std::ostringstream oss;
433        oss     << std::setw(2) << std::setfill('0') << (pTime->tm_mon + 1)
434            << std::setw(2) << std::setfill('0') << pTime->tm_mday
435            << std::setw(2) << std::setfill('0') << (pTime->tm_year + 1900)
436            << "_" << std::setw(2) << std::setfill('0') << pTime->tm_hour
437            << std::setw(2) << std::setfill('0') << pTime->tm_min
438            << std::setw(2) << std::setfill('0') << pTime->tm_sec
439            << std::setw(3) << std::setfill('0') << (mTimer->getMilliseconds() % 1000);
440        String filename = filenamePrefix + String(oss.str()) + filenameSuffix;
441        writeContentsToFile(filename);
442        return filename;
443
444    }
445    //-----------------------------------------------------------------------
446    void RenderTarget::_notifyCameraRemoved(const Camera* cam)
447    {
448        ViewportList::iterator i, iend;
449        iend = mViewportList.end();
450        for (i = mViewportList.begin(); i != iend; ++i)
451        {
452            Viewport* v = i->second;
453            if (v->getCamera() == cam)
454            {
455                // disable camera link
456                v->setCamera(0);
457            }
458        }
459    }
460    //-----------------------------------------------------------------------
461    void RenderTarget::setAutoUpdated(bool autoup)
462    {
463        mAutoUpdate = autoup;
464    }
465    //-----------------------------------------------------------------------
466    bool RenderTarget::isAutoUpdated(void) const
467    {
468        return mAutoUpdate;
469    }
470    //-----------------------------------------------------------------------
471    bool RenderTarget::isPrimary(void) const
472    {
473        // RenderWindow will override and return true for the primary window
474        return false;
475    }
476    //-----------------------------------------------------------------------
477    RenderTarget::Impl *RenderTarget::_getImpl()
478    {
479        return 0;
480    }
481
482}       
Note: See TracBrowser for help on using the repository browser.