SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneVideoTrackAruco.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneVideoTrackAruco.cpp
3  * \brief Implementation for an SLScene inherited class
4  * \details For more info about App framework and the scene assembly see:
5  * https://cpvrlab.github.io/SLProject4/app-framework.html
6  * \date May 2024
7  * \authors Marino von Wattenwyl
8  * \copyright http://opensource.org/licenses/GPL-3.0
9  * \remarks Please use clangformat to format the code. See more code style on
10  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
11  */
12 
14 #include <AppCommon.h>
15 #include <AppDemoSceneID.h>
16 #include <CVCapture.h>
17 #include <CVTrackedAruco.h>
18 #include <SLAssetLoader.h>
19 #include <SLLightSpot.h>
20 #include <SLBox.h>
21 #include <SLCoordAxis.h>
22 
23 // Global pointers declared in AppDemoVideo
25 extern CVTracked* gVideoTracker;
27 
28 //-----------------------------------------------------------------------------
30  : SLScene("Aruco Marker Tracking"),
31  _sceneID(sid)
32 {
34  {
35  name("Track Aruco (main cam.)");
36  info("Hold the Aruco board dictionary 0 into the field of view of "
37  "the main camera. You can find the Aruco markers in the file "
38  "data/Calibrations. If not all markers are tracked you may have "
39  "the mirror the video horizontally.");
40  }
41  else
42  {
43  name("Track Aruco (scnd. cam.)");
44  info("Hold the Aruco board dictionary 0 into the field of view of "
45  "the secondary camera. You can find the Aruco markers in the file "
46  "data/Calibrations. If not all markers are tracked you may have "
47  "the mirror the video horizontally.");
48  }
49 }
50 //-----------------------------------------------------------------------------
51 //! All assets the should be loaded in parallel must be registered in here.
53 {
54  // Create video texture on global pointer updated in AppDemoVideo
57  "LiveVideoError.png",
58  GL_LINEAR,
59  GL_LINEAR);
60 
61  // Create an ArUco tracker
62  al.addLoadTask([]()
63  {
65  gVideoTracker->drawDetection(true); });
66 }
67 //-----------------------------------------------------------------------------
68 //! After parallel loading of the assets the scene gets assembled in here.
70 {
71  /*
72  The tracking of markers is done in AppDemoVideo::onUpdateVideo by calling
73  the specific CVTracked::track method. If a marker was found it overwrites
74  the linked nodes object matrix (SLNode::_om). If the linked node is the
75  active camera the found transform is additionally inversed.
76  This would be the standard augmented reality use case.
77  */
78 
81  else
83 
84  // Material
85  SLMaterial* yellow = new SLMaterial(am,
86  "mY",
87  SLCol4f(1, 1, 0, 0.5f));
88  SLMaterial* cyan = new SLMaterial(am,
89  "mY",
90  SLCol4f(0, 1, 1, 0.5f));
91 
92  // Create a scene group node
93  SLNode* scene = new SLNode("scene node");
94  root3D(scene);
95 
96  // Create a camera node 1
97  SLCamera* cam1 = new SLCamera("Camera 1");
98  cam1->translation(0, 0, 5);
99  cam1->lookAt(0, 0, 0);
100  cam1->fov(CVCapture::instance()->activeCamera->calibration.cameraFovVDeg());
102  cam1->setInitialState();
104  scene->addChild(cam1);
105 
106  // Create a light source node
107  SLLightSpot* light1 = new SLLightSpot(am, this, 0.02f);
108  light1->translation(0.12f, 0.12f, 0.12f);
109  light1->name("light node");
110  scene->addChild(light1);
111 
112  // Get the half edge length of the aruco marker
113  SLfloat edgeLen = static_cast<CVTrackedAruco*>(gVideoTracker)->params().edgeLength;
114  SLfloat he = edgeLen * 0.5f;
115 
116  // Build mesh & node that will be tracked by the 1st marker (camera)
117  SLBox* box1 = new SLBox(am,
118  -he,
119  -he,
120  0.0f,
121  he,
122  he,
123  2 * he,
124  "Box 1",
125  yellow);
126  SLNode* boxNode1 = new SLNode(box1,
127  "Box Node 1");
128  SLNode* axisNode1 = new SLNode(new SLCoordAxis(am),
129  "Axis Node 1");
130  axisNode1->setDrawBitsRec(SL_DB_MESHWIRED, false);
131  axisNode1->scale(edgeLen);
132  boxNode1->addChild(axisNode1);
133  boxNode1->setDrawBitsRec(SL_DB_CULLOFF, true);
134  scene->addChild(boxNode1);
135 
136  // The tracker moves the box node
137  gVideoTrackedNode = boxNode1;
138 
139  // Set active camera
140  sv->camera(cam1);
141 
142  // Turn on constant redraw
143  sv->doWaitOnIdle(false);
144 }
145 //-----------------------------------------------------------------------------
The AppCommon class holds the top-level instances of the app-demo.
Definition of scene IDs in the demo app.
@ SID_VideoTrackArucoMain
SLNode * gVideoTrackedNode
SLGLTexture * gVideoTexture
CVTracked * gVideoTracker
Class declaration for an SLScene inherited class.
@ VT_SCND
Selfie camera on mobile devices.
Definition: CVCapture.h:43
@ VT_MAIN
Main camera on all on all all devices.
Definition: CVCapture.h:42
float SLfloat
Definition: SL.h:173
#define SL_DB_CULLOFF
Turn off face culling.
Definition: SLDrawBits.h:28
#define SL_DB_MESHWIRED
Draw polygons as wired mesh.
Definition: SLDrawBits.h:22
int SLSceneID
Scene identifier.
Definition: SLEnums.h:91
SLVec4< SLfloat > SLCol4f
Definition: SLVec4.h:237
static SLDeviceRotation devRot
Mobile device rotation from IMU.
Definition: AppCommon.h:64
static SLstring calibIniPath
That's where data/calibrations folder is located.
Definition: AppCommon.h:108
static SLDeviceLocation devLoc
Mobile device location from GPS.
Definition: AppCommon.h:65
static SLstring texturePath
Path to texture images.
Definition: AppCommon.h:86
void assemble(SLAssetManager *am, SLSceneView *sv) override
After parallel loading of the assets the scene gets assembled in here.
void registerAssetsToLoad(SLAssetLoader &al) override
All scene specific assets have to be registered for async loading in here.
void videoType(CVVideoType vt)
Setter for video type also sets the active calibration.
Definition: CVCapture.cpp:866
static CVCapture * instance()
Public static instance getter for singleton pattern.
Definition: CVCapture.h:65
OpenCV ArUco marker tracker class derived from CVTracked.
CVTracked is the pure virtual base class for tracking features in video.
Definition: CVTracked.h:50
void drawDetection(bool draw)
Definition: CVTracked.h:60
void addLoadTask(SLAssetLoadTask task)
Add generic task.
void addTextureToLoad(SLGLTexture *&texture, const SLstring &path, SLint min_filter=GL_LINEAR_MIPMAP_LINEAR, SLint mag_filter=GL_LINEAR, SLTextureType type=TT_unknown, SLint wrapS=GL_REPEAT, SLint wrapT=GL_REPEAT)
Add 2D textures with internal image allocation.
Toplevel holder of the assets meshes, materials, textures and shaders.
void texture(SLGLTexture *backgroundTexture, bool fixAspectRatio=false)
If flag _repeatBlurred is true the texture is not distorted if its size does not fit to screen aspect...
Axis aligned box mesh.
Definition: SLBox.h:31
Active or visible camera node class.
Definition: SLCamera.h:54
void devRotLoc(SLDeviceRotation *devRot, SLDeviceLocation *devLoc)
Definition: SLCamera.h:120
SLBackground & background()
Definition: SLCamera.h:165
void fov(const SLfloat fov)
vertical field of view
Definition: SLCamera.h:98
Axis aligned coordinate axis mesh.
Definition: SLCoordAxis.h:31
Texture object for OpenGL texturing.
Definition: SLGLTexture.h:110
SLLightSpot class for a spot light source.
Definition: SLLightSpot.h:36
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 addChild(SLNode *child)
Definition: SLNode.cpp:207
void translation(const SLVec3f &pos, SLTransformSpace relativeTo=TS_parent)
Definition: SLNode.cpp:828
void scale(SLfloat s)
Definition: SLNode.h:640
void setInitialState()
Definition: SLNode.cpp:1084
void lookAt(SLfloat targetX, SLfloat targetY, SLfloat targetZ, SLfloat upX=0, SLfloat upY=1, SLfloat upZ=0, SLTransformSpace relativeTo=TS_world)
Definition: SLNode.h:652
void setDrawBitsRec(SLuint bit, SLbool state)
Definition: SLNode.cpp:804
void name(const SLstring &Name)
Definition: SLObject.h:34
const SLstring & name() const
Definition: SLObject.h:38
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
SLNode * root3D()
Definition: SLScene.h:99
friend class SLNode
Definition: SLScene.h:48
SLstring & info()
Definition: SLScene.h:102
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69
void camera(SLCamera *camera)
Definition: SLSceneView.h:145
void doWaitOnIdle(SLbool doWI)
Definition: SLSceneView.h:149