Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/kicklib/src/modules/designtools/ScreenshotManager.cc @ 8081

Last change on this file since 8081 was 8081, checked in by rgrieder, 13 years ago

Fixed serious static initialisation problem. And I thought we caught them all…
Also replaced remaining references to Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP with our own code.

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