working lights!

- fix positions of lights
- fix normal vector bug (mat4->mat3)
- change light color
This commit is contained in:
2026-01-29 18:07:49 +01:00
parent 0d25e9b7f9
commit c635a0f0c2
5 changed files with 18 additions and 23 deletions

View File

@@ -17,7 +17,7 @@ float dither(vec2 uv) {
}
const vec3 objectColor = vec3(1.0, 1.0, 1.0);
const vec3 lightColor = vec3(1.0, 1.0, 1.0);
const vec3 lightColor = vec3(0.5, 0.5, 0.5);
const float ambientStrength = 0.1;
const float specularStrength = 0.5;
const float shininess = 32.0;
@@ -35,7 +35,7 @@ void main()
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
vec3 halfwayDir = normalize(lightDir + viewDir);
vec3 halfwayDir = normalize(lightDir + viewDir); // little optimization (blinn-phong)
float spec = pow(max(dot(norm, halfwayDir), 0.0), shininess);
vec3 specular = specularStrength * spec * lightColor;

View File

@@ -146,22 +146,14 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
auto padLight = lightNodes.at(1);
auto ballLight = lightNodes.at(2);
ceilingLight->position = {
boxDimensions.x / 2,
boxDimensions.y,
boxDimensions.z / 2
};
rootNode->children.push_back(ceilingLight);
boxNode->children.push_back(ceilingLight);
ceilingLight->position = { 0, 10, 0 };
padLight->position = {
padPositionX,
50,
padPositionZ
};
padNode->children.push_back(padLight);
padLight->position = { 0, 10, 0 };
ballLight->position = ballPosition;
ballNode->children.push_back(ballLight);
ballLight->position = { 0, 0, 0 };
rootNode->children.push_back(boxNode);
rootNode->children.push_back(padNode);
@@ -370,14 +362,15 @@ void updateFrame(GLFWwindow* window) {
boxNode->position.z - (boxDimensions.z/2) + (padDimensions.z/2) + (1 - padPositionZ) * (boxDimensions.z - padDimensions.z)
};
updateNodeTransformations(rootNode, VP);
updateNodeTransformations(rootNode, VP, glm::mat4(1.0f)); // Pass identity for initial model matrix
lightCoords.clear();
for (auto* light : lightNodes) {
lightCoords.push_back(light->worldPosition);
}
}
void updateNodeTransformations(SceneNode* node, glm::mat4 transformationThusFar) {
void updateNodeTransformations(SceneNode* node, glm::mat4 transformationThusFar, glm::mat4 modelMatrixThusFar) {
glm::mat4 transformationMatrix =
glm::translate(node->position)
* glm::translate(node->referencePoint)
@@ -387,23 +380,24 @@ void updateNodeTransformations(SceneNode* node, glm::mat4 transformationThusFar)
* glm::scale(node->scale)
* glm::translate(-node->referencePoint);
node->currentTransformationMatrix = transformationThusFar * transformationMatrix;
node->modelMatrix = transformationMatrix;
node->currentTransformationMatrix = transformationThusFar * transformationMatrix; // MVP
node->modelMatrix = modelMatrixThusFar * transformationMatrix; // Accumulated world transform
node->normalMatrix = glm::transpose(glm::inverse(glm::mat3(node->modelMatrix)));
switch(node->nodeType) {
case GEOMETRY: break;
case POINT_LIGHT:
node->worldPosition = glm::vec3(node->currentTransformationMatrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f));
node->worldPosition = glm::vec3(node->modelMatrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f));
break;
case SPOT_LIGHT: break;
}
for(SceneNode* child : node->children) {
updateNodeTransformations(child, node->currentTransformationMatrix);
updateNodeTransformations(child, node->currentTransformationMatrix, node->modelMatrix);
}
}
void renderNode(SceneNode* node) {
glUniformMatrix4fv(3, 1, GL_FALSE, glm::value_ptr(node->currentTransformationMatrix));
glUniformMatrix4fv(4, 1, GL_FALSE, glm::value_ptr(node->modelMatrix));

View File

@@ -5,7 +5,7 @@
#include <utilities/window.hpp>
#include "sceneGraph.hpp"
void updateNodeTransformations(SceneNode* node, glm::mat4 transformationThusFar);
void updateNodeTransformations(SceneNode* node, glm::mat4 transformationThusFar, glm::mat4 modelMatrixThusFar);
void initGame(GLFWwindow* window, CommandLineOptions options);
void updateFrame(GLFWwindow* window);
void renderFrame(GLFWwindow* window);
void renderFrame(GLFWwindow* window);

View File

@@ -1,5 +1,6 @@
// Local headers
#include "program.hpp"
#include "glad/glad.h"
#include "utilities/window.hpp"
#include "gamelogic.h"
#include <glm/glm.hpp>

View File

@@ -48,7 +48,7 @@ struct SceneNode {
glm::mat4 modelMatrix; // m
// pre-computed normal transformation matrix, (m^-1)^T
glm::mat4 normalMatrix;
glm::mat3 normalMatrix;
// The location of the node's reference point
glm::vec3 referencePoint;