This example shows how a custom post processor can consume the SensorFx pre-aperture radiance texture and display it in the main viewport.
The example creates two derived classes. ApertureRadiancePostProcessor derives from DtPostProcessor and implements the constructor and update function for a post processor that samples the SensorFx pre-aperture radiance texture. The class ApertureRadiancePostProcessorCreator is derived from DtPostProcessorCreator and is utilized in the DtPostProcessorFactory.
The plugin's init function inserts the post processor after "sensor_fx" so the pre-aperture radiance texture is populated for the current frame before it is sampled. It registers the post processor effect against a user defined ObserverSetting enumeration value and creates a dedicated "Pre-Aperture Radiance" observer mode wired to that setting. The observer mode is configured to auto-activate the "LWIR (SensorFX)" sensor so the SensorFx pipeline populates the pre-aperture radiance texture whenever the mode is active.
The update function looks up the per-channel "sensor_fx" post processor through DtPostProcessManager::postProcessorTypeMap(), casts it to jrmSenSimPostProcessor to reach its SenSimDrawable, and retrieves the pre-aperture radiance texture id via DtSensorFxDrawableInterface::getPreApertureRadianceTextureId(). The raw GL texture id is bound to texture unit 1 through an osg::Drawable::DrawCallback attached to the post processor's quad, since the texture object is owned by SensorFx rather than by an osg::Texture wrapper.
The shader for the post processor is in examples\examplePreApertureRadiance\apertureRadiance_ps.glsl. It applies an exposure and gamma tonemap so the linear HDR radiance is legible on an sRGB monitor.
This example is a plug-in. You can run it by running ./bin64/examplePreApertureRadiance_stealth.bat (on Windows) or ./bin64/examplePreApertureRadiance_stealth.sh (on Linux). The launcher loads both the sensorFx plugin and the examplePreApertureRadiance plugin. Attach the observer to an entity and select "Pre-Aperture Radiance" from the observer mode dropdown; the observer mode auto-activates the "LWIR (SensorFX)" sensor so the SensorFx pipeline populates the pre-aperture radiance texture. Left-click anywhere in the view to sample that pixel: the plugin reads back the pre-aperture radiance texture and prints the band-integrated radiance at that point (in W/(cm^2 sr)) to the console. For more information about running examples, please see Running Applications and Examples.
#include <jrmPluginInit.h>
#include <jrmSenSimPostProcessor.h>
#include <osg/Drawable>
#include <osg/Geode>
#include <osg/StateSet>
#include <osg/Uniform>
#include <osgGA/GUIEventHandler>
#include <osgViewer/View>
#include <boost/bind/bind.hpp>
#include <algorithm>
#include <atomic>
#include <iostream>
#include <vector>
using namespace makVrv;
static const int radianceTextureUnit = 1;
static const char* const kSensorModeName = "LWIR (SensorFX)";
static const bool kUseHdrPath = true;
static vrvSignalsLib::connection postInitializeConnection;
static std::atomic<bool> radianceClickPending(false);
static float radianceClickU = 0.0f;
static float radianceClickV = 0.0f;
class RadianceClickHandler : public osgGA::GUIEventHandler
{
public:
virtual bool handle(const osgGA::GUIEventAdapter& ea,
osgGA::GUIActionAdapter&) override
{
if (ea.getEventType() == osgGA::GUIEventAdapter::PUSH &&
ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
{
const float xRange = ea.getXmax() - ea.getXmin();
const float yRange = ea.getYmax() - ea.getYmin();
if (xRange > 0.0f && yRange > 0.0f)
{
radianceClickU = (ea.getX() - ea.getXmin()) / xRange;
radianceClickV = (ea.getY() - ea.getYmin()) / yRange;
radianceClickPending = true;
}
}
return false;
}
};
{
public:
BindRadianceTextureCallback()
: myRawGlId(0)
, myMaxRadiancePerCm2(0.0)
, myHandlerAdded(false)
{
}
void setRawGlId(unsigned int id) const { myRawGlId = id; }
void setMaxRadiance(double maxRadiancePerCm2) const
{ myMaxRadiancePerCm2 = maxRadiancePerCm2; }
virtual void drawImplementation(osg::RenderInfo& renderInfo,
const osg::Drawable* drawable) const override
{
osg::State* state = renderInfo.getState();
if (state != nullptr)
{
state->setActiveTextureUnit(radianceTextureUnit);
glBindTexture(GL_TEXTURE_2D, myRawGlId);
printClickedRadiance();
state->setActiveTextureUnit(0);
state->haveAppliedTextureAttribute(radianceTextureUnit,
osg::StateAttribute::TEXTURE);
}
osgViewer::View* view =
dynamic_cast<osgViewer::View*>(renderInfo.getView());
if (view != nullptr)
{
if (!myHandlerAdded)
{
view->addEventHandler(new RadianceClickHandler);
myHandlerAdded = true;
}
}
drawable->drawImplementation(renderInfo);
}
private:
void printClickedRadiance() const
{
if (myRawGlId == 0 || !radianceClickPending.exchange(false))
{
return;
}
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &texW);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &texH);
if (texW <= 0 || texH <= 0)
{
return;
}
std::vector<float> pixels(static_cast<size_t>(texW) * texH * 4);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels.data());
int px = static_cast<int>(radianceClickU * texW);
int py = static_cast<int>(radianceClickV * texH);
px = std::max(0, std::min(px, texW - 1));
py = std::max(0, std::min(py, texH - 1));
const float sampled =
pixels[(static_cast<size_t>(py) * texW + px) * 4];
float radiancePerCm2;
if (kUseHdrPath)
{
radiancePerCm2 = sampled;
}
else
{
radiancePerCm2 = sampled * static_cast<float>(myMaxRadiancePerCm2);
}
std::cout << "[PreApertureRadiance] pixel (" << px << ", " << py << ")\n"
<< " band radiance = " << radiancePerCm2 << " W/(cm^2 sr)"
<< std::endl;
}
mutable unsigned int myRawGlId;
mutable double myMaxRadiancePerCm2;
mutable bool myHandlerAdded;
};
{
public:
ApertureRadiancePostProcessor(
DtDe& de)
{
setFragmentShaderFile(
"../examples/examplePreApertureRadiance/apertureRadiance_ps.glsl");
addEffect("aperture_radiance_effect");
}
{
if (!effectEnabled("aperture_radiance_effect"))
{
return true;
}
osg::Camera* cam = cameraForChannel(channel);
if (!cam)
{
return false;
}
unsigned int rawId = 0;
double maxRadiancePerCm2 = 0.0;
if (ppm != nullptr)
{
if (ppMap != nullptr)
{
auto it = ppMap->find("sensor_fx");
if (it != ppMap->end())
{
jrmSenSimPostProcessor* jrmPp =
dynamic_cast<jrmSenSimPostProcessor*>(it->second);
if (jrmPp != nullptr)
{
if (drawable != nullptr)
{
if (radianceTex != nullptr)
{
rawId = radianceTex->
id();
}
}
if (!jrmPp->effectEnabled("sensor_fx") &&
{
sensorController.setTargetSensor(kSensorModeName);
sensorController.setParameterBool("sensorEffectsOn", false);
sensorController.setParameterBool("renderToHdrTexture", !kUseHdrPath);
sensorController.setParameterBool("renderToHdrTexture", kUseHdrPath);
sensorController.setParameterBool("sensorEffectsOn", true);
}
}
}
}
}
osg::Drawable* quadDrawable =
(myQuad && myQuad->getNumDrawables() > 0) ? myQuad->getDrawable(0) : nullptr;
if (quadDrawable != nullptr)
{
BindRadianceTextureCallback* binder = dynamic_cast<
BindRadianceTextureCallback*>(quadDrawable->getDrawCallback());
if (binder == nullptr)
{
binder = new BindRadianceTextureCallback;
quadDrawable->setDrawCallback(binder);
}
binder->setRawGlId(rawId);
binder->setMaxRadiance(maxRadiancePerCm2);
}
osg::StateSet* ss = cam->getOrCreateStateSet();
ss->getOrCreateUniform("vrv_preApertureRadianceTex",
osg::Uniform::INT)->set(radianceTextureUnit);
ss->getOrCreateUniform("vrv_apertureRadianceExposure",
osg::Uniform::FLOAT)->set(0.5f);
return true;
}
};
{
public:
{
return new ApertureRadiancePostProcessor(de);
}
};
void slot_postInit(
DtDe& de)
{
postInitializeConnection.disconnect();
if (radianceMode < 0)
{
"Pre-Aperture Radiance");
}
omMgr.
setSensor(radianceMode, kSensorModeName);
for (auto it = modes.begin(); it != modes.end(); ++it)
{
const bool enable = (it->first == radianceMode);
}
}
void initializeToolbar(
DtDe& de)
{
QString qTemp = QObject::tr("Pre-Aperture Radiance");
DtUnicode::fromQString<QString>(qTemp));
qTemp = QObject::tr("Show/Hide SensorFx Pre-Aperture Radiance View");
DtUnicode::fromQString<QString>(qTemp));
}
{
new ApertureRadiancePostProcessorCreator);
{
}
factory.
registerEffect(
"example_aperture_radiance",
"aperture_radiance_effect",
boost::bind(&initializeToolbar, std::ref(de)));
boost::bind(&slot_postInit, std::ref(de)));
de, "ApertureRadianceItem", observerSettingId));
}
{
return true;
}