Adding parameters¶
Introduction¶
You may add parameters to your type plugins, which are fully integrated in the graphical user interface and in the file formats, so that the user may change the node properties.
This is done by overloading the functions getParameters
and getParameter
in your node class,
using the IParamList
and IParamValue
interfaces.
Example¶
We will show how to add a parameter to our Environment plugin example. The blue and orange hemispheres were aligned on the X axis, we would like the user to freely choose the axis.
We start from the previous plugin code, copying the files to test2environmnent.cpp
,
test2environment.h
, and the class to Test2Environment.
Header¶
We first change the type name and pretty names in the new class:
const char * typeName() const { return "test2"; }
const char * prettyTypeName() const { return "SDK Test 2"; }
Then we add a new member variable for storing the parameter value. It is a 3D vector:
private:
Vec3<float> axis;
We set the default axis in the constructor, and keep X:
Test2Environment() : axis(1.0f, 0.0f, 0.0f) { }
Then we remove the default implementation for IEnvironmentPlugin::getParameters()
and IEnvironmentPlugin::setParameter()
in the header:
void getParameters(Ocean::Sdk::IParamList * iParamList);
bool setParameter(const char * parameterName,
const Ocean::Sdk::IParamValue & value);
The full class declaration looks now like:
#include <oceansdk/environmentplugin.h>
class Test2Environment : public Ocean::Sdk::IEnvironmentPlugin
{
public:
Test2Environment() : axis(1.0f, 0.0f, 0.0f) { }
~Test2Environment() { }
void addRadianceValues(Ocean::Sdk::IStokesContext *context,
const Vec3<float> &direction) const;
float samplePdf(Vec3<float> * dest, Ocean::Sdk::ISampler * sampler) const;
float pdf(const Vec3<float> & direction) const;
const char * typeName() const { return "test2"; }
const char * prettyTypeName() const { return "SDK Test 2"; }
Ocean::Sdk::IEnvironmentPlugin * clone() const
{ return new Test2Environment(*this); }
void getParameters(Ocean::Sdk::IParamList * iParamList);
bool setParameter(const char * parameterName,
const Ocean::Sdk::IParamValue & value);
void addChildren(Ocean::Sdk::IAddChildContext * /*iChildAdd*/) { }
bool prepare(const Ocean::Sdk::IPrepareContext * iPrepare);
private:
float wlMin;
float wlMax;
Vec3<float> axis;
};
Implementation¶
In IEnvironmentPlugin::addRadianceValues()
, instead of checking the
sign of direction[0]
, we check the sign of its dot product with axis
:
float l = direction.dotProduct(axis);
if(l>0.0f && wl>500E-9f || l<0.0f && wl<500E-9f)
{
sptr[0][i] += 0.5E9f;
}
No change is required to sampling and pdf functions as we still sample uniformly.
We then implement IEnvironmentPlugin::getParameters to declare the axis, as a vector parameter which is normalized (see IParamList::addUnitVector)
void Test2Environment::getParameters(Ocean::Sdk::IParamList * iParamList)
{
iParamList->addUnitVector("axis", axis);
}
We then implement IEnvironmentPlugin::setParameter to so that Ocean may ask the plugin to update the axis value, when a scene is loaded or when the user changes it:
bool Test2Environment::setParameter(const char * parameterName,
const Ocean::Sdk::IParamValue & value)
{
std::string name(parameterName);
if(name=="axis")
{
axis = value.toVec3();
}
else
{
//We do not recognize the parameter name
return false;
}
}
Result¶
The image shows the result with a tilted axis:
Environment definition:
<environment type="sdktest/test2" name="studio" axis="-0.714143 0.7 0"/>