From 44b027c3e2f745787395f956accbb64fcb3e6a89 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Sun, 26 Mar 2023 22:21:36 +0200 Subject: [PATCH] Blinn-Phong lighting --- shaders/shader.fs | 36 +++++++++++++++++++++++++++++++++--- shaders/shader.vs | 8 +++++--- src/chunkmanager.cpp | 1 - src/chunkmesher.cpp | 15 --------------- src/main.cpp | 2 ++ 5 files changed, 40 insertions(+), 22 deletions(-) diff --git a/shaders/shader.fs b/shaders/shader.fs index 6b13a29..fbcd60f 100644 --- a/shaders/shader.fs +++ b/shaders/shader.fs @@ -1,12 +1,42 @@ #version 330 core -in vec4 vColor; +in vec3 vColor; in vec3 vNormal; +in vec3 FragPos; out vec4 FragColor; +vec3 lightColor = vec3(1.0); +vec3 lightDir = -normalize(vec3(0.0, 100.0, 0.0) - vec3(32.0)); + +float ambientStrength = 0.5; +float diffuseStrength = 0.3; +float specularStrength = 0.2; + +uniform vec3 viewPos; +uniform float u_time; + void main() { - FragColor = vColor; - // FragColor= vec4(1.0f, 1.0f, 1.0f, 1.0f); + // offset the normal a tiny bit, so that the color of faces opposing lightDir is not completely + // flat + vec3 normal = normalize(vNormal); + + // Blinn-Phong lighting + // Ambient + vec3 ambient = lightColor*vColor; + + // Diffuse + float diff = max(dot(normal, lightDir), 0.0); + vec3 diffuse = vColor * diff; + + // Blinn Specular + vec3 viewDir = normalize(viewPos - FragPos); + vec3 halfwayDir = normalize(lightDir + viewDir); + float spec = pow(max(dot(normal, halfwayDir), 0.0), 64.0); + vec3 specular = lightColor * vColor * spec; + + // Final color + vec3 color = ambient * ambientStrength + diffuse * diffuseStrength + specular * specularStrength; + FragColor = vec4(color, 1.0); } diff --git a/shaders/shader.vs b/shaders/shader.vs index ae918f5..4ac362e 100644 --- a/shaders/shader.vs +++ b/shaders/shader.vs @@ -8,13 +8,15 @@ uniform mat4 model; uniform mat4 view; uniform mat4 projection; -out vec4 vColor; +out vec3 vColor; out vec3 vNormal; +out vec3 FragPos; void main() { - vColor = vec4(aColor, 1.0f); - vNormal = aNormal; + vColor = aColor; + vNormal = mat3(transpose(inverse(model))) * aNormal; + FragPos = vec3(model*vec4(aPos, 1.0)); gl_Position = projection * view * model * vec4(aPos, 1.0); } diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index d31d7b9..9edfde8 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -124,7 +124,6 @@ namespace chunkmanager chunks_indices[index][0] = x; chunks_indices[index][1] = y; chunks_indices[index][2] = z; - std::cout << index << "/" << chunks_volume << "\n"; index++; } } diff --git a/src/chunkmesher.cpp b/src/chunkmesher.cpp index 623fab2..c9abde2 100755 --- a/src/chunkmesher.cpp +++ b/src/chunkmesher.cpp @@ -323,21 +323,6 @@ void quad(Chunk::Chunk* chunk, glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec break; } - // Fake shadows - if ((bottomLeft.z == bottomRight.z && bottomRight.z == topLeft.z && topLeft.z == topRight.z)) - { - r += 0.1f; - g += 0.1f; - b += 0.1f; - } - - if ((bottomLeft.y == bottomRight.y && bottomRight.y == topLeft.y && topLeft.y == topRight.y)) - { - r += 0.12f; - g += 0.12f; - b += 0.12f; - } - for (int i = 0; i < 4; i++) { chunk->colors.push_back(r); diff --git a/src/main.cpp b/src/main.cpp index abe6aee..b9d6780 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -88,6 +88,8 @@ int main() // Camera theCamera.update(window, deltaTime); + theShader->setFloat("u_time", currentFrame); + theShader->setVec3("viewPos", theCamera.getPos()); // Reset blockping timeout if 200ms have passed if(glfwGetTime() - lastBlockPick > 0.1) blockpick = false;