| [8232] | 1 | /* | 
|---|
 | 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
 | 3 |  *                    > www.orxonox.net < | 
|---|
 | 4 |  * | 
|---|
 | 5 |  * | 
|---|
 | 6 |  *   License notice: | 
|---|
 | 7 |  * | 
|---|
 | 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
 | 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
 | 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
 | 11 |  *   of the License, or (at your option) any later version. | 
|---|
 | 12 |  * | 
|---|
 | 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
 | 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
 | 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
 | 16 |  *   GNU General Public License for more details. | 
|---|
 | 17 |  * | 
|---|
 | 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
 | 19 |  *   along with this program; if not, write to the Free Software | 
|---|
 | 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
 | 21 |  * | 
|---|
 | 22 |  *   Author: | 
|---|
 | 23 |  *      This code comes from http://www.ogre3d.org/tikiwiki/High+resolution+screenshots which is Public Domain. | 
|---|
 | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      Oli Scheuss | 
|---|
 | 26 |  *      Damian 'Mozork' Frick | 
|---|
 | 27 |  * | 
|---|
 | 28 |  */ | 
|---|
| [7015] | 29 |  | 
|---|
| [8232] | 30 | /** | 
|---|
 | 31 |    @file ScreenshotManager.cc | 
|---|
 | 32 |    @brief Implementation of the ScreenshotManager class. | 
|---|
 | 33 |    @ingroup Designtools | 
|---|
 | 34 | */ | 
|---|
 | 35 |  | 
|---|
| [7015] | 36 | #include "ScreenshotManager.h" | 
|---|
| [7041] | 37 |  | 
|---|
| [8232] | 38 | #include <OgreCamera.h> | 
|---|
 | 39 | #include <OgreRenderTexture.h> | 
|---|
| [7015] | 40 | #include <OgreRenderWindow.h> | 
|---|
| [8232] | 41 | #include <OgreRoot.h> | 
|---|
| [7015] | 42 | #include <OgreViewport.h> | 
|---|
 | 43 |  | 
|---|
| [8232] | 44 | #include "core/ConfigValueIncludes.h" | 
|---|
| [7015] | 45 | #include "core/GraphicsManager.h" | 
|---|
| [7041] | 46 | #include "core/PathConfig.h" | 
|---|
| [8351] | 47 | #include "core/Resource.h" | 
|---|
| [7284] | 48 | #include "core/command/ConsoleCommand.h" | 
|---|
| [8232] | 49 | #include "util/ScopedSingletonManager.h" | 
|---|
 | 50 | #include "util/StringUtils.h" | 
|---|
| [7015] | 51 |  | 
|---|
| [7041] | 52 | #include "CameraManager.h" | 
|---|
 | 53 | #include "graphics/Camera.h" | 
|---|
| [7015] | 54 |  | 
|---|
| [8297] | 55 | // #include <X11/Xlib.h> TODO: Needed? | 
|---|
 | 56 |  | 
