Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/usability/src/modules/designtools/ScreenshotManager.cc @ 8076

Last change on this file since 8076 was 8076, checked in by dafrick, 13 years ago

Adding screenshot to KeybindMenu and making "size" of screenhsot generated by printScreenshotHD adjustable.

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