INET Framework for OMNeT++/OMNEST
inet::visualizer::SceneOsgVisualizerBase Class Reference

#include <SceneOsgVisualizerBase.h>

Inheritance diagram for inet::visualizer::SceneOsgVisualizerBase:
inet::visualizer::SceneVisualizerBase inet::visualizer::VisualizerBase inet::visualizer::SceneOsgEarthVisualizer inet::visualizer::SceneOsgVisualizer

Protected Member Functions

virtual void initializeScene ()
 
virtual void initializeAxis (double axisLength)
 
virtual void initializeSceneFloor ()
 
virtual osg::Geode * createSceneFloor (const Coord &min, const Coord &max, cFigure::Color &color, osg::Image *image, double imageSize, double opacity, bool shading) const
 
virtual osg::BoundingSphere getNetworkBoundingSphere ()
 
- Protected Member Functions inherited from inet::visualizer::SceneVisualizerBase
virtual void initialize (int stage) override
 
virtual Box getSceneBounds ()
 
- Protected Member Functions inherited from inet::visualizer::VisualizerBase
virtual int numInitStages () const override
 
virtual Coord getPosition (const cModule *networkNode) const
 
virtual Coord getContactPosition (const cModule *networkNode, const Coord &fromPosition, const char *contactMode, double contactSpacing) const
 
virtual Quaternion getOrientation (const cModule *networkNode) const
 
virtual void mapChunks (const Ptr< const Chunk > &chunk, const std::function< void(const Ptr< const Chunk > &, int)> &thunk) const
 

Additional Inherited Members

- Protected Attributes inherited from inet::visualizer::SceneVisualizerBase
Coord sceneMin
 
Coord sceneMax
 
- Protected Attributes inherited from inet::visualizer::VisualizerBase
cModule * visualizationTargetModule = nullptr
 
cModule * visualizationSubjectModule = nullptr
 
const char * tags = nullptr
 

Member Function Documentation

◆ createSceneFloor()