|---|
| [7015] | 57 | namespace orxonox | 
|---|
 | 58 | { | 
|---|
| [8232] | 59 |  | 
|---|
 | 60 |     SetConsoleCommand("printScreenHD", &ScreenshotManager::makeScreenshot_s); | 
|---|
 | 61 |      | 
|---|
| [7041] | 62 |     ManageScopedSingleton(ScreenshotManager, ScopeID::Graphics, false); | 
|---|
| [7015] | 63 |  | 
|---|
| [8232] | 64 |     /** | 
|---|
 | 65 |     @brief | 
|---|
 | 66 |         Constructor.  | 
|---|
 | 67 |     */ | 
|---|
| [8413] | 68 |     ScreenshotManager::ScreenshotManager() : finalPicturePB_(NULL), data_(NULL) | 
|---|
| [7015] | 69 |     { | 
|---|
| [8232] | 70 |         RegisterRootObject(ScreenshotManager); | 
|---|
 | 71 |          | 
|---|
 | 72 |         this->setConfigValues(); | 
|---|
 | 73 |  | 
|---|
 | 74 |         // Flag for overlay rendering | 
|---|
 | 75 |         this->disableOverlays_ = true; | 
|---|
 | 76 |     } | 
|---|
 | 77 |  | 
|---|
 | 78 |     ScreenshotManager::~ScreenshotManager() | 
|---|
 | 79 |     { | 
|---|
| [8413] | 80 |         this->cleanup(); | 
|---|
| [8232] | 81 |     } | 
|---|
| [8413] | 82 |  | 
|---|
 | 83 |     /** | 
|---|
 | 84 |     @brief | 
|---|
 | 85 |         Frees used memory. | 
|---|
 | 86 |     */ | 
|---|
 | 87 |     void ScreenshotManager::cleanup(void) | 
|---|
 | 88 |     { | 
|---|
 | 89 |         if(this->finalPicturePB_ != NULL) | 
|---|
 | 90 |         { | 
|---|
 | 91 |             delete this->finalPicturePB_; | 
|---|
 | 92 |             this->finalPicturePB_ = NULL; | 
|---|
 | 93 |         } | 
|---|
 | 94 |         if(this->data_ != NULL) | 
|---|
 | 95 |         { | 
|---|
 | 96 |             delete this->data_; | 
|---|
 | 97 |             this->data_ = NULL; | 
|---|
 | 98 |         } | 
|---|
 | 99 |         if(!this->tempTexture_.isNull()) | 
|---|
 | 100 |             this->tempTexture_.freeMethod(); | 
|---|
 | 101 |     } | 
|---|
| [8232] | 102 |      | 
|---|
 | 103 |     /** | 
|---|
 | 104 |     @brief | 
|---|
 | 105 |         Sets some config values. | 
|---|
 | 106 |     */ | 
|---|
 | 107 |     void ScreenshotManager::setConfigValues(void) | 
|---|
 | 108 |     { | 
|---|
 | 109 |         // Set file extension for the Screenshot files. | 
|---|
 | 110 |         SetConfigValue(fileExtension_, ".png"); | 
|---|
 | 111 |         // The grid size. | 
|---|
 | 112 |         SetConfigValue(gridSize_, 3); | 
|---|
 | 113 |     } | 
|---|
 | 114 |  | 
|---|
 | 115 |     /** | 
|---|
 | 116 |     @brief | 
|---|
 | 117 |         Update internal parameters. | 
|---|
 | 118 |     */ | 
|---|
 | 119 |     void ScreenshotManager::update(void) | 
|---|
 | 120 |     { | 
|---|
| [7041] | 121 |         Ogre::RenderWindow* pRenderWindow = GraphicsManager::getInstance().getRenderWindow(); | 
|---|
 | 122 |  | 
|---|
| [8413] | 123 |         // Update current window size | 
|---|
 | 124 |         this->windowWidth_   = pRenderWindow->getWidth(); | 
|---|
 | 125 |         this->windowHeight_  = pRenderWindow->getHeight(); | 
|---|
| [7076] | 126 |  | 
|---|
| [8413] | 127 |         // Create temporary texture | 
|---|
 | 128 |         this->tempTexture_ = Ogre::TextureManager::getSingleton().createManual("ScreenShotTex", Resource::getDefaultResourceGroup(), Ogre::TEX_TYPE_2D, this->windowWidth_, this->windowHeight_, 0, Ogre::PF_B8G8R8, Ogre::TU_RENDERTARGET); | 
|---|
| [7015] | 129 |  | 
|---|
| [8413] | 130 |         // Get the current render target of the temporary texture | 
|---|
 | 131 |         this->renderTarget_ = this->tempTexture_->getBuffer()->getRenderTarget(); | 
|---|
| [7015] | 132 |  | 
|---|
| [8413] | 133 |         // HardwarePixelBufferSharedPtr to the buffer of the temporary texture | 
|---|
 | 134 |         this->buffer_ = this->tempTexture_->getBuffer(); | 
|---|
| [7015] | 135 |  | 
|---|
| [8413] | 136 |         // Create PixelBox | 
|---|
 | 137 |         this->data_ = new uint8_t[(this->windowWidth_ * this->gridSize_) * (this->windowHeight_ * this->gridSize_) * 3]; | 
|---|
 | 138 |         this->finalPicturePB_ = new Ogre::PixelBox(this->windowWidth_ * this->gridSize_, this->windowHeight_ * this->gridSize_, 1, Ogre::PF_B8G8R8, this->data_); | 
|---|
| [7015] | 139 |     } | 
|---|
 | 140 |  | 
|---|
| [8232] | 141 |     /** | 
|---|
 | 142 |     @brief | 
|---|
 | 143 |         Make a screenshot. | 
|---|
 | 144 |         The screenshot is saved in the log folder. | 
|---|
 | 145 |     */ | 
|---|
 | 146 |     void ScreenshotManager::makeScreenshot() | 
|---|
| [7015] | 147 |     { | 
|---|
| [8232] | 148 |         // Get the screenshot. | 
|---|
 | 149 |         Ogre::Image* finalImage = getScreenshot(); | 
|---|
 | 150 |         if(finalImage != NULL) | 
|---|
 | 151 |         { | 
|---|
 | 152 |             // Save it. | 
|---|
| [8406] | 153 |             finalImage->save(PathConfig::getInstance().getLogPathString() + "screenshot_" + getTimestamp() + this->fileExtension_); | 
|---|
| [8232] | 154 |             delete finalImage; | 
|---|
| [8858] | 155 |             orxout(user_info) << "Finished taking " << this->gridSize_*this->windowWidth_ << "x" << this->gridSize_*this->windowHeight_ << " pixel HD screenshot. Storing in log/." << endl; | 
|---|
| [8232] | 156 |         } | 
|---|
 | 157 |         else | 
|---|
| [8858] | 158 |             orxout(user_error) << "There needs to be an active camera to make screenshots." << endl; | 
|---|
| [8413] | 159 |  | 
|---|
 | 160 |         this->cleanup(); | 
|---|
| [7015] | 161 |     } | 
|---|
 | 162 |  | 
|---|
| [8232] | 163 |     /** | 
|---|
 | 164 |     @brief | 
|---|
 | 165 |         Creates a screenshot and returns it. | 
|---|
| [8413] | 166 |         After calling this method the ScreenshotManager should be cleaned using cleanup(). | 
|---|
| [8232] | 167 |     @return | 
|---|
| [8413] | 168 |         Returns a pointer to an Ogre::Image with the screenshot. The memory must be freed, when the image is no longer needed. | 
|---|
| [8232] | 169 |     */ | 
|---|
 | 170 |     Ogre::Image* ScreenshotManager::getScreenshot() | 
|---|
 | 171 |     { | 
|---|
 | 172 |         if(CameraManager::getInstance().getActiveCamera() == NULL ) | 
|---|
 | 173 |             return NULL; | 
|---|
 | 174 |         return this->getScreenshot(CameraManager::getInstance().getActiveCamera()->getOgreCamera()); | 
|---|
 | 175 |     } | 
|---|
| [7015] | 176 |  | 
|---|
| [8079] | 177 |     /** | 
|---|
 | 178 |     @brief | 
|---|
| [8232] | 179 |         Creates a screenshot with the given camera and returns it. | 
|---|
| [8413] | 180 |         After calling this method the ScreenshotManager should be cleaned using cleanup(). | 
|---|
| [8232] | 181 |     @param camera | 
|---|
 | 182 |         A pointer to the camera the screenshot should be taken with. | 
|---|
 | 183 |     @return | 
|---|
| [8413] | 184 |         Returns a pointer to an Ogre::Image with the screenshot. The memory must be freed, when the image is no longer needed. | 
|---|
| [7015] | 185 |     */ | 
|---|
| [8232] | 186 |     Ogre::Image* ScreenshotManager::getScreenshot(Ogre::Camera* camera) | 
|---|
| [7015] | 187 |     { | 
|---|
| [8232] | 188 |         if(camera == NULL) | 
|---|
 | 189 |             return NULL; | 
|---|
 | 190 |          | 
|---|
 | 191 |         // Update the internal parameters. | 
|---|
 | 192 |         this->update(); | 
|---|
| [7015] | 193 |  | 
|---|
| [8232] | 194 |         // Add the camera as viewport. | 
|---|
 | 195 |         this->renderTarget_->removeAllViewports(); | 
|---|
 | 196 |         this->renderTarget_->addViewport(camera); | 
|---|
| [7076] | 197 |  | 
|---|
| [8232] | 198 |         // Set the viewport settings | 
|---|
 | 199 |         Ogre::Viewport *vp = renderTarget_->getViewport(0); | 
|---|
| [7076] | 200 |         vp->setClearEveryFrame(true); | 
|---|
| [7015] | 201 |         vp->setOverlaysEnabled(false); | 
|---|
 | 202 |  | 
|---|
| [8232] | 203 |         // Remind current overlay flag | 
|---|
| [7015] | 204 |         bool enableOverlayFlag = GraphicsManager::getInstance().getViewport()->getOverlaysEnabled(); | 
|---|
| [8232] | 205 |          | 
|---|
 | 206 |         // We disable overlay rendering if it is set in config file and the viewport setting is enabled | 
|---|
 | 207 |         if(this->disableOverlays_ && enableOverlayFlag) | 
|---|
| [7015] | 208 |             GraphicsManager::getInstance().getViewport()->setOverlaysEnabled(false); | 
|---|
| [8232] | 209 |          | 
|---|
 | 210 |         Ogre::Image* finalImage = new Ogre::Image(); | 
|---|
 | 211 |          | 
|---|
 | 212 |         if(this->gridSize_ <= 1) | 
|---|
| [7015] | 213 |         { | 
|---|
 | 214 |             // Simple case where the contents of the screen are taken directly | 
|---|
 | 215 |             // Also used when an invalid value is passed within gridSize (zero or negative grid size) | 
|---|
| [8232] | 216 |             this->renderTarget_->update(); // Render | 
|---|
| [8414] | 217 |  | 
|---|
 | 218 |             this->buffer_->blitToMemory(*this->finalPicturePB_); | 
|---|
| [8413] | 219 |             finalImage->loadDynamicImage(static_cast<unsigned char*>(finalPicturePB_->data), finalPicturePB_->getWidth(), finalPicturePB_->getHeight(),Ogre::PF_B8G8R8); | 
|---|
| [7015] | 220 |         } | 
|---|
 | 221 |         else | 
|---|
 | 222 |         { | 
|---|
| [8232] | 223 |             // Define the original frustum extents variables | 
|---|
| [7015] | 224 |             Ogre::Real originalFrustumLeft, originalFrustumRight, originalFrustumTop, originalFrustumBottom; | 
|---|
| [8232] | 225 |             // Set the original Frustum extents | 
|---|
| [7015] | 226 |             camera->getFrustumExtents(originalFrustumLeft, originalFrustumRight, originalFrustumTop, originalFrustumBottom); | 
|---|
| [8232] | 227 |              | 
|---|
 | 228 |             // Compute the Stepsize for the grid | 
|---|
 | 229 |             Ogre::Real frustumGridStepHorizontal  = (originalFrustumRight * 2) / this->gridSize_; | 
|---|
 | 230 |             Ogre::Real frustumGridStepVertical  = (originalFrustumTop * 2) / this->gridSize_; | 
|---|
 | 231 |              | 
|---|
 | 232 |             // Process each grid | 
|---|
| [7015] | 233 |             Ogre::Real frustumLeft, frustumRight, frustumTop, frustumBottom; | 
|---|
| [8232] | 234 |             for (unsigned int nbScreenshots = 0; nbScreenshots < this->gridSize_ * this->gridSize_; nbScreenshots++) | 
|---|
| [7076] | 235 |             { | 
|---|
| [8232] | 236 |                 int y = nbScreenshots / this->gridSize_; | 
|---|
 | 237 |                 int x = nbScreenshots - y * this->gridSize_; | 
|---|
 | 238 |                  | 
|---|
| [7015] | 239 |                 // Shoggoth frustum extents setting | 
|---|
| [8232] | 240 |                 // Compute the new frustum extents | 
|---|
| [7039] | 241 |                 frustumLeft    = originalFrustumLeft + frustumGridStepHorizontal * x; | 
|---|
 | 242 |                 frustumRight  = frustumLeft + frustumGridStepHorizontal; | 
|---|
 | 243 |                 frustumTop    = originalFrustumTop - frustumGridStepVertical * y; | 
|---|
 | 244 |                 frustumBottom  = frustumTop - frustumGridStepVertical; | 
|---|
| [8232] | 245 |                  | 
|---|
 | 246 |                 // Set the frustum extents value to the camera | 
|---|
| [7015] | 247 |                 camera->setFrustumExtents(frustumLeft, frustumRight, frustumTop, frustumBottom); | 
|---|
| [8232] | 248 |                  | 
|---|
 | 249 |                 // Ignore time duration between frames | 
|---|
| [7015] | 250 |                 Ogre::Root::getSingletonPtr()->clearEventTimes(); | 
|---|
| [8232] | 251 |                 this->renderTarget_->update(); // Render | 
|---|
 | 252 |                  | 
|---|
 | 253 |                 // Define the current box | 
|---|
 | 254 |                 Ogre::Box subBox = Ogre::Box(x* this->windowWidth_,y * this->windowHeight_,x * this->windowWidth_ + this->windowWidth_, y * this->windowHeight_ + this->windowHeight_); | 
|---|
 | 255 |                 // Copy the content from the temp buffer into the final picture PixelBox | 
|---|
 | 256 |                 // Place the tempBuffer content at the right position | 
|---|
| [8413] | 257 |                 this->buffer_->blitToMemory(this->finalPicturePB_->getSubVolume(subBox)); | 
|---|
| [8232] | 258 |                  | 
|---|
| [8858] | 259 |                 orxout(internal_info) << "Created screenshot number " << nbScreenshots << " for multi grid HD screenshot." << endl; | 
|---|
| [8232] | 260 |                  | 
|---|
| [7015] | 261 |             } | 
|---|
| [8232] | 262 |              | 
|---|
 | 263 |             // Set frustum extents to previous settings | 
|---|
| [7015] | 264 |             camera->resetFrustumExtents(); | 
|---|
| [8232] | 265 |              | 
|---|
 | 266 |             // Insert the PixelBox data into the Image Object | 
|---|
| [8413] | 267 |             finalImage->loadDynamicImage(static_cast<unsigned char*>(this->finalPicturePB_->data), this->finalPicturePB_->getWidth(), this->finalPicturePB_->getHeight(), 1, Ogre::PF_B8G8R8, false); | 
|---|
| [7015] | 268 |         } | 
|---|
| [8232] | 269 |          | 
|---|
 | 270 |         // Do we have to re-enable our overlays? | 
|---|
| [7015] | 271 |         if(enableOverlayFlag) | 
|---|
 | 272 |             GraphicsManager::getInstance().getViewport()->setOverlaysEnabled(true); | 
|---|
| [8232] | 273 |          | 
|---|
 | 274 |         // Reset time since last frame to pause the scene | 
|---|
| [7015] | 275 |         Ogre::Root::getSingletonPtr()->clearEventTimes(); | 
|---|
| [8232] | 276 |          | 
|---|
 | 277 |         return finalImage; | 
|---|
| [7015] | 278 |     } | 
|---|
 | 279 |  | 
|---|
| [8079] | 280 |     /** | 
|---|
 | 281 |     @brief | 
|---|
 | 282 |         Set the size of the grid. | 
|---|
 | 283 |     @param size | 
|---|
 | 284 |         The size of the grid. | 
|---|
 | 285 |     */ | 
|---|
 | 286 |     void ScreenshotManager::setGridSize(unsigned int size) | 
|---|
 | 287 |     { | 
|---|
| [8232] | 288 |         if(size == this->gridSize_) | 
|---|
| [8079] | 289 |             return; | 
|---|
 | 290 |  | 
|---|
| [8232] | 291 |         this->gridSize_ = size; | 
|---|
| [8079] | 292 |     } | 
|---|
 | 293 |  | 
|---|
| [7015] | 294 | } | 
|---|