Skip to content

Commit 233d80f

Browse files
committed
Merge pull request #1804 from paroj:ovis_texup
2 parents ff9d313 + 0bb0989 commit 233d80f

File tree

2 files changed

+70
-29
lines changed

2 files changed

+70
-29
lines changed

modules/ovis/include/opencv2/ovis.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ enum SceneSettings
2424
SCENE_INTERACTIVE = 2,
2525
/// draw coordinate system crosses for debugging
2626
SCENE_SHOW_CS_CROSS = 4,
27-
/// @ref WindowScene::getScreenshot returns images as CV_32FC4 instead of CV_8UC3
28-
SCENE_RENDER_FLOAT = 8,
2927
/// Apply anti-aliasing. The first window determines the setting for all windows.
30-
SCENE_AA = 16
28+
SCENE_AA = 8
3129
};
3230

3331
enum MaterialProperty
@@ -161,6 +159,16 @@ class CV_EXPORTS_W WindowScene {
161159
*/
162160
CV_WRAP virtual void getScreenshot(OutputArray frame) = 0;
163161

162+
/**
163+
* read back the texture of an active compositor
164+
* @param compname name of the compositor
165+
* @param texname name of the texture inside the compositor
166+
* @param mrtIndex if texture is a MRT, specifies the attachment
167+
* @param out the texture contents
168+
*/
169+
CV_WRAP virtual void getCompositorTexture(const String& compname, const String& texname,
170+
OutputArray out, int mrtIndex = 0) = 0;
171+
164172
/**
165173
* get the depth for the current frame.
166174
*

modules/ovis/src/ovis.cpp

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ class WindowSceneImpl : public WindowScene
264264
Ptr<OgreBites::CameraMan> camman;
265265
Ptr<Rectangle2D> bgplane;
266266

267-
Ogre::RenderTarget* frameSrc;
268267
Ogre::RenderTarget* depthRTT;
269268
public:
270269
WindowSceneImpl(Ptr<Application> app, const String& _title, const Size& sz, int flags)
@@ -324,18 +323,6 @@ class WindowSceneImpl : public WindowScene
324323
}
325324

326325
rWin->addViewport(cam);
327-
frameSrc = rWin;
328-
329-
if (flags & SCENE_RENDER_FLOAT)
330-
{
331-
// also render into an offscreen texture
332-
// currently this draws everything twice, but we spare the float->byte conversion for display
333-
TexturePtr tex = TextureManager::getSingleton().createManual(
334-
title + "_rt", RESOURCEGROUP_NAME, TEX_TYPE_2D, sz.width, sz.height, 0, PF_FLOAT32_RGBA,
335-
TU_RENDERTARGET);
336-
frameSrc = tex->getBuffer()->getRenderTarget();
337-
frameSrc->addViewport(cam);
338-
}
339326
}
340327

341328
void setBackground(InputArray image) CV_OVERRIDE
@@ -361,9 +348,9 @@ class WindowSceneImpl : public WindowScene
361348
{
362349
CompositorManager& cm = CompositorManager::getSingleton();
363350
// this should be applied to all owned render targets
364-
Ogre::RenderTarget* targets[] = {frameSrc, rWin, depthRTT};
351+
Ogre::RenderTarget* targets[] = {rWin, depthRTT};
365352

366-
for(int j = (frameSrc == rWin); j < 3; j++) // skip frameSrc if it is the same as rWin
353+
for(int j = 0; j < 2; j++)
367354
{
368355
Ogre::RenderTarget* tgt = targets[j];
369356
if(!tgt) continue;
@@ -382,6 +369,57 @@ class WindowSceneImpl : public WindowScene
382369
}
383370
}
384371

372+
void getCompositorTexture(const String& compname, const String& texname, OutputArray out,
373+
int mrtIndex) CV_OVERRIDE
374+
{
375+
CompositorManager& cm = CompositorManager::getSingleton();
376+
CompositorChain* chain = cm.getCompositorChain(rWin->getViewport(0));
377+
CV_Assert(chain && "no active compositors");
378+
379+
CompositorInstance* inst = chain->getCompositor(compname);
380+
if(!inst)
381+
CV_Error_(Error::StsBadArg, ("no active compositor named: %s", compname.c_str()));
382+
383+
TexturePtr tex = inst->getTextureInstance(texname, mrtIndex);
384+
if(!tex)
385+
CV_Error_(Error::StsBadArg, ("no texture named: %s", texname.c_str()));
386+
387+
PixelFormat src_type = tex->getFormat();
388+
int dst_type;
389+
switch(src_type)
390+
{
391+
case PF_BYTE_RGB:
392+
dst_type = CV_8UC3;
393+
break;
394+
case PF_BYTE_RGBA:
395+
dst_type = CV_8UC4;
396+
break;
397+
case PF_FLOAT32_RGB:
398+
dst_type = CV_32FC3;
399+
break;
400+
case PF_FLOAT32_RGBA:
401+
dst_type = CV_32FC4;
402+
break;
403+
case PF_DEPTH16:
404+
dst_type = CV_16U;
405+
break;
406+
default:
407+
CV_Error(Error::StsNotImplemented, "unsupported texture format");
408+
}
409+
410+
out.create(tex->getHeight(), tex->getWidth(), dst_type);
411+
412+
Mat mat = out.getMat();
413+
PixelBox pb(tex->getWidth(), tex->getHeight(), 1, src_type, mat.ptr());
414+
tex->getBuffer()->blitToMemory(pb, pb);
415+
416+
if(CV_MAT_CN(dst_type) < 3)
417+
return;
418+
419+
// convert to OpenCV channel order
420+
cvtColor(mat, mat, CV_MAT_CN(dst_type) == 3 ? COLOR_RGB2BGR : COLOR_RGBA2BGRA);
421+
}
422+
385423
void setBackground(const Scalar& color) CV_OVERRIDE
386424
{
387425
// hide background plane
@@ -390,8 +428,6 @@ class WindowSceneImpl : public WindowScene
390428
// BGRA as uchar
391429
ColourValue _color = ColourValue(color[2], color[1], color[0], color[3]) / 255;
392430
rWin->getViewport(0)->setBackgroundColour(_color);
393-
if(frameSrc != rWin)
394-
frameSrc->getViewport(0)->setBackgroundColour(_color);
395431
}
396432

397433
void createEntity(const String& name, const String& meshname, InputArray tvec, InputArray rot) CV_OVERRIDE
@@ -543,17 +579,14 @@ class WindowSceneImpl : public WindowScene
543579

544580
void getScreenshot(OutputArray frame) CV_OVERRIDE
545581
{
546-
PixelFormat src_type = frameSrc->suggestPixelFormat();
547-
int dst_type = src_type == PF_BYTE_RGB ? CV_8UC3 : CV_32FC4;
548-
549-
frame.create(frameSrc->getHeight(), frameSrc->getWidth(), dst_type);
582+
frame.create(rWin->getHeight(), rWin->getWidth(), CV_8UC3);
550583

551584
Mat out = frame.getMat();
552-
PixelBox pb(frameSrc->getWidth(), frameSrc->getHeight(), 1, src_type, out.ptr());
553-
frameSrc->copyContentsToMemory(pb, pb);
585+
PixelBox pb(rWin->getWidth(), rWin->getHeight(), 1, PF_BYTE_RGB, out.ptr());
586+
rWin->copyContentsToMemory(pb, pb);
554587

555588
// convert to OpenCV channel order
556-
cvtColor(out, out, dst_type == CV_8UC3 ? COLOR_RGB2BGR : COLOR_RGBA2BGRA);
589+
cvtColor(out, out, COLOR_RGB2BGR);
557590
}
558591

559592
void getDepth(OutputArray depth) CV_OVERRIDE
@@ -564,8 +597,8 @@ class WindowSceneImpl : public WindowScene
564597
// render into an offscreen texture
565598
// currently this draws everything twice as OGRE lacks depth texture attachments
566599
TexturePtr tex = TextureManager::getSingleton().createManual(
567-
title + "_Depth", RESOURCEGROUP_NAME, TEX_TYPE_2D, frameSrc->getWidth(),
568-
frameSrc->getHeight(), 0, PF_DEPTH, TU_RENDERTARGET);
600+
title + "_Depth", RESOURCEGROUP_NAME, TEX_TYPE_2D, rWin->getWidth(),
601+
rWin->getHeight(), 0, PF_DEPTH, TU_RENDERTARGET);
569602
depthRTT = tex->getBuffer()->getRenderTarget();
570603
depthRTT->addViewport(cam);
571604
depthRTT->setAutoUpdated(false); // only update when requested

0 commit comments

Comments
 (0)