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