VR-Vantage
Loading...
Searching...
No Matches
examplePreApertureRadiancePlugin

Overview

This example shows how a custom post processor can consume the SensorFx pre-aperture radiance texture and display it in the main viewport.

Expected Result

Pre-Aperture Radiance Post Processor Result

Example Details

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.

Building the Example

To build the plugin, follow the instructions at Building VR-Vantage Examples, Applications, and Plug-ins.

Running the Example

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.

Example Source Files


examplePreApertureRadiancePlugin.cxx

/******************************************************************************
** Copyright (c) 2026 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
#include <vrvCore/DtDe.h>
#include <jrmPluginInit.h>
// SensorFx internal headers: we dynamic_cast the "sensor_fx" post processor
// to jrmSenSimPostProcessor to reach its SenSimDrawable, which exposes the
// pre-aperture radiance texture via getPreApertureRadianceTextureId().
#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;
// Create a setting id that will be different than all the VR-Vantage setting ids.
static const int observerSettingId = DtObserverSettingsManager::USER_START + 20;
// Texture unit used to bind the pre-aperture radiance texture into the
// fragment shader. Unit 0 is reserved for the base class' vrv_inputImage.
static const int radianceTextureUnit = 1;
// Name of the SensorFX sensor mode this example drives. The observer mode
// auto-activates this sensor, and the post processor forces its effect on
// (see ApertureRadiancePostProcessor::update).
static const char* const kSensorModeName = "LWIR (SensorFX)";
// This example explicitly drives the sensor's HDR path (renderToHdrTexture),
// so the pre-aperture radiance texture holds absolute band-integrated radiance
// in W/(cm^2 sr). Flip this to false to exercise the LDR (normalized 8-bit)
// path instead, where the sampled value is a [0,1] fraction of the sensor max
// radiance. Both the sensor setup (ApertureRadiancePostProcessor::update) and
// the texture read-back (printClickedRadiance) key off this single flag.
static const bool kUseHdrPath = true;
static vrvSignalsLib::connection postInitializeConnection;
// State shared between the mouse click handler (which runs during the event
// traversal) and the texture read-back (which runs later in the draw
// callback, where a valid GL context and the radiance texture are bound).
// A left-click records the normalized cursor position and raises the pending
// flag; the next draw reads that pixel and prints its radiance.
static std::atomic<bool> radianceClickPending(false);
static float radianceClickU = 0.0f; // [0,1], left to right
static float radianceClickV = 0.0f; // [0,1], bottom to top (GL orientation)
// GUIEventHandler that records the normalized cursor position of a left mouse
// click. It never consumes the event, so VR-Vantage keeps handling clicks
// normally.
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;
}
};
// osg::Drawable::DrawCallback that binds an externally owned raw GL texture
// id to a texture unit right before the wrapped drawable draws. Doing the
// bind here (rather than via setTextureAttributeAndModes on the StateSet)
// bypasses osg::State's texture-object tracking, which would otherwise
// require an osg::Texture wrapper we do not own.
class BindRadianceTextureCallback : public osg::Drawable::DrawCallback
{
public:
BindRadianceTextureCallback()
: myRawGlId(0)
, myMaxRadiancePerCm2(0.0)
, myHandlerAdded(false)
{
}
void setRawGlId(unsigned int id) const { myRawGlId = id; }
// Sensor max radiance (W/cm^2 sr), fetched via the public
// DtSensorFxDrawableInterface. Only needed to convert the normalized
// value back to absolute radiance in the non-HDR (LDR) path.
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);
// The pre-aperture radiance texture is now bound to GL_TEXTURE_2D
// on this unit, so this is the one place we can read it back.
printClickedRadiance();
state->setActiveTextureUnit(0);
// Invalidate osg::State's cached knowledge of unit 1 so it does
// not restore a stale binding on the next apply().
state->haveAppliedTextureAttribute(radianceTextureUnit,
osg::StateAttribute::TEXTURE);
}
osgViewer::View* view =
dynamic_cast<osgViewer::View*>(renderInfo.getView());
if (view != nullptr)
{
// Register the click handler once, using the view that is currently
// rendering this post-process quad. The post-process camera is not a
// slave of the view, so this is the reliable way to reach it.
if (!myHandlerAdded)
{
view->addEventHandler(new RadianceClickHandler);
myHandlerAdded = true;
}
}
drawable->drawImplementation(renderInfo);
}
private:
// Reads the pixel under the last left-click from the currently bound
// pre-aperture radiance texture and prints its radiance.
void printClickedRadiance() const
{
if (myRawGlId == 0 || !radianceClickPending.exchange(false))
{
return;
}
GLint texW = 0;
GLint texH = 0;
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)
{
// HDR path: the R channel already holds absolute band-integrated
// pre-aperture radiance in W/(cm^2 sr).
radiancePerCm2 = sampled;
}
else
{
// LDR (8-bit) path: the sampled value is a normalized [0,1] fraction,
// so recover absolute radiance by scaling with the sensor max
// radiance, exposed through
// DtSensorFxDrawableInterface::getPreApertureMaxRadiance() and stashed
// here via setMaxRadiance().
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;
};
class ApertureRadiancePostProcessor : public DtPostProcessor
{
public:
ApertureRadiancePostProcessor(DtDe& de)
: DtPostProcessor(de, "example_aperture_radiance", false)
{
// The false in DtPostProcessor constructor indicates that it is not
// activated in 2D orthographic views
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;
}
// Reach into SensorFx's per-channel "sensor_fx" post processor for
// the pre-aperture radiance texture. When no sensor mode is active
// on the channel, this look-up yields a zero id and the shader
// samples the default (black) texture.
unsigned int rawId = 0;
double maxRadiancePerCm2 = 0.0;
DtPostProcessManager* ppm = dynamic_cast<DtPostProcessManager*>(
&DtPostProcessManagerInterface::instance(myDe));
if (ppm != nullptr)
{
auto* ppMap = ppm->postProcessorTypeMap(channel);
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)
{
// idx 0 = main / left-eye pre-aperture radiance.
const DtTextureObject* radianceTex =
if (radianceTex != nullptr)
{
rawId = radianceTex->id();
}
// Sensor max radiance (W/cm^2 sr) via the public
// interface; used to convert the normalized value in
// the LDR path (see printClickedRadiance()).
maxRadiancePerCm2 = drawable->getPreApertureMaxRadiance(0);
}
// Ensure the SensorFx effect is actually running on this
// channel.
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);
}
}
}
}
}
// Bind the raw texture id via a draw callback on the post-processor
// quad. myQuad is owned by the base DtPostProcessor and survives
// effect camera re-creations, so attaching to its drawable is stable.
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;
}
};
// For DtPostProcessFactory
class ApertureRadiancePostProcessorCreator : public DtPostProcessorCreator
{
public:
DtPostProcessor* create(DtDe& de, const std::string&)
{
return new ApertureRadiancePostProcessor(de);
}
};
void slot_postInit(DtDe& de)
{
postInitializeConnection.disconnect();
// Register (or look up) a dedicated observer mode so switching to it is
// what enables the effect.
int radianceMode = omMgr.lookupObserverModeKey("Pre-Aperture Radiance");
if (radianceMode < 0)
{
radianceMode = omMgr.createObserverMode("Pre-Aperture Radiance",
"Pre-Aperture Radiance");
}
// Auto-activate an IR sensor when the observer enters the new mode so
// that vrvSenSim populates the pre-aperture radiance texture each frame.
omMgr.setSensor(radianceMode, kSensorModeName);
// Enable the effect for the Pre-Aperture Radiance mode only.
const auto& modes = omMgr.observerModes();
for (auto it = modes.begin(); it != modes.end(); ++it)
{
const bool enable = (it->first == radianceMode);
omMgr.setObserverSetting(it->first, observerSettingId, enable);
}
}
void initializeToolbar(DtDe& de)
{
= DtObserverSettingsManager::instance(de).displayNameProvider();
// setup the text in the menu and the tooltip for the toolbar button
QString qTemp = QObject::tr("Pre-Aperture Radiance");
nameProvider.setDisplayName(observerSettingId,
qTemp = QObject::tr("Show/Hide SensorFx Pre-Aperture Radiance View");
nameProvider.setToolTip(observerSettingId,
}
void init(DtDe& de)
{
// Ensure that init only gets called once
// Registers the built-in post processors
jrmPlugin::init(de);
factory.addCreator("example_aperture_radiance",
new ApertureRadiancePostProcessorCreator);
// Must run AFTER sensor_fx so the pre-aperture radiance texture is
// populated for the current frame before we sample it.
if (!factory.addPostProcessorAfter("example_aperture_radiance", "sensor_fx"))
{
factory.addPostProcessorAtEnd("example_aperture_radiance");
}
// Associate the post processor effect with our observer setting.
factory.registerEffect("example_aperture_radiance", "aperture_radiance_effect",
// initialize default state is when tooltips and display names can be set
boost::bind(&initializeToolbar, std::ref(de)));
// post initialize will be used to register the mode and default the setting
postInitializeConnection = de.signal_postInitialize.connect(
boost::bind(&slot_postInit, std::ref(de)));
// register the menu item for the new effect
de, "ApertureRadianceItem", observerSettingId));
// Register the new visual type to control the effect post processor.
DtVisualTypeManager::instance(de).registerVisualType(
DtVisualTypeManager::VisualType("aperture_radiance", observerSettingId,
(de.dePathConfiguration().iconsPath() + std::string("/UtilityStarWhite.svg")),
}
{
// Setup the plugin
init(*de);
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::DtQtMenuAssembler class.
Contains makVrv::DtSensorController class.
Contains DtSensorFxDrawableInterface class declaration.
Contains makVrv::DtSensorManager class.
int GLint
Definition DtTritonShaderUniformManager.h:27
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
static DtQtMenuAssembler & instance(DtDe &de)
Get an instance of the assembler.
virtual const makVrv::DtTextureObject * getPreApertureRadianceTextureId(int idx) const =0
virtual double getPreApertureMaxRadiance(int idx) const =0
unsigned int id() const
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

examplePreApertureRadiancePlugin.h

/******************************************************************************
** Copyright (c) 2026 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
#pragma once
namespace makVrv { class DtDe; }
// Get proper local export symbol
// Setup proper plugin export symbol
#define DT_DE_PLUGIN_EXPORT_MACRO DT_DLL_EXAMPLEAPERTURERADIANCE
// Declare & export the plugin initialization function (bool initDeModule(DtDe* de))
// Work function for the plugin initialization
#define DT_DLL_EXAMPLEAPERTURERADIANCE
Definition examplePreApertureRadiance.h:18