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