24 SScreenshotManager::SScreenshotManager(
SViewPort* viewport,
unsigned int width,
unsigned int height,
bool withOverlays,
bool transparentBackground)
29 mEnableOverlays = withOverlays;
30 mTransparentBackground = transparentBackground;
34 mWidthGridSize = (((float)width / (
float)mWindowWidth) > (width / mWindowWidth)) ? (width / mWindowWidth) + 1 : (width / mWindowWidth);
35 mHeightGridSize = (((float)height / (
float)mWindowHeight) > (height / mWindowHeight)) ? (height / mWindowHeight) + 1 : (height / mWindowHeight);
38 Ogre::PixelFormat pformat = (mTransparentBackground) ? Ogre::PF_B8G8R8A8 : Ogre::PF_B8G8R8;
39 mTempTex = Ogre::TextureManager::getSingleton().createManual(mViewport->
GetCamera()->
GetName() +
"/ScreenShotTex", SO3_INTERNAL_RESOURCE_GROUP, Ogre::TEX_TYPE_2D, mWindowWidth, mWindowHeight, 0, pformat, Ogre::TU_RENDERTARGET, 0,
false, 4);
41 mRT = mTempTex->getBuffer()->getRenderTarget();
42 mBuffer = mTempTex->getBuffer();
44 mData =
new uint8[(mWindowWidth * mWidthGridSize) * (mWindowHeight * mHeightGridSize) * Ogre::PixelUtil::getNumElemBytes(mRT->suggestPixelFormat())];
45 mFinalPicturePB = Ogre::PixelBox(mWindowWidth * mWidthGridSize, mWindowHeight * mHeightGridSize, 1, mRT->suggestPixelFormat(), mData);
69 mRT->removeAllViewports();
70 mRT->addViewport(camera);
72 Ogre::Vector2 foffset = camera->getFrustumOffset();
73 Ogre::Real lodBias = camera->getLodBias();
74 bool bmatrix = camera->isCustomProjectionMatrixEnabled();
75 Ogre::Matrix4 cmatrix = camera->getProjectionMatrix();
76 bool autoRatio = camera->getAutoAspectRatio();
77 float prevRatio = camera->getAspectRatio();
78 camera->setAspectRatio((
float)mDestWidth / (
float)mDestHeight);
79 camera->setFrustumOffset(Ogre::Vector2(0.0f, 0.0f));
80 camera->setLodBias(10.0f);
81 camera->setCustomProjectionMatrix(
false);
84 Ogre::Viewport *vp = mRT->getViewport(0);
85 vp->setClearEveryFrame(
true);
87 Ogre::ColourValue bgcolor = (mTransparentBackground) ? Ogre::ColourValue(1.0f, 1.0f, 1.0f, 0.0f) : mViewport->
GetOgreViewPortPointer()->getBackgroundColour();
88 vp->setBackgroundColour(bgcolor);
89 vp->setOverlaysEnabled(mEnableOverlays);
92 if (mWidthGridSize <= 1 && mHeightGridSize <= 1)
98 Ogre::PixelBox pb(mWindowWidth, mWindowHeight, 1, mRT->suggestPixelFormat());
99 pb.data =
new uint8[pb.getConsecutiveSize()];
100 mRT->copyContentsToMemory(pb, pb);
102 Ogre::PixelBox sizedImage = Ogre::PixelBox(mDestWidth, mDestHeight, 1, mRT->suggestPixelFormat());
103 sizedImage.data =
new uint8[sizedImage.getConsecutiveSize()];
104 Ogre::Image::scale(pb, sizedImage);
106 Ogre::Image finalImage;
107 finalImage.loadDynamicImage(
static_cast<unsigned char*
>(sizedImage.data), sizedImage.getWidth(), sizedImage.getHeight(), mRT->suggestPixelFormat());
110 finalImage.save(filename);
112 delete[] sizedImage.data;
117 vp->setOverlaysEnabled(
false);
120 Ogre::RealRect originalFrustum = camera->getFrustumExtents();
123 Ogre::Real frustumGridStepHorizontal = (originalFrustum.right * 2) / mWidthGridSize;
124 Ogre::Real frustumGridStepVertical = (originalFrustum.top * 2) / mHeightGridSize;
127 Ogre::Real frustumLeft, frustumRight, frustumTop, frustumBottom;
129 for (
unsigned int y = 0; y < mHeightGridSize; y++)
131 for (
unsigned int x = 0; x < mWidthGridSize; x++)
134 frustumLeft = originalFrustum.left + frustumGridStepHorizontal * x;
135 frustumRight = frustumLeft + frustumGridStepHorizontal;
136 frustumTop = originalFrustum.top - frustumGridStepVertical * y;
137 frustumBottom = frustumTop - frustumGridStepVertical;
140 camera->setFrustumExtents(frustumLeft, frustumRight, frustumTop, frustumBottom);
147 Ogre::Box subBox = Ogre::Box(x * mWindowWidth, y * mWindowHeight, x * mWindowWidth + mWindowWidth, y * mWindowHeight + mWindowHeight);
151 mBuffer->blitToMemory(mFinalPicturePB.getSubVolume(subBox));
156 camera->resetFrustumExtents();
159 Ogre::PixelBox sizedImage = Ogre::PixelBox(mDestWidth, mDestHeight, 1, mRT->suggestPixelFormat());
160 sizedImage.data =
new uint8[sizedImage.getConsecutiveSize()];
161 Ogre::Image::scale(mFinalPicturePB, sizedImage);
163 Ogre::Image finalImage;
164 finalImage.loadDynamicImage(
static_cast<unsigned char*
>(sizedImage.data), sizedImage.getWidth(), sizedImage.getHeight(), mRT->suggestPixelFormat());
166 finalImage.save(filename);
167 delete[] sizedImage.data;
171 camera->setAutoAspectRatio(autoRatio);
172 camera->setAspectRatio(prevRatio);
173 camera->setFrustumOffset(foffset);
174 camera->setLodBias(lodBias);
175 camera->setCustomProjectionMatrix(bmatrix, cmatrix);