From dab1bccd0ae662ed3031f869370eada25116a580 Mon Sep 17 00:00:00 2001 From: Maurice Mohlek Date: Sun, 26 Sep 2021 21:10:12 +0200 Subject: [PATCH] use ubo for camera --- engine | 2 +- include/shader/displayNormal.gs | 2 +- include/shader/displayNormal.vs | 2 +- include/shader/main.vs | 4 ++-- src/main.cpp | 12 +++--------- 5 files changed, 8 insertions(+), 14 deletions(-) diff --git a/engine b/engine index 5c8f56d..ef5a3d3 160000 --- a/engine +++ b/engine @@ -1 +1 @@ -Subproject commit 5c8f56ddffc0bd2ea088cc36212e4c58fedf4210 +Subproject commit ef5a3d353decaeb938b271a46c9314b691373248 diff --git a/include/shader/displayNormal.gs b/include/shader/displayNormal.gs index c2d1a57..b0a5e3a 100644 --- a/include/shader/displayNormal.gs +++ b/include/shader/displayNormal.gs @@ -9,7 +9,7 @@ R"( const float MAGNITUDE = 3.0; - layout(std140) uniform camera { + layout(std140, binding = 0) uniform camera { mat4 view; mat4 projection; }; diff --git a/include/shader/displayNormal.vs b/include/shader/displayNormal.vs index 4030dc3..0a51054 100644 --- a/include/shader/displayNormal.vs +++ b/include/shader/displayNormal.vs @@ -11,7 +11,7 @@ R"( } vs_out; uniform mat4 model; - layout(std140) uniform camera { + layout(std140, binding = 0) uniform camera { mat4 view; mat4 projection; }; diff --git a/include/shader/main.vs b/include/shader/main.vs index fb29fbf..0e5d7a5 100644 --- a/include/shader/main.vs +++ b/include/shader/main.vs @@ -8,7 +8,7 @@ R"( layout(location = 4) in vec3 in_bitangent; uniform mat4 model; - layout(std140) uniform camera { + layout(std140, binding = 0) uniform camera { mat4 view; mat4 projection; }; @@ -37,7 +37,7 @@ R"( mat3 TBN = transpose(mat3(T, B, N)); vs_out.TBN = TBN; - vec3 lightDir = normalize(vec3(sin(time * 0.001), 1.0, cos(time * 0.001))); + vec3 lightDir = normalize(vec3(sin(float(time) * 0.001), 1.0, cos(float(time) * 0.001))); vs_out.tangentLightDir = TBN * lightDir; vs_out.tangentViewPos = TBN * transpose(view)[3].xyz; vs_out.tangentFragPos = TBN * vs_out.fragPos; diff --git a/src/main.cpp b/src/main.cpp index d35ee3f..3951e34 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -63,8 +63,8 @@ int main(int argc, char** argv) { Engine::Camera camera; struct CameraUBO { - glm::mat4 projection; glm::mat4 view; + glm::mat4 projection; } cameraUbo; fprintf(stdout, "%d\n", sizeof(CameraUBO)); @@ -223,6 +223,7 @@ int main(int argc, char** argv) { Engine::VertexArrayObject vao; vao.setBuffer(0, vbo); vao.setBuffer(0, elements); + vao.setBuffer(0, cameraBuffer); vao.setBuffer(1, norm); vao.setBuffer(2, uvs); vao.setBuffer(3, tangent); @@ -271,10 +272,8 @@ int main(int argc, char** argv) { GLint uniformModelHouse = program.getUniformLocation("model"); GLint uniformProjHouse = program.getUniformLocation("projection"); GLint uniformTime = program.getUniformLocation("time"); - GLint uniformCamera = program.getUniformBlockIndex("camera"); glUniformMatrix4fv(uniformModelHouse, 1, GL_FALSE, glm::value_ptr(modelHouse)); - glUniformBlockBinding(program.getProgramId(), uniformCamera, 0); Engine::ShaderProgram displayNormalShader; Engine::Shader displayNormalVertex(GL_VERTEX_SHADER, Shader::normalDisplayVert); @@ -287,9 +286,7 @@ int main(int argc, char** argv) { displayNormalShader.use(); GLint uniformModelDisplayNormal = displayNormalShader.getUniformLocation("model"); - GLint uniformCameraDisplayNormal = displayNormalShader.getUniformBlockIndex("camera"); glUniformMatrix4fv(uniformModelDisplayNormal, 1, GL_FALSE, glm::value_ptr(modelHouse)); - glUniformBlockBinding(displayNormalShader.getProgramId(), uniformCameraDisplayNormal, 0); Engine::ShaderProgram programTerrain; Engine::Shader vertexShaderTerrain(GL_VERTEX_SHADER, Shader::terrainVertex); @@ -307,11 +304,8 @@ int main(int argc, char** argv) { camera.position.y += 200; GLint uniformModelTerrain = programTerrain.getUniformLocation("model"); - GLint uniformCameraTerrain = programTerrain.getUniformBlockIndex("camera"); - glBindBufferBase(GL_UNIFORM_BUFFER, 0, cameraBuffer._bufferId); glUniformMatrix4fv(uniformModelTerrain, 1, GL_FALSE, glm::value_ptr(modelTerrain)); - glUniformBlockBinding(programTerrain.getProgramId(), uniformCameraTerrain, 0); double factor = 0.14; Engine::Terrain terrain(500, 500, projection, programTerrain); @@ -335,7 +329,7 @@ int main(int argc, char** argv) { float yaw, pitch; float lastX = 400, lastY = 300; bool moveCamera = false; - ImVec4 clearColor(1.0, 1.0, 1.0, 1.0); + ImVec4 clearColor(0.0, 0.7, 1.0, 1.0); do { SDL_Event event;