From f6803ced78871f285f135c1f44795b146f0193e8 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Sat, 29 Oct 2022 13:38:48 +0200 Subject: [PATCH] Fixed camera bug --- headers/renderer.hpp | 2 -- shaders/vertex.glsl | 13 ++++++++----- src/main.cpp | 11 +++++++---- src/renderer.cpp | 9 +++++---- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/headers/renderer.hpp b/headers/renderer.hpp index ddd3c37..bbbb6c5 100644 --- a/headers/renderer.hpp +++ b/headers/renderer.hpp @@ -52,8 +52,6 @@ namespace Renderer { Camera(GLFWwindow* win, glm::vec3 pos, glm::vec3 angle); void setFOV(float deg); - - glm::mat4 view = glm::mat4(1.0f); glm::mat4 projection = glm::mat4(1.0f); private: GLFWwindow* window; diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 12947e9..e69fbc3 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -6,14 +6,17 @@ layout (location = 2) in vec2 aTexCoord; out vec4 VertexColor; out vec2 TexCoord; -uniform mat4 rotation = mat4(1.0); -uniform mat4 position = mat4(1.0); +uniform mat4 modelRotation = mat4(1.0); +uniform mat4 modelPosition = mat4(1.0); uniform mat4 model = mat4(1.0); -uniform mat4 view = mat4(1.0); -uniform mat4 projection = mat4(1.0); + +uniform mat4 camPos = mat4(1.0); +uniform mat4 camRot = mat4(1.0); +uniform mat4 camProjection = mat4(1.0); void main() { - gl_Position = projection * view * position * rotation * model * vec4(aPos, 1.0); + // projection { view } { model } + gl_Position = camProjection * (camPos * camRot) * modelPosition * modelRotation * model * vec4(aPos, 1.0); VertexColor = vec4(aColor, 1.0); TexCoord = aTexCoord; } diff --git a/src/main.cpp b/src/main.cpp index 80c0f79..35eeeb8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -43,8 +43,6 @@ int main() { return 1; } - Renderer::Scene scene(win); - glfwMakeContextCurrent(win); if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) { @@ -148,6 +146,9 @@ int main() { float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); + // Create scene + Renderer::Scene scene(win); + Renderer::TexturedObject ro(verts, indices); ro.setPosition(glm::vec3(0.2f, -1.0f, -4.0f)); @@ -160,6 +161,7 @@ int main() { scene.spawnObject(ro); scene.spawnObject(ro2); scene.camera.setPosition(glm::vec3(0.0f, 0.0f, -8.0f)); + scene.camera.setFOV(60.0f); // Window width & height while (!glfwWindowShouldClose(win)) { @@ -170,10 +172,11 @@ int main() { renderCallback(); float time = glfwGetTime(); - float gVal = sin(time) / 10.5f; + float gVal = sin(time); // Move the camera left and right - scene.camera.setPosition(glm::vec3(gVal/10.0f, 0.0f, 0.0f)); + scene.camera.setPosition(glm::vec3(gVal, 0.0f, 0.0f)); + scene.camera.rotate(glm::vec3(0.0f, 0.0f, 1.0f)); // Move the objects & stuff float rotang = time; diff --git a/src/renderer.cpp b/src/renderer.cpp index accee47..7eb3564 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -154,11 +154,12 @@ namespace Renderer { // TODO: Make prerender instead of render void RenderObject::render(GLFWwindow* win, Camera cam) { - shader.setMat4("position", getPositionTransform()); - shader.setMat4("rotation", getRotationTransform()); + shader.setMat4("modelPosition", getPositionTransform()); + shader.setMat4("modelRotation", getRotationTransform()); - shader.setMat4("view", cam.view); - shader.setMat4("projection", cam.projection); + shader.setMat4("camPos", cam.getPositionTransform()); + shader.setMat4("camRot", cam.getRotationTransform()); + shader.setMat4("camProjection", cam.projection); shader.use();