Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/modules/designtools/ScreenshotManager.cc @ 7022

Last change on this file since 7022 was 7015, checked in by scheusso, 14 years ago

adding ScreenshotManager in order to be able to make high-resolution screenshots (not testet or embedded yet, but compiles)

File size: 6.1 KB
Line 
1/* COPYRIGHT: this code comes from http://www.ogre3d.org/wiki/index.php/High_resolution_screenshots */
2
3//#include <stdafx.h>
4#include "ScreenshotManager.h"
5#include <OgreRenderWindow.h>
6#include <OgreViewport.h>
7#include <OgreRenderTexture.h>
8#include <OgreCamera.h>
9#include <OgreRoot.h>
10
11#include "core/GraphicsManager.h"
12
13//using namespace Ogre;
14
15namespace orxonox
16{
17
18    ScreenshotManager::ScreenshotManager(Ogre::RenderWindow* pRenderWindow, int gridSize, std::string fileExtension, bool overlayFlag)
19    {
20        //set file extension for the Screenshot files
21        mFileExtension = fileExtension;
22        // the gridsize
23        mGridSize         = gridSize;
24        // flag for overlay rendering
25        mDisableOverlays  = overlayFlag;
26        //get current window size
27        mWindowWidth     = pRenderWindow->getWidth();
28        mWindowHeight    = pRenderWindow->getHeight();
29        //create temporary texture
30        mTempTex = Ogre::TextureManager::getSingleton().createManual("ScreenShotTex", 
31                                                                  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D,
32                                                                    mWindowWidth, mWindowHeight,0, Ogre::PF_B8G8R8, Ogre::TU_RENDERTARGET); 
33       
34        //get The current Render Target of the temp Texture
35        mRT = mTempTex->getBuffer()->getRenderTarget();
36
37        //HardwarePixelBufferSharedPtr to the Buffer of the temp Texture
38        mBuffer = mTempTex->getBuffer();
39
40        //create PixelBox
41            uint8_t* data_ = new uint8_t[(mWindowWidth * mGridSize) * (mWindowHeight * mGridSize) * 3];
42        mFinalPicturePB = Ogre::PixelBox(mWindowWidth * mGridSize,mWindowHeight * mGridSize,1,Ogre::PF_B8G8R8,data_);
43
44    }
45
46
47    ScreenshotManager::~ScreenshotManager()
48    {
49      delete[] data_;
50    }
51
52
53    /* Creates a screenshot with the given camera.
54    * @param camera Pointer to the camera "looking at" the scene of interest
55    * @param fileName the filename of the screenshot file.
56    */
57    void ScreenshotManager::makeScreenshot(Ogre::Camera* camera, std::string fileName) const
58    {
59
60        //Remove all viewports, so the added Viewport(camera) ist the only
61        mRT->removeAllViewports();
62        mRT->addViewport(camera);
63               
64        //set the viewport settings
65        Ogre::Viewport *vp = mRT->getViewport(0);
66        vp->setClearEveryFrame(true);   
67        vp->setOverlaysEnabled(false);
68
69        // remind current overlay flag
70        bool enableOverlayFlag = GraphicsManager::getInstance().getViewport()->getOverlaysEnabled();
71
72        // we disable overlay rendering if it is set in config file and the viewport setting is enabled
73        if(mDisableOverlays && enableOverlayFlag)
74            GraphicsManager::getInstance().getViewport()->setOverlaysEnabled(false);
75
76        if(mGridSize <= 1)
77        {
78            // Simple case where the contents of the screen are taken directly
79            // Also used when an invalid value is passed within gridSize (zero or negative grid size)
80            mRT->update();              //render
81
82            //write the file on the Harddisk
83            mRT->writeContentsToFile(fileName + "." + mFileExtension);
84        }
85        else
86        {
87            //define the original frustum extents variables
88            Ogre::Real originalFrustumLeft, originalFrustumRight, originalFrustumTop, originalFrustumBottom;
89            // set the original Frustum extents
90            camera->getFrustumExtents(originalFrustumLeft, originalFrustumRight, originalFrustumTop, originalFrustumBottom);
91           
92            // compute the Stepsize for the drid
93            Ogre::Real frustumGridStepHorizontal        = (originalFrustumRight * 2) / mGridSize;
94            Ogre::Real frustumGridStepVertical  = (originalFrustumTop * 2) / mGridSize;
95
96            // process each grid
97            Ogre::Real frustumLeft, frustumRight, frustumTop, frustumBottom;
98            for (unsigned int nbScreenshots = 0; nbScreenshots < mGridSize * mGridSize; nbScreenshots++) 
99            { 
100                int y = nbScreenshots / mGridSize; 
101                int x = nbScreenshots - y * mGridSize; 
102               
103                // Shoggoth frustum extents setting
104                // compute the new frustum extents
105                frustumLeft             = originalFrustumLeft + frustumGridStepHorizontal * x;
106                frustumRight    = frustumLeft + frustumGridStepHorizontal;
107                frustumTop              = originalFrustumTop - frustumGridStepVertical * y;
108                frustumBottom   = frustumTop - frustumGridStepVertical;
109               
110                // set the frustum extents value to the camera
111                camera->setFrustumExtents(frustumLeft, frustumRight, frustumTop, frustumBottom);
112
113                // ignore time duration between frames
114                Ogre::Root::getSingletonPtr()->clearEventTimes();
115                mRT->update();          //render
116               
117                //define the current
118                Ogre::Box subBox = Ogre::Box(x* mWindowWidth,y * mWindowHeight,x * mWindowWidth + mWindowWidth, y * mWindowHeight + mWindowHeight);
119                //copy the content from the temp buffer into the final picture PixelBox
120                //Place the tempBuffer content at the right position
121                mBuffer->blitToMemory(mFinalPicturePB.getSubVolume(subBox));
122
123            }
124           
125            // set frustum extents to previous settings
126            camera->resetFrustumExtents();
127           
128            Ogre::Image finalImage; //declare the final Image Object
129            //insert the PixelBox data into the Image Object
130            finalImage = finalImage.loadDynamicImage(static_cast<unsigned char*>(mFinalPicturePB.data), mFinalPicturePB.getWidth(),mFinalPicturePB.getHeight(),Ogre::PF_B8G8R8);
131            // Save the Final image to a file
132            finalImage.save(fileName + "." + mFileExtension);
133           
134        }
135
136        // do we have to re-enable our overlays?
137        if(enableOverlayFlag)
138            GraphicsManager::getInstance().getViewport()->setOverlaysEnabled(true);
139
140
141        // reset time since last frame to pause the scene
142        Ogre::Root::getSingletonPtr()->clearEventTimes();
143    }
144
145}
Note: See TracBrowser for help on using the repository browser.