osg::Geode * inet::visualizer::SceneOsgVisualizerBase::createSceneFloor ( const Coord min,
const Coord max,
cFigure::Color &  color,
osg::Image *  image,
double  imageSize,
double  opacity,
bool  shading 
) const
protectedvirtual
89 {
90  auto dx = max.x - min.x;
91  auto dy = max.y - min.y;
92 
93  if (!std::isfinite(dx) || !std::isfinite(dy))
94  return new osg::Geode();
95 
96  auto d = shading ? sqrt(dx * dx + dy * dy) : 0;
97  auto width = dx + 2 * d;
98  auto height = dy + 2 * d;
99  auto r = width / imageSize / 2;
100  auto t = height / imageSize / 2;
101  osg::Geometry *geometry = nullptr;
102  if (image == nullptr)
103  geometry = inet::osg::createQuadGeometry(Coord(min.x, min.y, 0.0), Coord(max.x, max.y, 0.0));
104  else
105  geometry = osg::createTexturedQuadGeometry(osg::Vec3(min.x - d, min.y - d, 0.0), osg::Vec3(width, 0.0, 0.0), osg::Vec3(0.0, height, 0.0), -r, -t, r, t);
106  auto stateSet = inet::osg::createStateSet(color, opacity, false);
107  geometry->setStateSet(stateSet);
108  osg::Texture2D *texture = nullptr;
109  if (image != nullptr) {
110  texture = new osg::Texture2D();
111  texture->setImage(image);
112  texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
113  texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
114  }
115  stateSet->setTextureAttributeAndModes(0, texture);
116  if (shading) {
117  auto program = new osg::Program();
118  auto vertexShader = new osg::Shader(osg::Shader::VERTEX);
119  auto fragmentShader = new osg::Shader(osg::Shader::FRAGMENT);
120  vertexShader->setShaderSource(R"(
121  varying vec4 verpos;
122  void main() {
123  gl_Position = ftransform();
124  verpos = gl_Vertex;
125  gl_TexCoord[0]=gl_MultiTexCoord0;
126  })");
127  if (texture != nullptr) {
128  fragmentShader->setShaderSource(R"(
129  varying vec4 verpos;
130  uniform vec3 center;
131  uniform float min, max;
132  uniform sampler2D texture;
133  void main(void) {
134  float alpha = 1.0 - smoothstep(min, max, length(verpos.xyz - center));
135  gl_FragColor = vec4(texture2D(texture, gl_TexCoord[0].xy).rgb, alpha);
136  })");
137  stateSet->addUniform(new osg::Uniform("texture", 0));
138  }
139  else {
140  fragmentShader->setShaderSource(R"(
141  varying vec4 verpos;
142  uniform vec3 center;
143  uniform vec3 color;
144  uniform float min, max;
145  void main(void) {
146  float alpha = 1.0 - smoothstep(min, max, length(verpos.xyz - center));
147  gl_FragColor = vec4(color, alpha);
148  })");
149  stateSet->addUniform(new osg::Uniform("color", osg::Vec3((double)color.red / 255.0, (double)color.green / 255.0, (double)color.blue / 255.0)));
150  }
151  program->addShader(vertexShader);
152  program->addShader(fragmentShader);
153  auto center = (max + min) / 2;
154  stateSet->addUniform(new osg::Uniform("center", osg::Vec3(center.x, center.y, center.z)));
155  stateSet->addUniform(new osg::Uniform("min", (float)d / 2));
156  stateSet->addUniform(new osg::Uniform("max", (float)d));
157  stateSet->setAttributeAndModes(program, osg::StateAttribute::ON);
158  }
159  auto polygonOffset = new osg::PolygonOffset();
160  polygonOffset->setFactor(1.0);
161  polygonOffset->setUnits(1.0);
162  stateSet->setAttributeAndModes(polygonOffset, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
163  auto geode = new osg::Geode();
164  geode->addDrawable(geometry);
165  return geode;
166 }

Referenced by initializeSceneFloor().

◆ getNetworkBoundingSphere()

osg::BoundingSphere inet::visualizer::SceneOsgVisualizerBase::getNetworkBoundingSphere ( )
protectedvirtual
169 {
170  int nodeCount = 0;
171  auto nodes = new osg::Group();
172  auto networkNodeVisualizer = getModuleFromPar<NetworkNodeOsgVisualizer>(par("networkNodeVisualizerModule"), this);
173  for (cModule::SubmoduleIterator it(visualizationSubjectModule); !it.end(); it++) {
174  auto networkNode = *it;
175  if (isNetworkNode(networkNode)) {
176  nodeCount++;
177  // NOTE: ignore network node annotations
178  if (auto networkNodeVisualization = networkNodeVisualizer->findNetworkNodeVisualization(networkNode)) {
179  auto mainNode = networkNodeVisualization->getMainPart();
180  auto radius = std::max(0.0f, mainNode->computeBound().radius());
181  auto drawable = new osg::ShapeDrawable(new osg::Sphere(networkNodeVisualization->getPosition(), radius));
182  auto geode = new osg::Geode();
183  geode->addDrawable(drawable);
184  nodes->addChild(geode);
185  }
186  }
187  }
188  if (nodeCount == 0)
189  return osg::BoundingSphere(osg::Vec3d(0, 0, 0), 100);
190  else if (nodeCount == 1)
191  return osg::BoundingSphere(nodes->getBound().center(), 100);
192  else
193  return nodes->getBound();
194 }

Referenced by inet::visualizer::SceneOsgVisualizer::initializeViewpoint().

◆ initializeAxis()

void inet::visualizer::SceneOsgVisualizerBase::initializeAxis ( double  axisLength)
protectedvirtual
57 {
58  auto geode = new osg::Group();
59  geode->addChild(inet::osg::createLine(Coord::ZERO, Coord(axisLength, 0.0, 0.0), cFigure::ARROW_NONE, cFigure::ARROW_BARBED));
60  geode->addChild(inet::osg::createLine(Coord::ZERO, Coord(0.0, axisLength, 0.0), cFigure::ARROW_NONE, cFigure::ARROW_BARBED));
61  geode->addChild(inet::osg::createLine(Coord::ZERO, Coord(0.0, 0.0, axisLength), cFigure::ARROW_NONE, cFigure::ARROW_BARBED));
62  auto stateSet = inet::osg::createStateSet(cFigure::BLACK, 1.0, false);
63  geode->setStateSet(stateSet);
65  scene->addChild(geode);
66  double spacing = 1;
67  scene->addChild(inet::osg::createAutoTransform(inet::osg::createText("X", Coord::ZERO, cFigure::BLACK), osg::AutoTransform::ROTATE_TO_SCREEN, true, Coord(axisLength + spacing, 0.0, 0.0)));
68  scene->addChild(inet::osg::createAutoTransform(inet::osg::createText("Y", Coord::ZERO, cFigure::BLACK), osg::AutoTransform::ROTATE_TO_SCREEN, true, Coord(0.0, axisLength + spacing, 0.0)));
69  scene->addChild(inet::osg::createAutoTransform(inet::osg::createText("Z", Coord::ZERO, cFigure::BLACK), osg::AutoTransform::ROTATE_TO_SCREEN, true, Coord(0.0, 0.0, axisLength + spacing)));
70 }

Referenced by inet::visualizer::SceneOsgVisualizer::initialize().

◆ initializeScene()

void inet::visualizer::SceneOsgVisualizerBase::initializeScene ( )
protectedvirtual

Reimplemented in inet::visualizer::SceneOsgVisualizer.

25 {
26  auto osgCanvas = visualizationTargetModule->getOsgCanvas();
27  if (osgCanvas->getScene() != nullptr)
28  throw cRuntimeError("OSG canvas scene at '%s' has been already initialized", visualizationTargetModule->getFullPath().c_str());
29  else {
30  auto topLevelScene = new inet::osg::TopLevelScene();
31  osgCanvas->setScene(topLevelScene);
32  const char *clearColor = par("clearColor");
33  if (*clearColor != '\0')
34  osgCanvas->setClearColor(cFigure::Color(clearColor));
35  osgCanvas->setZNear(par("zNear"));
36  osgCanvas->setZFar(par("zFar"));
37  osgCanvas->setFieldOfViewAngle(par("fieldOfView"));
38  const char *cameraManipulatorString = par("cameraManipulator");
39  cOsgCanvas::CameraManipulatorType cameraManipulator;
40  if (!strcmp(cameraManipulatorString, "auto"))
41  cameraManipulator = cOsgCanvas::CAM_AUTO;
42  else if (!strcmp(cameraManipulatorString, "trackball"))
43  cameraManipulator = cOsgCanvas::CAM_TRACKBALL;
44  else if (!strcmp(cameraManipulatorString, "terrain"))
45  cameraManipulator = cOsgCanvas::CAM_TERRAIN;
46  else if (!strcmp(cameraManipulatorString, "overview"))
47  cameraManipulator = cOsgCanvas::CAM_OVERVIEW;
48  else if (!strcmp(cameraManipulatorString, "earth"))
49  cameraManipulator = cOsgCanvas::CAM_EARTH;
50  else
51  throw cRuntimeError("Unknown camera manipulator: '%s'", cameraManipulatorString);
52  osgCanvas->setCameraManipulatorType(cameraManipulator);
53  }
54 }

Referenced by inet::visualizer::SceneOsgVisualizer::initializeScene().

◆ initializeSceneFloor()

void inet::visualizer::SceneOsgVisualizerBase::initializeSceneFloor ( )
protectedvirtual
73 {
74  Box sceneBounds = getSceneBounds();
75  if (sceneBounds.getMin() != sceneBounds.getMax()) {
76  const char *imageName = par("sceneImage");
77  osg::Image *image = *imageName ? inet::osg::createImageFromResource(imageName) : nullptr;
78  double imageSize = par("sceneImageSize");
79  auto color = cFigure::Color(par("sceneColor"));
80  double opacity = par("sceneOpacity");
81  bool shading = par("sceneShading");
82  auto sceneFloor = createSceneFloor(sceneBounds.getMin(), sceneBounds.getMax(), color, image, imageSize, opacity, shading);
84  scene->addChild(sceneFloor);
85  }
86 }

Referenced by inet::visualizer::SceneOsgVisualizer::initialize().


The documentation for this class was generated from the following files:
inet::osg::createLine
osg::Node * createLine(const Coord &start, const Coord &end, cFigure::Arrowhead startArrowhead, cFigure::Arrowhead endArrowhead)
Definition: OsgUtils.cc:171
inet::Coord::ZERO
static const Coord ZERO
Definition: Coord.h:27
inet::visualizer::SceneVisualizerBase::getSceneBounds
virtual Box getSceneBounds()
Definition: SceneVisualizerBase.cc:35
inet::sctp::min
double min(const double a, const double b)
Returns the minimum of a and b.
Definition: SctpAssociation.h:261
inet::osg::createText
osgText::Text * createText(const char *string, const Coord &position, const cFigure::Color &color)
Definition: OsgUtils.cc:199
inet::osg::createQuadGeometry
Geometry * createQuadGeometry(const Coord &min, const Coord &max)
Definition: OsgUtils.cc:135
inet::visualizer::VisualizerBase::visualizationTargetModule
cModule * visualizationTargetModule
Definition: VisualizerBase.h:25
inet::osg::TopLevelScene::getSimulationScene
virtual SimulationScene * getSimulationScene()
Definition: OsgScene.cc:22
inet::units::sqrt
value< Value, pow< Unit, 1, 2 > > sqrt(const value< Value, Unit > &a)
Definition: Units.h:272
inet::visualizer::SceneOsgVisualizerBase::createSceneFloor
virtual osg::Geode * createSceneFloor(const Coord &min, const Coord &max, cFigure::Color &color, osg::Image *image, double imageSize, double opacity, bool shading) const
Definition: SceneOsgVisualizerBase.cc:88
inet::isNetworkNode
bool isNetworkNode(const cModule *mod)
Returns true if the given module is a network node, i.e.
Definition: ModuleAccess.cc:18
inet::sctp::max
double max(const double a, const double b)
Returns the maximum of a and b.
Definition: SctpAssociation.h:266
inet::osg::createImageFromResource
osg::Image * createImageFromResource(const char *imageName)
Definition: OsgUtils.cc:250
inet::osg::createStateSet
StateSet * createStateSet(const cFigure::Color &color, double opacity, bool cullBackFace)
Definition: OsgUtils.cc:273
inet::osg::TopLevelScene
This class is used for the topmost node in the OSG node hierarchy.
Definition: OsgScene.h:49
inet::DiffservUtil::Color
Color
Definition: DiffservUtil.h:17
inet::visualizer::VisualizerBase::visualizationSubjectModule
cModule * visualizationSubjectModule
Definition: VisualizerBase.h:26
inet::osg::createAutoTransform
AutoTransform * createAutoTransform(Drawable *drawable, AutoTransform::AutoRotateMode mode, bool autoScaleToScreen, const Coord &position)
Definition: OsgUtils.cc:209