ADTF Display Toolbox
Loading...
Searching...
No Matches
Source Code for Demo 3D Road Mixin
Location
./src/examples/src/roadmixin
This example shows:
  • how to create a road-like 3D object
  • how to extend the context menu to add object specific actions
  • how to offer a possibility to transform a 3D object
  • how to offer a possibility to select (pick) an object
Header
#pragma once
#include <osgFX/Scribe>
#include <memory>
class cRoadMixin : public adtf::disptb::mixinlib::cMixin, public adtf::disptb::mixinlib::cSensorManipulator::Delegate
{
public:
ADTF_CLASS_ID_NAME(cRoadMixin, "demo_road.3d_mixin.disptb.cid", "Demo 3D Road Mixin");
cRoadMixin();
tResult InitScene() override;
tResult
OnPick(tInt32 nPosX, tInt32 nPosY, tUInt32 nFlags, const adtf::disptb::mixinlib::tNodePath& sNodePath) override;
const adtf::disptb::mixinlib::tNodePath& sNodePath) override;
tResult HandleMenuEvent(const tChar* strMenuText, tVoid* /*pvEventData*/) override;
tVoid ValuesChangedInManipulator(adtf::disptb::mixinlib::cSensorManipulator* pManip) override;
private:
std::unique_ptr<adtf::disptb::mixinlib::cSensorManipulator> m_pSensorManipulator = nullptr;
osg::ref_ptr<osg::PositionAttitudeTransform> m_pRoadtransform = nullptr;
osg::ref_ptr<osgFX::Scribe> m_pScribe = nullptr;
osg::ref_ptr<osg::Switch> m_pPickSwitch = nullptr;
};
Interface for managing a (context) menu.
Definition menu_intf.h:161
Base class for Mixin Filters.
Definition mixin.h:240
virtual tResult AddMenuItemForPick(chewbacca::IMenu &, const tNodePath &)
Return context menu entries for one of your nodes.
Definition mixin.h:345
virtual tResult OnPick(tInt32, tInt32, tUInt32, const tNodePath &)
Handle a left click on one of your nodes.
Definition mixin.h:333
virtual tResult InitScene()
Impelement this to add your graph elements.
Definition mixin.h:443
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Implementation
#include "roadmixin.h"
#include <osg/PositionAttitudeTransform>
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/Switch>
#include <osgFX/Scribe>
ADTF_PLUGIN_VERSION(
"Demo 3D Road Mixin Plugin", disptb, DISPTB_VERSION_MAJOR, DISPTB_VERSION_MINOR, DISPTB_VERSION_PATCH, cRoadMixin)
cRoadMixin::cRoadMixin()
{
SetDescription("Use this Mixin to model a 3D road for the scene.");
adtf::streaming::set_help_link(*this,
"$(ADTF_DISPLAY_TOOLBOX_DIR)/doc/html/page_3d_road_mixin_example_readme.html");
}
tResult cRoadMixin::InitScene()
{
m_pSensorManipulator = std::make_unique<adtf::disptb::mixinlib::cSensorManipulator>();
m_pSensorManipulator->setDelegate(this);
m_pSensorManipulator->setWindowTitle("Road Mixin Manipulator");
m_pRoadtransform = new osg::PositionAttitudeTransform;
// osg nodes are managed with the internal osg reference counting
auto pRoad = new osg::Geode;
m_pRoadtransform->addChild(pRoad);
auto pRoadGeometry = new osg::Geometry;
pRoad->addDrawable(pRoadGeometry);
auto pCoords = new osg::Vec3Array;
auto pColors = new osg::Vec4Array;
auto pNormals = new osg::Vec3Array;
float z_level = 0.02f;
osg::Vec3 road_corner_1(-50, 1.5f, z_level);
pCoords->push_back(road_corner_1);
osg::Vec3 road_corner_2(100, 1.5f, z_level);
pCoords->push_back(road_corner_2);
osg::Vec3 road_corner_3(100, -1.5f, z_level);
pCoords->push_back(road_corner_3);
osg::Vec3 road_corner_4(-50, -1.5f, z_level);
pCoords->push_back(road_corner_4);
pNormals->push_back(osg::Vec3(0, 0, 1));
pColors->push_back(osg::Vec4(.2f, .2f, .3f, 1.0f));
pRoadGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));
pRoadGeometry->setVertexArray(pCoords);
pRoadGeometry->setColorArray(pColors, osg::Array::BIND_PER_PRIMITIVE_SET);
pRoadGeometry->setNormalArray(pNormals, osg::Array::BIND_PER_PRIMITIVE_SET);
m_pScribe = new osgFX::Scribe();
m_pScribe->addChild(m_pRoadtransform);
m_pPickSwitch = new osg::Switch;
m_pPickSwitch->addChild(m_pRoadtransform);
m_pPickSwitch->addChild(m_pScribe);
m_pPickSwitch->setSingleChildOn(0);
GetRoot()->addChild(m_pPickSwitch);
RETURN_NOERROR;
}
tResult cRoadMixin::OnPick(tInt32 /*nPosX*/,
tInt32 /*nPosY*/,
tUInt32 nFlags,
const adtf::disptb::mixinlib::tNodePath& /*sNodePath*/)
{
if (nFlags & INodeHandler::CM_LEFT_MB)
{
if (m_pPickSwitch->getChildValue(m_pRoadtransform))
{
m_pPickSwitch->setSingleChildOn(1);
}
else
{
m_pPickSwitch->setSingleChildOn(0);
}
}
RETURN_NOERROR;
}
tResult cRoadMixin::AddMenuItemForPick(adtf::disptb::mixinlib::IMenu& oMenu,
const adtf::disptb::mixinlib::tNodePath& /*sNodePath*/)
{
std::string strName;
GetName(adtf_string_intf(strName));
adtf::disptb::mixinlib::IMenuItem* pMenuItem = oMenu.AddMenuItem((strName + " Properties").c_str());
pMenuItem->SetCheckable(tTrue);
pMenuItem->SetChecked(m_pSensorManipulator->isVisible());
pMenuItem->SetEventHandler(this);
RETURN_NOERROR;
}
tResult cRoadMixin::HandleMenuEvent(const tChar* /*strMenuText*/, tVoid* /*pvEventData*/)
{
// we only have one menu entry, so we do not have to check the text
if (m_pSensorManipulator->isVisible())
{
m_pSensorManipulator->hide();
}
else
{
m_pSensorManipulator->show();
}
RETURN_NOERROR;
}
void cRoadMixin::ValuesChangedInManipulator(adtf::disptb::mixinlib::cSensorManipulator* pManip)
{
tFloat64 fX, fY, fZ;
tFloat64 fRoll, fPitch, fYaw;
if (m_pRoadtransform)
{
// get values
pManip->getXYZ(fX, fY, fZ);
pManip->getRollPitchYawRad(fRoll, fPitch, fYaw);
m_pRoadtransform->setPosition(osg::Vec3d(fX, fY, fZ));
m_pRoadtransform->setAttitude(
osg::Quat(fRoll, osg::Vec3d(1, 0, 0), fPitch, osg::Vec3d(0, 1, 0), fYaw, osg::Vec3d(0, 0, 1)));
}
}
virtual adtf::disptb::mixinlib::chewbacca::IMenuItem * AddMenuItem(const tChar *strText)=0
Creates a new menu item.
Interface for creating context menu entries.
Definition menu_intf.h:43
virtual tVoid SetCheckable(tBool bCheckable)=0
Mark this item as checkable.
virtual tVoid SetChecked(tBool bChecked)=0
Set chack state of this item.
virtual tVoid SetEventHandler(IMenuEventSink *pEvent)=0
Set an event handler.