From dfce02cd1cbb5b780679a34494ae186f85ad201e Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 5 Sep 2020 15:39:53 +0200 Subject: [PATCH] Cleaned up code --- src/CMakeLists.txt | 2 +- src/main.cpp | 155 ++++++++++---------------------- src/objects/Camera.cpp | 41 +++++++++ src/objects/Camera.hpp | 27 ++++++ src/objects/Shader.cpp | 6 +- src/objects/Shader.hpp | 6 +- src/objects/primitives/Cube.cpp | 148 ++++++++++++++++++------------ src/objects/primitives/Cube.hpp | 18 +++- 8 files changed, 232 insertions(+), 171 deletions(-) create mode 100644 src/objects/Camera.cpp create mode 100644 src/objects/Camera.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1f5d238..0537431 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,7 +3,7 @@ set(VENDOR ${CMAKE_SOURCE_DIR}/vendor) add_executable(example "main.cpp" - "objects/Texture.hpp" "objects/Texture.cpp" "objects/primitives/Cube.hpp") + ) file(GLOB SOURCE_FILES "${VENDOR}/src/*.c" diff --git a/src/main.cpp b/src/main.cpp index ee72598..5c02a97 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -11,10 +12,14 @@ #include #include +#include "Camera.hpp" #include "Shader.hpp" #include "Texture.hpp" #include "Cube.hpp" +int width = 800; +int height = 800; + void LogGlfwError(const char* message) { const char* error; @@ -46,15 +51,19 @@ void PrintVersionInfo() void FramebufferSizeCallback(GLFWwindow* window, int w, int h) { + width = w; + height = h; glViewport(0, 0, w, h); } int main(int argc, char** argv) { + // Initialize ImGui IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); + // Initialize GLFW int success; success = glfwInit(); if (!success) @@ -77,129 +86,78 @@ int main(int argc, char** argv) glfwMakeContextCurrent(window); - ImGui_ImplGlfw_InitForOpenGL(window, true); - + // Load OpenGL Bindings if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cerr << "Failed to initialize GLAD" << std::endl; return -1; } + // Finally initialize ImGui + ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplOpenGL3_Init((char*)glGetString(GL_NUM_SHADING_LANGUAGE_VERSIONS)); - ImGui::StyleColorsDark(); PrintVersionInfo(); + // Set Viewport and callbacks glViewport(0, 0, 800, 800); glfwSetFramebufferSizeCallback(window, FramebufferSizeCallback); - /* - Shader shader("basic.vert", "basic.frag"); - Texture texture1("lauch.jpg"); - Texture texture2("tomato.jpg"); - - shader.Use(); - shader.SetUniformInt("texture1", 0); - shader.SetUniformInt("texture2", 1); - - glm::mat4 model = glm::mat4(1.0f); - model = glm::translate(model, glm::vec3(0.f, -0.7f, 0.f)); - model = glm::rotate(model, glm::radians(-55.f), glm::vec3(1.0f, 0.0f, 0.0f)); - - glm::mat4 view = glm::mat4(1.0f); - view = glm::translate(view, glm::vec3(0.f, 0.f, -3.f)); - - glm::mat4 projection = glm::mat4(1.0f); - projection = glm::perspective(glm::radians(45.f), 1.f, 0.1f, 100.0f); - - shader.SetUniformMat4("model", glm::value_ptr(model)); - shader.SetUniformMat4("view", glm::value_ptr(view)); - shader.SetUniformMat4("projection", glm::value_ptr(projection)); - - float vertices[4 * (3 + 3 + 2)] = { - // Position //Color // Texture - -0.5f, 0.5f, 1.0f, 0.3f, 1.0f, 0.6f, 0.0f, 1.0f, - 0.5f, 0.5f, 1.0f, 0.8f, 0.1f, 0.5f, 1.0f, 1.0f, - 0.5f, -0.5f, 1.0f, 0.5f, 0.6f, 0.2f, 1.0f, 0.0f, - -0.5f, -0.5f, 1.0f, 0.2f, 0.7f, 0.8f, 0.0f, 0.0f, - }; - - unsigned int indices[2 * 3] = { - 0, 1, 3, - 1, 2, 3 - }; - - unsigned int VAO, VBO, EBO; - glGenVertexArrays(1, &VAO); - glGenBuffers(1, &VBO); - glGenBuffers(1, &EBO); - - glBindVertexArray(VAO); - - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, (3 + 3 + 2) * sizeof(float), (void*)0); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, (3 + 3 + 2) * sizeof(float), (void*)(3 * sizeof(float))); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, (3 + 3 + 2) * sizeof(float), (void*)(6 * sizeof(float))); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - - */ - Shader cubeShader("cube.vert", "cube.frag"); - Texture cubeTexture("crate.jpg"); cubeShader.Use(); - glm::mat4 model = glm::mat4(1.0f); - - glm::mat4 view = glm::mat4(1.0f); - glm::mat4 perspective = glm::mat4(1.0f); - cubeShader.SetUniformMat4("model", glm::value_ptr(model)); - cubeShader.SetUniformMat4("view", glm::value_ptr(view)); - + Texture cubeTexture("crate.jpg"); cubeShader.SetUniformInt("lauch", 0); - Cube cube(0.0f, 0.0f, 0.0f, 0.5f); - glm::vec3 cubePositions[] = { glm::vec3(0.0f, 0.0f, 0.0f), - glm::vec3(2.0f, 5.0f, -15.0f), + glm::vec3(2.0f, 5.0f, -7.0f), glm::vec3(-1.5f, -2.2f, -2.5f), - glm::vec3(-3.8f, -2.0f, -12.3f), - glm::vec3(2.4f, -0.4f, -3.5f), + glm::vec3(-3.8f, -2.0f, 6.3f), + glm::vec3(2.4f, -0.4f, 3.5f), glm::vec3(-1.7f, 3.0f, -7.5f), - glm::vec3(1.3f, -2.0f, -2.5f), + glm::vec3(1.3f, -2.0f, 2.5f), glm::vec3(1.5f, 2.0f, -2.5f), glm::vec3(1.5f, 0.2f, -1.5f), - glm::vec3(-1.3f, 1.0f, -1.5f) + glm::vec3(-1.3f, 1.0f, 1.5f) }; + std::vector cubes; + for (int i = 0; i < 10; i++) + { + Cube* newCube = new Cube(cubePositions[i], glm::vec3(0.5f)); + newCube->Rotate(glm::radians(20.0f * i), glm::vec3(1.0f, 0.3f, 0.5f)); + + cubes.push_back(newCube); + } + + // A camera + Camera cam(glm::vec3(0.0f, 0.0f, 10.0f), glm::vec3(0.0f, 0.0f, 0.0f)); + + // So that stuff isnt clipping through each other glEnable(GL_DEPTH_TEST); + + + while (!glfwWindowShouldClose(window)) { glfwPollEvents(); float t = glfwGetTime(); + + static float PI = glm::pi(); + static float fov = glm::radians(45.0f); static float zNear = 0.1f; static float zFar = 100.0f; - static float camX = 0.0f; - static float camY = 0.0f; - static float camZ = -3.0f; - static float yaw = 0.0f; - static float pitch = 0.0f; - static float roll = 0.0f; - //model = glm::rotate(model, glm::radians(1.0f), glm::vec3(1.0f, 1.0f, 1.0f)); + static float orbitSpeed = 1.0f; + static float orbitRadius = 10.0f; + glClearColor(0.1f, 0.4f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -217,40 +175,25 @@ int main(int argc, char** argv) ImGui::SliderFloat("zFar", &zFar, 10.0f, 100.0f); } - if (ImGui::CollapsingHeader("View")) + if (ImGui::CollapsingHeader("Orbit")) { - ImGui::SliderFloat("CamX", &camX, -2.0f, 2.0f); - ImGui::SliderFloat("CamY", &camY, -2.0f, 2.0f); - ImGui::SliderFloat("CamZ", &camZ, -10.0f, 0.0f); - ImGui::Separator(); - ImGui::SliderAngle("Yaw", &yaw, -45.0f, 45.0f, "%.0f°"); - ImGui::SliderAngle("Pitch", &pitch, -45.0f, 45.0f, "%.0f°"); - ImGui::SliderAngle("Roll", &roll, -45.0f, 45.0f, "%.0f°"); + ImGui::SliderFloat("Speed", &orbitSpeed, 0.0f, 4 * PI, "%.2f rad/s"); + ImGui::SliderFloat("Radius", &orbitRadius, 1.0f, 50.0f, "%.1f u"); } ImGui::End(); - perspective = glm::perspective(fov, 1.0f, zNear, zFar); - view = glm::mat4(1.0f); - view = glm::rotate(view, yaw, glm::vec3(0.f, 1.f, 0.f)); - view = glm::rotate(view, pitch, glm::vec3(1.f, 0.f, 0.f)); - view = glm::rotate(view, roll, glm::vec3(0.f, 0.f, 1.f)); - view = glm::translate(view, glm::vec3(camX, camY, camZ)); + perspective = glm::perspective(fov, (float)width/(float)height, zNear, zFar); cubeTexture.Bind(); cubeShader.Use(); cubeShader.SetUniformMat4("projection", &perspective[0][0]); - cubeShader.SetUniformMat4("view", &view[0][0]); - for (int i = 0; i < 10; i++) - { - model = glm::mat4(1.0f); - model = glm::translate(model, cubePositions[i]); - model = glm::rotate(model, glm::radians(20.0f * i), glm::vec3(1.0f, 0.3f, 0.5f)); + cam.SetPosition(orbitRadius * glm::vec3(cos(orbitSpeed * t), 0.0f, sin(orbitSpeed * t))); + cam.Use(cubeShader); - cubeShader.SetUniformMat4("model", &model[0][0]); - cube.Draw(cubeShader); - } + for (Cube* cube : cubes) + cube->Draw(cubeShader); ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); diff --git a/src/objects/Camera.cpp b/src/objects/Camera.cpp new file mode 100644 index 0000000..f92f089 --- /dev/null +++ b/src/objects/Camera.cpp @@ -0,0 +1,41 @@ +#include "Camera.hpp" + +#include + +Camera::Camera(glm::vec3 position, glm::vec3 target) : + position(position), target(target) +{ + CalculateCamera(); +} + +void Camera::SetPosition(glm::vec3 newPos) +{ + position = newPos; + CalculateCamera(); +} + +void Camera::SetTarget(glm::vec3 newTarget) +{ + target = newTarget; + CalculateCamera(); +} + +void Camera::Move(glm::vec3 movement) +{ + position += movement; + CalculateCamera(); +} + +void Camera::Use(const Shader& program) +{ + program.SetUniformMat4("view", &viewMatrix[0][0]); +} + +void Camera::CalculateCamera() +{ + direction = glm::normalize(target - position); + right = glm::normalize(glm::cross(glm::vec3(0.0f, 1.0f, 0.0f), direction)); + up = glm::cross(direction, right); + + viewMatrix = glm::lookAt(position, target, up); +} diff --git a/src/objects/Camera.hpp b/src/objects/Camera.hpp new file mode 100644 index 0000000..af1a438 --- /dev/null +++ b/src/objects/Camera.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include "Shader.hpp" + +class Camera +{ +public: + Camera() = default; + Camera(glm::vec3 position, glm::vec3 target); + + void SetPosition(glm::vec3 newPos); + void SetTarget(glm::vec3 newTarget); + + void Move(glm::vec3 movement); + + void Use(const Shader& program); + +private: + void CalculateCamera(); + + glm::vec3 position, target; + glm::vec3 direction, up, right; + + glm::mat4 viewMatrix; +}; \ No newline at end of file diff --git a/src/objects/Shader.cpp b/src/objects/Shader.cpp index b2d9d18..bf477d8 100644 --- a/src/objects/Shader.cpp +++ b/src/objects/Shader.cpp @@ -96,17 +96,17 @@ void Shader::Use() glUseProgram(program); } -void Shader::SetUniformInt(const char* uniform, int value) +void Shader::SetUniformInt(const char* uniform, int value) const { glUniform1i(glGetUniformLocation(program, uniform), value); } -void Shader::SetUniformFloat(const char* uniform, float value) +void Shader::SetUniformFloat(const char* uniform, float value) const { glUniform1f(glGetUniformLocation(program, uniform), value); } -void Shader::SetUniformMat4(const char* uniform, const float* ptrToMatrix) +void Shader::SetUniformMat4(const char* uniform, const float* ptrToMatrix) const { glUniformMatrix4fv(glGetUniformLocation(program, uniform), 1, GL_FALSE, ptrToMatrix); } diff --git a/src/objects/Shader.hpp b/src/objects/Shader.hpp index bf86e61..7d55ffd 100644 --- a/src/objects/Shader.hpp +++ b/src/objects/Shader.hpp @@ -13,9 +13,9 @@ public: void Use(); - void SetUniformInt(const char* uniform, int value); - void SetUniformFloat(const char* uniform, float value); - void SetUniformMat4(const char* uniform, const float* ptrToMatrix); + void SetUniformInt(const char* uniform, int value) const; + void SetUniformFloat(const char* uniform, float value) const; + void SetUniformMat4(const char* uniform, const float* ptrToMatrix) const; private: unsigned int program; diff --git a/src/objects/primitives/Cube.cpp b/src/objects/primitives/Cube.cpp index 6933f27..92d2556 100644 --- a/src/objects/primitives/Cube.cpp +++ b/src/objects/primitives/Cube.cpp @@ -2,79 +2,115 @@ #include -Cube::Cube(float x, float y, float z, float a) : - VAO(0), VBO(0), EBO(0) +#include + +const float Cube::VERTICES[36 * (3 + 2)] = { + -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, + 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, + 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, + -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, + + -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, + 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, + 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, + -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, + + -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, + -1.0f, -1.0f, -1.0f, 0.0f, 1.0f, + -1.0f, -1.0f, -1.0f, 0.0f, 1.0f, + -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, + -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + + 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, + 1.0f, -1.0f, -1.0f, 0.0f, 1.0f, + 1.0f, -1.0f, -1.0f, 0.0f, 1.0f, + 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, + 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + + -1.0f, -1.0f, -1.0f, 0.0f, 1.0f, + 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, + 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, + 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, + -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, + -1.0f, -1.0f, -1.0f, 0.0f, 1.0f, + + -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, + 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, + -1.0f, 1.0f, -1.0f, 0.0f, 1.0f +}; + +unsigned int Cube::VAO = 0; +unsigned int Cube::VBO = 0; + +Cube::Cube(glm::vec3 position, glm::vec3 sidelengths) { - float vertices[36 * (3 + 2)] = { - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + if (VAO == 0) + { + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + glBindVertexArray(VAO); - -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), VERTICES, GL_STATIC_DRAW); - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f - }; - - glGenVertexArrays(1, &VAO); - glGenBuffers(1, &VBO); - - glBindVertexArray(VAO); - - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, (3 + 2) * sizeof(float), (void*)0); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, (3 + 2) * sizeof(float), (void*)(3 * sizeof(float))); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, (3 + 2) * sizeof(float), (void*)0); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, (3 + 2) * sizeof(float), (void*)(3 * sizeof(float))); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + } + ResetTransformation(); + Move(position); + Scale(sidelengths); } Cube::~Cube() { - glDeleteBuffers(1, &EBO); glDeleteBuffers(1, &VBO); glDeleteVertexArrays(1, &VAO); } void Cube::Draw(const Shader& program) { + program.SetUniformMat4("model", &transformation[0][0]); + glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glDrawArrays(GL_TRIANGLES, 0, 36); } + +void Cube::ResetTransformation() +{ + transformation = glm::mat4(1.0f); +} + +void Cube::Move(glm::vec3 movement) +{ + transformation = glm::translate(transformation, movement); +} + +void Cube::Rotate(float angleInRad, glm::vec3 axis) +{ + transformation = glm::rotate(transformation, angleInRad, axis); +} + +void Cube::Scale(glm::vec3 scaling) +{ + transformation = glm::scale(transformation, scaling); +} + +const float* Cube::GetMatrix() const +{ + return &transformation[0][0]; +} diff --git a/src/objects/primitives/Cube.hpp b/src/objects/primitives/Cube.hpp index 2169715..db4caa0 100644 --- a/src/objects/primitives/Cube.hpp +++ b/src/objects/primitives/Cube.hpp @@ -2,14 +2,28 @@ #include "Shader.hpp" +#include + class Cube { public: - Cube(float x, float y, float z, float a); + static const float VERTICES[36 * (3 + 2)]; +public: + Cube(glm::vec3 position, glm::vec3 sidelengths); ~Cube(); void Draw(const Shader& program); + // These should probably all go in a base class + void ResetTransformation(); + void Move(glm::vec3 movement); + void Rotate(float angleInRad, glm::vec3 axis); + void Scale(glm::vec3 scaling); + + const float* GetMatrix() const; + private: - unsigned int VAO, VBO, EBO; + static unsigned int VAO, VBO; + + glm::mat4 transformation; }; \ No newline at end of file