SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLHorizonNode Class Reference

Scene node that visualises the horizon estimated from the device rotation. More...

#include <SLHorizonNode.h>

Inheritance diagram for SLHorizonNode:
[legend]

Public Member Functions

 SLHorizonNode (SLstring name, SLDeviceRotation *devRot, SLTexFont *font, SLstring shaderDir, int scrW, int scrH)
 Builds the horizon line and its text label. More...
 
 ~SLHorizonNode ()
 Deletes the program, material and meshes created in the constructor. More...
 
void doUpdate () override
 Re-aligns the horizon line and refreshes the angle label. More...
 
- Public Member Functions inherited from SLNode
 SLNode (const SLstring &name="Node")
 Construct a new SLNode::SLNode object. More...
 
 SLNode (SLMesh *mesh, const SLstring &name="Node")
 
 SLNode (SLMesh *mesh, const SLVec3f &translation, const SLstring &name)
 
 ~SLNode () override
 
virtual void cull3DRec (SLSceneView *sv)
 
virtual void cullChildren3D (SLSceneView *sv)
 Initializer function to call SLNode::cull3DRec recursively. More...
 
virtual void cull2DRec (SLSceneView *sv)
 
virtual bool hitRec (SLRay *ray)
 
virtual void statsRec (SLNodeStats &stats)
 
virtual SLNodecopyRec ()
 
virtual SLAABBoxupdateAABBRec (SLbool updateAlsoAABBinOS)
 
virtual void dumpRec ()
 
void setDrawBitsRec (SLuint bit, SLbool state)
 
void setPrimitiveTypeRec (SLGLPrimitiveType primitiveType)
 
virtual void addMesh (SLMesh *mesh)
 
virtual void drawMesh (SLSceneView *sv)
 Draws the single mesh. More...
 
bool removeMesh ()
 Returns true if a mesh was assigned and set it to nullptr. More...
 
bool removeMesh (SLMesh *mesh)
 Returns true if the passed mesh was assigned and sets it to nullptr. More...
 
SLint numChildren ()
 
void addChild (SLNode *child)
 
bool insertChild (SLNode *insertC, SLNode *afterC)
 
void deleteChildren ()
 
bool deleteChild ()
 
bool deleteChild (SLNode *child)
 
bool deleteChild (const SLstring &name)
 
bool removeChild (SLNode *child)
 remove child from vector of children. Removes false if not found, else true. More...
 
template<typename T >
T * find (const SLstring &name="", SLbool findRecursive=true)
 
template<typename T >
T * findChild (const SLstring &name="", SLbool findRecursive=true)
 
template<typename T >
deque< T * > findChildren (const SLstring &name="", SLbool findRecursive=true, SLbool canContain=false)
 
deque< SLNode * > findChildren (const SLMesh *mesh, SLbool findRecursive=true)
 
deque< SLNode * > findChildren (SLuint drawbit, SLbool findRecursive=true)
 
SLVec3f translationOS () const
 
SLVec3f forwardOS () const
 
SLVec3f rightOS () const
 
SLVec3f upOS () const
 
SLVec3f axisXOS () const
 
SLVec3f axisYOS () const
 
SLVec3f axisZOS () const
 
SLVec3f translationWS () const
 
SLVec3f forwardWS () const
 
SLVec3f rightWS () const
 
SLVec3f upWS () const
 
SLVec3f axisXWS () const
 
SLVec3f axisYWS () const
 
SLVec3f axisZWS () const
 
void translation (const SLVec3f &pos, SLTransformSpace relativeTo=TS_parent)
 
void translation (SLfloat x, SLfloat y, SLfloat z, SLTransformSpace relativeTo=TS_parent)
 
void rotation (const SLQuat4f &rot, SLTransformSpace relativeTo=TS_parent)
 
void rotation (SLfloat angleDeg, const SLVec3f &axis, SLTransformSpace relativeTo=TS_parent)
 
void scaling (SLfloat s)
 
void scaling (SLfloat x, SLfloat y, SLfloat z)
 
void scaling (const SLVec3f &scaling)
 
void lookAt (SLfloat targetX, SLfloat targetY, SLfloat targetZ, SLfloat upX=0, SLfloat upY=1, SLfloat upZ=0, SLTransformSpace relativeTo=TS_world)
 
void lookAt (const SLVec3f &target, const SLVec3f &up=SLVec3f::AXISY, SLTransformSpace relativeTo=TS_world)
 
