SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneMeshLoad.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneMeshLoad.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 Marcus Hudritsch, 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 
13 #include <AppDemoSceneMeshLoad.h>
14 #include <AppCommon.h>
15 #include <SLAssetLoader.h>
16 #include <SLLightSpot.h>
17 #include <SLRectangle.h>
18 #include <SLSkybox.h>
19 
20 //-----------------------------------------------------------------------------
22 {
23  info("We use the assimp library to load 3D file formats including materials, skeletons and animations. "
24  "You can view the skeleton with key K. You can stop all animations with SPACE key.\n"
25  "Switch between perspective and orthographic projection with key 5. "
26  "Switch to front view with key 1, to side view with key 3 and to top view with key 7.\n"
27  "Try the different stereo rendering modes in the menu Camera.");
28 }
29 //-----------------------------------------------------------------------------
30 //! All assets the should be loaded in parallel must be registered in here.
32 {
34  al.modelPath() +
35  "GLTF/glTF-Sample-Models/hdris/approaching_storm_4k.hdr",
36  SLVec2i(512, 512),
37  "HDR Skybox");
38 
41  "GLTF/glTF-Sample-Models/2.0/DamagedHelmet/glTF/DamagedHelmet.gltf");
44  "FBX/Duck/duck.fbx");
47  "DAE/AstroBoy/AstroBoy.dae");
48 }
49 //-----------------------------------------------------------------------------
50 //! After parallel loading of the assets the scene gets assembled in here.
52 {
53  SLMaterial* matBlu = new SLMaterial(am, "Blue", SLCol4f(0, 0, 0.2f), SLCol4f(1, 1, 1), 100, 0.8f, 0);
54  SLMaterial* matRed = new SLMaterial(am, "Red", SLCol4f(0.2f, 0, 0), SLCol4f(1, 1, 1), 100, 0.8f, 0);
55  SLMaterial* matGre = new SLMaterial(am, "Green", SLCol4f(0, 0.2f, 0), SLCol4f(1, 1, 1), 100, 0.8f, 0);
56  SLMaterial* matGra = new SLMaterial(am, "Gray", SLCol4f(0.3f, 0.3f, 0.3f), SLCol4f(1, 1, 1), 100, 0, 0);
57 
58  SLCamera* cam1 = new SLCamera("Camera 1");
59  cam1->clipNear(.1f);
60  cam1->clipFar(30);
61  cam1->translation(0, 0, 12);
62  cam1->lookAt(0, 0, 0);
63  cam1->focalDist(12);
64  cam1->stereoEyeSeparation(cam1->focalDist() / 30.0f);
65  cam1->background().colors(SLCol4f(0.6f, 0.6f, 0.6f), SLCol4f(0.3f, 0.3f, 0.3f));
66  cam1->setInitialState();
68 
69  SLLightSpot* light1 = new SLLightSpot(am, this, 2.5f, 2.5f, 2.5f, 0.2f);
70  light1->powers(0.1f, 1.0f, 1.0f);
71  light1->attenuation(1, 0, 0);
72  SLAnimation* anim = this->animManager().createNodeAnimation("anim_light1_backforth", 2.0f, true, EC_inOutQuad, AL_pingPongLoop);
73  anim->createNodeAnimTrackForTranslation(light1, SLVec3f(0.0f, 0.0f, -5.0f));
74 
75  SLLightSpot* light2 = new SLLightSpot(am, this, -2.5f, -2.5f, 2.5f, 0.2f);
76  light2->powers(0.1f, 1.0f, 1.0f);
77  light2->attenuation(1, 0, 0);
78  anim = this->animManager().createNodeAnimation("anim_light2_updown", 2.0f, true, EC_inOutQuint, AL_pingPongLoop);
79  anim->createNodeAnimTrackForTranslation(light2, SLVec3f(0.0f, 5.0f, 0.0f));
80 
81  // Start animation
82  SLAnimPlayback* charAnim = this->animManager().animPlaybacksBack();
83  charAnim->playForward();
84  charAnim->playbackRate(0.8f);
85 
86  // Scale to so that the AstroBoy is about 2 (meters) high.
87  if (_meshGLTF)
88  {
89  _meshGLTF->scale(0.8f);
90  _meshGLTF->translate(-2.4f, 0, -1.5f, TS_object);
91 
92  // Update all materials and set their skybox to _skybox
94  { m->skybox(_skybox); },
95  true);
96  _skybox->exposure(4.0f);
97  }
98  if (_meshDAE)
99  {
100  _meshDAE->translate(0, -3, 0, TS_object);
101  _meshDAE->scale(2.7f);
102  }
103  if (_meshFBX)
104  {
105  _meshFBX->scale(0.1f);
106  _meshFBX->scale(0.1f);
107  _meshFBX->translate(200, 30, -30, TS_object);
108  _meshFBX->rotate(-90, 0, 1, 0);
109  }
110 
111  // define rectangles for the surrounding box
112  SLfloat b = 3; // edge size of rectangles
113  SLNode *rb, *rl, *rr, *rf, *rt;
114  SLuint res = 20;
115  rb = new SLNode(new SLRectangle(am, SLVec2f(-b, -b), SLVec2f(b, b), res, res, "rectB", matBlu), "rectBNode");
116  rb->translate(0, 0, -b, TS_object);
117  rl = new SLNode(new SLRectangle(am, SLVec2f(-b, -b), SLVec2f(b, b), res, res, "rectL", matRed), "rectLNode");
118  rl->rotate(90, 0, 1, 0);
119  rl->translate(0, 0, -b, TS_object);
120  rr = new SLNode(new SLRectangle(am, SLVec2f(-b, -b), SLVec2f(b, b), res, res, "rectR", matGre), "rectRNode");
121  rr->rotate(-90, 0, 1, 0);
122  rr->translate(0, 0, -b, TS_object);
123  rf = new SLNode(new SLRectangle(am, SLVec2f(-b, -b), SLVec2f(b, b), res, res, "rectF", matGra), "rectFNode");
124  rf->rotate(-90, 1, 0, 0);
125  rf->translate(0, 0, -b, TS_object);
126  rt = new SLNode(new SLRectangle(am, SLVec2f(-b, -b), SLVec2f(b, b), res, res, "rectT", matGra), "rectTNode");
127  rt->rotate(90, 1, 0, 0);
128  rt->translate(0, 0, -b, TS_object);
129 
130  SLNode* scene = new SLNode("Scene");
131  this->root3D(scene);
132  this->skybox(_skybox);
133  scene->addChild(light1);
134  scene->addChild(light2);
135  scene->addChild(rb);
136  scene->addChild(rl);
137  scene->addChild(rr);
138  scene->addChild(rf);
139  scene->addChild(rt);
140  if (_meshGLTF) scene->addChild(_meshGLTF);
141  if (_meshFBX) scene->addChild(_meshFBX);
142  if (_meshDAE) scene->addChild(_meshDAE);
143  scene->addChild(cam1);
144 
145  sv->camera(cam1);
146 }
147 //-----------------------------------------------------------------------------
The AppCommon class holds the top-level instances of the app-demo.
Class declaration for an SLScene inherited class.
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
@ EC_inOutQuad
quadratic easing in and then out
Definition: SLEnums.h:184
@ EC_inOutQuint
quintic easing in and then out
Definition: SLEnums.h:196
@ TS_object
Definition: SLEnums.h:210
@ AL_pingPongLoop
loop forward and backwards
Definition: SLEnums.h:171
SLVec2< SLfloat > SLVec2f
Definition: SLVec2.h:141
SLVec3< SLfloat > SLVec3f
Definition: SLVec3.h:318
SLVec4< SLfloat > SLCol4f
Definition: SLVec4.h:237
static SLDeviceRotation devRot
Mobile device rotation from IMU.
Definition: AppCommon.h:64
static SLDeviceLocation devLoc
Mobile device location from GPS.
Definition: AppCommon.h:65
static SLstring modelPath
Path to 3D models.
Definition: AppCommon.h:85
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.
SLAnimation * createNodeAnimation(SLfloat duration)
SLAnimPlayback * animPlaybacksBack()
Definition: SLAnimManager.h:50
Manages the playback of an SLAnimation.
SLfloat playbackRate() const
SLAnimation is the base container for all animation data.
Definition: SLAnimation.h:33
SLNodeAnimTrack * createNodeAnimTrackForTranslation(SLNode *target, const SLVec3f &endPos)
void addSkyboxToLoad(SLSkybox *&skybox, const SLstring &path, SLVec2i resolution, SLstring name)
Add skybox with HDR texture to load.
SLstring modelPath() const
Definition: SLAssetLoader.h:69
void addNodeToLoad(SLNode *&node, const SLstring &modelPath, SLSkybox *skybox=nullptr, SLbool deleteTexImgAfterBuild=false, SLbool loadMeshesOnly=true, SLMaterial *overrideMat=nullptr, float ambientFactor=0.5f, SLbool forceCookTorranceRM=false, SLuint flags=SLProcess_Triangulate|SLProcess_JoinIdenticalVertices|SLProcess_RemoveRedundantMaterials|SLProcess_FindDegenerates|SLProcess_FindInvalidData|SLProcess_SplitLargeMeshes)
Add mesh from file to load via assimp loader.
Toplevel holder of the assets meshes, materials, textures and shaders.
void colors(const SLCol4f &uniformColor)
Sets a uniform background color.
Active or visible camera node class.
Definition: SLCamera.h:54
void stereoEyeSeparation(const SLfloat es)
Definition: SLCamera.h:119
void clipFar(const SLfloat cFar)
Definition: SLCamera.h:109
void clipNear(const SLfloat cNear)
Definition: SLCamera.h:108
void devRotLoc(SLDeviceRotation *devRot, SLDeviceLocation *devLoc)
Definition: SLCamera.h:120
void focalDist(const SLfloat f)
Definition: SLCamera.h:116
SLBackground & background()
Definition: SLCamera.h:165
void attenuation(const SLfloat kConstant, const SLfloat kLinear, const SLfloat kQuadratic)
Definition: SLLight.h:116
void powers(SLfloat ambiPow, SLfloat diffPow, SLfloat specPow, const SLCol4f &ambiDiffSpecCol=SLCol4f::WHITE)
Sets the ambient, diffuse and specular powers all with the same color.
Definition: SLLight.h:74
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
void skybox(SLSkybox *sb)
Definition: SLMaterial.h:207
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 rotate(const SLQuat4f &rot, SLTransformSpace relativeTo=TS_object)
Definition: SLNode.cpp:945
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 updateMeshMat(std::function< void(SLMaterial *m)> setMat, bool recursive)
Updates the mesh material recursively with a material lambda.
Definition: SLNode.cpp:1161
void translate(const SLVec3f &vec, SLTransformSpace relativeTo=TS_object)
Definition: SLNode.cpp:906
SLRectangle creates a rectangular mesh with a certain resolution.
Definition: SLRectangle.h:29
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
SLSkybox * skybox()
Definition: SLScene.h:101
SLNode * root3D()
Definition: SLScene.h:99
friend class SLNode
Definition: SLScene.h:48
SLAnimManager & animManager()
Definition: SLScene.h:97
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
SLfloat exposure()
Definition: SLSkybox.h:54