[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" |
---|
[8081] | 14 | #include "core/Resource.h" |
---|
[7284] | 15 | #include "core/command/ConsoleCommand.h" |
---|
[7015] | 16 | |
---|
[7041] | 17 | #include "CameraManager.h" |
---|
| 18 | #include "graphics/Camera.h" |
---|
[7015] | 19 | |
---|
| 20 | namespace orxonox |
---|
| 21 | { |
---|
[7041] | 22 | ManageScopedSingleton(ScreenshotManager, ScopeID::Graphics, false); |
---|
[7284] | 23 | SetConsoleCommand("printScreenHD", &ScreenshotManager::makeScreenshot_s); |
---|
[7015] | 24 | |
---|
[7041] | 25 | ScreenshotManager::ScreenshotManager() |
---|
[7015] | 26 | { |
---|
[7041] | 27 | Ogre::RenderWindow* pRenderWindow = GraphicsManager::getInstance().getRenderWindow(); |
---|
| 28 | int gridSize = 3; |
---|
| 29 | std::string fileExtension = ".png"; |
---|
| 30 | bool overlayFlag = true; |
---|
| 31 | |
---|
[7015] | 32 | //set file extension for the Screenshot files |
---|
[7041] | 33 | mFileExtension = fileExtension; |
---|
[7015] | 34 | // the gridsize |
---|
[7041] | 35 | mGridSize = gridSize; |
---|
[7015] | 36 | // flag for overlay rendering |
---|
[7041] | 37 | mDisableOverlays = overlayFlag; |
---|
[7015] | 38 | //get current window size |
---|
[7039] | 39 | mWindowWidth = pRenderWindow->getWidth(); |
---|
| 40 | mWindowHeight = pRenderWindow->getHeight(); |
---|
[7015] | 41 | //create temporary texture |
---|
[7076] | 42 | mTempTex = Ogre::TextureManager::getSingleton().createManual("ScreenShotTex", |
---|
[8081] | 43 | Resource::getDefaultResourceGroup(), Ogre::TEX_TYPE_2D, |
---|
| 44 | mWindowWidth, mWindowHeight,0, Ogre::PF_B8G8R8, Ogre::TU_RENDERTARGET); |
---|
[7076] | 45 | |
---|
[7015] | 46 | //get The current Render Target of the temp Texture |
---|
| 47 | mRT = mTempTex->getBuffer()->getRenderTarget(); |
---|
| 48 | |
---|
[7076] | 49 | //HardwarePixelBufferSharedPtr to the Buffer of the temp Texture |
---|
[7015] | 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 | { |
---|
[7041] | 61 | // Don't delete data_. Somehow this pointer points anywhere but to memory location. |
---|
| 62 | //delete[] data_; |
---|
[7015] | 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 | */ |
---|
[7041] | 70 | void ScreenshotManager::makeScreenshot() const |
---|
[7015] | 71 | { |
---|
[7041] | 72 | Ogre::Camera* camera = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
| 73 | std::string fileName = PathConfig::getInstance().getLogPathString() + "screenshot_" + this->getTimestamp(); |
---|
[7015] | 74 | |
---|
[7076] | 75 | //Remove all viewports, so the added Viewport(camera) ist the only |
---|
[7015] | 76 | mRT->removeAllViewports(); |
---|
| 77 | mRT->addViewport(camera); |
---|
[7076] | 78 | |
---|
[7015] | 79 | //set the viewport settings |
---|
| 80 | Ogre::Viewport *vp = mRT->getViewport(0); |
---|
[7076] | 81 | vp->setClearEveryFrame(true); |
---|
[7015] | 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) |
---|
[7039] | 95 | mRT->update(); //render |
---|
[7015] | 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); |
---|
[7076] | 106 | |
---|
[7015] | 107 | // compute the Stepsize for the drid |
---|
[7039] | 108 | Ogre::Real frustumGridStepHorizontal = (originalFrustumRight * 2) / mGridSize; |
---|
| 109 | Ogre::Real frustumGridStepVertical = (originalFrustumTop * 2) / mGridSize; |
---|
[7015] | 110 | |
---|
| 111 | // process each grid |
---|
| 112 | Ogre::Real frustumLeft, frustumRight, frustumTop, frustumBottom; |
---|
[7076] | 113 | for (unsigned int nbScreenshots = 0; nbScreenshots < mGridSize * mGridSize; nbScreenshots++) |
---|
| 114 | { |
---|
| 115 | int y = nbScreenshots / mGridSize; |
---|
| 116 | int x = nbScreenshots - y * mGridSize; |
---|
| 117 | |
---|
[7015] | 118 | // Shoggoth frustum extents setting |
---|
| 119 | // compute the new frustum extents |
---|
[7039] | 120 | frustumLeft = originalFrustumLeft + frustumGridStepHorizontal * x; |
---|
| 121 | frustumRight = frustumLeft + frustumGridStepHorizontal; |
---|
| 122 | frustumTop = originalFrustumTop - frustumGridStepVertical * y; |
---|
| 123 | frustumBottom = frustumTop - frustumGridStepVertical; |
---|
[7076] | 124 | |
---|
[7015] | 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(); |
---|
[7039] | 130 | mRT->update(); //render |
---|
[7076] | 131 | |
---|
| 132 | //define the current |
---|
[7015] | 133 | Ogre::Box subBox = Ogre::Box(x* mWindowWidth,y * mWindowHeight,x * mWindowWidth + mWindowWidth, y * mWindowHeight + mWindowHeight); |
---|
[7076] | 134 | //copy the content from the temp buffer into the final picture PixelBox |
---|
[7015] | 135 | //Place the tempBuffer content at the right position |
---|
| 136 | mBuffer->blitToMemory(mFinalPicturePB.getSubVolume(subBox)); |
---|
| 137 | |
---|
| 138 | } |
---|
[7076] | 139 | |
---|
[7015] | 140 | // set frustum extents to previous settings |
---|
| 141 | camera->resetFrustumExtents(); |
---|
[7076] | 142 | |
---|
[7015] | 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); |
---|
[7076] | 148 | |
---|
[7015] | 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 | |
---|
[7041] | 160 | std::string ScreenshotManager::getTimestamp() |
---|
| 161 | { |
---|
[7129] | 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; |
---|
[7041] | 172 | return oss.str(); |
---|
| 173 | } |
---|
| 174 | |
---|
[7015] | 175 | } |
---|