SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
WAIAppTest.cpp
Go to the documentation of this file.
1 #include "WAIAppTest.h"
2 
3 #include <AppCommon.h>
4 #include <SLGLTexture.h>
5 #include <SLGLProgram.h>
6 #include <SLAssimpImporter.h>
7 #include <SLSceneView.h>
8 #include <SLLightSpot.h>
9 #include <SLTexFont.h>
10 #include <SLSphere.h>
11 #include <SLText.h>
12 #include <SENSCamera.h>
13 
14 #define WAIAPP_DEBUG(...) Utils::log("WAIApp", __VA_ARGS__)
15 #define WAIAPP_INFO(...) Utils::log("WAIApp", __VA_ARGS__)
16 #define WAIAPP_WARN(...) Utils::log("WAIApp", __VA_ARGS__)
17 
18 //#define WAIAPP_DEBUG(...) // nothing
19 //#define WAIAPP_INFO(...) // nothing
20 //#define WAIAPP_WARN(...) // nothing
21 
22 #define WAIAPPSTATE_DEBUG(...) Utils::log("WAIAppStateHandler", __VA_ARGS__)
23 #define WAIAPPSTATE_INFO(...) Utils::log("WAIAppStateHandler", __VA_ARGS__)
24 #define WAIAPPSTATE_WARN(...) Utils::log("WAIAppStateHandler", __VA_ARGS__)
25 
26 //#define WAIAPPSTATE_DEBUG(...) // nothing
27 //#define WAIAPPSTATE_INFO(...) // nothing
28 //#define WAIAPPSTATE_WARN(...) // nothing
29 
31  : SLInputEventInterface(AppCommon::inputManager)
32 {
33 }
34 
35 void WAIApp::init(int screenWidth, int screenHeight, int screenDpi, AppDirectories directories)
36 {
37  WAIAPPSTATE_DEBUG("init");
38  // Utils::initFileLog(directories.logFileDir, true);
39 
41  {
42  initDirectories(directories);
43  initSceneGraph(screenWidth, screenHeight, screenDpi);
44  _initSceneGraphDone = true;
45  }
47  {
49  _initIntroSceneDone = true;
50  }
51 }
52 
54 {
55  _closeAppCallback = cb;
56 }
57 
58 void WAIApp::initCamera(SENSCamera* camera)
59 {
60  _camera = camera;
61 
62  /*
63  if (_sv)
64  _sv->setViewportFromRatio(SLVec2i(_camera->getFrameSize().width, _camera->getFrameSize().height), SLViewportAlign::VA_center, true);
65 
66  if (_sceneCamera)
67  {
68  //make sure scene camera is initialized
69  initSceneCamera();
70  }
71 
72  if (!_videoImage)
73  {
74  _videoImage = new SLGLTexture("LiveVideoError.png", GL_LINEAR, GL_LINEAR);
75  _sceneCamera->background().texture(_videoImage);
76  }
77  */
78 }
79 
81 {
82  WAIAPP_DEBUG("initDirectories");
83  _dirs = directories;
84  // Default paths for all loaded resources
85  SLGLProgram::defaultPath = _dirs.slDataRoot + "/shaders/";
86  SLGLTexture::defaultPath = _dirs.slDataRoot + "/images/textures/";
87  SLGLTexture::defaultPathFonts = _dirs.slDataRoot + "/images/fonts/";
88  CVImage::defaultPath = _dirs.slDataRoot + "/images/textures/";
89  SLAssimpImporter::defaultPath = _dirs.slDataRoot + "/models/";
91 }
92 
94 {
95  WAIAPP_DEBUG("initSceneGraph");
96  if (!AppCommon::scene)
97  {
98  AppCommon::name = "WAI Demo App";
99  AppCommon::scene = new SLScene("WAI Demo App", nullptr);
100 
101  int screenWidth = scrWidth;
102  int screenHeight = scrHeight;
103 
104  // setupGUI(AppCommon::name, AppCommon::configPath, dpi);
105  // Set default font sizes depending on the dpi no matter if ImGui is used
106  // todo: is this still needed?
107  if (!AppCommon::dpi)
109 
110  _sv = new SLSceneView();
111  _sv->init("SceneView",
112  screenWidth,
113  screenHeight,
114  nullptr,
115  nullptr,
116  nullptr);
117  }
118  else
119  {
120  WAIAPP_WARN("initSceneGraph: SLScene already loaded!");
121  }
122 }
123 
125 {
127  {
129  _goBackRequested = false;
130  }
131 
132  if (!_initSceneGraphDone)
133  return false;
134 
135  // WAIAPP_DEBUG("render");
137  {
138  if (_camera)
139  {
140  // if (_camera->started())
141  //{
142 
143  //}
144 
145  // copy video image to background
146  SENSFramePtr frame = _camera->getLatestFrame();
147  if (frame)
148  {
149  if (!_videoImage)
150  {
151  if (!_sceneCamera)
152  {
153  // make sure scene camera is initialized
154  initSceneCamera();
155  }
156 
157  if (_sv)
158  _sv->setViewportFromRatio(SLVec2i(_camera->getFrameSize().width,
159  _camera->getFrameSize().height),
161  true);
162 
163  _videoImage = new SLGLTexture("LiveVideoError.png", GL_LINEAR, GL_LINEAR);
165  }
166 
167  WAIAPP_DEBUG("valid frame");
168  cv::Mat& img = frame->imgRGB;
169  _videoImage->copyVideoImage(img.cols,
170  img.rows,
171  CVImage::cv2glPixelFormat(img.type()),
172  img.data,
173  img.isContinuous(),
174  true);
175  }
176  }
177 
178  // update scene
180 
181  // update sceneviews
182  bool needUpdate = false;
183  for (auto sv : AppCommon::scene->sceneViews())
184  if (sv->onPaint() && !needUpdate)
185  needUpdate = true;
186 
187  return needUpdate;
188  }
189  else
190  {
191  WAIAPP_WARN("render: SLScene not initialized!");
192  return false;
193  }
194 }
195 
197 {
199  _initSceneGraphDone = false;
200  _initIntroSceneDone = false;
201 }
202 
204 {
205  WAIAPP_DEBUG("goBack");
206  // todo: enqueue event
207  _goBackRequested = true;
208 }
209 
211 {
212  // Deletes all remaining sceneviews the current scene instance
213  if (AppCommon::scene)
214  {
215  delete AppCommon::scene;
216  AppCommon::scene = nullptr;
217  }
218 }
219 
221 {
222  if (!_sceneCamera)
223  {
224  _sceneCamera = new SLCamera("Camera 1");
225  _sceneCamera->clipNear(0.1f);
226  _sceneCamera->clipFar(100);
227  _sceneCamera->translation(0, 0, 5);
228  _sceneCamera->lookAt(0, 0, 0);
230  _sceneCamera->background().colors(SLCol4f(0.7f, 0.7f, 0.7f),
231  SLCol4f(0.2f, 0.2f, 0.2f));
233  }
234 }
235 
237 {
238  WAIAPP_DEBUG("initIntroScene");
240  // clear old scene content
241  s->init();
242 
243  s->name("Loading scene");
244  s->info("Scene shown while starting application");
245 
246  SLMaterial* m1 = new SLMaterial("m1", SLCol4f::RED);
247 
248  initSceneCamera(); //_sceneCamera is valid now
249 
250  SLLightSpot* light1 = new SLLightSpot(10, 10, 10, 0.3f);
251  light1->ambient(SLCol4f(0.2f, 0.2f, 0.2f));
252  light1->diffuse(SLCol4f(0.8f, 0.8f, 0.8f));
253  light1->specular(SLCol4f(1, 1, 1));
254  light1->attenuation(1, 0, 0);
255 
256  // Because all text objects get their sizes in pixels we have to scale them down
257  SLfloat scale = 0.01f;
258  SLstring txt = "This is text in 3D with font07";
259  SLVec2f size = SLTexFont::font07->calcTextSize(txt);
260  SLNode* t07 = new SLText(txt, SLTexFont::font07);
261  t07->translate(-size.x * 0.5f * scale, 1.0f, 0);
262  t07->scale(scale);
263 
264  txt = "This is text in 3D with font22";
265  size = SLTexFont::font22->calcTextSize(txt);
266  SLNode* t22 = new SLText(txt, SLTexFont::font22);
267  t22->translate(-size.x * 0.5f * scale, -1.2f, 0);
268  t22->scale(scale);
269 
270  // Assemble 3D scene as usual with camera and light
271  SLNode* scene3D = new SLNode("root3D");
272  scene3D->addChild(_sceneCamera);
273  scene3D->addChild(light1);
274  scene3D->addChild(new SLNode(new SLSphere(0.5f, 32, 32, "Sphere", m1)));
275  scene3D->addChild(t07);
276  scene3D->addChild(t22);
277 
279  _sv->doWaitOnIdle(false);
280 
281  s->root3D(scene3D);
282 
283  for (auto sceneView : s->sceneViews())
284  if (sceneView != nullptr)
285  sceneView->onInitialize();
286 
287  /*
288  WAIAPP_DEBUG("initIntroScene");
289  SLScene* s = AppCommon::scene;
290  //clear old scene content
291  s->init();
292 
293  s->name("Loading scene");
294  s->info("Scene shown while starting application");
295 
296  SLMaterial* m1 = new SLMaterial("m1", SLCol4f::RED);
297 
298  SLCamera* cam1 = new SLCamera("Camera 1");
299  cam1->clipNear(0.1f);
300  cam1->clipFar(100);
301  cam1->translation(0, 0, 5);
302  cam1->lookAt(0, 0, 0);
303  cam1->focalDist(5);
304  cam1->background().colors(SLCol4f(0.1f, 0.1f, 0.1f));
305  cam1->setInitialState();
306 
307  SLLightSpot* light1 = new SLLightSpot(10, 10, 10, 0.3f);
308  light1->ambient(SLCol4f(0.2f, 0.2f, 0.2f));
309  light1->diffuse(SLCol4f(0.8f, 0.8f, 0.8f));
310  light1->specular(SLCol4f(1, 1, 1));
311  light1->attenuation(1, 0, 0);
312 
313  // Because all text objects get their sizes in pixels we have to scale them down
314  SLfloat scale = 0.01f;
315  SLstring txt = "This is text in 3D with font07";
316  SLVec2f size = SLTexFont::font07->calcTextSize(txt);
317  SLNode* t07 = new SLText(txt, SLTexFont::font07);
318  t07->translate(-size.x * 0.5f * scale, 1.0f, 0);
319  t07->scale(scale);
320 
321  txt = "This is text in 3D with font09";
322  size = SLTexFont::font09->calcTextSize(txt);
323  SLNode* t09 = new SLText(txt, SLTexFont::font09);
324  t09->translate(-size.x * 0.5f * scale, 0.8f, 0);
325  t09->scale(scale);
326 
327  txt = "This is text in 3D with font12";
328  size = SLTexFont::font12->calcTextSize(txt);
329  SLNode* t12 = new SLText(txt, SLTexFont::font12);
330  t12->translate(-size.x * 0.5f * scale, 0.6f, 0);
331  t12->scale(scale);
332 
333  txt = "This is text in 3D with font20";
334  size = SLTexFont::font20->calcTextSize(txt);
335  SLNode* t20 = new SLText(txt, SLTexFont::font20);
336  t20->translate(-size.x * 0.5f * scale, -0.8f, 0);
337  t20->scale(scale);
338 
339  txt = "This is text in 3D with font22";
340  size = SLTexFont::font22->calcTextSize(txt);
341  SLNode* t22 = new SLText(txt, SLTexFont::font22);
342  t22->translate(-size.x * 0.5f * scale, -1.2f, 0);
343  t22->scale(scale);
344 
345  // Now create 2D text but don't scale it (all sizes in pixels)
346  txt = "This is text in 2D with font16";
347  size = SLTexFont::font16->calcTextSize(txt);
348  SLNode* t2D16 = new SLText(txt, SLTexFont::font16);
349  t2D16->translate(-size.x * 0.5f, 0, 0);
350 
351  // Assemble 3D scene as usual with camera and light
352  SLNode* scene3D = new SLNode("root3D");
353  scene3D->addChild(cam1);
354  scene3D->addChild(light1);
355  scene3D->addChild(new SLNode(new SLSphere(0.5f, 32, 32, "Sphere", m1)));
356  scene3D->addChild(t07);
357  scene3D->addChild(t09);
358  scene3D->addChild(t12);
359  scene3D->addChild(t20);
360  scene3D->addChild(t22);
361 
362  // Assemble 2D scene
363  SLNode* scene2D = new SLNode("root2D");
364  scene2D->addChild(t2D16);
365 
366  _sv->camera(cam1);
367  _sv->doWaitOnIdle(true);
368 
369  s->root3D(scene3D);
370  s->root2D(scene2D);
371  */
372 }
373 
374 /*
375 void WAIApp::enableSceneGraph()
376 {
377  SLScene* s = AppCommon::scene;
378  for (auto sceneView : s->sceneViews())
379  if (sceneView != nullptr)
380  sceneView->onInitialize();
381 
382 }
383 */
384 
385 /*
386 WAIAppStateHandler::WAIAppStateHandler(CloseAppCallback cb)
387  : SLInputEventInterface(AppCommon::inputManager),
388  _closeAppCallback(cb)
389 {
390  WAIAPPSTATE_DEBUG("constructor");
391  _waiApp = std::make_unique<WAIApp>();
392 }
393 
394 bool WAIAppStateHandler::update()
395 {
396  //WAIAPPSTATE_DEBUG("update");
397 
398  if (_goBackRequested && _closeAppCallback)
399  {
400  _closeAppCallback();
401  _goBackRequested = false;
402  }
403 
404  if (_initIntroSceneDone)
405  {
406  if (_initIntroSceneDone)
407  {
408  _waiApp->render();
409  }
410  }
411 
412  return true;
413  //checkStateTransition();
414  //return processState();
415 }
416  */
417 
418 /*
419 void WAIAppStateHandler::checkStateTransition()
420 {
421  WAIAPPSTATE_DEBUG("checkStateTransition");
422  switch (_state)
423  {
424  case State::STARTUP:
425  {
426  if (_initIntroSceneDone)
427  {
428  WAIAPPSTATE_DEBUG("checkStateTransition: transition to state INTROSCENE");
429  _state = State::INTROSCENE;
430  }
431  break;
432  }
433  case State::INTROSCENE:
434  {
435 
436  break;
437  }
438  case State::START_SLAM_SCENE:
439  {
440  break;
441  }
442  case State::SLAM_SCENE:
443  {
444  break;
445  }
446  };
447 }
448 
449 bool WAIAppStateHandler::processState()
450 {
451  WAIAPPSTATE_DEBUG("processState");
452  bool updateScreen = false;
453  switch (_state)
454  {
455  case State::STARTUP:
456  {
457  break;
458  }
459  case State::INTROSCENE:
460  {
461  //_waiApp->updateIntroScene();
462  updateScreen = _waiApp->render();
463  break;
464  }
465  case State::START_SLAM_SCENE:
466  {
467  break;
468  }
469  case State::SLAM_SCENE:
470  {
471  break;
472  }
473  };
474 
475  return updateScreen;
476 }
477  */
478 
479 /*
480 
481 void WAIAppStateHandler::init(int screenWidth, int screenHeight, int screenDpi, AppDirectories directories)
482 {
483  WAIAPPSTATE_DEBUG("init");
484  //Utils::initFileLog(directories.logFileDir, true);
485 
486  if (!_initSceneGraphDone)
487  {
488  _waiApp->initDirectories(directories);
489  _waiApp->initSceneGraph(screenWidth, screenHeight, screenDpi);
490  _initSceneGraphDone = true;
491  }
492  if (!_initIntroSceneDone)
493  {
494  _waiApp->initIntroScene();
495  _initIntroSceneDone = true;
496  }
497 }
498 /*
499 
500 /*
501 void WAIAppStateHandler::show()
502 {
503  WAIAPPSTATE_DEBUG("show");
504  //_waiApp->enableSceneGraph();
505  //_waiApp->initIntroScene();
506 }
507 
508 void WAIAppStateHandler::hide()
509 {
510  WAIAPPSTATE_DEBUG("hide");
511 }
512  */
513 
514 /*
515 void WAIAppStateHandler::close()
516 {
517  WAIAPPSTATE_DEBUG("close");
518  _waiApp->deleteSceneGraph();
519  _initSceneGraphDone = false;
520  _initIntroSceneDone = false;
521 }
522 
523 //back button was pressed
524 void WAIAppStateHandler::goBack()
525 {
526  WAIAPPSTATE_DEBUG("goBack");
527  //todo: enqueue event
528  _goBackRequested = true;
529 }
530 */
The AppCommon class holds the top-level instances of the app-demo.
static SLint dpi
Dot per inch resolution of screen.
Definition: AppGLFW.cpp:41
static SLint scrHeight
Window height at start up.
Definition: AppGLFW.cpp:38
static SLint scrWidth
Window width at start up.
Definition: AppGLFW.cpp:37
float SLfloat
Definition: SL.h:173
string SLstring
Definition: SL.h:158
@ VA_center
Definition: SLEnums.h:255
SLVec2< SLint > SLVec2i
Definition: SLVec2.h:140
SLVec4< SLfloat > SLCol4f
Definition: SLVec4.h:237
#define WAIAPPSTATE_DEBUG(...)
Definition: WAIAppTest.cpp:22
#define WAIAPP_DEBUG(...)
Definition: WAIAppTest.cpp:14
#define WAIAPP_WARN(...)
Definition: WAIAppTest.cpp:16
Top level class for the major global instances off an SLProject app.
Definition: AppCommon.h:55
static SLstring name
Application name.
Definition: AppCommon.h:72
static SLstring configPath
Default path for calibration files.
Definition: AppCommon.h:81
static SLScene * scene
Pointer to the one and only SLScene instance.
Definition: AppCommon.h:61
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...
void colors(const SLCol4f &uniformColor)
Sets a uniform background color.
Active or visible camera node class.
Definition: SLCamera.h:54
void clipFar(const SLfloat cFar)
Definition: SLCamera.h:109
void clipNear(const SLfloat cNear)
Definition: SLCamera.h:108
void focalDist(const SLfloat f)
Definition: SLCamera.h:116
SLBackground & background()
Definition: SLCamera.h:165
Texture object for OpenGL texturing.
Definition: SLGLTexture.h:110
SLbool copyVideoImage(SLint camWidth, SLint camHeight, CVPixelFormatGL glFormat, SLuchar *data, SLbool isContinuous, SLbool isTopLeft)
Copies the image data from a video camera into the current video image.
void attenuation(const SLfloat kConstant, const SLfloat kLinear, const SLfloat kQuadratic)
Definition: SLLight.h:116
SLLightSpot class for a spot light source.
Definition: SLLightSpot.h:36
SLCol4f diffuse() override
Returns normally _diffuseColor * _diffusePower.
Definition: SLLightSpot.h:85
SLCol4f specular() override
Returns normally _specularColor * _specularPower.
Definition: SLLightSpot.h:86
SLCol4f ambient() override
Return normally _ambientColor * _ambientPower.
Definition: SLLightSpot.h:84
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 translate(const SLVec3f &vec, SLTransformSpace relativeTo=TS_object)
Definition: SLNode.cpp:906
void name(const SLstring &Name)
Definition: SLObject.h:34
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
bool onUpdate(bool renderTypeIsRT, bool voxelsAreShown, bool forceCPUSkinning)
Updates animations and AABBs.
Definition: SLScene.cpp:124
void root3D(SLNode *root3D)
Definition: SLScene.h:78
void info(SLstring i)
Definition: SLScene.h:93
void init(SLAssetManager *am)
Definition: SLScene.cpp:72
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 init(SLstring name, SLint screenWidth, SLint screenHeight, void *onWndUpdateCallback, void *onSelectNodeMeshCallback, SLUiInterface *gui, const string &configPath)
Definition: SLSceneView.cpp:64
SLbool onPaint()
void setViewportFromRatio(const SLVec2i &vpRatio, SLViewportAlign vpAlignment, SLbool vpSameAsVideo)
Sets the viewport ratio and the viewport rectangle.
void doWaitOnIdle(SLbool doWI)
Definition: SLSceneView.h:149
SLSphere creates a sphere mesh based on SLSpheric w. 180 deg polar angle.
Definition: SLSphere.h:33
SLText creates a mesh using a textured font from SLTexFont.
Definition: SLText.h:30
T x
Definition: SLVec2.h:30
static SLVec4 RED
Definition: SLVec4.h:216
SENSCamera * _camera
Definition: WAIAppTest.h:52
SLGLTexture * _videoImage
Definition: WAIAppTest.h:56
void initSceneCamera()
Definition: WAIAppTest.cpp:220
void init(int screenWidth, int screenHeight, int screenDpi, AppDirectories directories)
Definition: WAIAppTest.cpp:35
void initIntroScene()
Definition: WAIAppTest.cpp:236
bool _goBackRequested
Definition: WAIAppTest.h:63
void initDirectories(AppDirectories directories)
Definition: WAIAppTest.cpp:80
std::function< void()> CloseAppCallback
Definition: WAIAppTest.h:26
void initCloseAppCallback(CloseAppCallback cb)
Definition: WAIAppTest.cpp:53
void deleteSceneGraph()
Definition: WAIAppTest.cpp:210
void close()
Definition: WAIAppTest.cpp:196
void goBack()
Definition: WAIAppTest.cpp:203
bool _initIntroSceneDone
Definition: WAIAppTest.h:65
AppDirectories _dirs
Definition: WAIAppTest.h:58
bool _initSceneGraphDone
Definition: WAIAppTest.h:64
void initCamera(SENSCamera *camera)
Definition: WAIAppTest.cpp:58
void initSceneGraph(int scrWidth, int scrHeight, int dpi)
Definition: WAIAppTest.cpp:93
SLCamera * _sceneCamera
Definition: WAIAppTest.h:55
SLSceneView * _sv
Definition: WAIAppTest.h:54
CloseAppCallback _closeAppCallback
Definition: WAIAppTest.h:61
bool update()
Definition: WAIAppTest.cpp:124
std::string writableDir
Definition: WAIAppTest.h:15
std::string slDataRoot
Definition: WAIAppTest.h:17