From 25f9beb3d43f8b785ba27995a6e3b267a0a9952d Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 28 Oct 2022 21:15:44 +0200 Subject: [PATCH] Refactored camera & such (WIP) --- headers/renderer.hpp | 56 ++++++++++++++++++--- shaders/vertex.glsl | 5 +- src/main.cpp | 21 ++++---- src/renderer.cpp | 117 ++++++++++++++++++++++++++++++++++++------- 4 files changed, 159 insertions(+), 40 deletions(-) diff --git a/headers/renderer.hpp b/headers/renderer.hpp index 7de6d36..ddd3c37 100644 --- a/headers/renderer.hpp +++ b/headers/renderer.hpp @@ -19,15 +19,56 @@ #define FAR_PLANE 100.0f namespace Renderer { - class RenderObject { + class Object { public: + Object(); + Object(glm::vec3 pos); + Object(glm::vec3 pos, glm::vec3 angle); + + glm::mat4 getPositionTransform(); + void setPosition(glm::vec3 pos); + void translate(glm::vec3 dpos); + + glm::mat4 getRotationTransform(); + void setRotation(glm::vec3 angle); + void rotate(glm::vec3 dangle); + + void transform(glm::mat4 T); + private: + void updatePositionTransform(); + void updateRotationTransform(); + glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f); + glm::vec3 angle = glm::vec3(0.0f, 0.0f, 0.0f); + glm::mat4 positionTransform = glm::mat4(1.0f); + glm::mat4 rotationTransform = glm::mat4(1.0f); + glm::mat4 modelTransform = glm::mat4(1.0f); + }; + + class Camera : public Object { + public: + Camera(GLFWwindow* win); + Camera(GLFWwindow* win, glm::vec3 pos); + 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; + }; + + + class RenderObject : public Object { + public: RenderObject(std::vector verts, std::vector indices); - void render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform); - void setRotation(float angle, glm::vec3 axis); - void transform(glm::mat4 T); + void render(GLFWwindow* win, Camera cam); void preRenderHook(); + void transform(glm::mat4 T); + + void setPosition(glm::vec3 pos); + void setRotation(glm::vec3 angle); private: Shaders::Shader shader; unsigned int EBO; @@ -39,18 +80,17 @@ namespace Renderer { class Scene { public: + Camera camera; + Scene(GLFWwindow* win); Scene(GLFWwindow* win, std::vector ROs); - void setCamera(glm::vec3 pos); - void setFOV(float deg); + void setCamera(Camera cam); void spawnObject(RenderObject ro); void render(); private: std::vector renderObjects = std::vector(); GLFWwindow* window; - glm::mat4 cameraTransform = glm::mat4(1.0f); - glm::mat4 projectionTransform = glm::mat4(1.0f); }; class TexturedObject : public RenderObject { diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 203cf32..12947e9 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -6,13 +6,14 @@ layout (location = 2) in vec2 aTexCoord; out vec4 VertexColor; out vec2 TexCoord; -uniform mat4 baseModel = mat4(1.0); +uniform mat4 rotation = mat4(1.0); +uniform mat4 position = mat4(1.0); uniform mat4 model = mat4(1.0); uniform mat4 view = mat4(1.0); uniform mat4 projection = mat4(1.0); void main() { - gl_Position = projection * view * model * baseModel * vec4(aPos, 1.0); + gl_Position = projection * view * position * rotation * model * vec4(aPos, 1.0); VertexColor = vec4(aColor, 1.0); TexCoord = aTexCoord; } diff --git a/src/main.cpp b/src/main.cpp index f22d279..80c0f79 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -149,17 +149,17 @@ int main() { glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); Renderer::TexturedObject ro(verts, indices); - ro.position = glm::vec3(0.2f, -1.0f, -4.0f); + ro.setPosition(glm::vec3(0.2f, -1.0f, -4.0f)); Renderer::TexturedObject ro2(verts, indices); - ro2.position = glm::vec3(0.5f, 0.0, -2.0f); + ro2.setPosition(glm::vec3(0.5f, 0.0, -2.0f)); - ro2.setTexture("assets/textures/meep.jpg"); ro.setTexture(RUSTY_METAL_TEXTURE); + ro2.setTexture("assets/textures/meep.jpg"); scene.spawnObject(ro); scene.spawnObject(ro2); - scene.setCamera(glm::vec3(0.0f, 0.0f, -8.0f)); + scene.camera.setPosition(glm::vec3(0.0f, 0.0f, -8.0f)); // Window width & height while (!glfwWindowShouldClose(win)) { @@ -169,18 +169,17 @@ int main() { // rendering renderCallback(); - /* OBJECT RENDERING */ float time = glfwGetTime(); float gVal = sin(time) / 10.5f; - scene.setCamera(glm::vec3(gVal/10.0f, 0.0f, 0.0f)); - // Transformation - float rotang = time; + // Move the camera left and right + scene.camera.setPosition(glm::vec3(gVal/10.0f, 0.0f, 0.0f)); - glm::mat4 T = glm::mat4(1.0f); - T = glm::rotate(T, rotang, glm::vec3(1.0, 0.0, 1.0)); - ro.transform(T); + // Move the objects & stuff + float rotang = time; + ro.rotate(glm::vec3(time, 0.0f, time)); + // Render new frame scene.render(); // glfw diff --git a/src/renderer.cpp b/src/renderer.cpp index 93aa933..accee47 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -9,12 +9,69 @@ #include "shaders.hpp" namespace Renderer { + // Object + Object::Object() {} + + Object::Object(glm::vec3 pos) : Object() { + setPosition(pos); + } + + Object::Object(glm::vec3 pos, glm::vec3 angle) : Object(pos) { + setRotation(angle); + } + + void Object::transform(glm::mat4 T) { + modelTransform = T; + } + + glm::mat4 Object::getPositionTransform() { return positionTransform; } + + void Object::updatePositionTransform() { + glm::mat4 T = glm::mat4(1.0f); + positionTransform = glm::translate(T, position); + } + + void Object::setPosition(glm::vec3 pos) { + position = pos; + updatePositionTransform(); + } + + void Object::translate(glm::vec3 dpos) { + position += dpos; + updatePositionTransform(); + } + + glm::mat4 Object::getRotationTransform() { return rotationTransform; } + + void Object::updateRotationTransform() { + float x_Ang, y_Ang, z_Ang; + x_Ang = angle[0]; + y_Ang = angle[1]; + z_Ang = angle[2]; + + glm::mat4 T = glm::mat4(1.0f); + T = glm::rotate(T, glm::radians(x_Ang), glm::vec3(1.0f, 0.0f, 0.0f)); + T = glm::rotate(T, glm::radians(y_Ang), glm::vec3(0.0f, 1.0f, 0.0f)); + T = glm::rotate(T, glm::radians(z_Ang), glm::vec3(0.0f, 0.0f, 1.0f)); + + rotationTransform = T; + } + + void Object::setRotation(glm::vec3 ang) { + angle = ang; + updateRotationTransform(); + } + + void Object::rotate(glm::vec3 dangle) { + angle += dangle; + updateRotationTransform(); + } // Scene - Scene::Scene(GLFWwindow* win) { + Scene::Scene(GLFWwindow* win) : camera(win) { window = win; - setFOV(DEFAULT_FOV); + camera.setFOV(DEFAULT_FOV); } Scene::Scene(GLFWwindow* win, std::vector ROs) : Scene(win) { @@ -25,19 +82,32 @@ namespace Renderer { renderObjects.push_back(ro); } - void Scene::setFOV(float fov) { - int width, height; - glfwGetWindowSize(window, &width, &height); - projectionTransform = glm::perspective(glm::radians(fov), (float)width / (float)height, NEAR_PLANE, FAR_PLANE); - } - - void Scene::setCamera(glm::vec3 pos) { - cameraTransform = glm::translate(cameraTransform, pos); + void Scene::setCamera(Camera cam) { + camera = cam; } void Scene::render() { for ( RenderObject ro: renderObjects ) - ro.render(window, cameraTransform, projectionTransform); + ro.render(window, camera); + } + + // Camera + Camera::Camera(GLFWwindow* win) { + window = win; + } + + Camera::Camera(GLFWwindow* win, glm::vec3 pos) : Camera(win) { + setPosition(pos); + } + + Camera::Camera(GLFWwindow* win, glm::vec3 pos, glm::vec3 angle) : Camera(win, pos) { + setRotation(angle); + } + + void Camera::setFOV(float fov) { + int width, height; + glfwGetWindowSize(window, &width, &height); + projection = glm::perspective(glm::radians(fov), (float)width / (float)height, NEAR_PLANE, FAR_PLANE); } // RenderObject @@ -82,23 +152,32 @@ namespace Renderer { void RenderObject::preRenderHook() {} - void RenderObject::render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform) { - glm::mat4 posT = glm::mat4(1.0f); - posT = glm::translate(posT, position); + // TODO: Make prerender instead of render + void RenderObject::render(GLFWwindow* win, Camera cam) { + shader.setMat4("position", getPositionTransform()); + shader.setMat4("rotation", getRotationTransform()); - shader.setMat4("baseModel", posT); - shader.setMat4("view", cameraTransform); - shader.setMat4("projection", projectionTransform); + shader.setMat4("view", cam.view); + shader.setMat4("projection", cam.projection); shader.use(); - preRenderHook(); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glDrawArrays(GL_TRIANGLES, 0, indicesVec.size()); } + void RenderObject::setPosition(glm::vec3 pos) { + Object::setPosition(pos); + shader.setMat4("position", getPositionTransform()); + } + + void RenderObject::setRotation(glm::vec3 angle) { + Object::setRotation(angle); + shader.setMat4("rotation", getRotationTransform()); + } + void RenderObject::transform(glm::mat4 T) { + Object::transform(T); shader.setMat4("model", T); }