SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneLevelOfDetail.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneLevelOfDetail.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, Marcus Hudritsch
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 <SLAssetLoader.h>
17 #include <SLRectangle.h>
18 #include <SLNodeLOD.h>
19 
20 //-----------------------------------------------------------------------------
22  : SLScene("Level of Detail Test"),
23  _sceneID(sceneID)
24 {
25 }
26 //-----------------------------------------------------------------------------
27 //! All assets the should be loaded in parallel must be registered in here.
29 {
32  "GLTF/CorinthianColumn/PavementSlateSquare2_2K_DIF.jpg",
36  "GLTF/CorinthianColumn/PavementSlateSquare2_2K_NRM.jpg",
40  "GLTF/CorinthianColumn/Corinthian-Column-Round-LOD.gltf");
41 }
42 //-----------------------------------------------------------------------------
43 //! After parallel loading of the assets the scene gets assembled in here.
45 {
46  SLchar name[512];
47  SLint size;
49  {
50  size = 25;
51  snprintf(name,
52  sizeof(name),
53  "%d corinthian columns without LOD",
54  size * size);
55  this->name(name);
56  }
57  else
58  {
59  size = 50;
60  snprintf(name,
61  sizeof(name),
62  "%d corinthian columns with LOD",
63  size * size);
64  this->name(name);
65  }
66  info(this->name() + " with cascaded shadow mapping. In the Day-Time dialogue you can change the sun angle.");
67 
68  // Create ground material
69  SLMaterial* matFloor = new SLMaterial(am,
70  "matFloor",
72  _texFloorNrm);
73 
74  // Define camera
75  SLCamera* cam1 = new SLCamera;
76  cam1->translation(0, 1.7f, 20);
77  cam1->lookAt(0, 1.7f, 0);
78  cam1->focalDist(cam1->translationOS().length());
79  cam1->clipFar(600);
80  cam1->background().colors(SLCol4f(0.1f, 0.4f, 0.8f));
81  cam1->setInitialState();
82 
83  // Create directional light for the sunlight
84  SLLightDirect* sunLight = new SLLightDirect(am, this, 1.0f);
85  sunLight->powers(0.25f, 1.0f, 1.0f);
86  sunLight->attenuation(1, 0, 0);
87  sunLight->translation(0, 1.7f, 0);
88  sunLight->lookAt(-1, 0, -1);
89  sunLight->doSunPowerAdaptation(true);
90 
91  // Add cascaded shadow mapping
92  sunLight->createsShadows(true);
93  sunLight->createShadowMapAutoSize(cam1);
94  sunLight->doSmoothShadows(true);
95  sunLight->castsShadows(false);
96  sunLight->shadowMinBias(0.003f);
97  sunLight->shadowMaxBias(0.012f);
98 
99  // Let the sun be rotated by time and location
102  7.24337,
103  488.2); // Ecke Giosa
105  7.24310,
106  488.7 + 1.7); // auf Parkplatz
107 
108  // Floor rectangle
109  SLNode* rect = new SLNode(new SLRectangle(am,
110  SLVec2f(-200, -200),
111  SLVec2f(200, 200),
112  SLVec2f(0, 0),
113  SLVec2f(50, 50),
114  50,
115  50,
116  "Floor",
117  matFloor));
118  rect->rotate(90, -1, 0, 0);
119  rect->castsShadows(false);
120 
121  // Configure the corinthian column
122  SLNode* columnL0 = _columnLOD->findChild<SLNode>("Corinthian-Column-Round-L0");
123  SLNode* columnL1 = _columnLOD->findChild<SLNode>("Corinthian-Column-Round-L1");
124  SLNode* columnL2 = _columnLOD->findChild<SLNode>("Corinthian-Column-Round-L2");
125  SLNode* columnL3 = _columnLOD->findChild<SLNode>("Corinthian-Column-Round-L3");
126 
127  // Assemble scene
128  SLNode* scene = new SLNode("Scene");
129  root3D(scene);
130  scene->addChild(sunLight);
131  scene->addChild(rect);
132  scene->addChild(cam1);
133 
134  // create loads of pillars
135  SLint numColumns = size * size;
136  SLfloat offset = 5.0f;
137  SLfloat z = (float)(size - 1) * offset * 0.5f;
138 
139  for (SLint iZ = 0; iZ < size; ++iZ)
140  {
141  SLfloat x = -(float)(size - 1) * offset * 0.5f;
142 
143  for (SLint iX = 0; iX < size; ++iX)
144  {
145  SLint iZX = iZ * size + iX;
146 
148  {
149  // Without just the level 0 node
150  string strNode = "Node" + std::to_string(iZX);
151  SLNode* column = new SLNode(columnL1->mesh(),
152  strNode + "-L0");
153  column->translate(x, 0, z, TS_object);
154  scene->addChild(column);
155  }
156  else
157  {
158  // With LOD parent node and 3 levels
159  string strLOD = "LOD" + std::to_string(iZX);
160  SLNodeLOD* lod_group = new SLNodeLOD(strLOD);
161  lod_group->translate(x, 0, z, TS_object);
162  lod_group->addChildLOD(new SLNode(columnL1->mesh(),
163  strLOD + "-L0"),
164  0.1f,
165  3);
166  lod_group->addChildLOD(new SLNode(columnL2->mesh(),
167  strLOD + "-L1"),
168  0.01f,
169  3);
170  lod_group->addChildLOD(new SLNode(columnL3->mesh(),
171  strLOD + "-L2"),
172  0.0001f,
173  3);
174  scene->addChild(lod_group);
175  }
176  x += offset;
177  }
178  z -= offset;
179  }
180 
181  // Set active camera & the root pointer
182  sv->camera(cam1);
183  sv->doWaitOnIdle(false);
184 }
185 //-----------------------------------------------------------------------------
The AppCommon class holds the top-level instances of the app-demo.
Definition of scene IDs in the demo app.
@ SID_Benchmark_ColumnsNoLOD
Class declaration for an SLScene inherited class.
float SLfloat
Definition: SL.h:173
char SLchar
Definition: SL.h:162
int SLint
Definition: SL.h:170
int SLSceneID
Scene identifier.
Definition: SLEnums.h:91
@ TS_object
Definition: SLEnums.h:210
#define SL_ANISOTROPY_MAX
Definition: SLGLTexture.h:34
SLScene SLSceneView SLint sceneID
Definition: SLScene.h:33
SLVec2< SLfloat > SLVec2f
Definition: SLVec2.h:141
SLVec4< SLfloat > SLCol4f
Definition: SLVec4.h:237
static SLDeviceLocation devLoc
Mobile device location from GPS.
Definition: AppCommon.h:65
static SLstring modelPath
Path to 3D models.
Definition: AppCommon.h:85
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.
AppDemoSceneLevelOfDetail(SLSceneID sceneID)
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.
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 clipFar(const SLfloat cFar)
Definition: SLCamera.h:109
void focalDist(const SLfloat f)
Definition: SLCamera.h:116
SLBackground & background()
Definition: SLCamera.h:165
void defaultLatLonAlt(int degreesLat, int minutesLat, double secondsLat, int degreesLon, int minutesLon, double secondsLon, double altitudeM)
Default coordinate setter in WGS84 Lat-Lon in degrees, minutes and seconds.
void originLatLonAlt(int degreesLat, int minutesLat, double secondsLat, int degreesLon, int minutesLon, double secondsLon, double altitudeM)
Origin coordinate setter in WGS84 Lat-Lon in degrees, minutes and seconds.
void sunLightNode(SLLightDirect *sln)
SLLightDirect class for a directional light source.
Definition: SLLightDirect.h:40
void doSunPowerAdaptation(SLbool enabled)
Definition: SLLightDirect.h:82
void createShadowMapAutoSize(SLCamera *camera, SLVec2i texSize=SLVec2i(1024, 1024), int numCascades=4) override
void createsShadows(SLbool createsShadows)
Definition: SLLight.cpp:98
void shadowMaxBias(SLfloat maxBias)
Definition: SLLight.h:129
void doSmoothShadows(SLbool doSS)
Definition: SLLight.h:126
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
void shadowMinBias(SLfloat minBias)
Definition: SLLight.h:128
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 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 castsShadows(SLbool castsShadows)
Definition: SLNode.h:282
T * findChild(const SLstring &name="", SLbool findRecursive=true)
Definition: SLNode.h:388
SLVec3f translationOS() const
Definition: SLNode.h:468
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
Level of detail (LOD) group node based on screen space coverage.
Definition: SLNodeLOD.h:28
void addChildLOD(SLNode *child, SLfloat minLodLimit, SLubyte levelForSM=0)
Adds an LOD node with forced decreasing min LOD coverage.
Definition: SLNodeLOD.cpp:32
const SLstring & name() const
Definition: SLObject.h:38
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
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
T length() const
Definition: SLVec3.h:122