void translate (const SLVec3f &vec, SLTransformSpace relativeTo=TS_object)
 
void translate (SLfloat x, SLfloat y, SLfloat z, SLTransformSpace relativeTo=TS_object)
 
void rotate (const SLQuat4f &rot, SLTransformSpace relativeTo=TS_object)
 
void rotate (SLfloat angleDeg, const SLVec3f &axis, SLTransformSpace relativeTo=TS_object)
 
void rotate (SLfloat angleDeg, SLfloat x, SLfloat y, SLfloat z, SLTransformSpace relativeTo=TS_object)
 
void rotateAround (const SLVec3f &point, SLVec3f &axis, SLfloat angleDeg, SLTransformSpace relativeTo=TS_world)
 
void scale (SLfloat s)
 
void scale (SLfloat x, SLfloat y, SLfloat z)
 
void scale (const SLVec3f &scale)
 
void scaleToCenter (SLfloat maxDim)
 
void setInitialState ()
 
void resetToInitialState ()
 
void parent (SLNode *p)
 
void entityID (SLint entityID)
 
void om (const SLMat4f &mat)
 
void animation (SLAnimation *a)
 
void castsShadows (SLbool castsShadows)
 
virtual void needUpdate ()
 
void needWMUpdate ()
 
void needAABBUpdate ()
 
void isSelected (bool isSelected)
 
void minLodCoverage (SLfloat minLodCoverage)
 
void levelForSM (SLubyte lfsm)
 
void onUpdateCB (function< void()> callbackFunc)
 
SLNodeparent ()
 
SLint depth () const
 
SLint entityID () const
 
const SLMat4fom ()
 
const SLMat4finitialOM ()
 
const SLMat4fupdateAndGetWM () const
 
const SLMat4fupdateAndGetWMI () const
 
SLDrawBitsdrawBits ()
 
SLbool drawBit (SLuint bit)
 
SLAABBoxaabb ()
 
SLAnimationanimation ()
 
SLbool castsShadows ()
 
SLMeshmesh ()
 
SLVNodechildren ()
 
const SLAnimSkeletonskeleton ()
 Returns the first skeleton found in the meshes. More...
 
void updateRec ()
 
bool updateMeshSkins (bool forceCPUSkinning, const std::function< void(SLMesh *)> &cbInformNodes)
 Update all skinned meshes recursively. More...
 
void updateMeshAccelStructs ()
 
void updateMeshMat (std::function< void(SLMaterial *m)> setMat, bool recursive)
 Updates the mesh material recursively with a material lambda. More...
 
void setMeshMat (SLMaterial *mat, bool recursive)
 Set the mesh material recursively. More...
 
bool isSelected ()
 
SLfloat minLodCoverage ()
 
SLubyte levelForSM ()
 
- Public Member Functions inherited from SLObject
 SLObject (const SLstring &Name="", const SLstring &url="")
 
virtual ~SLObject ()
 
void name (const SLstring &Name)
 
void url (const SLstring &url)
 
const SLstringname () const
 
const SLstringurl () const
 
- Public Member Functions inherited from SLEventHandler
 SLEventHandler ()
 
virtual ~SLEventHandler ()
 
virtual SLbool onMouseDown (const SLMouseButton button, const SLint x, const SLint y, const SLKey mod)
 
virtual SLbool onMouseUp (const SLMouseButton button, const SLint x, const SLint y, const SLKey mod)
 
virtual SLbool onMouseMove (const SLMouseButton button, const SLint x, const SLint y, const SLKey mod)
 
virtual SLbool onDoubleClick (const SLMouseButton button, const SLint x, const SLint y, const SLKey mod)
 
virtual SLbool onMouseWheel (const SLint delta, const SLKey mod)
 
virtual SLbool onTouch2Down (const SLint x1, const SLint y1, const SLint x2, const SLint y2)
 
virtual SLbool onTouch2Move (const SLint x1, const SLint y1, const SLint x2, const SLint y2)
 
virtual SLbool onTouch2Up (const SLint x1, const SLint y1, const SLint x2, const SLint y2)
 
virtual SLbool onTouch3Down (const SLint x1, const SLint y1)
 
virtual SLbool onTouch3Move (const SLint x1, const SLint y1)
 
virtual SLbool onTouch3Up (const SLint x1, const SLint y1)
 
virtual SLbool onKeyPress (const SLKey key, const SLKey mod)
 
