Skip to content

Commit 15b77b0

Browse files
committed
Fixed code in some of light set-ups and added comments
1 parent c3347e7 commit 15b77b0

File tree

1 file changed

+65
-58
lines changed

1 file changed

+65
-58
lines changed

9-model/main.cpp

Lines changed: 65 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ std::string pwd = std::filesystem::current_path().string();
6262
// Shader sources
6363
std::string lightVShaderPath = pwd + "/../shaders/light.vs";
6464
std::string lightFShaderPath = pwd + "/../shaders/light.fs";
65-
std::string modelVShaderPath = pwd + "/../shaders/mesh.vs";
66-
std::string modelFShaderPath = pwd + "/../shaders/mesh.fs";
65+
std::string meshVShaderPath = pwd + "/../shaders/mesh.vs";
66+
std::string meshFShaderPath = pwd + "/../shaders/mesh.fs";
6767
std::string modelPath = pwd + "/../../assets/backpack/backpack.obj";
6868
std::string cubePath = pwd + "/../../assets/cube/cube.obj";
6969

@@ -110,7 +110,8 @@ float constant = CONSTANT;
110110
float linear = LINEAR;
111111
float quadratic = QUADRATIC;
112112

113-
float alpha = 1.0f;
113+
float lightIntensity = 0.5f;
114+
float lightAlpha = 1.0f;
114115

115116
glm::vec4 backgroundColor = DEFAULT_BACKGROUND_COLOR;
116117
glm::vec3 lightColor = DEFAULT_LIGHT_COLOR;
@@ -150,55 +151,54 @@ int main()
150151
glfwSetScrollCallback(window.getGlWindow(), scroll_callback);
151152
glfwSetKeyCallback(window.getGlWindow(), key_callback);
152153

153-
// tell stb_image.h to flip loaded texture's on the y-axis (before loading model).
154-
// stbi_set_flip_vertically_on_load(true);
155-
156-
// configure global opengl state
154+
// Configure OpenGL to use depth buffer
157155
// -----------------------------
158156
glEnable(GL_DEPTH_TEST);
159157

160-
// build and compile shaders
161-
// -------------------------
162-
// tell stb_image.h to flip loaded texture's on the y-axis (before loading model).
158+
// Tell stb_image.h to flip loaded texture's on the y-axis (before loading model).
163159
stbi_set_flip_vertically_on_load(true);
164160

165-
// build and compile shaders
161+
// Build and compile shaders
166162
// -------------------------
167-
Shader meshShader(modelVShaderPath.c_str(), modelFShaderPath.c_str());
163+
Shader meshShader(meshVShaderPath.c_str(), meshFShaderPath.c_str());
168164
Shader lightShader(lightVShaderPath.c_str(), lightFShaderPath.c_str());
169165

170166
Shader::enableDepth();
171167

172-
// load models
168+
// Load models
173169
// -----------
174170
Model backpack(modelPath.c_str());
175171
Model cube(cubePath.c_str());
176172

177-
// draw in wireframe
178-
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
173+
// Uncomment to render models in wireframe
174+
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
179175

180-
// render loop
176+
// Render loop
181177
// -----------
182178
while (!window.isShouldClose())
183179
{
184-
// input
185-
// -----
180+
// Process input
181+
// -------------
186182
processMovement(window.getGlWindow(), &camera);
187183

188-
// render
189-
// ------
184+
// Set background color and clear color buffer and depth buffer
185+
// ------------------------------------------------------------
190186
glClearColor(backgroundColor.x, backgroundColor.y, backgroundColor.z, backgroundColor.t);
191187
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
192188

193-
// don't forget to enable shader before setting uniforms
194-
// meshShader.use();
189+
// Enabling our shader before using it's uniforms
190+
// ----------------------------------------------
195191
lightShader.use();
196192

197193
lightShader.setFloat("material.shininess", specularIntensity);
198194

195+
// Directional light creation
196+
// --------------------------
199197
DirectionalLight directionalLight(lightDirection, directionalAmbient, directionalDiffuse, directionalSpecular);
200198
directionalLight.load(lightShader.getID());
201199

200+
// Point lights creation
201+
// ---------------------
202202
std::vector<PointLight> pointLights;
203203
for(unsigned int i = 0; i < 5; i++)
204204
{
@@ -207,21 +207,28 @@ int main()
207207
pointLights[i].load(lightShader.getID());
208208
}
209209

210-
// view/projection transformations
210+
// Transformations for view and projection
211+
// ---------------------------------------
211212
glm::mat4 projection = glm::perspective(glm::radians(camera.getFov()), width / height, 0.1f, 100.0f);
212213
glm::mat4 view = camera.calculateLookAtMatrix(camera.getPosition(), camera.getPosition() + camera.getFront(), camera.getUp());
213214
lightShader.setMatrix4fv("projection", 1, GL_FALSE, projection);
214215
lightShader.setMatrix4fv("view", 1, GL_FALSE, view);
215216

216-
// render the loaded model
217+
// Transformations for model
218+
// -------------------------
217219
glm::mat4 model = glm::mat4(1.0f);
218220
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f)); // translate it down so it's at the center of the scene
219221
model = glm::scale(model, glm::vec3(1.0f, 1.0f, 1.0f)); // it's a bit too big for our scene, so scale it down
220222
lightShader.setMatrix3fv("normalMatrix", 1, GL_TRUE, glm::mat3(glm::inverse(model)));
221223
lightShader.setMatrix4fv("model", 1, GL_FALSE, model);
224+
225+
// Render model
226+
// ------------
222227
backpack.draw(lightShader);
223228
lightShader.unbind();
224229

