From 120328eb2b08dc5ab2222bcca90c3a04321821f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Garc=C3=ADa=20Li=C3=B1=C3=A1n?= Date: Tue, 24 Dec 2019 03:31:28 +0100 Subject: [PATCH 1/3] Removed 'using namespace' from header --- simgear/scene/material/Effect.hxx | 13 +++++-------- simgear/scene/viewer/CompositorPass.cxx | 10 +++++----- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/simgear/scene/material/Effect.hxx b/simgear/scene/material/Effect.hxx index 496805a2..ba255d0a 100644 --- a/simgear/scene/material/Effect.hxx +++ b/simgear/scene/material/Effect.hxx @@ -53,9 +53,6 @@ class Technique; class Effect; class SGReaderWriterOptions; -using namespace osg; -using namespace std; - /** * Object to be initialized at some point after an effect -- and its * containing effect geode -- are hooked into the scene graph. Some @@ -179,9 +176,9 @@ void mergePropertyTrees(SGPropertyNode* resultNode, class UniformFactoryImpl { public: - ref_ptr getUniform( Effect * effect, + osg::ref_ptr getUniform( Effect * effect, const string & name, - Uniform::Type uniformType, + osg::Uniform::Type uniformType, SGConstPropertyNode_ptr valProp, const SGReaderWriterOptions* options ); void updateListeners( SGPropertyNode* propRoot ); @@ -194,9 +191,9 @@ private: SGMutex _mutex; - typedef boost::tuple UniformCacheKey; - typedef boost::tuple, SGPropertyChangeListener*> UniformCacheValue; - std::map > uniformCache; + typedef boost::tuple UniformCacheKey; + typedef boost::tuple, SGPropertyChangeListener*> UniformCacheValue; + std::map > uniformCache; typedef std::queue DeferredListenerList; DeferredListenerList deferredListenerList; diff --git a/simgear/scene/viewer/CompositorPass.cxx b/simgear/scene/viewer/CompositorPass.cxx index 2187a494..0b0b6ac3 100644 --- a/simgear/scene/viewer/CompositorPass.cxx +++ b/simgear/scene/viewer/CompositorPass.cxx @@ -89,7 +89,7 @@ PassBuilder::build(Compositor *compositor, const SGPropertyNode *root, pass->type = root->getStringValue("type"); pass->effect_scheme = root->getStringValue("effect-scheme"); - osg::Camera *camera = new Camera; + osg::Camera *camera = new osg::Camera; pass->camera = camera; camera->setName(pass->name); @@ -107,8 +107,8 @@ PassBuilder::build(Compositor *compositor, const SGPropertyNode *root, camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); // XXX: Should we make this configurable? - camera->setCullingMode(CullSettings::SMALL_FEATURE_CULLING - | CullSettings::VIEW_FRUSTUM_CULLING); + camera->setCullingMode(osg::CullSettings::SMALL_FEATURE_CULLING + | osg::CullSettings::VIEW_FRUSTUM_CULLING); osg::Node::NodeMask cull_mask = std::stoul(root->getStringValue("cull-mask", "0xffffffff"), nullptr, 0); @@ -639,8 +639,8 @@ public: protected: // Given a projection matrix, return a new one with the same frustum // sides and new near / far values. - void makeNewProjMat(Matrixd& oldProj, double znear, - double zfar, Matrixd& projection) { + void makeNewProjMat(osg::Matrixd& oldProj, double znear, + double zfar, osg::Matrixd& projection) { projection = oldProj; // Slightly inflate the near & far planes to avoid objects at the // extremes being clipped out. From 14845bf3f2b658d7cdb5895e9355d3171b6028a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Garc=C3=ADa=20Li=C3=B1=C3=A1n?= Date: Tue, 24 Dec 2019 03:34:37 +0100 Subject: [PATCH 2/3] Compositor: Provide previous frame transformation matrices and the sunlight direction as uniforms --- simgear/scene/model/SGLight.cxx | 6 +++++- simgear/scene/viewer/Compositor.cxx | 30 ++++++++++++++++++++++++++++- simgear/scene/viewer/Compositor.hxx | 5 +++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/simgear/scene/model/SGLight.cxx b/simgear/scene/model/SGLight.cxx index 269829e1..d7af021b 100644 --- a/simgear/scene/model/SGLight.cxx +++ b/simgear/scene/model/SGLight.cxx @@ -118,7 +118,11 @@ SGLight::appendLight(const SGPropertyNode *configNode, light->getRange()); } osg::ShapeDrawable *debug_drawable = new osg::ShapeDrawable(debug_shape); - debug_drawable->setColor(osg::Vec4(1.0, 0.0, 0.0, 1.0)); + debug_drawable->setColor( + osg::Vec4(configNode->getFloatValue("debug-color/r", 1.0f), + configNode->getFloatValue("debug-color/g", 0.0f), + configNode->getFloatValue("debug-color/b", 0.0f), + configNode->getFloatValue("debug-color/a", 1.0f))); osg::Geode *debug_geode = new osg::Geode; debug_geode->addDrawable(debug_drawable); diff --git a/simgear/scene/viewer/Compositor.cxx b/simgear/scene/viewer/Compositor.cxx index 964e387d..e91b1d44 100644 --- a/simgear/scene/viewer/Compositor.cxx +++ b/simgear/scene/viewer/Compositor.cxx @@ -28,10 +28,20 @@ #include #include #include +#include #include #include "CompositorUtil.hxx" + +class LightDirectionCallback : public osg::Uniform::Callback { +public: + virtual void operator()(osg::Uniform *uniform, osg::NodeVisitor *nv) { + SGUpdateVisitor *uv = dynamic_cast(nv); + uniform->set(toOsg(uv->getLightDirection())); + } +}; + namespace simgear { namespace compositor { @@ -113,10 +123,16 @@ Compositor::Compositor(osg::View *view, new osg::Uniform("fg_ViewMatrixInverse", osg::Matrixf()), new osg::Uniform("fg_ProjectionMatrix", osg::Matrixf()), new osg::Uniform("fg_ProjectionMatrixInverse", osg::Matrixf()), + new osg::Uniform("fg_PrevViewMatrix", osg::Matrixf()), + new osg::Uniform("fg_PrevViewMatrixInverse", osg::Matrixf()), + new osg::Uniform("fg_PrevProjectionMatrix", osg::Matrixf()), + new osg::Uniform("fg_PrevProjectionMatrixInverse", osg::Matrixf()), new osg::Uniform("fg_CameraPositionCart", osg::Vec3f()), - new osg::Uniform("fg_CameraPositionGeod", osg::Vec3f()) + new osg::Uniform("fg_CameraPositionGeod", osg::Vec3f()), + new osg::Uniform("fg_LightDirection", osg::Vec3f()) } { + _uniforms[LIGHT_DIRECTION]->setUpdateCallback(new LightDirectionCallback); } Compositor::~Compositor() @@ -149,6 +165,18 @@ Compositor::update(const osg::Matrix &view_matrix, SGGeod camera_pos_geod = SGGeod::fromCart( SGVec3d(camera_pos.x(), camera_pos.y(), camera_pos.z())); + osg::Matrixf prev_view_matrix, prev_view_matrix_inv; + _uniforms[VIEW_MATRIX]->get(prev_view_matrix); + _uniforms[VIEW_MATRIX_INV]->get(prev_view_matrix_inv); + osg::Matrixf prev_proj_matrix, prev_proj_matrix_inv; + _uniforms[PROJECTION_MATRIX]->get(prev_proj_matrix); + _uniforms[PROJECTION_MATRIX_INV]->get(prev_proj_matrix_inv); + + _uniforms[PREV_VIEW_MATRIX]->set(prev_view_matrix); + _uniforms[PREV_VIEW_MATRIX_INV]->set(prev_view_matrix_inv); + _uniforms[PREV_PROJECTION_MATRIX]->set(prev_proj_matrix); + _uniforms[PREV_PROJECTION_MATRIX_INV]->set(prev_proj_matrix_inv); + for (int i = 0; i < TOTAL_BUILTIN_UNIFORMS; ++i) { osg::ref_ptr u = _uniforms[i]; switch (i) { diff --git a/simgear/scene/viewer/Compositor.hxx b/simgear/scene/viewer/Compositor.hxx index f2d0cc75..23855347 100644 --- a/simgear/scene/viewer/Compositor.hxx +++ b/simgear/scene/viewer/Compositor.hxx @@ -51,8 +51,13 @@ public: VIEW_MATRIX_INV, PROJECTION_MATRIX, PROJECTION_MATRIX_INV, + PREV_VIEW_MATRIX, + PREV_VIEW_MATRIX_INV, + PREV_PROJECTION_MATRIX, + PREV_PROJECTION_MATRIX_INV, CAMERA_POSITION_CART, CAMERA_POSITION_GEOD, + LIGHT_DIRECTION, TOTAL_BUILTIN_UNIFORMS }; From fa1e3cb18396a1cee47370d9c42444b2b9e121c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Garc=C3=ADa=20Li=C3=B1=C3=A1n?= Date: Tue, 24 Dec 2019 03:37:14 +0100 Subject: [PATCH 3/3] Support in Effects for reading 3D textures from disk as an array of 2D textures --- simgear/scene/material/TextureBuilder.cxx | 92 ++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/simgear/scene/material/TextureBuilder.cxx b/simgear/scene/material/TextureBuilder.cxx index 01315aa2..6fff6348 100644 --- a/simgear/scene/material/TextureBuilder.cxx +++ b/simgear/scene/material/TextureBuilder.cxx @@ -356,7 +356,7 @@ namespace { TextureBuilder::Registrar install1D("1d", new TexBuilder("1d")); TextureBuilder::Registrar install2D("2d", new TexBuilder("2d")); -TextureBuilder::Registrar install3D("3d", new TexBuilder("3d")); +//TextureBuilder::Registrar install3D("3d", new TexBuilder("3d")); } class WhiteTextureBuilder : public TextureBuilder @@ -776,6 +776,96 @@ namespace { TextureBuilder::Registrar installCubeMap("cubemap", new CubeMapBuilder); } + +class Texture3DBuilder : public TextureBuilder +{ +public: + Texture* build(Effect* effect, Pass* pass, const SGPropertyNode*, + const SGReaderWriterOptions* options); +protected: + typedef map > TexMap; + TexMap texMap; +}; + +Texture* Texture3DBuilder::build(Effect* effect, Pass* pass, + const SGPropertyNode* props, + const SGReaderWriterOptions* options) +{ + TexTuple attrs = makeTexTuple(effect, props, options, "3d"); + typename TexMap::iterator itr = texMap.find(attrs); + + ref_ptr tex; + if ((itr != texMap.end())&& + (itr->second.lock(tex))) + { + return tex.release(); + } + + tex = new Texture3D; + + const string& imageName = attrs.get<0>(); + if (imageName.empty()) + return NULL; + + osgDB::ReaderWriter::ReadResult result; + + // load texture for effect + SGReaderWriterOptions::LoadOriginHint origLOH = options->getLoadOriginHint(); + if(attrs.get<8>() == ImageInternalFormat::Normalized) + options->setLoadOriginHint(SGReaderWriterOptions::LoadOriginHint::ORIGIN_EFFECTS_NORMALIZED); + else + options->setLoadOriginHint(SGReaderWriterOptions::LoadOriginHint::ORIGIN_EFFECTS); +#if OSG_VERSION_LESS_THAN(3,4,2) + result = osgDB::readImageFile(imageName, options); +#else + result = osgDB::readRefImageFile(imageName, options); +#endif + options->setLoadOriginHint(origLOH); + osg::ref_ptr image; + if (result.success()) + image = result.getImage(); + if (image.valid()) + { + osg::ref_ptr image3d = new osg::Image; + int size = image->t(); + int depth = image->s() / image->t(); + image3d->allocateImage(size, size, depth, + image->getPixelFormat(), image->getDataType()); + + for (int i = 0; i < depth; ++i) { + osg::ref_ptr subimage = new osg::Image; + subimage->allocateImage(size, size, 1, + image->getPixelFormat(), image->getDataType()); + copySubImage(image, size * i, 0, size, size, subimage.get(), 0, 0); + image3d->copySubImage(0, 0, i, subimage.get()); + } + + image3d->setInternalTextureFormat(image->getInternalTextureFormat()); + image3d = computeMipmap(image3d.get(), attrs.get<7>()); + tex->setImage(image3d.get()); + } else { + SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file " << imageName); + return NULL; + } + + tex->setFilter(Texture::MIN_FILTER, attrs.get<1>()); + tex->setFilter(Texture::MAG_FILTER, attrs.get<2>()); + tex->setWrap(Texture::WRAP_S, attrs.get<3>()); + tex->setWrap(Texture::WRAP_T, attrs.get<4>()); + tex->setWrap(Texture::WRAP_R, attrs.get<5>()); + + if (itr == texMap.end()) + texMap.insert(make_pair(attrs, tex)); + else + itr->second = tex; // update existing, but empty observer + return tex.release(); +} + +namespace { +TextureBuilder::Registrar install3D("3d", new Texture3DBuilder); +} + + EffectNameValue combineParamInit[] = { {"replace", TexEnvCombine::REPLACE},