virtual SLbool onKeyRelease (const SLKey key, const SLKey mod)
 
virtual SLbool onRotationPYR (const SLfloat pitchRAD, const SLfloat yawRAD, const SLfloat rollRAD)
 
void mouseRotationFactor (SLfloat rf)
 
SLfloat mouseRotationFactor ()
 

Private Attributes

SLDeviceRotation_devRot = nullptr
 Rotation sensor, not owned. More...
 
SLTexFont_font = nullptr
 Font of the angle label, not owned. More...
 
SLstring _shaderDir
 Directory of the line shader. More...
 
SLGLProgram_prog = nullptr
 Program drawing the horizon line. More...
 
SLMaterial_mat = nullptr
 Material of the horizon line. More...
 
SLPolyline_line = nullptr
 Mesh of the horizon line. More...
 
SLNode_horizonNode = nullptr
 Child node carrying the line. More...
 
SLNode_textNode = nullptr
 Child node carrying the angle text. More...
 
SLMat3f _sRc
 Sensor to camera rotation used by the horizon estimation. More...
 

Additional Inherited Members

- Static Public Attributes inherited from SLNode
static SLuint numWMUpdates = 0
 NO. of calls to updateWMRec per frame. More...
 
static unsigned int instanceIndex = 0
 ??? More...
 
- Protected Attributes inherited from SLNode
SLNode_parent
 pointer to the parent node More...
 
SLVNode _children
 vector of children nodes More...
 
SLMesh_mesh
 pointer to a single mesh More...
 
SLint _depth
 depth of the node in a scene tree More...
 
SLint _entityID
 ID in the SLVEntity graph for Data Oriented Design. More...
 
SLMat4f _om
 object matrix for local transforms More...
 
SLMat4f _initialOM
 the initial om state More...
 
SLMat4f _wm
 world matrix for world transform More...
 
SLMat4f _wmI
 inverse world matrix More...
 
SLbool _isWMUpToDate
 is the WM of this node still valid More...
 
SLbool _isWMIUpToDate
 is the inverse WM of this node still valid More...
 
SLbool _isAABBUpToDate
 is the saved aabb still valid More...
 
bool _castsShadows
 flag if meshes of node should cast shadows More...
 
bool _isSelected
 flag if node and one or more of its meshes are selected More...
 
SLDrawBits _drawBits
 node level drawing flags More...
 
SLAABBox _aabb
 axis aligned bounding box More...
 
SLAnimation_animation
 animation of the node More...
 
SLfloat _minLodCoverage
 Min. LOD coverage for visibility (0.0 < _minLodCoverage < 1.0) More...
 
SLubyte _levelForSM
 Level of LOD to use for shadow mapping (0 = the visible one will be drawn) More...
 
function< void()> _onUpdateCB
 Optional lambda callback once per update. More...
 
- Protected Attributes inherited from SLObject
SLstring _name
 name of an object More...
 
SLstring _url
 uniform resource locator More...
 
- Protected Attributes inherited from SLEventHandler
SLfloat _mouseRotationFactor
 Mouse rotation sensibility. More...
 
SLfloat _keyboardDeltaPos
 Delta dist. for keyboard translation. More...
 

Detailed Description

Scene node that visualises the horizon estimated from the device rotation.

The node owns two children: a polyline drawn as the horizon itself and an SLText showing the roll angle in degrees. Every frame doUpdate() reads the averaged rotation from the SLDeviceRotation passed to the constructor, derives the horizon direction with SLAlgo::estimateHorizon and rotates the line to match. The displayed angle is negated, so it expresses the rotation of the device with respect to the horizon rather than the other way round.

Intended for AR scenes on mobile devices, where it gives the user a reference for how far the device is tilted.

Definition at line 32 of file SLHorizonNode.h.

Constructor & Destructor Documentation

◆ SLHorizonNode()

SLHorizonNode::SLHorizonNode ( SLstring  name,
SLDeviceRotation devRot,
SLTexFont font,
SLstring  shaderDir,
int  scrW,
int  scrH 
)

Builds the horizon line and its text label.

Construct a new SLHorizonNode::SLHorizonNode object.

