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>
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)
: DtPostProcessor(de, "example_aperture_radiance", false)
{
setFragmentShaderFile(
"../examples/examplePreApertureRadiance/apertureRadiance_ps.glsl");
addEffect("aperture_radiance_effect");
}
virtual bool update(DtChannel* channel)
{
if (!effectEnabled("aperture_radiance_effect"))
{
return true;
}
osg::Camera* cam = cameraForChannel(channel);
if (!cam)
{
return false;
}
unsigned int rawId = 0;
double maxRadiancePerCm2 = 0.0;
DtPostProcessManager* ppm = dynamic_cast<DtPostProcessManager*>(
&DtPostProcessManagerInterface::instance(myDe));
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)
{
DtSensorFxDrawableInterface* drawable = jrmPp->senSimDrawable();
if (drawable != nullptr)
{
const DtTextureObject* radianceTex =
if (radianceTex != nullptr)
{
rawId = radianceTex->
id();
}
}
if (!jrmPp->effectEnabled("sensor_fx") &&
DtSensorManager::instance(myDe).findSensor(kSensorModeName) != nullptr)
{
DtSensorController sensorController(myDe, 0);
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:
DtPostProcessor* create(DtDe& de, const std::string&)
{
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");
qTemp = QObject::tr("Show/Hide SensorFx Pre-Aperture Radiance View");
}
{
jrmPlugin::init(de);
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;
}
Contains makVrv::DtDe class.
#define DT_DE_INIT_ONCE(de)
This macro is called by modules in their init() function. This macro ensures that the module is only ...
Definition DtDe.h:30
Contains vrvCore::DtObserverMode class.
Contains the DtObserverModeManager class declaration.
Contains makVrv::DtObserverSettingsDisplayNameProvider class.
Contains classes used as menu items on the observer menu.
Contains makVrv::DtObserverSettingsManager class.
Contains makVrv::DtPostProcessManager class.
Contains makVrv::DtPostProcessManagerInterface class.
Contains makVrv::DtSensorController class.
Contains DtSensorFxDrawableInterface class declaration.
Contains makVrv::DtSensorManager class.
Defines the DtVisualTypeManager class.
Contains makVrv::DtVrvApplication implementation.
The DtDe is the core component of any VR-Vantage application. It owns a DtRenderer,...
Definition DtDe.h:77
boost::signalslib::signal< void()> signal_initializeDefaultState
Definition DtDe.h:529
virtual const DtDePathConfiguration & dePathConfiguration() const
Return the DtDePathConfiguration.
boost::signalslib::signal< void()> signal_postInitialize
Definition DtDe.h:535
virtual std::string iconsPath() const
Accessors to resolved paths.
virtual void setToolTip(int intKey, const DtUnicode &displayName)
Change the tool tip associated with a key (if it exists) or add a new tool tip to key mapping.
virtual void setDisplayName(int intKey, const DtUnicode &displayName)
Change the dislay name associated with a key (if it exists) or add a new display name to key mapping.
DtObserverModeManager is responsible for loading and saving observer modes, and providing a mechanism...
Definition DtObserverModeManager.h:55
bool setSensor(int modeKey, const std::string &sensorName)
int lookupObserverModeKey(const std::string &stringKey) const
const ObserverModes & observerModes() const
void setObserverSetting(int modeKey, int settingKey, bool val)
virtual int createObserverMode(const std::string &stringKey, const std::string &displayName)
Add a observer mode. An modeKey will be assigned. The user can then set the key map and model set to ...
static DtObserverModeManager & instance(DtDe &de)
Get an instance used to emit, or to connect.
Definition DtObserverSettingsItems.h:300
DtObserverSettingsDisplayNameProvider is an interface for a class to provide display names for observ...
Definition DtObserverSettingsDisplayNameProvider.h:24
ObserverSetting
Definition DtObserverSettingsManager.h:42
@ USER_START
Definition DtObserverSettingsManager.h:86
static DtObserverSettingsManager & instance(DtDe &de)
Get an instance used to emit, or to connect.
ProcessorTypeToPostProcessorMap * postProcessorTypeMap(DtChannel *channel)
Get the map of post processor types to post processor pointers.
Definition DtPostProcessor.h:235
Factory to create the specific DtPostProcessors registered when vrvOsg starts up.
Definition DtPostProcessorFactory.h:27
void registerEffect(std::string postProcessor, const std::string &effectName, DtObserverSettingsManager::ObserverSetting obsSettingId)
virtual void addCreator(const std::string &postProcessorType, DtPostProcessorCreator *creator)
Add a creator function for the specified post processor type.
bool addPostProcessorAfter(const std::string &processorType, const std::string &afterType)
static DtPostProcessorFactory & instance(DtDe &de)
Get an instance to the DtPostProcessorFactory.
bool addPostProcessorAtEnd(const std::string &processorType)
This class provides the OSG-specific implementation of a post processor. A post processor has a type ...
Definition DtPostProcessor.h:67
virtual const makVrv::DtTextureObject * getPreApertureRadianceTextureId(int idx) const =0
virtual double getPreApertureMaxRadiance(int idx) const =0
static DtUnicode fromQString(QStringType qstr)
Template function for setting from Qt 4 style string.
Definition DtUnicode.h:107
@ ModelSetAll
Definition DtVisualTypeManager.h:48
static DtVisualTypeManager & instance(DtDe &de)
Static accessor. Get the instance of the manager rooted in the DtDe.
DT_DLL_EXAMPLEACCESSORY void init(makVrv::DtDe &de)
DT_DE_PLUGIN_EXPORT_MACRO bool initDeModule(makVrv::DtDe *de)
DT_DLL_VRVCOREQT void init(DtDe &de)
DT_DLL_VRVPOSTPROCESSOR void init(DtDe &anIG)
Initialize the VRV Post Processor library.
Contains DtCreateSensorFieldOfViewCommand and DtCreateSensorFieldOfViewCommandCreator class declarati...
Definition DtGridLinesSettingsRecord.h:16
Information about a visual type.
Definition DtVisualTypeManager.h:53