SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLHorizonNode.cpp
Go to the documentation of this file.
1 /**
2  * \file SLHorizonNode.cpp
3  * \date November 2020
4  * \remarks Please use clangformat to format the code. See more code style on
5  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
6  * \authors Michael Göttlicher, Marcus Hudritsch
7  * \copyright http://opensource.org/licenses/GPL-3.0
8 */
9 
10 #include <SLHorizonNode.h>
11 #include <SLText.h>
12 #include <SLAlgo.h>
13 #include <cmath>
14 
15 //-----------------------------------------------------------------------------
16 /**
17  * @brief Construct a new SLHorizonNode::SLHorizonNode object
18  * @remarks It is important that during instantiation NO OpenGL functions (gl*)
19  * get called because this constructor will be most probably called in a parallel
20  * thread from within an SLScene::registerAssetsToLoad or SLScene::assemble
21  * function. All objects that get rendered have to do their OpenGL initialization
22  * when they are used the first time during rendering in the main thread.
23  * @param name Name of the node
24  * @param devRot Device rotation object
25  * @param font Font for the text label
26  * @param shaderDir Path to the shader directory
27  * @param scrW Width of the screen in pixels
28  * @param scrH Height of the sceen in pixels
29  */
31  SLDeviceRotation* devRot,
32  SLTexFont* font,
33  SLstring shaderDir,
34  int scrW,
35  int scrH)
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 }
89 //-----------------------------------------------------------------------------
91 {
92  delete _prog;
93  delete _mat;
94  delete _line;
95 }
96 //-----------------------------------------------------------------------------
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 }
126 //-----------------------------------------------------------------------------
float SLfloat
Definition: SL.h:173
string SLstring
Definition: SL.h:158
Container for general algorithm functions.
@ TS_object
Definition: SLEnums.h:210
@ UT_const
constant value
Definition: SLEnums.h:233
SLGLUniform< SLfloat > SLGLUniform1f
Definition: SLGLUniform.h:162
vector< SLVec3f > SLVVec3f
Definition: SLVec3.h:325
SLVec3< SLfloat > SLVec3f
Definition: SLVec3.h:318
Encapsulation of a mobile device rotation set by the device's IMU sensor.
SLMat3f rotationAveraged()
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
Definition: SLHorizonNode.h:40
SLTexFont * _font
Definition: SLHorizonNode.h:37
void doUpdate() override
SLNode * _horizonNode
Definition: SLHorizonNode.h:43
SLHorizonNode(SLstring name, SLDeviceRotation *devRot, SLTexFont *font, SLstring shaderDir, int scrW, int scrH)
Construct a new SLHorizonNode::SLHorizonNode object.
SLPolyline * _line
Definition: SLHorizonNode.h:42
SLDeviceRotation * _devRot
Definition: SLHorizonNode.h:36
SLMaterial * _mat
Definition: SLHorizonNode.h:41
SLNode * _textNode
Definition: SLHorizonNode.h:44
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
SLNode represents a node in a hierarchical scene graph.
Definition: SLNode.h:147
void rotation(const SLQuat4f &rot, SLTransformSpace relativeTo=TS_parent)
Definition: SLNode.cpp:846
void addChild(SLNode *child)
Definition: SLNode.cpp:207
void scale(SLfloat s)
Definition: SLNode.h:640
SLNode(const SLstring &name="Node")
Construct a new SLNode::SLNode object.
Definition: SLNode.cpp:40
bool deleteChild()
Definition: SLNode.cpp:267
void translate(const SLVec3f &vec, SLTransformSpace relativeTo=TS_object)
Definition: SLNode.cpp:906
SLPolyline creates a polyline object.
Definition: SLPolyline.h:23
Texture Font class inherits SLGLTexture for alpha blended font rendering.
Definition: SLTexFont.h:40
SLVec2f calcTextSize(const SLstring &text, SLfloat maxWidth=0.0f, SLfloat lineHeightFactor=1.5f)
Definition: SLTexFont.cpp:243
SLText creates a mesh using a textured font from SLTexFont.
Definition: SLText.h:30
T y
Definition: SLVec2.h:30
T x
Definition: SLVec2.h:30
T y
Definition: SLVec3.h:43
T x
Definition: SLVec3.h:43
static SLVec4 WHITE
Definition: SLVec4.h:215
bool estimateHorizon(const SLMat3f &enuRs, const SLMat3f &sRc, SLVec3f &horizon)
Definition: SLAlgo.cpp:17
static const float RAD2DEG
Definition: Utils.h:238