From f2ed6449b862ac20409a97b9122f5066d1f69a97 Mon Sep 17 00:00:00 2001 From: Maurice Mohlek Date: Wed, 15 Sep 2021 23:13:57 +0200 Subject: [PATCH] feat: added lighting --- engine | 2 +- include/shader.h | 7 +++---- src/main.cpp | 51 ++++++++++++++++++++++++++++++------------------ 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/engine b/engine index 48cc4c4..c267d6c 160000 --- a/engine +++ b/engine @@ -1 +1 @@ -Subproject commit 48cc4c43249da371c66f09535cbd5b022249f9f5 +Subproject commit c267d6c47402fbfa172c06bce4f924b169637520 diff --git a/include/shader.h b/include/shader.h index f1740fc..5b03eef 100644 --- a/include/shader.h +++ b/include/shader.h @@ -74,8 +74,7 @@ namespace Shader { vec3 color = texture(diffuseMap, fs_in.uv).rgb; vec3 ambient = 0.1 * color; vec3 lightDir = fs_in.tangentLightDir; - float diff = dot(lightDir, normal); - diff = sign(diff) * diff; + float diff = max(dot(lightDir, normal), 0.0); vec3 diffuse = diff * color; vec3 viewDir = normalize(fs_in.tangentViewPos - fs_in.tangentFragPos); vec3 reflectDir = reflect(-lightDir, normal); @@ -83,7 +82,7 @@ namespace Shader { float spec = pow(max(dot(normal, halfwayDir), 0.0), 32.0); vec3 specular = vec3(0.2) * spec; - fragColor = vec4(isnan(lightDir.x), isnan(lightDir.y), isnan(lightDir.z), 1.0); // + specular, 1.0); + fragColor = vec4(ambient + diffuse + specular, 1.0); // + specular, 1.0); } )"; @@ -186,7 +185,7 @@ namespace Shader { out vec4 fragColor; void main() { - fragColor = texture(groundTexture, texCoord*10); + fragColor = texture(groundTexture, texCoord*500); } )"; } diff --git a/src/main.cpp b/src/main.cpp index 80a68e1..025c21f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,7 +41,7 @@ struct Clock }; int main(int argc, char** argv) { -feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); + // feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); Engine::Window* win = new Engine::Window("Medieval something", 800, 600); if (!win->create()) { @@ -143,7 +143,7 @@ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); i + 0 - (i % 3), // i1 i + 1 - (i % 3), // i2 i + 2 - (i % 3) // i3 - } + }; /* example: @@ -174,9 +174,9 @@ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); glm::vec2 deltaUV1 = uv2 - uv1; glm::vec2 deltaUV2 = uv3 - uv1; - constexpr auto epsilon = std::numeric_limits::epsilon(); - float fd = (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y) - float f = 1.0f / (fd + std::copysign(epsilon, fd); // add epsilon .. fixed division by zero problem.. + constexpr auto epsilon = std::numeric_limits::epsilon(); + float fd = (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y); + float f = 1.0f / (fd + std::copysign(epsilon, fd)); // add epsilon .. fixed division by zero problem.. glm::vec3 t; t.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x); @@ -242,8 +242,8 @@ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)win->getWidth() / (float)win->getHeight(), 0.1f, 10000.0f); glm::mat4 modelHouse = glm::mat4(1.0f); - modelHouse = glm::scale(modelHouse, glm::vec3(0.5)); - modelHouse = glm::translate(modelHouse, glm::vec3(5000, 800, 8065)); + modelHouse = glm::scale(modelHouse, glm::vec3(0.01)); + modelHouse = glm::translate(modelHouse, glm::vec3(50000, 23450, 80065)); GLint uniformModelHouse = program.getUniformLocation("model"); GLint uniformViewHouse = program.getUniformLocation("view"); @@ -303,6 +303,11 @@ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + //SDL_ShowCursor(false); + SDL_SetRelativeMouseMode(SDL_TRUE); + float yaw, pitch; + float lastX = 400, lastY = 300; + glClearColor(1.0, 1.0, 1.0, 1.0); do { SDL_Event event; @@ -323,11 +328,25 @@ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); } int mouseX, mouseY; - SDL_GetMouseState(&mouseX, &mouseY); + SDL_GetRelativeMouseState(&mouseX, &mouseY); + + const float sensitivity = 0.05f; + mouseX *= sensitivity; + mouseY *= sensitivity; + + yaw += mouseX; + pitch += -mouseY; - // fprintf(stdout, "%d %d\n", mouseX, mouseY); + if(pitch > 89.0f) + pitch = 89.0f; + if(pitch < -89.0f) + pitch = -89.0f; - float cameraSpeed = (float)(clock.delta / 1.0f); + camera.direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); + camera.direction.y = sin(glm::radians(pitch)); + camera.direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch)); + + float cameraSpeed = (float)(clock.delta / 10.0f); if (KEYS[SDLK_w]) { camera.position += camera.direction * cameraSpeed; } @@ -356,6 +375,9 @@ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); terrain.setFactor(factor); fprintf(stdout, "factor %f\n", factor); } + if (KEYS[SDLK_q]) { + win->close(); + } clock.tick(); @@ -367,7 +389,6 @@ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); glUniformMatrix4fv(uniformViewTerrain, 1, GL_FALSE, glm::value_ptr(viewTerrain)); terrain.render(); - program.use(); vao.bind(); textureDiffHouse.bind(); @@ -375,14 +396,6 @@ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); glUniform1ui(uniformTime, SDL_GetTicks()); glUniformMatrix4fv(uniformViewHouse, 1, GL_FALSE, glm::value_ptr(viewTerrain)); glDrawElements(GL_TRIANGLES, faceIndex.size(), GL_UNSIGNED_INT, 0); - - displayNormalShader.use(); - vao.bind(); - textureDiffHouse.bind(); - textureNormalHouse.bind(); - glUniformMatrix4fv(uniformViewDisplayNormal, 1, GL_FALSE, glm::value_ptr(viewTerrain)); - glDrawElements(GL_TRIANGLES, faceIndex.size(), GL_UNSIGNED_INT, 0); - } while (win->loop()); return 0;