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] 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 };