Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/designtools/ScreenshotManager.cc @ 8222

Last change on this file since 8222 was 8108, checked in by dafrick, 14 years ago

Merging changes from tetris branch into trunk, since they are also useful, there.

  • Property svn:eol-style set to native
File size: 7.8 KB
RevLine 
[7015]1/* COPYRIGHT: this code comes from http://www.ogre3d.org/wiki/index.php/High_resolution_screenshots */
2
3#include "ScreenshotManager.h"
[7041]4
[7015]5#include <OgreRenderWindow.h>
6#include <OgreViewport.h>
7#include <OgreRenderTexture.h>
8#include <OgreCamera.h>
9#include <OgreRoot.h>
10
[7284]11#include "util/ScopedSingletonManager.h"
[7015]12#include "core/GraphicsManager.h"
[7041]13#include "core/PathConfig.h"
[7284]14#include "core/command/ConsoleCommand.h"
[7015]15
[7041]16#include "CameraManager.h"
17#include "graphics/Camera.h"
[7015]18
19namespace orxonox
20{
[7041]21    ManageScopedSingleton(ScreenshotManager, ScopeID::Graphics, false);
[7284]22    SetConsoleCommand("printScreenHD", &ScreenshotManager::makeScreenshot_s);
[7015]23
[7041]24    ScreenshotManager::ScreenshotManager()
[7015]25    {
[7041]26        Ogre::RenderWindow* pRenderWindow = GraphicsManager::getInstance().getRenderWindow();
27
[7015]28        //set file extension for the Screenshot files
[8079]29        this->mFileExtension_  = ".png";
[7015]30        // the gridsize
[8079]31        this->mGridSize_ = 3;
[7015]32        // flag for overlay rendering
[8079]33        this->mDisableOverlays_ = true;
[7015]34        //get current window size
[8079]35        this->mWindowWidth_   = pRenderWindow->getWidth();
36        this->mWindowHeight_  = pRenderWindow->getHeight();
[7015]37        //create temporary texture
[8079]38        this->mTempTex_ = Ogre::TextureManager::getSingleton().createManual("ScreenShotTex", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, this->mWindowWidth_, this->mWindowHeight_, 0, Ogre::PF_B8G8R8, Ogre::TU_RENDERTARGET);
[7076]39
[7015]40        //get The current Render Target of the temp Texture
[8079]41        this->mRT_ = this->mTempTex_->getBuffer()->getRenderTarget();
[7015]42
[7076]43        //HardwarePixelBufferSharedPtr to the Buffer of the temp Texture
[8079]44        this->mBuffer_ = this->mTempTex_->getBuffer();
[7015]45
46        //create PixelBox
[8079]47        uint8_t* data_ = new uint8_t[(this->mWindowWidth_ * this->mGridSize_) * (this->mWindowHeight_ * this->mGridSize_) * 3];
48        this->mFinalPicturePB_ = Ogre::PixelBox(this->mWindowWidth_ * this->mGridSize_, this->mWindowHeight_ * this->mGridSize_, 1, Ogre::PF_B8G8R8, data_);
[7015]49
50    }
51
52
53    ScreenshotManager::~ScreenshotManager()
54    {
[7041]55        // Don't delete data_. Somehow this pointer points anywhere but to memory location.
56        //delete[] data_;
[7015]57    }
58
59
[8079]60    /**
61    @brief
62        Creates a screenshot with the given camera.
[7015]63    */
[7041]64    void ScreenshotManager::makeScreenshot() const
[7015]65    {
[7041]66        Ogre::Camera* camera = CameraManager::getInstance().getActiveCamera()->getOgreCamera();
67        std::string fileName = PathConfig::getInstance().getLogPathString() + "screenshot_" + this->getTimestamp();
[7015]68
[7076]69        //Remove all viewports, so the added Viewport(camera) ist the only
[8079]70        mRT_->removeAllViewports();
71        mRT_->addViewport(camera);
[7076]72
[7015]73        //set the viewport settings
[8079]74        Ogre::Viewport *vp = mRT_->getViewport(0);
[7076]75        vp->setClearEveryFrame(true);
[7015]76        vp->setOverlaysEnabled(false);
77
78        // remind current overlay flag
79        bool enableOverlayFlag = GraphicsManager::getInstance().getViewport()->getOverlaysEnabled();
80
81        // we disable overlay rendering if it is set in config file and the viewport setting is enabled
[8079]82        if(mDisableOverlays_ && enableOverlayFlag)
[7015]83            GraphicsManager::getInstance().getViewport()->setOverlaysEnabled(false);
84
[8079]85        if(mGridSize_ <= 1)
[7015]86        {
87            // Simple case where the contents of the screen are taken directly
88            // Also used when an invalid value is passed within gridSize (zero or negative grid size)
[8079]89            mRT_->update();    //render
[7015]90
91            //write the file on the Harddisk
[8079]92            mRT_->writeContentsToFile(fileName + "." + mFileExtension_);
[7015]93        }
94        else
95        {
96            //define the original frustum extents variables
97            Ogre::Real originalFrustumLeft, originalFrustumRight, originalFrustumTop, originalFrustumBottom;
98            // set the original Frustum extents
99            camera->getFrustumExtents(originalFrustumLeft, originalFrustumRight, originalFrustumTop, originalFrustumBottom);
[7076]100
[7015]101            // compute the Stepsize for the drid
[8079]102            Ogre::Real frustumGridStepHorizontal  = (originalFrustumRight * 2) / mGridSize_;
103            Ogre::Real frustumGridStepVertical  = (originalFrustumTop * 2) / mGridSize_;
[7015]104
105            // process each grid
106            Ogre::Real frustumLeft, frustumRight, frustumTop, frustumBottom;
[8079]107            for (unsigned int nbScreenshots = 0; nbScreenshots < mGridSize_ * mGridSize_; nbScreenshots++)
[7076]108            {
[8079]109                int y = nbScreenshots / mGridSize_;
110                int x = nbScreenshots - y * mGridSize_;
[7076]111
[7015]112                // Shoggoth frustum extents setting
113                // compute the new frustum extents
[7039]114                frustumLeft    = originalFrustumLeft + frustumGridStepHorizontal * x;
115                frustumRight  = frustumLeft + frustumGridStepHorizontal;
116                frustumTop    = originalFrustumTop - frustumGridStepVertical * y;
117                frustumBottom  = frustumTop - frustumGridStepVertical;
[7076]118
[7015]119                // set the frustum extents value to the camera
120                camera->setFrustumExtents(frustumLeft, frustumRight, frustumTop, frustumBottom);
121
122                // ignore time duration between frames
123                Ogre::Root::getSingletonPtr()->clearEventTimes();
[8079]124                mRT_->update();    //render
[7076]125
126                //define the current
[8079]127                Ogre::Box subBox = Ogre::Box(x* mWindowWidth_,y * mWindowHeight_,x * mWindowWidth_ + mWindowWidth_, y * mWindowHeight_ + mWindowHeight_);
[7076]128                //copy the content from the temp buffer into the final picture PixelBox
[7015]129                //Place the tempBuffer content at the right position
[8079]130                mBuffer_->blitToMemory(mFinalPicturePB_.getSubVolume(subBox));
[7015]131
132            }
[7076]133
[7015]134            // set frustum extents to previous settings
135            camera->resetFrustumExtents();
[7076]136
[7015]137            Ogre::Image finalImage; //declare the final Image Object
138            //insert the PixelBox data into the Image Object
[8079]139            finalImage = finalImage.loadDynamicImage(static_cast<unsigned char*>(mFinalPicturePB_.data), mFinalPicturePB_.getWidth(), mFinalPicturePB_.getHeight(),Ogre::PF_B8G8R8);
[7015]140            // Save the Final image to a file
[8079]141            finalImage.save(fileName + "." + mFileExtension_);
[7076]142
[7015]143        }
144
145        // do we have to re-enable our overlays?
146        if(enableOverlayFlag)
147            GraphicsManager::getInstance().getViewport()->setOverlaysEnabled(true);
148
149
150        // reset time since last frame to pause the scene
151        Ogre::Root::getSingletonPtr()->clearEventTimes();
152    }
153
[8079]154    /**
155    @brief
156        Set the size of the grid.
157    @param size
158        The size of the grid.
159    */
160    void ScreenshotManager::setGridSize(unsigned int size)
161    {
162        if(size == this->mGridSize_)
163            return;
164
165        this->mGridSize_ = size;
166        // New PixelBox for the changed size.
167        uint8_t* data_ = new uint8_t[(this->mWindowWidth_ * this->mGridSize_) * (this->mWindowHeight_ * this->mGridSize_) * 3];
168        this->mFinalPicturePB_ = Ogre::PixelBox(this->mWindowWidth_ * this->mGridSize_, this->mWindowHeight_ * this->mGridSize_, 1, Ogre::PF_B8G8R8, data_);
169    }
170
171    /**
172    @brief
173        Get a timestamp for the curent time instant.
174    @return
175        Returns a string with the timestamp.
176    */
[7041]177    std::string ScreenshotManager::getTimestamp()
178    {
[7129]179        struct tm *pTime;
180        time_t ctTime; time(&ctTime);
181        pTime = localtime( &ctTime );
182        std::ostringstream oss;
183        oss << std::setw(2) << std::setfill('0') << (pTime->tm_mon + 1)
184            << std::setw(2) << std::setfill('0') << pTime->tm_mday
185            << std::setw(2) << std::setfill('0') << (pTime->tm_year + 1900)
186            << "_" << std::setw(2) << std::setfill('0') << pTime->tm_hour
187            << std::setw(2) << std::setfill('0') << pTime->tm_min
188            << std::setw(2) << std::setfill('0') << pTime->tm_sec;
[7041]189        return oss.str();
190    }
191
[7015]192}
Note: See TracBrowser for help on using the repository browser.