Parameters
nameName of the node
devRotRotation sensor to read the orientation from, not owned
fontFont for the angle label; if nullptr no label is shown
shaderDirDirectory the line shader is loaded from
scrWScreen width in pixels, used to size the line
scrHScreen height in pixels, used to size the line
Remarks
It is important that during instantiation NO OpenGL functions (gl*) get called because this constructor will be most probably called in a parallel thread from within an SLScene::registerAssetsToLoad or SLScene::assemble function. All objects that get rendered have to do their OpenGL initialization when they are used the first time during rendering in the main thread.
Parameters
nameName of the node
devRotDevice rotation object
fontFont for the text label
shaderDirPath to the shader directory
scrWWidth of the screen in pixels
scrHHeight of the sceen in pixels

Definition at line 30 of file SLHorizonNode.cpp.

36  : SLNode(name),
37  _devRot(devRot),
38  _font(font),
39  _shaderDir(shaderDir)
40 {
41  // make sure device rotation is enabled
42  if (!_devRot->isUsed())
43  _devRot->isUsed(true);
44 
45  // rotation of camera w.r.t sensor
46  _sRc.rotation(-90, 0, 0, 1);
47 
48  // init visualization node and meshes
49  //(this node is owner of instantiated programs, meshes and materials)
50  _prog = new SLGLProgramGeneric(nullptr,
51  shaderDir + "ColorUniformPoint.vert",
52  shaderDir + "Color.frag");
54  "u_pointSize",
55  1.0f));
56  _mat = new SLMaterial(nullptr, _prog, SLCol4f::WHITE, "White");
57  // define mesh points
58  int refLen = std::min(scrW, scrH);
59  SLfloat cs; // center size
60  if (_font)
61  {
62  SLfloat scale = 1.f;
63  SLstring txt = "-359.9";
64  SLVec2f size = _font->calcTextSize(txt);
65  cs = size.x;
66  }
67  else
68  cs = (float)refLen * 0.01f; // center size
69 
70  float l = (float)refLen * 0.35f;
71 
72  SLVVec3f points = {{-l, 0, 0},
73  {-cs, 0, 0},
74  {0, -cs, 0},
75  {cs, 0, 0},
76  {l, 0, 0},
77  {cs, 0, 0},
78  {0, cs, 0},
79  {-cs, 0, 0}};
80 
81  _line = new SLPolyline(nullptr,
82  points,
83  true,
84  "Horizon line",
85  _mat);
86  _horizonNode = new SLNode(_line, "Horizon node");
87  this->addChild(_horizonNode);
88 }
float SLfloat
analog to GLfloat
Definition: SL.h:200
string SLstring
Redefinition of standard types for platform independence.
Definition: SL.h:185
@ UT_const
constant value
Definition: SLEnums.h:233
SLGLUniform< SLfloat > SLGLUniform1f
Definition: SLGLUniform.h:162
vector< SLVec3f > SLVVec3f
Definition: SLVec3.h:325
void isUsed(SLbool isUsed)
Setter that turns on the device rotation sensor.
Generic Shader Program class inherited from SLGLProgram.
void addUniform1f(SLGLUniform1f *u)
add float uniform
SLGLProgram * _prog
Program drawing the horizon line.
Definition: SLHorizonNode.h:62
SLTexFont * _font
Font of the angle label, not owned.
Definition: SLHorizonNode.h:59
SLNode * _horizonNode
Child node carrying the line.
Definition: SLHorizonNode.h:65
SLstring _shaderDir
Directory of the line shader.
Definition: SLHorizonNode.h:60
SLMat3f _sRc
Sensor to camera rotation used by the horizon estimation.
Definition: SLHorizonNode.h:68
SLPolyline * _line
Mesh of the horizon line.
Definition: SLHorizonNode.h:64
SLDeviceRotation * _devRot
Rotation sensor, not owned.
Definition: SLHorizonNode.h:58
SLMaterial * _mat
Material of the horizon line.
Definition: SLHorizonNode.h:63
void rotation(const T angleDEG, const SLVec3< T > &axis)
Sets the rotation components
Definition: SLMat3.h:392
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
void addChild(SLNode *child)
Definition: SLNode.cpp:207
void scale(SLfloat s)
Definition: SLNode.h:641
SLNode(const SLstring &name="Node")
Construct a new SLNode::SLNode object.
Definition: SLNode.cpp:40
const SLstring & name() const
Definition: SLObject.h:38
SLPolyline creates a polyline object.
Definition: SLPolyline.h:23
SLVec2f calcTextSize(const SLstring &text, SLfloat maxWidth=0.0f, SLfloat lineHeightFactor=1.5f)
Definition: SLTexFont.cpp:243
T x
Definition: SLVec2.h:30
static SLVec4 WHITE
Definition: SLVec4.h:215

