Skip to content

Commit 6d5f440

Browse files
committed
Merge pull request #2799 from paroj:oshadow
2 parents b91a781 + 234bf19 commit 6d5f440

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

modules/ovis/include/opencv2/ovis.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ enum SceneSettings
5252
/// Apply anti-aliasing. The first window determines the setting for all windows.
5353
SCENE_AA = 8,
5454
/// Render off-screen without a window. Allows separate AA setting. Requires manual update via @ref WindowScene::update
55-
SCENE_OFFSCREEN = 16
55+
SCENE_OFFSCREEN = 16,
56+
/// Enable real-time shadows in the scene. All entities cast shadows by default. Control via @ref ENTITY_CAST_SHADOWS
57+
SCENE_SHADOWS = 32
5658
};
5759

5860
enum MaterialProperty
@@ -74,7 +76,8 @@ enum EntityProperty
7476
ENTITY_MATERIAL,
7577
ENTITY_SCALE,
7678
ENTITY_AABB_WORLD,
77-
ENTITY_ANIMBLEND_MODE
79+
ENTITY_ANIMBLEND_MODE,
80+
ENTITY_CAST_SHADOWS
7881
};
7982

8083
/**

modules/ovis/src/ovis.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ struct Application : public OgreBites::ApplicationContext, public OgreBites::Inp
187187
uint32_t h;
188188
int key_pressed;
189189
int flags;
190+
Ogre::MaterialPtr casterMat;
190191

191192
Application(const Ogre::String& _title, const Size& sz, int _flags)
192193
: OgreBites::ApplicationContext("ovis"), mainWin(NULL), title(_title), w(sz.width),
@@ -287,6 +288,9 @@ struct Application : public OgreBites::ApplicationContext, public OgreBites::Inp
287288
MaterialManager& matMgr = MaterialManager::getSingleton();
288289
matMgr.setDefaultTextureFiltering(TFO_ANISOTROPIC);
289290
matMgr.setDefaultAnisotropy(16);
291+
casterMat = matMgr.create("DepthCaster", Ogre::RGN_INTERNAL);
292+
casterMat->setLightingEnabled(false);
293+
casterMat->setDepthBias(-1, -1);
290294
}
291295
};
292296

@@ -318,6 +322,21 @@ class WindowSceneImpl : public WindowScene
318322
RTShader::ShaderGenerator& shadergen = RTShader::ShaderGenerator::getSingleton();
319323
shadergen.addSceneManager(sceneMgr); // must be done before we do anything with the scene
320324

325+
if (flags & SCENE_SHADOWS)
326+
{
327+
sceneMgr->setShadowTechnique(SHADOWTYPE_TEXTURE_MODULATIVE_INTEGRATED);
328+
sceneMgr->setShadowTexturePixelFormat(PF_DEPTH32);
329+
// arbitrary heuristic for shadowmap size
330+
sceneMgr->setShadowTextureSize(std::max(sz.width, sz.height) * 2);
331+
sceneMgr->setShadowCameraSetup(FocusedShadowCameraSetup::create());
332+
sceneMgr->setShadowTextureCasterMaterial(app->casterMat);
333+
334+
// inject shadowmap into materials
335+
const auto& schemeName = RTShader::ShaderGenerator::DEFAULT_SCHEME_NAME;
336+
auto rs = shadergen.getRenderState(schemeName);
337+
rs->addTemplateSubRenderState(shadergen.createSubRenderState("SGX_IntegratedPSSM3"));
338+
}
339+
321340
sceneMgr->setAmbientLight(ColourValue(.1, .1, .1));
322341
_createBackground();
323342
}
@@ -716,6 +735,13 @@ class WindowSceneImpl : public WindowScene
716735
SceneNode& node = _getSceneNode(sceneMgr, name);
717736
switch(prop)
718737
{
738+
case ENTITY_CAST_SHADOWS:
739+
{
740+
Entity* ent = dynamic_cast<Entity*>(node.getAttachedObject(name));
741+
CV_Assert(ent && "invalid entity");
742+
ent->setCastShadows(bool(value[0]));
743+
break;
744+
}
719745
case ENTITY_SCALE:
720746
{
721747
node.setScale(value[0], value[1], value[2]);

0 commit comments

Comments
 (0)