From 11215105d3d2b3f189116c580d13f12eab87fc82 Mon Sep 17 00:00:00 2001 From: Fredrik Robertsen Date: Tue, 27 Jan 2026 09:23:45 +0100 Subject: [PATCH] all shader inputs set, time to implement phong frag --- res/shaders/simple.frag | 14 +++++++++++--- res/shaders/simple.vert | 6 +++++- src/gamelogic.cpp | 30 +++++++++++++++++++++++++----- src/sceneGraph.hpp | 11 ++++++++++- 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/res/shaders/simple.frag b/res/shaders/simple.frag index ada7191..421d5c3 100644 --- a/res/shaders/simple.frag +++ b/res/shaders/simple.frag @@ -2,13 +2,21 @@ in layout(location = 0) vec3 normal; in layout(location = 1) vec2 textureCoordinates; +in layout(location = 2) vec3 worldPositions; + +uniform vec3 lightPositions[3]; +uniform vec3 cameraPosition; out vec4 color; -float rand(vec2 co) { return fract(sin(dot(co.xy, vec2(12.9898,78.233))) * 43758.5453); } -float dither(vec2 uv) { return (rand(uv)*2.0-1.0) / 256.0; } +float rand(vec2 co) { + return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453); +} +float dither(vec2 uv) { + return (rand(uv) * 2.0 - 1.0) / 256.0; +} void main() { color = vec4(0.5 * normal + 0.5, 1.0); -} \ No newline at end of file +} diff --git a/res/shaders/simple.vert b/res/shaders/simple.vert index b38c72d..091c861 100644 --- a/res/shaders/simple.vert +++ b/res/shaders/simple.vert @@ -5,13 +5,17 @@ in layout(location = 1) vec3 normal_in; in layout(location = 2) vec2 textureCoordinates_in; uniform layout(location = 3) mat4 MVP; +uniform layout(location = 4) mat4 modelMatrix; +uniform layout(location = 5) mat3 normalMatrix; out layout(location = 0) vec3 normal_out; out layout(location = 1) vec2 textureCoordinates_out; +out layout(location = 2) vec3 worldSpaceVertices; void main() { - normal_out = normal_in; + worldSpaceVertices = vec3(modelMatrix * vec4(position, 1.0)); + normal_out = normalize(normalMatrix * normal_in); textureCoordinates_out = textureCoordinates_in; gl_Position = MVP * vec4(position, 1.0f); } diff --git a/src/gamelogic.cpp b/src/gamelogic.cpp index 4edef5f..f6f0b98 100644 --- a/src/gamelogic.cpp +++ b/src/gamelogic.cpp @@ -13,7 +13,10 @@ #include #include #include +#include #include "gamelogic.h" +#include "glm/ext/vector_float3.hpp" +#include "glm/matrix.hpp" #include "sceneGraph.hpp" #define GLM_ENABLE_EXPERIMENTAL #include @@ -37,7 +40,11 @@ SceneNode* rootNode; SceneNode* boxNode; SceneNode* ballNode; SceneNode* padNode; + std::vector lightNodes; +std::vector lightCoords; + +glm::vec3 cameraPosition; double ballRadius = 3.0f; @@ -339,7 +346,7 @@ void updateFrame(GLFWwindow* window) { glm::mat4 projection = glm::perspective(glm::radians(80.0f), float(windowWidth) / float(windowHeight), 0.1f, 350.f); - glm::vec3 cameraPosition = glm::vec3(0, 2, -20); + cameraPosition = glm::vec3(0, 2, -20); // Some math to make the camera move in a nice way float lookRotation = -0.6 / (1 + exp(-5 * (padPositionX-0.5))) + 0.3; @@ -365,9 +372,9 @@ void updateFrame(GLFWwindow* window) { updateNodeTransformations(rootNode, VP); - - - + for (auto* light : lightNodes) { + lightCoords.push_back(light->worldPosition); + } } void updateNodeTransformations(SceneNode* node, glm::mat4 transformationThusFar) { @@ -381,10 +388,14 @@ void updateNodeTransformations(SceneNode* node, glm::mat4 transformationThusFar) * glm::translate(-node->referencePoint); node->currentTransformationMatrix = transformationThusFar * transformationMatrix; + node->modelMatrix = transformationMatrix; + node->normalMatrix = glm::transpose(glm::inverse(glm::mat3(node->modelMatrix))); switch(node->nodeType) { case GEOMETRY: break; - case POINT_LIGHT: break; + case POINT_LIGHT: + node->worldPosition = glm::vec3(node->currentTransformationMatrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)); + break; case SPOT_LIGHT: break; } @@ -395,6 +406,15 @@ void updateNodeTransformations(SceneNode* node, glm::mat4 transformationThusFar) void renderNode(SceneNode* node) { glUniformMatrix4fv(3, 1, GL_FALSE, glm::value_ptr(node->currentTransformationMatrix)); + glUniformMatrix4fv(4, 1, GL_FALSE, glm::value_ptr(node->modelMatrix)); + glUniformMatrix4fv(5, 1, GL_FALSE, glm::value_ptr(node->normalMatrix)); + + GLint lightPosLoc = shader->getUniformFromName("lightPositions"); + glUniform3fv(lightPosLoc, 3, glm::value_ptr(lightCoords[0])); + + GLint cameraPosLoc = shader->getUniformFromName("cameraPosition"); + glUniform3fv(cameraPosLoc, 1, glm::value_ptr(cameraPosition)); + switch(node->nodeType) { case GEOMETRY: diff --git a/src/sceneGraph.hpp b/src/sceneGraph.hpp index 762f0ad..0138430 100644 --- a/src/sceneGraph.hpp +++ b/src/sceneGraph.hpp @@ -42,7 +42,13 @@ struct SceneNode { glm::vec3 scale; // A transformation matrix representing the transformation of the node's location relative to its parent. This matrix is updated every frame. - glm::mat4 currentTransformationMatrix; + glm::mat4 currentTransformationMatrix; // mvp + + // the model transformation matrix into world space + glm::mat4 modelMatrix; // m + + // pre-computed normal transformation matrix, (m^-1)^T + glm::mat4 normalMatrix; // The location of the node's reference point glm::vec3 referencePoint; @@ -56,6 +62,9 @@ struct SceneNode { // indexes into global array of light nodes unsigned int lightIdx; + + // used by light nodes + glm::vec3 worldPosition; }; SceneNode* createSceneNode();