avoids two transforms in sky.cpp (#3150)

As we discovered in #3148, `Transform` nodes and their low level equivalence `pushModelViewMatrix` are somewhat costly involving a `Matrix::invert` operation per frame.

With this PR we avoid one `Transform` node for sun flashes and avoid another `pushModelViewMatrix` call in case the sun is fully visible.
pull/3153/head
Bo Svensson 3 years ago committed by GitHub
parent 0d1d3e67bc
commit 442a0e8434
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -81,15 +81,15 @@ namespace
return mat; return mat;
} }
osg::ref_ptr<osg::Geometry> createTexturedQuad(int numUvSets=1) osg::ref_ptr<osg::Geometry> createTexturedQuad(int numUvSets=1, float scale=1.f)
{ {
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry; osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> verts = new osg::Vec3Array; osg::ref_ptr<osg::Vec3Array> verts = new osg::Vec3Array;
verts->push_back(osg::Vec3f(-0.5, -0.5, 0)); verts->push_back(osg::Vec3f(-0.5*scale, -0.5*scale, 0));
verts->push_back(osg::Vec3f(-0.5, 0.5, 0)); verts->push_back(osg::Vec3f(-0.5*scale, 0.5*scale, 0));
verts->push_back(osg::Vec3f(0.5, 0.5, 0)); verts->push_back(osg::Vec3f(0.5*scale, 0.5*scale, 0));
verts->push_back(osg::Vec3f(0.5, -0.5, 0)); verts->push_back(osg::Vec3f(0.5*scale, -0.5*scale, 0));
geom->setVertexArray(verts); geom->setVertexArray(verts);
@ -621,14 +621,13 @@ private:
tex->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); tex->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
tex->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); tex->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
osg::ref_ptr<osg::PositionAttitudeTransform> transform (new osg::PositionAttitudeTransform); osg::ref_ptr<osg::Group> group (new osg::Group);
const float scale = 2.6f;
transform->setScale(osg::Vec3f(scale,scale,scale));
mTransform->addChild(transform); mTransform->addChild(group);
osg::ref_ptr<osg::Geometry> geom = createTexturedQuad(); const float scale = 2.6f;
transform->addChild(geom); osg::ref_ptr<osg::Geometry> geom = createTexturedQuad(1, scale);
group->addChild(geom);
osg::StateSet* stateset = geom->getOrCreateStateSet(); osg::StateSet* stateset = geom->getOrCreateStateSet();
@ -637,7 +636,7 @@ private:
stateset->setRenderBinDetails(RenderBin_SunGlare, "RenderBin"); stateset->setRenderBinDetails(RenderBin_SunGlare, "RenderBin");
stateset->setNestRenderBins(false); stateset->setNestRenderBins(false);
mSunFlashNode = transform; mSunFlashNode = group;
mSunFlashCallback = new SunFlashCallback(mOcclusionQueryVisiblePixels, mOcclusionQueryTotalPixels); mSunFlashCallback = new SunFlashCallback(mOcclusionQueryVisiblePixels, mOcclusionQueryTotalPixels);
mSunFlashNode->addCullCallback(mSunFlashCallback); mSunFlashNode->addCullCallback(mSunFlashCallback);
@ -785,10 +784,12 @@ private:
stateset = new osg::StateSet; stateset = new osg::StateSet;
stateset->setAttributeAndModes(mat, osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); stateset->setAttributeAndModes(mat, osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
} }
else if (visibleRatio < 1.f)
{
const float threshold = 0.6; const float threshold = 0.6;
visibleRatio = visibleRatio * (1.f - threshold) + threshold; visibleRatio = visibleRatio * (1.f - threshold) + threshold;
} }
}
float scale = visibleRatio; float scale = visibleRatio;
@ -797,11 +798,13 @@ private:
// no traverse // no traverse
return; return;
} }
else if (scale == 1.f)
traverse(node, cv);
else else
{ {
osg::Matrix modelView = *cv->getModelViewMatrix(); osg::Matrix modelView = *cv->getModelViewMatrix();
modelView.preMultScale(osg::Vec3f(visibleRatio, visibleRatio, visibleRatio)); modelView.preMultScale(osg::Vec3f(scale, scale, scale));
if (stateset) if (stateset)
cv->pushStateSet(stateset); cv->pushStateSet(stateset);

Loading…
Cancel
Save