[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 | */ |
---|
[7041] | 68 | ScreenshotManager::ScreenshotManager() |
---|
[7015] | 69 | { |
---|
[8232] | 70 | RegisterRootObject(ScreenshotManager); |
---|
| 71 | |
---|
| 72 | this->setConfigValues(); |
---|
| 73 | |
---|
| 74 | // Flag for overlay rendering |
---|
| 75 | this->disableOverlays_ = true; |
---|
| 76 | |
---|
| 77 | // Update the values |
---|
| 78 | this->update(); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | ScreenshotManager::~ScreenshotManager() |
---|
| 82 | { |
---|
| 83 | |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | @brief |
---|
| 88 | Sets some config values. |
---|
| 89 | */ |
---|
| 90 | void ScreenshotManager::setConfigValues(void) |
---|
| 91 | { |
---|
| 92 | // Set file extension for the Screenshot files. |
---|
| 93 | SetConfigValue(fileExtension_, ".png"); |
---|
| 94 | // The grid size. |
---|
| 95 | SetConfigValue(gridSize_, 3); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | @brief |
---|
| 100 | Update internal parameters. |
---|
| 101 | */ |
---|
| 102 | void ScreenshotManager::update(void) |
---|
| 103 | { |
---|
[7041] | 104 | Ogre::RenderWindow* pRenderWindow = GraphicsManager::getInstance().getRenderWindow(); |
---|
| 105 | |
---|
[8232] | 106 | // If the window size has changed |
---|
| 107 | if(this->windowWidth_ != pRenderWindow->getWidth() || this->windowHeight_ != pRenderWindow->getHeight()) |
---|
| 108 | { |
---|
| 109 | // Update current window size |
---|
| 110 | this->windowWidth_ = pRenderWindow->getWidth(); |
---|
| 111 | this->windowHeight_ = pRenderWindow->getHeight(); |
---|
[7076] | 112 | |
---|
[8232] | 113 | // Create temporary texture |
---|
[8351] | 114 | this->tempTexture_ = Ogre::TextureManager::getSingleton().createManual("ScreenShotTex", |
---|
| 115 | Resource::getDefaultResourceGroup(), Ogre::TEX_TYPE_2D, this->windowWidth_, |
---|
| 116 | this->windowHeight_, 0, Ogre::PF_B8G8R8, Ogre::TU_RENDERTARGET); |
---|
[7015] | 117 | |
---|
[8232] | 118 | // Get the current render target of the temporary texture |
---|
| 119 | this->renderTarget_ = this->tempTexture_->getBuffer()->getRenderTarget(); |
---|
[7015] | 120 | |
---|
[8232] | 121 | // HardwarePixelBufferSharedPtr to the buffer of the temporary texture |
---|
| 122 | this->buffer_ = this->tempTexture_->getBuffer(); |
---|
[7015] | 123 | |
---|
[8232] | 124 | // Create PixelBox |
---|
| 125 | this->data_ = new uint8_t[(this->windowWidth_ * this->gridSize_) * (this->windowHeight_ * this->gridSize_) * 3]; |
---|
| 126 | this->finalPicturePB_ = Ogre::PixelBox(this->windowWidth_ * this->gridSize_, this->windowHeight_ * this->gridSize_, 1, Ogre::PF_B8G8R8, this->data_); |
---|
| 127 | } |
---|
[7015] | 128 | } |
---|
| 129 | |
---|
| 130 | |
---|
[8232] | 131 | /** |
---|
| 132 | @brief |
---|
| 133 | Make a screenshot. |
---|
| 134 | The screenshot is saved in the log folder. |
---|
| 135 | */ |
---|
| 136 | void ScreenshotManager::makeScreenshot() |
---|
[7015] | 137 | { |
---|
[8232] | 138 | // Get the screenshot. |
---|
| 139 | Ogre::Image* finalImage = getScreenshot(); |
---|
| 140 | if(finalImage != NULL) |
---|
| 141 | { |
---|
| 142 | // Save it. |
---|
| 143 | finalImage->save(PathConfig::getInstance().getLogPathString() + "screenshot_" + getTimestamp() + "." + this->fileExtension_); |
---|
| 144 | delete finalImage; |
---|
| 145 | COUT(3) << "Finished taking " << this->gridSize_*this->windowWidth_ << "x" << this->gridSize_*this->windowHeight_ << " pixel HD screenshot. Storing in log/." << endl; |
---|
| 146 | } |
---|
| 147 | else |
---|
| 148 | { |
---|
| 149 | COUT(1) << "There needs to be an active camera to make screenshots." << endl; |
---|
| 150 | return; |
---|
| 151 | } |
---|
[7015] | 152 | } |
---|
| 153 | |
---|
[8232] | 154 | /** |
---|
| 155 | @brief |
---|
| 156 | Creates a screenshot and returns it. |
---|
| 157 | @return |
---|
| 158 | Returns a pointer to an Ogre::Image with the screenshot. |
---|
| 159 | */ |
---|
| 160 | Ogre::Image* ScreenshotManager::getScreenshot() |
---|
| 161 | { |
---|
| 162 | if(CameraManager::getInstance().getActiveCamera() == NULL ) |
---|
| 163 | return NULL; |
---|
| 164 | return this->getScreenshot(CameraManager::getInstance().getActiveCamera()->getOgreCamera()); |
---|
| 165 | } |
---|
[7015] | 166 | |
---|
[8079] | 167 | /** |
---|
| 168 | @brief |
---|
[8232] | 169 | Creates a screenshot with the given camera and returns it. |
---|
| 170 | @param camera |
---|
| 171 | A pointer to the camera the screenshot should be taken with. |
---|
| 172 | @return |
---|
| 173 | Returns a pointer to an Ogre::Image with the screenshot. |
---|
[7015] | 174 | */ |
---|
[8232] | 175 | Ogre::Image* ScreenshotManager::getScreenshot(Ogre::Camera* camera) |
---|
[7015] | 176 | { |
---|
[8232] | 177 | if(camera == NULL) |
---|
| 178 | return NULL; |
---|
| 179 | |
---|
| 180 | // Update the internal parameters. |
---|
| 181 | this->update(); |
---|
[7015] | 182 | |
---|
[8232] | 183 | // Add the camera as viewport. |
---|
| 184 | this->renderTarget_->removeAllViewports(); |
---|
| 185 | this->renderTarget_->addViewport(camera); |
---|
[7076] | 186 | |
---|
[8232] | 187 | // Set the viewport settings |
---|
| 188 | Ogre::Viewport *vp = renderTarget_->getViewport(0); |
---|
[7076] | 189 | vp->setClearEveryFrame(true); |
---|
[7015] | 190 | vp->setOverlaysEnabled(false); |
---|
| 191 | |
---|
[8232] | 192 | // Remind current overlay flag |
---|
[7015] | 193 | bool enableOverlayFlag = GraphicsManager::getInstance().getViewport()->getOverlaysEnabled(); |
---|
[8232] | 194 | |
---|
| 195 | // We disable overlay rendering if it is set in config file and the viewport setting is enabled |
---|
| 196 | if(this->disableOverlays_ && enableOverlayFlag) |
---|
[7015] | 197 | GraphicsManager::getInstance().getViewport()->setOverlaysEnabled(false); |
---|
[8232] | 198 | |
---|
| 199 | Ogre::Image* finalImage = new Ogre::Image(); |
---|
| 200 | |
---|
| 201 | if(this->gridSize_ <= 1) |
---|
[7015] | 202 | { |
---|
| 203 | // Simple case where the contents of the screen are taken directly |
---|
| 204 | // Also used when an invalid value is passed within gridSize (zero or negative grid size) |
---|
[8232] | 205 | this->renderTarget_->update(); // Render |
---|
| 206 | |
---|
| 207 | finalImage = &finalImage->loadDynamicImage(static_cast<unsigned char*>(finalPicturePB_.data), finalPicturePB_.getWidth(), finalPicturePB_.getHeight(),Ogre::PF_B8G8R8); |
---|
[7015] | 208 | } |
---|
| 209 | else |
---|
| 210 | { |
---|
[8232] | 211 | // Define the original frustum extents variables |
---|
[7015] | 212 | Ogre::Real originalFrustumLeft, originalFrustumRight, originalFrustumTop, originalFrustumBottom; |
---|
[8232] | 213 | // Set the original Frustum extents |
---|
[7015] | 214 | camera->getFrustumExtents(originalFrustumLeft, originalFrustumRight, originalFrustumTop, originalFrustumBottom); |
---|
[8232] | 215 | |
---|
| 216 | // Compute the Stepsize for the grid |
---|
| 217 | Ogre::Real frustumGridStepHorizontal = (originalFrustumRight * 2) / this->gridSize_; |
---|
| 218 | Ogre::Real frustumGridStepVertical = (originalFrustumTop * 2) / this->gridSize_; |
---|
| 219 | |
---|
| 220 | // Process each grid |
---|
[7015] | 221 | Ogre::Real frustumLeft, frustumRight, frustumTop, frustumBottom; |
---|
[8232] | 222 | for (unsigned int nbScreenshots = 0; nbScreenshots < this->gridSize_ * this->gridSize_; nbScreenshots++) |
---|
[7076] | 223 | { |
---|
[8232] | 224 | int y = nbScreenshots / this->gridSize_; |
---|
| 225 | int x = nbScreenshots - y * this->gridSize_; |
---|
| 226 | |
---|
[7015] | 227 | // Shoggoth frustum extents setting |
---|
[8232] | 228 | // Compute the new frustum extents |
---|
[7039] | 229 | frustumLeft = originalFrustumLeft + frustumGridStepHorizontal * x; |
---|
| 230 | frustumRight = frustumLeft + frustumGridStepHorizontal; |
---|
| 231 | frustumTop = originalFrustumTop - frustumGridStepVertical * y; |
---|
| 232 | frustumBottom = frustumTop - frustumGridStepVertical; |
---|
[8232] | 233 | |
---|
| 234 | // Set the frustum extents value to the camera |
---|
[7015] | 235 | camera->setFrustumExtents(frustumLeft, frustumRight, frustumTop, frustumBottom); |
---|
[8232] | 236 | |
---|
| 237 | // Ignore time duration between frames |
---|
[7015] | 238 | Ogre::Root::getSingletonPtr()->clearEventTimes(); |
---|
[8232] | 239 | this->renderTarget_->update(); // Render |
---|
| 240 | |
---|
| 241 | // Define the current box |
---|
| 242 | Ogre::Box subBox = Ogre::Box(x* this->windowWidth_,y * this->windowHeight_,x * this->windowWidth_ + this->windowWidth_, y * this->windowHeight_ + this->windowHeight_); |
---|
| 243 | // Copy the content from the temp buffer into the final picture PixelBox |
---|
| 244 | // Place the tempBuffer content at the right position |
---|
| 245 | this->buffer_->blitToMemory(this->finalPicturePB_.getSubVolume(subBox)); |
---|
| 246 | |
---|
| 247 | COUT(4) << "Created screenshot number " << nbScreenshots << " for multi grid HD screenshot." << endl; |
---|
| 248 | |
---|
[7015] | 249 | } |
---|
[8232] | 250 | |
---|
| 251 | // Set frustum extents to previous settings |
---|
[7015] | 252 | camera->resetFrustumExtents(); |
---|
[8232] | 253 | |
---|
| 254 | // Insert the PixelBox data into the Image Object |
---|
| 255 | finalImage->loadDynamicImage(static_cast<unsigned char*>(this->finalPicturePB_.data), this->finalPicturePB_.getWidth(), this->finalPicturePB_.getHeight(), 1, Ogre::PF_B8G8R8, false); |
---|
[7015] | 256 | } |
---|
[8232] | 257 | |
---|
| 258 | // Do we have to re-enable our overlays? |
---|
[7015] | 259 | if(enableOverlayFlag) |
---|
| 260 | GraphicsManager::getInstance().getViewport()->setOverlaysEnabled(true); |
---|
[8232] | 261 | |
---|
| 262 | // Reset time since last frame to pause the scene |
---|
[7015] | 263 | Ogre::Root::getSingletonPtr()->clearEventTimes(); |
---|
[8232] | 264 | |
---|
| 265 | return finalImage; |
---|
[7015] | 266 | } |
---|
| 267 | |
---|
[8079] | 268 | /** |
---|
| 269 | @brief |
---|
| 270 | Set the size of the grid. |
---|
| 271 | @param size |
---|
| 272 | The size of the grid. |
---|
| 273 | */ |
---|
| 274 | void ScreenshotManager::setGridSize(unsigned int size) |
---|
| 275 | { |
---|
[8232] | 276 | if(size == this->gridSize_) |
---|
[8079] | 277 | return; |
---|
| 278 | |
---|
[8232] | 279 | this->gridSize_ = size; |
---|
[8079] | 280 | // New PixelBox for the changed size. |
---|
[8232] | 281 | this->data_ = new uint8_t[(this->windowWidth_ * this->gridSize_) * (this->windowHeight_ * this->gridSize_) * 3]; |
---|
| 282 | this->finalPicturePB_ = Ogre::PixelBox(this->windowWidth_ * this->gridSize_, this->windowHeight_ * this->gridSize_, 1, Ogre::PF_B8G8R8, this->data_); |
---|
[8079] | 283 | } |
---|
| 284 | |
---|
[7015] | 285 | } |
---|