source: OGRE/trunk/ogrenew/OgreMain/src/OgreRenderTarget.cpp @ 657

Revision 657, 13.4 KB checked in by mattausch, 19 years ago (diff)

added ogre dependencies and patched ogre sources

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    {
40        // Default to no stats display
41        mActive = true;
42        mAutoUpdate = true;
43        mPriority = OGRE_DEFAULT_RT_GROUP;
44        mTimer = Root::getSingleton().getTimer();
45        resetStatistics();
46    }
47
48    RenderTarget::~RenderTarget()
49    {
50        // Delete viewports
51        for (ViewportList::iterator i = mViewportList.begin();
52            i != mViewportList.end(); ++i)
53        {
54            delete (*i).second;
55        }
56
57
58        // Write closing message
59                StringUtil::StrStreamType msg;
60                msg << "Render Target '" << mName << "' "
61                        << "Average FPS: " << mStats.avgFPS << " "
62                        << "Best FPS: " << mStats.bestFPS << " "
63                        << "Worst FPS: " << mStats.worstFPS;
64        LogManager::getSingleton().logMessage(msg.str());
65
66    }
67
68    const String& RenderTarget::getName(void) const
69    {
70        return mName;
71    }
72
73
74    void RenderTarget::getMetrics(unsigned int& width, unsigned int& height, unsigned int& colourDepth)
75    {
76        width = mWidth;
77        height = mHeight;
78        colourDepth = mColourDepth;
79    }
80
81    unsigned int RenderTarget::getWidth(void) const
82    {
83        return mWidth;
84    }
85    unsigned int RenderTarget::getHeight(void) const
86    {
87        return mHeight;
88    }
89    unsigned int RenderTarget::getColourDepth(void) const
90    {
91        return mColourDepth;
92    }
93
94    void RenderTarget::update(void)
95    {
96
97        // notify listeners (pre)
98        firePreUpdate();
99
100        mStats.triangleCount = 0;
101        // Go through viewports in Z-order
102        // Tell each to refresh
103        ViewportList::iterator it = mViewportList.begin();
104        while (it != mViewportList.end())
105        {
106            fireViewportPreUpdate((*it).second);
107            (*it).second->update();
108            mStats.triangleCount += (*it).second->_getNumRenderedFaces();
109            fireViewportPostUpdate((*it).second);
110            ++it;
111        }
112
113        // notify listeners (post)
114        firePostUpdate();
115
116        // Update statistics (always on top)
117        updateStats();
118
119
120    }
121
122    Viewport* RenderTarget::addViewport(Camera* cam, int ZOrder, float left, float top ,
123        float width , float height)
124    {
125        // Check no existing viewport with this Z-order
126        ViewportList::iterator it = mViewportList.find(ZOrder);
127
128        if (it != mViewportList.end())
129        {
130                        StringUtil::StrStreamType str;
131                        str << "Can't create another viewport for "
132                                << mName << " with Z-Order " << ZOrder
133                                << " because a viewport exists with this Z-Order already.";
134            OGRE_EXCEPT(9999, str.str(), "RenderTarget::addViewport");
135        }
136        // Add viewport to list
137        // Order based on Z-Order
138        Viewport* vp = new Viewport(cam, this, left, top, width, height, ZOrder);
139
140        mViewportList.insert(ViewportList::value_type(ZOrder, vp));
141
142        return vp;
143    }
144
145    void RenderTarget::removeViewport(int ZOrder)
146    {
147        ViewportList::iterator it = mViewportList.find(ZOrder);
148
149        if (it != mViewportList.end())
150        {
151            delete (*it).second;
152            mViewportList.erase(ZOrder);
153        }
154    }
155
156    void RenderTarget::removeAllViewports(void)
157    {
158
159
160        for (ViewportList::iterator it = mViewportList.begin(); it != mViewportList.end(); ++it)
161        {
162            delete (*it).second;
163        }
164
165        mViewportList.clear();
166
167    }
168
169    void RenderTarget::getStatistics(float& lastFPS, float& avgFPS,
170        float& bestFPS, float& worstFPS) const
171    {
172
173        // Note - the will have been updated by the last render
174        lastFPS = mStats.lastFPS;
175        avgFPS = mStats.avgFPS;
176        bestFPS = mStats.bestFPS;
177        worstFPS = mStats.worstFPS;
178
179
180    }
181
182    const RenderTarget::FrameStats& RenderTarget::getStatistics(void) const
183    {
184        return mStats;
185    }
186
187    float RenderTarget::getLastFPS() const
188    {
189        return mStats.lastFPS;
190    }
191    float RenderTarget::getAverageFPS() const
192    {
193        return mStats.avgFPS;
194    }
195    float RenderTarget::getBestFPS() const
196    {
197        return mStats.bestFPS;
198    }
199    float RenderTarget::getWorstFPS() const
200    {
201        return mStats.worstFPS;
202    }
203
204    size_t RenderTarget::getTriangleCount(void) const
205    {
206        return mStats.triangleCount;
207    }
208
209    float RenderTarget::getBestFrameTime() const
210    {
211        return mStats.bestFrameTime;
212    }
213
214    float RenderTarget::getWorstFrameTime() const
215    {
216        return mStats.worstFrameTime;
217    }
218
219    void RenderTarget::resetStatistics(void)
220    {
221        mStats.avgFPS = 0.0;
222        mStats.bestFPS = 0.0;
223        mStats.lastFPS = 0.0;
224        mStats.worstFPS = 999.0;
225        mStats.triangleCount = 0;
226        mStats.bestFrameTime = 999999;
227        mStats.worstFrameTime = 0;
228
229        mLastTime = mTimer->getMilliseconds();
230        mLastSecond = mLastTime;
231        mFrameCount = 0;
232    }
233
234    void RenderTarget::updateStats(void)
235    {
236        ++mFrameCount;
237        unsigned long thisTime = mTimer->getMilliseconds();
238
239        // check frame time
240        unsigned long frameTime = thisTime - mLastTime ;
241        mLastTime = thisTime ;
242
243        mStats.bestFrameTime = std::min(mStats.bestFrameTime, frameTime);
244        mStats.worstFrameTime = std::max(mStats.worstFrameTime, frameTime);
245
246        // check if new second (update only once per second)
247        if (thisTime - mLastSecond > 1000)
248        {
249            // new second - not 100% precise
250            mStats.lastFPS = (float)mFrameCount / (float)(thisTime - mLastSecond) * 1000.0;
251
252            if (mStats.avgFPS == 0)
253                mStats.avgFPS = mStats.lastFPS;
254            else
255                mStats.avgFPS = (mStats.avgFPS + mStats.lastFPS) / 2; // not strictly correct, but good enough
256
257            mStats.bestFPS = std::max(mStats.bestFPS, mStats.lastFPS);
258            mStats.worstFPS = std::min(mStats.worstFPS, mStats.lastFPS);
259
260            mLastSecond = thisTime ;
261            mFrameCount  = 0;
262
263        }
264
265    }
266
267    void RenderTarget::getCustomAttribute(const String& name, void* pData)
268    {
269        OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Attribute not found.", "RenderTarget::getCustomAttribute");
270    }
271    //-----------------------------------------------------------------------
272    void RenderTarget::setDebugText(const String& text)
273    {
274        mDebugText = text;
275    }
276    //-----------------------------------------------------------------------
277    const String & RenderTarget::getDebugText() const
278    {
279        return mDebugText;
280    }
281    //-----------------------------------------------------------------------
282    void RenderTarget::addListener(RenderTargetListener* listener)
283    {
284        mListeners.push_back(listener);
285    }
286    //-----------------------------------------------------------------------
287    void RenderTarget::removeListener(RenderTargetListener* listener)
288    {
289        RenderTargetListenerList::iterator i;
290        for (i = mListeners.begin(); i != mListeners.end(); ++i)
291        {
292            if (*i == listener)
293            {
294                mListeners.erase(i);
295                break;
296            }
297        }
298
299    }
300    //-----------------------------------------------------------------------
301    void RenderTarget::removeAllListeners(void)
302    {
303        mListeners.clear();
304    }
305    //-----------------------------------------------------------------------
306    void RenderTarget::firePreUpdate(void)
307    {
308        RenderTargetEvent evt;
309        evt.source = this;
310
311        RenderTargetListenerList::iterator i, iend;
312        i = mListeners.begin();
313        iend = mListeners.end();
314        for(; i != iend; ++i)
315        {
316            (*i)->preRenderTargetUpdate(evt);
317        }
318
319
320    }
321    //-----------------------------------------------------------------------
322    void RenderTarget::firePostUpdate(void)
323    {
324        RenderTargetEvent evt;
325        evt.source = this;
326
327        RenderTargetListenerList::iterator i, iend;
328        i = mListeners.begin();
329        iend = mListeners.end();
330        for(; i != iend; ++i)
331        {
332            (*i)->postRenderTargetUpdate(evt);
333        }
334    }
335    //-----------------------------------------------------------------------
336    unsigned short RenderTarget::getNumViewports(void) const
337    {
338        return (unsigned short)mViewportList.size();
339
340    }
341    //-----------------------------------------------------------------------
342    Viewport* RenderTarget::getViewport(unsigned short index)
343    {
344        assert (index < mViewportList.size() && "Index out of bounds");
345
346        ViewportList::iterator i = mViewportList.begin();
347        while (index--)
348            ++i;
349        return i->second;
350    }
351    //-----------------------------------------------------------------------
352    bool RenderTarget::isActive() const
353    {
354        return mActive;
355    }
356    //-----------------------------------------------------------------------
357    void RenderTarget::setActive( bool state )
358    {
359        mActive = state;
360    }
361    //-----------------------------------------------------------------------
362    void RenderTarget::fireViewportPreUpdate(Viewport* vp)
363    {
364        RenderTargetViewportEvent evt;
365        evt.source = vp;
366
367        RenderTargetListenerList::iterator i, iend;
368        i = mListeners.begin();
369        iend = mListeners.end();
370        for(; i != iend; ++i)
371        {
372            (*i)->preViewportUpdate(evt);
373        }
374    }
375    //-----------------------------------------------------------------------
376    void RenderTarget::fireViewportPostUpdate(Viewport* vp)
377    {
378        RenderTargetViewportEvent evt;
379        evt.source = vp;
380
381        RenderTargetListenerList::iterator i, iend;
382        i = mListeners.begin();
383        iend = mListeners.end();
384        for(; i != iend; ++i)
385        {
386            (*i)->postViewportUpdate(evt);
387        }
388    }
389    //-----------------------------------------------------------------------
390    String RenderTarget::writeContentsToTimestampedFile(const String& filenamePrefix, const String& filenameSuffix)
391    {
392        struct tm *pTime;
393        time_t ctTime; time(&ctTime);
394        pTime = localtime( &ctTime );
395        std::ostringstream oss;
396        oss     << std::setw(2) << std::setfill('0') << (pTime->tm_mon + 1)
397            << std::setw(2) << std::setfill('0') << pTime->tm_mday
398            << std::setw(2) << std::setfill('0') << (pTime->tm_year + 1900)
399            << "_" << std::setw(2) << std::setfill('0') << pTime->tm_hour
400            << std::setw(2) << std::setfill('0') << pTime->tm_min
401            << std::setw(2) << std::setfill('0') << pTime->tm_sec
402            << std::setw(3) << std::setfill('0') << (mTimer->getMilliseconds() % 1000);
403        String filename = filenamePrefix + String(oss.str()) + filenameSuffix;
404        writeContentsToFile(filename);
405        return filename;
406
407    }
408    //-----------------------------------------------------------------------
409    void RenderTarget::_notifyCameraRemoved(const Camera* cam)
410    {
411        ViewportList::iterator i, iend;
412        iend = mViewportList.end();
413        for (i = mViewportList.begin(); i != iend; ++i)
414        {
415            Viewport* v = i->second;
416            if (v->getCamera() == cam)
417            {
418                // disable camera link
419                v->setCamera(0);
420            }
421        }
422    }
423    //-----------------------------------------------------------------------
424    void RenderTarget::setAutoUpdated(bool autoup)
425    {
426        mAutoUpdate = autoup;
427    }
428    //-----------------------------------------------------------------------
429    bool RenderTarget::isAutoUpdated(void) const
430    {
431        return mAutoUpdate;
432    }
433    //-----------------------------------------------------------------------
434    bool RenderTarget::isPrimary(void) const
435    {
436        // RenderWindow will override and return true for the primary window
437        return false;
438    }
439
440
441}       
Note: See TracBrowser for help on using the repository browser.