Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8680 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
Line 
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 */
29
30/**
31   @file ScreenshotManager.cc
32   @brief Implementation of the ScreenshotManager class.
33   @ingroup Designtools
34*/
35
36#include "ScreenshotManager.h"
37
38#include <OgreCamera.h>
39#include <OgreRenderTexture.h>
40#include <OgreRenderWindow.h>
41#include <OgreRoot.h>
42#include <OgreViewport.h>
43// #include <X11/Xlib.h> TODO: Needed?
44
45#include "core/ConfigValueIncludes.h"
46#include "core/GraphicsManager.h"
47#include "core/PathConfig.h"
48#include "core/Resource.h"
49#include "core/command/ConsoleCommand.h"
50#include "util/ScopedSingletonManager.h"
51#include "util/StringUtils.h"
52
53#include "CameraManager.h"
54#include "graphics/Camera.h"
55
56namespace orxonox
57{
58
59    SetConsoleCommand("printScreenHD", &ScreenshotManager::makeScreenshot_s);
60   
61    ManageScopedSingleton(ScreenshotManager, ScopeID::Graphics, false);
62
63    /**
64    @brief
65        Constructor.
66    */
67    ScreenshotManager::ScreenshotManager()
68    {
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    {
103        Ogre::RenderWindow* pRenderWindow = GraphicsManager::getInstance().getRenderWindow();
104
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();
111
112            // Create temporary texture
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);
116
117            // Get the current render target of the temporary texture
118            this->renderTarget_ = this->tempTexture_->getBuffer()->getRenderTarget();
119
120            // HardwarePixelBufferSharedPtr to the buffer of the temporary texture
121            this->buffer_ = this->tempTexture_->getBuffer();
122
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        }
127    }
128
129
130    /**
131    @brief
132        Make a screenshot.
133        The screenshot is saved in the log folder.
134    */
135    void ScreenshotManager::makeScreenshot()
136    {
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        }
151    }
152
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    }
165
166    /**
167    @brief
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.
173    */
174    Ogre::Image* ScreenshotManager::getScreenshot(Ogre::Camera* camera)
175    {
176        if(camera == NULL)
177            return NULL;
178       
179        // Update the internal parameters.
180        this->update();
181
182        // Add the camera as viewport.
183        this->renderTarget_->removeAllViewports();
184        this->renderTarget_->addViewport(camera);
185
186        // Set the viewport settings
187        Ogre::Viewport *vp = renderTarget_->getViewport(0);
188        vp->setClearEveryFrame(true);
189        vp->setOverlaysEnabled(false);
190
191        // Remind current overlay flag
192        bool enableOverlayFlag = GraphicsManager::getInstance().getViewport()->getOverlaysEnabled();
193       
194        // We disable overlay rendering if it is set in config file and the viewport setting is enabled
195        if(this->disableOverlays_ && enableOverlayFlag)
196            GraphicsManager::getInstance().getViewport()->setOverlaysEnabled(false);
197       
198        Ogre::Image* finalImage = new Ogre::Image();
199       
200        if(this->gridSize_ <= 1)
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)
204            this->renderTarget_->update(); // Render
205           
206            finalImage = &finalImage->loadDynamicImage(static_cast<unsigned char*>(finalPicturePB_.data), finalPicturePB_.getWidth(), finalPicturePB_.getHeight(),Ogre::PF_B8G8R8);
207        }
208        else
209        {
210            // Define the original frustum extents variables
211            Ogre::Real originalFrustumLeft, originalFrustumRight, originalFrustumTop, originalFrustumBottom;
212            // Set the original Frustum extents
213            camera->getFrustumExtents(originalFrustumLeft, originalFrustumRight, originalFrustumTop, originalFrustumBottom);
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
220            Ogre::Real frustumLeft, frustumRight, frustumTop, frustumBottom;
221            for (unsigned int nbScreenshots = 0; nbScreenshots < this->gridSize_ * this->gridSize_; nbScreenshots++)
222            {
223                int y = nbScreenshots / this->gridSize_;
224                int x = nbScreenshots - y * this->gridSize_;
225               
226                // Shoggoth frustum extents setting
227                // Compute the new frustum extents
228                frustumLeft    = originalFrustumLeft + frustumGridStepHorizontal * x;
229                frustumRight  = frustumLeft + frustumGridStepHorizontal;
230                frustumTop    = originalFrustumTop - frustumGridStepVertical * y;
231                frustumBottom  = frustumTop - frustumGridStepVertical;
232               
233                // Set the frustum extents value to the camera
234                camera->setFrustumExtents(frustumLeft, frustumRight, frustumTop, frustumBottom);
235               
236                // Ignore time duration between frames
237                Ogre::Root::getSingletonPtr()->clearEventTimes();
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               
248            }
249           
250            // Set frustum extents to previous settings
251            camera->resetFrustumExtents();
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);
255        }
256       
257        // Do we have to re-enable our overlays?
258        if(enableOverlayFlag)
259            GraphicsManager::getInstance().getViewport()->setOverlaysEnabled(true);
260       
261        // Reset time since last frame to pause the scene
262        Ogre::Root::getSingletonPtr()->clearEventTimes();
263       
264        return finalImage;
265    }
266
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    {
275        if(size == this->gridSize_)
276            return;
277
278        this->gridSize_ = size;
279        // New PixelBox for the changed size.
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_);
282    }
283
284}
Note: See TracBrowser for help on using the repository browser.