230+
// Light cubes creation
231+
// --------------------
225232
meshShader.use();
226233
meshShader.setVec3("color", glm::value_ptr(lightColor));
227234
meshShader.setMatrix4fv("projection", 1, GL_FALSE, projection);
@@ -237,14 +244,10 @@ int main()
237244
}
238245
meshShader.unbind();
239246

240-
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
241-
// -------------------------------------------------------------------------------
242247
window.swapBuffers();
243248
glfwPollEvents();
244249
}
245250

246-
// glfw: terminate, clearing all previously allocated GLFW resources.
247-
// ------------------------------------------------------------------
248251
glfwTerminate();
249252
return 0;
250253
}
@@ -353,23 +356,24 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
353356
// Simple Day light set-up when user presses "1" key
354357
if(key == GLFW_KEY_1 && action == GLFW_PRESS)
355358
{
356-
directionalAmbient = (DIR_LIGHT_AMBIENT_VEC + glm::vec3(0.2f)) * colors[1];
357-
directionalDiffuse = (DIR_LIGHT_DIFFUSE_VEC + glm::vec3(0.4f)) * colors[1];
358-
directionalSpecular = (DIR_LIGHT_SPECULAR_VEC - glm::vec3(0.4f)) * colors[1];
359+
lightColor = colors[4];
360+
lightAlpha = 0.5f;
361+
362+
directionalAmbient = (DIR_LIGHT_AMBIENT_VEC + glm::vec3(0.2f)) * (lightColor * lightAlpha);
363+
directionalDiffuse = (DIR_LIGHT_DIFFUSE_VEC + glm::vec3(0.4f)) * (lightColor * lightAlpha);
364+
directionalSpecular = (DIR_LIGHT_SPECULAR_VEC - glm::vec3(0.4f)) * (lightColor * lightAlpha);
359365

360366
spotLightAmbient = glm::vec3(0.0f);
361367
spotLightDiffuse = glm::vec3(0.0f);
362368
spotLightSpecular = glm::vec3(0.0f);
363369

364370

365-
pointLightAmbient = POINT_LIGHT_AMBIENT_VEC * colors[4];
366-
pointLightDiffuse = POINT_LIGHT_DIFFUSE_VEC * colors[4];
367-
pointLightSpecular = POINT_LIGHT_SPECULAR_VEC * colors[4];
371+
pointLightAmbient = POINT_LIGHT_AMBIENT_VEC * (lightColor * lightAlpha);
372+
pointLightDiffuse = POINT_LIGHT_DIFFUSE_VEC * (lightColor * lightAlpha);
373+
pointLightSpecular = POINT_LIGHT_SPECULAR_VEC * (lightColor * lightAlpha);
368374

369375
backgroundColor = DESERT_BACKGROUND_COLOR;
370376

371-
lightColor = colors[4];
372-
373377
specularIntensity = 1;
374378

375379
cutOff = 0;
@@ -384,18 +388,19 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
384388
if(key == GLFW_KEY_2 && action == GLFW_PRESS)
385389
{
386390
lightColor = colors[6];
391+
lightAlpha = 0.5f;
387392

388-
directionalAmbient = (DIR_LIGHT_AMBIENT_VEC + glm::vec3(0.05f)) * lightColor;
389-
directionalDiffuse = (DIR_LIGHT_DIFFUSE_VEC + glm::vec3(0.1f)) * lightColor;
390-
directionalSpecular = (DIR_LIGHT_SPECULAR_VEC - glm::vec3(0.2f)) * lightColor;
393+
directionalAmbient = DIR_LIGHT_AMBIENT_VEC * lightColor * lightAlpha;
394+
directionalDiffuse = DIR_LIGHT_DIFFUSE_VEC * lightColor * lightAlpha;
395+
directionalSpecular = DIR_LIGHT_SPECULAR_VEC * lightColor * lightAlpha;
391396

392397
spotLightAmbient = SPOT_LIGHT_AMBIENT_VEC * lightColor;
393398
spotLightDiffuse = SPOT_LIGHT_DIFFUSE_VEC * lightColor;
394399
spotLightSpecular = SPOT_LIGHT_SPECULAR_VEC * lightColor;
395400

396-
pointLightAmbient = POINT_LIGHT_AMBIENT_VEC * lightColor;
397-
pointLightDiffuse = POINT_LIGHT_DIFFUSE_VEC * lightColor;
398-
pointLightSpecular = POINT_LIGHT_SPECULAR_VEC * lightColor;
401+
pointLightAmbient = POINT_LIGHT_AMBIENT_VEC * lightColor * lightAlpha;
402+
pointLightDiffuse = POINT_LIGHT_DIFFUSE_VEC * lightColor * lightAlpha;
403+
pointLightSpecular = POINT_LIGHT_SPECULAR_VEC * lightColor * lightAlpha;
399404

400405
backgroundColor = FACTORY_BACKGROUND_COLOR;
401406

@@ -412,22 +417,23 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
412417
// Simple horror light set-up when user presses "3" key
413418
if(key == GLFW_KEY_3 && action == GLFW_PRESS)
414419
{
415-
directionalAmbient = glm::vec3(0.1f);
416-
directionalDiffuse = glm::vec3(0.1f);
417-
directionalSpecular = glm::vec3(0.1f);
420+
lightColor = colors[0];
421+
lightAlpha = 0.3f;
422+
423+
directionalAmbient = (DIR_LIGHT_AMBIENT_VEC - glm::vec3(0.15f)) * lightColor * lightAlpha;
424+
directionalDiffuse = (DIR_LIGHT_DIFFUSE_VEC - glm::vec3(0.2f)) * lightColor * lightAlpha;
425+
directionalSpecular = (DIR_LIGHT_SPECULAR_VEC - glm::vec3(0.2f)) * lightColor * lightAlpha;
418426

419427
spotLightAmbient = SPOT_LIGHT_AMBIENT_VEC;
420428
spotLightDiffuse = SPOT_LIGHT_DIFFUSE_VEC;
421429
spotLightSpecular = SPOT_LIGHT_SPECULAR_VEC;
422430

423-
pointLightAmbient = POINT_LIGHT_AMBIENT_VEC - glm::vec3(0.1f);
424-
pointLightDiffuse = POINT_LIGHT_DIFFUSE_VEC - glm::vec3(0.1f);
425-
pointLightSpecular = POINT_LIGHT_SPECULAR_VEC - glm::vec3(0.1f);
431+
pointLightAmbient = POINT_LIGHT_AMBIENT_VEC * lightColor * lightAlpha;
432+
pointLightDiffuse = POINT_LIGHT_DIFFUSE_VEC * lightColor * lightAlpha;
433+
pointLightSpecular = POINT_LIGHT_SPECULAR_VEC * lightColor * lightAlpha;
426434

427435
backgroundColor = DEFAULT_BACKGROUND_COLOR;
428436

429-
lightColor = colors[0];
430-
431437
specularIntensity = SPECULAR_INTENSITY;
432438

433439
cutOff = 5.0f;
@@ -441,22 +447,23 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
441447
// Simple lab light set-up when user presses "4" key
442448
if(key == GLFW_KEY_4 && action == GLFW_PRESS)
443449
{
444-
directionalAmbient = DIR_LIGHT_AMBIENT_VEC * colors[3];
445-
directionalDiffuse = DIR_LIGHT_DIFFUSE_VEC * colors[3];
446-
directionalSpecular = DIR_LIGHT_SPECULAR_VEC * colors[3];
450+
lightColor = colors[5];
451+
lightAlpha = 0.5f;
452+
453+
directionalAmbient = (DIR_LIGHT_AMBIENT_VEC + glm::vec3(0.1f)) * lightColor * lightAlpha;
454+
directionalDiffuse = (DIR_LIGHT_DIFFUSE_VEC + glm::vec3(0.1f)) * lightColor * lightAlpha;
455+
directionalSpecular = (DIR_LIGHT_SPECULAR_VEC + glm::vec3(0.1f)) * lightColor * lightAlpha;
447456

448457
spotLightAmbient = SPOT_LIGHT_AMBIENT_VEC;
449458
spotLightDiffuse = SPOT_LIGHT_DIFFUSE_VEC;
450459
spotLightSpecular = SPOT_LIGHT_SPECULAR_VEC;
451460

452-
pointLightAmbient = POINT_LIGHT_AMBIENT_VEC * colors[3];
453-
pointLightDiffuse = POINT_LIGHT_DIFFUSE_VEC;
454-
pointLightSpecular = POINT_LIGHT_SPECULAR_VEC;
461+
pointLightAmbient = (POINT_LIGHT_AMBIENT_VEC + glm::vec3(0.1f)) * lightColor * lightAlpha;
462+
pointLightDiffuse = (POINT_LIGHT_DIFFUSE_VEC + glm::vec3(0.1f)) * lightColor * lightAlpha;
463+
pointLightSpecular = (POINT_LIGHT_SPECULAR_VEC + glm::vec3(0.1f)) * lightColor * lightAlpha;
455464

456465
backgroundColor = LAB_BACKGROUND_COLOR;
457466

458-
lightColor = colors[3];
459-
460467
specularIntensity = SPECULAR_INTENSITY;
461468

462469
cutOff = CUT_OFF;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy