SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneTextureBlend.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneTextureBlend.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
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 <SLAssetLoader.h>
15 #include <SLLightSpot.h>
16 #include <SLPolygon.h>
17 #include <AppCommon.h>
18 
19 //-----------------------------------------------------------------------------
21 {
22  info("Texture map blending with depth sorting. Transparent tree rectangles in view "
23  "frustum are rendered back to front. You can turn on/off alpha sorting in the "
24  "menu Preferences of press key J.");
25 }
26 //-----------------------------------------------------------------------------
27 //! All assets the should be loaded in parallel must be registered in here.
29 {
32  "tree1_1024_C.png",
33  GL_LINEAR_MIPMAP_LINEAR,
34  GL_LINEAR,
35  TT_diffuse,
36  GL_CLAMP_TO_EDGE,
37  GL_CLAMP_TO_EDGE);
40  "grass0512_C.jpg",
41  GL_LINEAR_MIPMAP_LINEAR,
42  GL_LINEAR);
44  AppCommon::shaderPath + "PerVrtTm.vert",
45  AppCommon::shaderPath + "PerVrtTm.frag");
46 }
47 //-----------------------------------------------------------------------------
48 //! After parallel loading of the assets the scene gets assembled in here.
50 {
51  SLMaterial* m1 = new SLMaterial(am,
52  "m1",
53  SLCol4f(1, 1, 1),
54  SLCol4f(0, 0, 0),
55  100);
56  SLMaterial* m2 = new SLMaterial(am,
57  "m2",
58  SLCol4f(1, 1, 1),
59  SLCol4f(0, 0, 0),
60  100);
61  m1->program(_sp);
62  m1->addTexture(_t1);
63  m2->addTexture(_t2);
64 
65  SLCamera* cam1 = new SLCamera("Camera 1");
66  cam1->translation(6.5f, 0.5f, -18);
67  cam1->lookAt(0, 0, 0);
68  cam1->focalDist(18);
69  cam1->background().colors(SLCol4f(0.6f, 0.6f, 1));
70  cam1->setInitialState();
72 
73  SLLightSpot* light = new SLLightSpot(am, this, 0.1f);
74  light->translation(5, 5, 5);
75  light->lookAt(0, 0, 0);
76  light->attenuation(1, 0, 0);
77 
78  // Build arrays for polygon vertices and texture coordinates for tree
79  SLVVec3f pNW, pSE;
80  SLVVec2f tNW, tSE;
81  pNW.push_back(SLVec3f(0, 0, 0));
82  tNW.push_back(SLVec2f(0.5f, 0.0f));
83  pNW.push_back(SLVec3f(1, 0, 0));
84  tNW.push_back(SLVec2f(1.0f, 0.0f));
85  pNW.push_back(SLVec3f(1, 2, 0));
86  tNW.push_back(SLVec2f(1.0f, 1.0f));
87  pNW.push_back(SLVec3f(0, 2, 0));
88  tNW.push_back(SLVec2f(0.5f, 1.0f));
89  pSE.push_back(SLVec3f(-1, 0, 0));
90  tSE.push_back(SLVec2f(0.0f, 0.0f));
91  pSE.push_back(SLVec3f(0, 0, 0));
92  tSE.push_back(SLVec2f(0.5f, 0.0f));
93  pSE.push_back(SLVec3f(0, 2, 0));
94  tSE.push_back(SLVec2f(0.5f, 1.0f));
95  pSE.push_back(SLVec3f(-1, 2, 0));
96  tSE.push_back(SLVec2f(0.0f, 1.0f));
97 
98  // Build tree out of 4 polygons
99  SLNode* p1 = new SLNode(new SLPolygon(am, pNW, tNW, "Tree+X", m1));
100  SLNode* p2 = new SLNode(new SLPolygon(am, pNW, tNW, "Tree-Z", m1));
101  p2->rotate(90, 0, 1, 0);
102  SLNode* p3 = new SLNode(new SLPolygon(am, pSE, tSE, "Tree-X", m1));
103  SLNode* p4 = new SLNode(new SLPolygon(am, pSE, tSE, "Tree+Z", m1));
104  p4->rotate(90, 0, 1, 0);
105 
106  // Turn face culling off so that we see both sides
107  p1->drawBits()->on(SL_DB_CULLOFF);
108  p2->drawBits()->on(SL_DB_CULLOFF);
109  p3->drawBits()->on(SL_DB_CULLOFF);
110  p4->drawBits()->on(SL_DB_CULLOFF);
111 
112  // Build tree group
113  SLNode* tree = new SLNode("grTree");
114  tree->addChild(p1);
115  tree->addChild(p2);
116  tree->addChild(p3);
117  tree->addChild(p4);
118 
119  // Build arrays for polygon vertices and texcoords for ground
120  SLVVec3f pG;
121  SLVVec2f tG;
122  SLfloat size = 22.0f;
123  pG.push_back(SLVec3f(-size, 0, size));
124  tG.push_back(SLVec2f(0, 0));
125  pG.push_back(SLVec3f(size, 0, size));
126  tG.push_back(SLVec2f(30, 0));
127  pG.push_back(SLVec3f(size, 0, -size));
128  tG.push_back(SLVec2f(30, 30));
129  pG.push_back(SLVec3f(-size, 0, -size));
130  tG.push_back(SLVec2f(0, 30));
131 
132  SLNode* scene = new SLNode("grScene");
133  this->root3D(scene);
134  scene->addChild(light);
135  scene->addChild(tree);
136  scene->addChild(new SLNode(new SLPolygon(am,
137  pG,
138  tG,
139  "Ground",
140  m2)));
141 
142  // create 21*21*21-1 references around the center tree
143  SLint res = 10;
144  for (SLint iZ = -res; iZ <= res; ++iZ)
145  {
146  for (SLint iX = -res; iX <= res; ++iX)
147  {
148  if (iX != 0 || iZ != 0)
149  {
150  SLNode* t = tree->copyRec();
151  t->translate(float(iX) * 2 + Utils::random(0.7f, 1.4f),
152  0,
153  float(iZ) * 2 + Utils::random(0.7f, 1.4f),
154  TS_object);
155  t->rotate(Utils::random(0.f, 90.f), 0, 1, 0);
156  t->scale(Utils::random(0.5f, 1.0f));
157  scene->addChild(t);
158  }
159  }
160  }
161 
162  scene->addChild(cam1);
163 
164  sv->camera(cam1);
165 }
166 //-----------------------------------------------------------------------------
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
int SLint
Definition: SL.h:170
#define SL_DB_CULLOFF
Turn off face culling.
Definition: SLDrawBits.h:28
@ TS_object
Definition: SLEnums.h:210
@ TT_diffuse
Definition: SLGLTexture.h:78
vector< SLVec2f > SLVVec2f
Definition: SLVec2.h:143
SLVec2< SLfloat > SLVec2f
Definition: SLVec2.h:141
vector< SLVec3f > SLVVec3f
Definition: SLVec3.h:325
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 texturePath
Path to texture images.
Definition: AppCommon.h:86
static SLstring shaderPath
Path to GLSL shader programs.
Definition: AppCommon.h:84
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 addProgramToLoad(SLGLProgram *&program, const SLstring &vertShaderFile, const SLstring &fragShaderFile)
Add generic GLSL program with shader files to load.
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 colors(const SLCol4f &uniformColor)
Sets a uniform background color.
Active or visible camera node class.
Definition: SLCamera.h:54
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 on(SLuint bit)
Turns the specified bit on.
Definition: SLDrawBits.h:51
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
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
void addTexture(SLGLTexture *texture)
Adds the passed texture to the equivalent texture type vector.
Definition: SLMaterial.cpp:348
void program(SLGLProgram *sp)
Definition: SLMaterial.h:205
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
virtual SLNode * copyRec()
Definition: SLNode.cpp:572
SLDrawBits * drawBits()
Definition: SLNode.h:299
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
SLPolygon creates a convex polyon mesh.
Definition: SLPolygon.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
float random(float min, float max)
Returns a uniform distributed random float number between min and max.
Definition: Utils.h:265