◆ ~SLHorizonNode()

SLHorizonNode::~SLHorizonNode ( )

Deletes the program, material and meshes created in the constructor.

Definition at line 90 of file SLHorizonNode.cpp.

91 {
92  delete _prog;
93  delete _mat;
94  delete _line;
95 }

Member Function Documentation

◆ doUpdate()

void SLHorizonNode::doUpdate ( )
overridevirtual

Re-aligns the horizon line and refreshes the angle label.

Called once per frame by the scene update. The text node is destroyed and rebuilt on every call because the label text changes.

Reimplemented from SLNode.

Definition at line 97 of file SLHorizonNode.cpp.

98 {
99  // get latest orientation and update horizon
100  SLVec3f horizon;
102 
103  // rotate node to align it to horizon
104  float horizonAngle = std::atan2(horizon.y, horizon.x) * RAD2DEG;
105  _horizonNode->rotation(horizonAngle,
106  SLVec3f(0, 0, 1),
108 
109  // update text
110  if (_font)
111  {
112  if (_textNode)
113  this->deleteChild(_textNode);
114 
115  stringstream ss;
116  // we invert the sign to express the rotation of the device w.r.t the horizon
117  ss << std::fixed << std::setprecision(1) << -horizonAngle;
118  SLstring txt = ss.str();
119 
120  SLVec2f size = _font->calcTextSize(txt);
121  _textNode = new SLText(txt, _font, SLCol4f::WHITE);
122  _textNode->translate(-size.x * 0.5f, -size.y * 0.5f, 0);
123  this->addChild(_textNode);
124  }
125 }
@ TS_object
Definition: SLEnums.h:210
SLVec3< SLfloat > SLVec3f
Definition: SLVec3.h:318
SLMat3f rotationAveraged()
SLNode * _textNode
Child node carrying the angle text.
Definition: SLHorizonNode.h:66
void rotation(const SLQuat4f &rot, SLTransformSpace relativeTo=TS_parent)
Definition: SLNode.cpp:846
bool deleteChild()
Definition: SLNode.cpp:267
void translate(const SLVec3f &vec, SLTransformSpace relativeTo=TS_object)
Definition: SLNode.cpp:906
SLText creates a mesh using a textured font from SLTexFont.
Definition: SLText.h:30
T y
Definition: SLVec2.h:30
T y
Definition: SLVec3.h:43
T x
Definition: SLVec3.h:43
bool estimateHorizon(const SLMat3f &enuRs, const SLMat3f &sRc, SLVec3f &horizon)
Definition: SLAlgo.cpp:17
static const float RAD2DEG
Definition: Utils.h:238

Member Data Documentation

◆ _devRot

SLDeviceRotation* SLHorizonNode::_devRot = nullptr
private

Rotation sensor, not owned.

Definition at line 58 of file SLHorizonNode.h.

◆ _font

SLTexFont* SLHorizonNode::_font = nullptr
private

Font of the angle label, not owned.

Definition at line 59 of file SLHorizonNode.h.

◆ _horizonNode

SLNode* SLHorizonNode::_horizonNode = nullptr
private

Child node carrying the line.

Definition at line 65 of file SLHorizonNode.h.

◆ _line

SLPolyline* SLHorizonNode::_line = nullptr
private

Mesh of the horizon line.

Definition at line 64 of file SLHorizonNode.h.

◆ _mat

SLMaterial* SLHorizonNode::_mat = nullptr
private

Material of the horizon line.

Definition at line 63 of file SLHorizonNode.h.

◆ _prog

SLGLProgram* SLHorizonNode::_prog = nullptr
private

Program drawing the horizon line.

Definition at line 62 of file SLHorizonNode.h.

◆ _shaderDir

SLstring SLHorizonNode::_shaderDir
private

Directory of the line shader.

Definition at line 60 of file SLHorizonNode.h.

◆ _sRc

SLMat3f SLHorizonNode::_sRc
private

Sensor to camera rotation used by the horizon estimation.

Definition at line 68 of file SLHorizonNode.h.

◆ _textNode

SLNode* SLHorizonNode::_textNode = nullptr
private

Child node carrying the angle text.

Definition at line 66 of file SLHorizonNode.h.


The documentation for this class was generated from the following files: