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