all shader inputs set, time to implement phong frag

This commit is contained in:
2026-01-27 09:23:45 +01:00
parent 071ce35d11
commit 11215105d3
4 changed files with 51 additions and 10 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -13,7 +13,10 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <fmt/format.h>
#include <vector>
#include "gamelogic.h"
#include "glm/ext/vector_float3.hpp"
#include "glm/matrix.hpp"
#include "sceneGraph.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/transform.hpp>
@@ -37,7 +40,11 @@ SceneNode* rootNode;
SceneNode* boxNode;
SceneNode* ballNode;
SceneNode* padNode;
std::vector<SceneNode*> lightNodes;
std::vector<glm::vec3> 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:

View File

@@ -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();