From 6c1349b526c1d1985b6f8a70cef511bdc33e3246 Mon Sep 17 00:00:00 2001 From: Elias Almqvist Date: Mon, 7 Nov 2022 14:57:45 +0100 Subject: [PATCH 1/3] Refactor --- headers/renderer.hpp | 3 +++ src/main.cpp | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/headers/renderer.hpp b/headers/renderer.hpp index ba190ad..c83ac7b 100644 --- a/headers/renderer.hpp +++ b/headers/renderer.hpp @@ -18,6 +18,8 @@ #define NEAR_PLANE 0.1f #define FAR_PLANE 100.0f +#define CAM_SPEED 0.05f + namespace Renderer { class Object { public: @@ -58,6 +60,7 @@ namespace Renderer { glm::mat4 view = glm::mat4(1.0f); private: GLFWwindow* window; + // glm::vec3 front = glm::vec3(0.0f, 0.0f, -1.0f); }; diff --git a/src/main.cpp b/src/main.cpp index 05611b7..2498556 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -177,12 +177,12 @@ int main() { // scene.camera.setRotation(glm::vec3(gVal * 5, 5.0f * gVal, gVal * 20.0f)); // scene.camera.translate(glm::vec3(0.0f, 0.0f, 0.02f + gVal/100.0f)); scene.camera.pointAt(glm::vec3(0.0f, 0.0f, 0.0f)); - scene.camera.setPosition(glm::vec3(cos(time)*10.0f, 0.0f, gVal*10.0f)); + float radius = 10.0f; + scene.camera.setPosition(glm::vec3(cos(time) * radius, 0.0f, sin(time) * radius)); // Move the objects & stuff float rotang = time; - ro.rotate(glm::vec3(0.0f, 0.0f, gVal)); - + ro.rotate(glm::vec3(gVal, 0.0f, gVal)); ro2.setPosition(glm::vec3(0.0f, gVal * 2.0f, 1.0f + gVal*5.0f)); // Render new frame From a2cc45117d3a8ea89508568c98ee3e618fad5d53 Mon Sep 17 00:00:00 2001 From: Elias Almqvist Date: Mon, 7 Nov 2022 15:02:58 +0100 Subject: [PATCH 2/3] Controller boilerplate --- headers/controller.hpp | 12 ++++++++++++ headers/renderer.hpp | 8 ++++---- 2 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 headers/controller.hpp diff --git a/headers/controller.hpp b/headers/controller.hpp new file mode 100644 index 0000000..92923fc --- /dev/null +++ b/headers/controller.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "renderer.hpp" +#include "GLFW/glfw3.h" + +class Controller { + Controller(GLFWwindow* win); + Controller(GLFWwindow* win, Renderer::Camera cam); + + protected: + Renderer::Camera cam; +}; diff --git a/headers/renderer.hpp b/headers/renderer.hpp index c83ac7b..0a4ecf3 100644 --- a/headers/renderer.hpp +++ b/headers/renderer.hpp @@ -48,11 +48,11 @@ namespace Renderer { }; class Camera : public Object { - public: - Camera(GLFWwindow* win); - Camera(GLFWwindow* win, glm::vec3 pos); - Camera(GLFWwindow* win, glm::vec3 pos, glm::vec3 angle); + Camera(GLFWwindow* win); + Camera(GLFWwindow* win, glm::vec3 pos); + Camera(GLFWwindow* win, glm::vec3 pos, glm::vec3 angle); + public: void setFOV(float deg); void pointAt(glm::vec3 target); From 1e66e3e6ac42c3870600b0719a3c0730beea4c0e Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 7 Nov 2022 21:03:36 +0100 Subject: [PATCH 3/3] Added camera controller class --- headers/controller.hpp | 21 +++- headers/renderer.hpp | 31 +++--- shaders/vertex.glsl | 2 +- src/controller.cpp | 70 +++++++++++++ src/main.cpp | 220 ++++++++++++++++++++--------------------- src/renderer.cpp | 30 +++--- 6 files changed, 227 insertions(+), 147 deletions(-) create mode 100644 src/controller.cpp diff --git a/headers/controller.hpp b/headers/controller.hpp index 92923fc..ca28aa7 100644 --- a/headers/controller.hpp +++ b/headers/controller.hpp @@ -1,12 +1,23 @@ #pragma once +#include "glm/fwd.hpp" #include "renderer.hpp" #include "GLFW/glfw3.h" -class Controller { - Controller(GLFWwindow* win); - Controller(GLFWwindow* win, Renderer::Camera cam); +#define CAM_MAX_ANGLE 89.99f - protected: - Renderer::Camera cam; +class Controller : public Renderer::Camera { + public: + using Renderer::Camera::Camera; + + float sensitivity = 0.04f; + + void processInput(float deltaTime); + + private: + float pitch, yaw; + double lastX, lastY; + bool firstMouseInput = true; + + void processMouseInput(float deltaTime); }; diff --git a/headers/renderer.hpp b/headers/renderer.hpp index 0a4ecf3..72e783a 100644 --- a/headers/renderer.hpp +++ b/headers/renderer.hpp @@ -18,8 +18,6 @@ #define NEAR_PLANE 0.1f #define FAR_PLANE 100.0f -#define CAM_SPEED 0.05f - namespace Renderer { class Object { public: @@ -35,6 +33,7 @@ namespace Renderer { void setRotation(glm::vec3 angle); void rotate(glm::vec3 dangle); + protected: void updatePositionTransform(); void updateRotationTransform(); @@ -42,25 +41,26 @@ namespace Renderer { glm::mat4 positionTransform = glm::mat4(1.0f); glm::mat4 rotationTransform = glm::mat4(1.0f); glm::mat4 modelTransform = glm::mat4(1.0f); - glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f); glm::vec3 angle = glm::vec3(0.0f, 0.0f, 0.0f); }; class Camera : public Object { - Camera(GLFWwindow* win); - Camera(GLFWwindow* win, glm::vec3 pos); - Camera(GLFWwindow* win, glm::vec3 pos, glm::vec3 angle); - public: - void setFOV(float deg); - void pointAt(glm::vec3 target); - glm::mat4 projection = glm::mat4(1.0f); glm::mat4 view = glm::mat4(1.0f); - private: + + Camera(GLFWwindow* win); + Camera(GLFWwindow* win, glm::vec3 pos); + Camera(GLFWwindow* win, glm::vec3 pos, glm::vec3 angle); + + void setFOV(float deg); + protected: GLFWwindow* window; - // glm::vec3 front = glm::vec3(0.0f, 0.0f, -1.0f); + glm::vec3 front = glm::vec3(0.0f, 0.0f, 1.0f); + glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f); + + void updateCameraTransforms(); }; @@ -80,17 +80,20 @@ namespace Renderer { class Scene { public: - Camera camera; + float deltaTime = 0.0f; Scene(GLFWwindow* win); Scene(GLFWwindow* win, std::vector ROs); - void setCamera(Camera cam); + void setCamera(Camera *cam); void spawnObject(RenderObject *ro); void render(); + protected: + Camera *camera; private: std::vector renderObjects = std::vector(); GLFWwindow* window; + float lastFrame = 0.0f; }; class TexturedObject : public RenderObject { diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 338e43d..2ec9988 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -14,7 +14,7 @@ uniform mat4 view = mat4(1.0); uniform mat4 projection = mat4(1.0); void main() { - // projection view { model } + // projection view { model } gl_Position = projection * view * (modelRotation * modelPosition * model) * vec4(aPos, 1.0); VertexColor = vec4(aColor, 1.0); TexCoord = aTexCoord; diff --git a/src/controller.cpp b/src/controller.cpp new file mode 100644 index 0000000..ff110d0 --- /dev/null +++ b/src/controller.cpp @@ -0,0 +1,70 @@ +#include "controller.hpp" +#include "GLFW/glfw3.h" +#include "glm/fwd.hpp" +#include "glm/trigonometric.hpp" +#include "renderer.hpp" +#include +#include + +void Controller::processMouseInput(float deltaTime) { + double x, y; + glfwGetCursorPos(window, &x, &y); + + if (firstMouseInput) { + lastX = x; + lastY = y; + firstMouseInput = false; + } + + float xOffset = x - lastX; + float yOffset = y - lastY; + lastX = x; + lastY = y; + + xOffset *= sensitivity; + yOffset *= sensitivity; + + yaw += xOffset; + pitch += yOffset; + + // Clamp camera values + pitch = glm::clamp(pitch, -CAM_MAX_ANGLE, CAM_MAX_ANGLE); + // yaw = glm::clamp(yaw, -CAM_MAX_ANGLE, CAM_MAX_ANGLE); + yaw = std::fmod(yaw, 360.0f); + + // Update the front vector + glm::vec3 dir; + dir.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); + dir.y = sin(glm::radians(-pitch)); + dir.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch)); + front = glm::normalize(dir); + + updateCameraTransforms(); + + printf("%f %f %f (pitch: %f yaw: %f)\n", position.x, position.y, position.z, pitch, yaw); +} + +void Controller::processInput(float deltaTime) { + processMouseInput(deltaTime); + + float cameraSpeed = deltaTime * 2.5f; + + if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { + translate(cameraSpeed * front); + } + if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { + translate(-cameraSpeed * front); + } + if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { + translate(-glm::normalize(glm::cross(front, up)) * cameraSpeed); + } + if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { + translate(glm::normalize(glm::cross(front, up)) * cameraSpeed); + } + if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { + translate(cameraSpeed * up); + } + if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) { + translate(cameraSpeed * -up); + } +} diff --git a/src/main.cpp b/src/main.cpp index 2498556..57a06a0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include #include +#include "controller.hpp" #include "renderer.hpp" #define WINDOW_WIDTH 640 @@ -14,10 +15,101 @@ #define RUSTY_METAL_TEXTURE "assets/textures/rusty_metal.jpg" +std::vector verts({ + -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, + + + -0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, + -0.5f, -0.5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1.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f +}); +// Vert struc: x y z r g b tx ty + +std::vector indices({ + 0, 1, 3, + 1, 2, 3, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, + 11, 12, 13, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, + 11, 12, 13, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, + 11, 12, 13, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, + 11, 12, 13, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, + 11, 12, 13, +}); + void framebuffer_size_callback(GLFWwindow* win, int w, int h) { glViewport(0, 0, w, h); } +void mouse_callback(GLFWwindow* win, double x, double y) {} + void processInput(GLFWwindow *win) { int action = glfwGetKey(win, GLFW_KEY_ESCAPE); if (action == GLFW_PRESS) { @@ -25,14 +117,6 @@ void processInput(GLFWwindow *win) { } } -void renderCallback() { - // Make background - glClearColor(0.1f, 0.1f, 0.1f, 1.0f); - // glClearColor(1.0f, 1.0f, 1.0f, 1.0f); - // glClearColor(0.0f, 0.0f, 0.0f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); -} - int main() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); @@ -52,99 +136,15 @@ int main() { return 1; } - std::vector verts({ - -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, - 0.5f, -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, 1.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, - - - -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, - - 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, - -0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, - -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, - - - -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, - - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, - -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f - }); - // Vert struc: x y z r g b tx ty - - std::vector indices({ - 0, 1, 3, - 1, 2, 3, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, - 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, - 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, - 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, - 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, - 11, 12, 13, - }); - glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); - glfwSetFramebufferSizeCallback(win, framebuffer_size_callback); + glfwSetFramebufferSizeCallback(win, framebuffer_size_callback); // Framebuffer + glEnable(GL_DEPTH_TEST); + // Input + glfwSetInputMode(win, GLFW_CURSOR, GLFW_CURSOR_DISABLED); // Disable cursor + glfwSetCursorPosCallback(win, mouse_callback); // Mouse capture callback + float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); @@ -163,27 +163,17 @@ int main() { scene.spawnObject(&ro); scene.spawnObject(&ro2); + // Controller test + Controller player(win, glm::vec3(0.0f, 0.0f, -8.0f)); + scene.setCamera(&player); + while (!glfwWindowShouldClose(win)) { // Handle input + player.processInput(scene.deltaTime); processInput(win); - // rendering - renderCallback(); - - float time = glfwGetTime(); - float gVal = sin(time); - - // Move the camera left and right - // scene.camera.setRotation(glm::vec3(gVal * 5, 5.0f * gVal, gVal * 20.0f)); - // scene.camera.translate(glm::vec3(0.0f, 0.0f, 0.02f + gVal/100.0f)); - scene.camera.pointAt(glm::vec3(0.0f, 0.0f, 0.0f)); - float radius = 10.0f; - scene.camera.setPosition(glm::vec3(cos(time) * radius, 0.0f, sin(time) * radius)); - - // Move the objects & stuff - float rotang = time; - ro.rotate(glm::vec3(gVal, 0.0f, gVal)); - ro2.setPosition(glm::vec3(0.0f, gVal * 2.0f, 1.0f + gVal*5.0f)); + ro.translate(glm::vec3(0.0f, 0.0f, 0.001f)); + // ro2.setPosition(glm::vec3(0.0f, 0.0f, -1000.0f)); // Render new frame scene.render(); diff --git a/src/renderer.cpp b/src/renderer.cpp index 24496da..07c92a8 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -43,8 +43,7 @@ namespace Renderer { } void Object::translate(glm::vec3 dpos) { - position += dpos; - updatePositionTransform(); + setPosition(position + dpos); } void Object::updateRotationTransform() { @@ -68,15 +67,12 @@ namespace Renderer { } void Object::rotate(glm::vec3 dangle) { - angle += dangle; - updateRotationTransform(); + setRotation(angle + dangle); } // Scene - Scene::Scene(GLFWwindow* win) : camera(win) { + Scene::Scene(GLFWwindow* win) { window = win; - - camera.setFOV(DEFAULT_FOV); } Scene::Scene(GLFWwindow* win, std::vector ROs) : Scene(win) { @@ -87,18 +83,29 @@ namespace Renderer { renderObjects.push_back(ro); } - void Scene::setCamera(Camera cam) { + void Scene::setCamera(Camera *cam) { camera = cam; } void Scene::render() { + // Record deltaTime + float curFrame = glfwGetTime(); + deltaTime = curFrame - lastFrame; + + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + for ( RenderObject *ro: renderObjects ) - ro->render(window, camera); + ro->render(window, *camera); + + // Record the last frame + lastFrame = curFrame; } // Camera Camera::Camera(GLFWwindow* win) { window = win; + setFOV(DEFAULT_FOV); } Camera::Camera(GLFWwindow* win, glm::vec3 pos) : Camera(win) { @@ -115,11 +122,10 @@ namespace Renderer { projection = glm::perspective(glm::radians(fov), (float)width / (float)height, NEAR_PLANE, FAR_PLANE); } - void Camera::pointAt(glm::vec3 target) { - view = glm::lookAt(position, target, glm::vec3(0.0f, 1.0f, 0.0f)); + void Camera::updateCameraTransforms() { + view = glm::lookAt(position, position + front, up); } - // RenderObject RenderObject::RenderObject(std::vector verts, std::vector indices) : shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE) {