Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/kicklib2/src/modules/designtools/ScreenshotManager.cc @ 8501

Last change on this file since 8501 was 8288, checked in by rgrieder, 14 years ago

Resource::DEFAULT_GROUP has been replaced and Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME should not be used anymore in Orxonox.

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