SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneShaderBlinn.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneShaderBlinn.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 
14 #include <SLAssetLoader.h>
15 #include <SLLightSpot.h>
16 #include <SLLightRect.h>
17 #include <SLSphere.h>
18 #include <AppCommon.h>
19 
20 //-----------------------------------------------------------------------------
22  : SLScene(name),
23  _perVertex(perVertex)
24 {
25  if (_perVertex)
26  {
27  info("Per-vertex lighting with Blinn-Phong reflection model. "
28  "The reflection of 5 light sources is calculated per vertex. "
29  "The green and the white light are attached to the camera, the others are in the scene. "
30  "The light calculation per vertex is the fastest but leads to artefacts with spot lights");
31  }
32  else
33  {
34  info("Per-pixel lighting with Blinn-Phong reflection model. "
35  "The reflection of 5 light sources is calculated per pixel. "
36  "The light calculation is done in the fragment shader.");
37  }
38 }
39 //-----------------------------------------------------------------------------
40 //! All assets the should be loaded in parallel must be registered in here.
42 {
45  "earth2048_C_Q95.jpg");
48  "earth2048_N.jpg");
51  "earth2048_H.jpg");
53  AppCommon::shaderPath + "PerVrtBlinnTm.vert",
54  AppCommon::shaderPath + "PerVrtBlinnTm.frag");
56  AppCommon::shaderPath + "PerVrtBlinn.vert",
57  AppCommon::shaderPath + "PerVrtBlinn.frag");
59  AppCommon::shaderPath + "PerPixBlinnTmNm.vert",
60  AppCommon::shaderPath + "PerPixBlinnTmPm.frag");
61 }
62 //-----------------------------------------------------------------------------
63 //! After parallel loading of the assets the scene gets assembled in here.
65 {
66  SLMaterial* mL = nullptr;
67  SLMaterial* mM = nullptr;
68  SLMaterial* mR = nullptr;
69 
70  if (_perVertex)
71  {
72  mL = new SLMaterial(am, "mL", _texC, nullptr, nullptr, nullptr, _perVrtTm);
73  mM = new SLMaterial(am, "mM", _perVrt);
74  mR = new SLMaterial(am, "mR", _texC, nullptr, nullptr, nullptr, _perVrtTm);
75  }
76  else
77  { // per pixel
78  SLGLUniform1f* scale = new SLGLUniform1f(UT_const, "u_scale", 0.02f, 0.002f, 0, 1);
79  SLGLUniform1f* offset = new SLGLUniform1f(UT_const, "u_offset", -0.02f, 0.002f, -1, 1);
80  _perPix->addUniform1f(scale);
81  _perPix->addUniform1f(offset);
82  mL = new SLMaterial(am, "mL", _texC);
83  mM = new SLMaterial(am, "mM");
84  mR = new SLMaterial(am, "mR", _texC, _texN, _texH, nullptr, _perPix);
85  }
86 
87  mM->shininess(500);
88 
89  // Base root group node for the scene
90  SLNode* scene = new SLNode;
91  this->root3D(scene);
92 
93  SLCamera* cam1 = new SLCamera("Camera 1");
94  cam1->translation(0, 0, 7);
95  cam1->lookAt(0, 0, 0);
96  cam1->focalDist(7);
97  cam1->background().colors(SLCol4f(0.1f, 0.1f, 0.1f));
98  cam1->setInitialState();
99  scene->addChild(cam1);
100 
101  // Define 5 light sources
102 
103  // A rectangular white light attached to the camera
104  SLLightRect* lightW = new SLLightRect(am, this, 2.0f, 1.0f);
105  lightW->ambiDiffPowers(0, 5);
106  lightW->translation(0, 2.5f, 0);
107  lightW->translation(0, 2.5f, -7);
108  lightW->rotate(-90, 1, 0, 0);
109  lightW->attenuation(0, 0, 1);
110  cam1->addChild(lightW);
111 
112  // A red point light from the front attached in the scene
113  SLLightSpot* lightR = new SLLightSpot(am, this, 0.1f);
114  lightR->ambientColor(SLCol4f(0, 0, 0));
115  lightR->diffuseColor(SLCol4f(1, 0, 0));
116  lightR->specularColor(SLCol4f(1, 0, 0));
117  lightR->translation(0, 0, 2);
118  lightR->lookAt(0, 0, 0);
119  lightR->attenuation(0, 0, 1);
120  scene->addChild(lightR);
121 
122  // A green spot head light with 40 deg. spot angle from front right
123  SLLightSpot* lightG = new SLLightSpot(am, this, 0.1f, 20, true);
124  lightG->ambientColor(SLCol4f(0, 0, 0));
125  lightG->diffuseColor(SLCol4f(0, 1, 0));
126  lightG->specularColor(SLCol4f(0, 1, 0));
127  lightG->translation(1.5f, 1, -5.5f);
128  lightG->lookAt(0, 0, -7);
129  lightG->attenuation(1, 0, 0);
130  cam1->addChild(lightG);
131 
132  // A blue spot light with 40 deg. spot angle from front left
133  SLLightSpot* lightB = new SLLightSpot(am, this, 0.1f, 20.0f, true);
134  lightB->ambientColor(SLCol4f(0, 0, 0));
135  lightB->diffuseColor(SLCol4f(0, 0, 1));
136  lightB->specularColor(SLCol4f(0, 0, 1));
137  lightB->translation(-1.5f, 1.5f, 1.5f);
138  lightB->lookAt(0, 0, 0);
139  lightB->attenuation(1, 0, 0);
140  SLAnimation* light3Anim = animManager().createNodeAnimation("Ball3_anim",
141  1.0f,
142  true,
143  EC_outQuad,
145  light3Anim->createNodeAnimTrackForTranslation(lightB, SLVec3f(0, -2, 0));
146  scene->addChild(lightB);
147 
148  // A yellow directional light from the back-bottom
149  // Do constant attenuation for directional lights since it is infinitely far away
150  SLLightDirect* lightY = new SLLightDirect(am, this);
151  lightY->ambientColor(SLCol4f(0, 0, 0));
152  lightY->diffuseColor(SLCol4f(1, 1, 0));
153  lightY->specularColor(SLCol4f(1, 1, 0));
154  lightY->translation(-1.5f, -1.5f, 1.5f);
155  lightY->lookAt(0, 0, 0);
156  lightY->attenuation(1, 0, 0);
157  scene->addChild(lightY);
158 
159  // Add some meshes to be lighted
160  SLNode* sphereL = new SLNode(new SLSpheric(am, 1.0f, 0.0f, 180.0f, 36, 36, "Sphere", mL));
161  sphereL->translate(-2, 0, 0);
162  sphereL->rotate(90, -1, 0, 0);
163  SLNode* sphereM = new SLNode(new SLSpheric(am, 1.0f, 0.0f, 180.0f, 36, 36, "Sphere", mM));
164  SLNode* sphereR = new SLNode(new SLSpheric(am, 1.0f, 0.0f, 180.0f, 36, 36, "Sphere", mR));
165  sphereR->translate(2, 0, 0);
166  sphereR->rotate(90, -1, 0, 0);
167 
168  scene->addChild(sphereL);
169  scene->addChild(sphereM);
170  scene->addChild(sphereR);
171  sv->camera(cam1);
172 }
173 //-----------------------------------------------------------------------------
The AppCommon class holds the top-level instances of the app-demo.
Class declaration for an SLScene inherited class.
string SLstring
Definition: SL.h:158
@ EC_outQuad
quadratic easing out, decelerating to zero velocity
Definition: SLEnums.h:183
@ AL_pingPongLoop
loop forward and backwards
Definition: SLEnums.h:171
@ UT_const
constant value
Definition: SLEnums.h:233
SLGLUniform< SLfloat > SLGLUniform1f
Definition: SLGLUniform.h:162
SLVec3< SLfloat > SLVec3f
Definition: SLVec3.h:318
SLVec4< SLfloat > SLCol4f
Definition: SLVec4.h:237
static SLstring texturePath
Path to texture images.
Definition: AppCommon.h:86
static SLstring shaderPath
Path to GLSL shader programs.
Definition: AppCommon.h:84
AppDemoSceneShaderBlinn(SLstring name, bool perVertex)
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.
SLAnimation * createNodeAnimation(SLfloat duration)
SLAnimation is the base container for all animation data.
Definition: SLAnimation.h:33
SLNodeAnimTrack * createNodeAnimTrackForTranslation(SLNode *target, const SLVec3f &endPos)
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 focalDist(const SLfloat f)
Definition: SLCamera.h:116
SLBackground & background()
Definition: SLCamera.h:165
void addUniform1f(SLGLUniform1f *u)
add float uniform
Template for a single GLSL uniform variable.
Definition: SLGLUniform.h:24
SLLightDirect class for a directional light source.
Definition: SLLightDirect.h:40
void ambientColor(const SLCol4f &ambi)
Definition: SLLight.h:105
void ambiDiffPowers(SLfloat ambiPow, SLfloat diffPow, const SLCol4f &ambiDiffCol=SLCol4f::WHITE)
Sets the ambient and diffuse powers with the same color.
Definition: SLLight.h:88
void diffuseColor(const SLCol4f &diff)
Definition: SLLight.h:107
void specularColor(const SLCol4f &spec)
Definition: SLLight.h:109
void attenuation(const SLfloat kConstant, const SLfloat kLinear, const SLfloat kQuadratic)
Definition: SLLight.h:116
Light node class for a rectangular light source.
Definition: SLLightRect.h:39
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 shininess(SLfloat shin)
Definition: SLMaterial.h:177
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 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
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
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
SLSphere creates a sphere mesh based on SLRevolver.
Definition: SLSpheric.h:21