Compositor: Added support for logarithmic depth buffer.

This commit is contained in:
Fernando García Liñán 2020-03-22 16:49:18 +01:00
parent 1b6545fc05
commit 73f00bd842
3 changed files with 17 additions and 1 deletions

View File

@ -129,6 +129,8 @@ Compositor::Compositor(osg::View *view,
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_NearFarPlanes", osg::Vec3f()),
new osg::Uniform("fg_Fcoef", 0.0f),
new osg::Uniform("fg_LightDirection", osg::Vec3f())
}
{
@ -165,6 +167,9 @@ Compositor::update(const osg::Matrix &view_matrix,
SGGeod camera_pos_geod = SGGeod::fromCart(
SGVec3d(camera_pos.x(), camera_pos.y(), camera_pos.z()));
double left, right, bottom, top, zNear, zFar;
proj_matrix.getFrustum(left, right, bottom, top, zNear, zFar);
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);
@ -203,6 +208,12 @@ Compositor::update(const osg::Matrix &view_matrix,
camera_pos_geod.getLatitudeRad(),
camera_pos_geod.getElevationM()));
break;
case NEAR_FAR_PLANES:
u->set(osg::Vec3f(-zFar, -zFar * zNear, zFar - zNear));
break;
case FCOEF:
u->set(2.0f / log2(float(zFar) + 1.0f));
break;
default:
// Unknown uniform
break;

View File

@ -57,6 +57,8 @@ public:
PREV_PROJECTION_MATRIX_INV,
CAMERA_POSITION_CART,
CAMERA_POSITION_GEOD,
NEAR_FAR_PLANES,
FCOEF,
LIGHT_DIRECTION,
TOTAL_BUILTIN_UNIFORMS
};

View File

@ -385,7 +385,6 @@ public:
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv) {
osg::Camera *camera = static_cast<osg::Camera *>(node);
EffectCullVisitor *cv = dynamic_cast<EffectCullVisitor *>(nv);
traverse(node, nv);
@ -720,6 +719,10 @@ public:
}
}
osg::StateSet *ss = camera->getOrCreateStateSet();
auto &uniforms = compositor->getUniforms();
ss->addUniform(uniforms[Compositor::FCOEF]);
return pass.release();
}
};