SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppMinimalMain.cpp
Go to the documentation of this file.
1 /**
2  * \file AppMinimal.cpp
3  * \brief Single file minimal full app with SLProject
4  * \details For more info on how to create a new app with SLProject see:
5  * https://github.com/cpvrlab/SLProject4/wiki/Creating-a-New-App
6  * For more info about App framework see:
7  * https://cpvrlab.github.io/SLProject4/app-framework.html
8  * \date June 2024
9  * \authors Marino von Wattenwyl
10  * \copyright http://opensource.org/licenses/GPL-3.0
11  * \remarks Please use clangformat to format the code. See more code style on
12  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
13  */
14 
15 #include <App.h>
16 #include <AppCommon.h>
17 #include <SLScene.h>
18 #include <SLBox.h>
19 #include <SLLightDirect.h>
20 #include <SLSceneView.h>
21 #include <SLAssetLoader.h>
22 #include <SLImGui.h>
23 #include <SLInterface.h>
24 
25 //-----------------------------------------------------------------------------
26 //! Scene class derived from SLScene
27 class AppMinimalScene : public SLScene
28 {
29 public:
31 
32  //! All scene specific assets have to be registered for async loading in here.
33  /*! @remark All scene sspecific assets have to be loaded async by overriding
34  SLScene::registerAssetsToLoad and SLScene::assemble. Async loading and
35  assembling means that it happens in a parallel thread and that in there are
36  no OpenGL calls allowed. OpenGL calls are only allowed in the main thread.*/
37  void registerAssetsToLoad(SLAssetLoader& al) override;
38 
39  //! After parallel loading of the assets the scene gets assembled in here.
40  /*! @remark All scene-specific assets have to be loaded async by overriding
41  SLScene::registerAssetsToLoad and SLScene::assemble. Async loading and
42  assembling means that it happens in a parallel thread and that in there
43  are no OpenGL calls allowed. OpenGL calls are only allowed in the main
44  thread. It is important that all object instantiations within
45  SLScene::assemble do NOT call any OpenGL functions (gl*) because they happen
46  in a parallel thread. All objects that get rendered have to do their
47  initialization when they are used the first time during rendering in the
48  main thread.*/
49  void assemble(SLAssetManager* am, SLSceneView* sv) override;
50 
51 private:
53 };
54 //-----------------------------------------------------------------------------
55 AppMinimalScene::AppMinimalScene() : SLScene("Minimal Demo Scene")
56 {
57 }
58 //-----------------------------------------------------------------------------
59 //! All assets the should be loaded in parallel must be registered in here.
61 {
63  AppCommon::texturePath + "wood0_0512_C.jpg");
64 }
65 //-----------------------------------------------------------------------------
66 //! After parallel loading of the assets the scene gets assembled in here.
68 {
69  SLCamera* camera = new SLCamera();
70  camera->translation(2, 1, 3);
71  camera->lookAt(0, 0, 0);
72  camera->focalDist(camera->translationWS().distance(SLVec3f::ZERO));
73 
74  SLLightDirect* light = new SLLightDirect(am, this);
75  light->translate(-0.5f, 1.0f, 0.75f);
76  light->lookAt(0, 0, 0);
77 
78  SLMaterial* material = new SLMaterial(am,
79  "Wood Material",
80  _woodTexture);
81  SLNode* box = new SLNode("Box Node");
82  box->addMesh(new SLBox(am,
83  -0.5f,
84  -0.5f,
85  -0.5f,
86  0.5f,
87  0.5f,
88  0.5f,
89  "Box",
90  material));
91 
92  SLNode* scene = new SLNode();
93  scene->addChild(light);
94  scene->addChild(camera);
95  scene->addChild(box);
96  root3D(scene);
97 
98  sv->camera(camera);
99  sv->doWaitOnIdle(true);
100 }
101 //-----------------------------------------------------------------------------
102 //! A static function in which you can create the new scene
104 {
105  return new AppMinimalScene();
106 }
107 //-----------------------------------------------------------------------------
108 //! A static function that builds the UI with ImGui
109 static void buildGui(SLScene* s, SLSceneView* sv)
110 {
111  if (ImGui::BeginMainMenuBar())
112  {
113  if (ImGui::BeginMenu("View"))
114  {
115  if (ImGui::MenuItem("Reset Camera"))
116  {
117  sv->camera()->translation(2, 1, 3);
118  sv->camera()->lookAt(0, 0, 0);
119  }
120 
121  ImGui::EndMenu();
122  }
123 
124  ImGui::EndMainMenuBar();
125  }
126 }
127 //-----------------------------------------------------------------------------
128 int SL_MAIN_FUNCTION(int argc, char* argv[])
129 {
131  config.argc = argc,
132  config.argv = argv;
133  config.windowWidth = 640;
134  config.windowHeight = 480;
135  config.windowTitle = "SLProject Minimal App";
138 
139  return App::run(config);
140 }
141 //-----------------------------------------------------------------------------
The App namespace declares the App::Config struct and the App::run function.
The AppCommon class holds the top-level instances of the app-demo.
int SL_MAIN_FUNCTION(int argc, char *argv[])
static void buildGui(SLScene *s, SLSceneView *sv)
A static function that builds the UI with ImGui.
static SLScene * createScene(SLSceneID sceneID)
A static function in which you can create the new scene.
int SLSceneID
Scene identifier.
Definition: SLEnums.h:91
Wrapper Class around the external ImGui GUI-framework.
Declaration of the main Scene Library C-Interface.
SLScene SLSceneView SLint sceneID
Definition: SLScene.h:33
static SLstring texturePath
Path to texture images.
Definition: AppCommon.h:86
Scene class derived from SLScene.
SLGLTexture * _woodTexture
void registerAssetsToLoad(SLAssetLoader &al) override
All scene specific assets have to be registered for async loading in here.
void assemble(SLAssetManager *am, SLSceneView *sv) override
After parallel loading of the assets the scene gets assembled in here.
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.
Axis aligned box mesh.
Definition: SLBox.h:31
Active or visible camera node class.
Definition: SLCamera.h:54
void focalDist(const SLfloat f)
Definition: SLCamera.h:116
Texture object for OpenGL texturing.
Definition: SLGLTexture.h:110
SLLightDirect class for a directional light source.
Definition: SLLightDirect.h:40
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
SLVec3f translationWS() const
Definition: SLNode.h:531
virtual void addMesh(SLMesh *mesh)
Definition: SLNode.cpp:157
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
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
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
T distance(const SLVec3 &p) const
Calculate the distance to point p.
Definition: SLVec3.h:168
static SLVec3 ZERO
Definition: SLVec3.h:285
int run(Config config)
App::run implementation from App.h for the Emscripten platform.
Definition: AppAndroid.cpp:78
Config config
The configuration set in App::run.
Definition: AppAndroid.cpp:34
App configuration struct to be passed to the App::run function.
Definition: App.h:57
SLint windowWidth
Definition: App.h:60
SLint windowHeight
Definition: App.h:61
OnNewSceneCallback onNewScene
Definition: App.h:66
OnGuiBuildCallback onGuiBuild
Definition: App.h:72
int argc
Definition: App.h:58
char ** argv
Definition: App.h:59
SLstring windowTitle
Definition: App.h:62