diff --git a/src/Axis.cpp b/src/Axis.cpp new file mode 100644 index 0000000..5711f35 --- /dev/null +++ b/src/Axis.cpp @@ -0,0 +1,105 @@ +#include "Axis.hpp" + +#define TWO_PI 6.28318530718 +#define PI 3.14159265359 + +#include + +#include +#include + +#include "Shader.hpp" +#include "Camera.hpp" + +Shader* Axis::defaultShader = nullptr; + +Axis::Axis(const glm::vec3& direction, float length) +{ + if (defaultShader == nullptr) + { + defaultShader = new Shader( + R"( + #version 460 core + + layout(location = 0) in vec3 position; + + out vec3 outColor; + + uniform mat4 model; + uniform mat4 view; + uniform mat4 projection; + + void main() + { + outColor = vec3(0.6f, 0.6f, 0.6f); + gl_Position = projection * view * model * vec4(position, 1.0f); + } + )", + + R"( + #version 460 core + + in vec3 outColor; + out vec4 FragColor; + + void main() + { + FragColor = vec4(outColor, 1.0f); + } + )" + ); + } + + const unsigned int axisRingResolution = 80; + + vertices.push_back(0.0f); + vertices.push_back(0.0f); + vertices.push_back(length); + for (int i = 0; i < axisRingResolution; i++) + { + float angle = TWO_PI * (float)i / (float)axisRingResolution; + float x = std::cos(angle); + float y = std::sin(angle); + vertices.push_back(x); + vertices.push_back(y); + vertices.push_back(-length); + + vertices.push_back(x); + vertices.push_back(y); + vertices.push_back(length - 0.5f); + + vertices.push_back(2.0f * x); + vertices.push_back(2.0f * y); + vertices.push_back(length - 0.5f); + + indices.push_back(3 * i + 1); + indices.push_back((3 * i + 1) % (axisRingResolution * 3) + 1); + indices.push_back((3 * i + 4) % (axisRingResolution * 3) + 1); + + indices.push_back(3 * i + 1); + indices.push_back((3*i + 4) % (axisRingResolution * 3) + 1); + indices.push_back((3*i + 3) % (axisRingResolution * 3) + 1); + + indices.push_back((3 * i + 2) % (axisRingResolution * 3) + 1); + indices.push_back(0); + indices.push_back((3 * i + 5) % (axisRingResolution * 3) + 1); + } + + CreateVAO(); + + float angleBetweenVectors = std::acos(glm::dot(direction, glm::vec3(0.0f, 0.0f, 1.0f)) / glm::length(direction)); + if (angleBetweenVectors > 0.01) + { + glm::vec3 rotationAxis = glm::cross(direction, glm::vec3(0.0f, 0.0f, 1.0f)); + modelMatrix = glm::rotate(modelMatrix, -angleBetweenVectors, rotationAxis); + } + modelMatrix = glm::scale(modelMatrix, glm::vec3(0.03f, 0.03f, 1.0f)); +} + +void Axis::BindDefaultShader(Camera& camera) +{ + defaultShader->Bind(); + defaultShader->SetMatrix("model", glm::value_ptr(modelMatrix)); + defaultShader->SetMatrix("view", glm::value_ptr(camera.GetViewMatrix())); + defaultShader->SetMatrix("projection", glm::value_ptr(camera.GetProjectionMatrix())); +} diff --git a/src/Axis.hpp b/src/Axis.hpp new file mode 100644 index 0000000..73856fd --- /dev/null +++ b/src/Axis.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include +#include "Model.hpp" + +class Shader; +class Camera; + +class Axis : public Model +{ +public: + Axis(const glm::vec3& direction, float length); + + void BindDefaultShader(Camera& camera); + +private: + static Shader* defaultShader; +}; \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 98a0018..050228a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ -add_executable(orbitals "main.cpp" "Model.cpp" "Shader.cpp" "Camera.cpp" "Orbital.cpp") +add_executable(orbitals "main.cpp" "Model.cpp" "Shader.cpp" "Camera.cpp" "Orbital.cpp" "Axis.cpp") # Add GLFW, GLM, GLAD and ImGui include directories to target target_include_directories(orbitals PRIVATE diff --git a/src/Orbital.cpp b/src/Orbital.cpp index 224a403..03bea42 100644 --- a/src/Orbital.cpp +++ b/src/Orbital.cpp @@ -65,7 +65,7 @@ Orbital::Orbital(int l, int m) : CreateVAO(); UpdateModel(); - modelMatrix = glm::rotate(modelMatrix, glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f)); + // modelMatrix = glm::rotate(modelMatrix, glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f)); modelMatrix = glm::scale(modelMatrix, glm::vec3(3.0f)); } diff --git a/src/Orbital.hpp b/src/Orbital.hpp index fd6c2b4..f449693 100644 --- a/src/Orbital.hpp +++ b/src/Orbital.hpp @@ -23,6 +23,5 @@ public: int l, m; private: - static Shader* defaultShader; }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index dddd7f1..7b97dc5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ #include #include "Orbital.hpp" +#include "Axis.hpp" #include "Shader.hpp" #include "Camera.hpp" @@ -74,6 +75,9 @@ int main(int argc, char** argv) // Create some orbital and set up its transformation matrix // TODO: the matrix should probably be part of Model Orbital orbital(2, 1); + Axis axisX(glm::vec3(1.0f, 0.0f, 0.0f), 4.0f); + Axis axisY(glm::vec3(0.0f, 1.0f, 0.0f), 4.0f); + Axis axisZ(glm::vec3(0.0f, 0.0f, 1.0f), 4.0f); glm::mat4 modelMatrix = glm::mat4(1.0f); modelMatrix = glm::rotate(modelMatrix, glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f)); @@ -123,6 +127,15 @@ int main(int argc, char** argv) orbital.BindDefaultShader(camera); orbital.Draw(); + axisX.BindDefaultShader(camera); + axisX.Draw(); + + axisY.BindDefaultShader(camera); + axisY.Draw(); + + axisZ.BindDefaultShader(camera); + axisZ.Draw(); + ImGui::Begin("Orbital Settings"); if (ImGui::CollapsingHeader("Properties"))