added axes

This commit is contained in:
Lauchmelder 2021-11-18 17:43:30 +01:00
parent df3c8e302b
commit 3b900db1db
6 changed files with 138 additions and 3 deletions

105
src/Axis.cpp Normal file
View file

@ -0,0 +1,105 @@
#include "Axis.hpp"
#define TWO_PI 6.28318530718
#define PI 3.14159265359
#include <cmath>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#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()));
}

18
src/Axis.hpp Normal file
View file

@ -0,0 +1,18 @@
#pragma once
#include <glm/vec3.hpp>
#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;
};

View file

@ -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 # Add GLFW, GLM, GLAD and ImGui include directories to target
target_include_directories(orbitals PRIVATE target_include_directories(orbitals PRIVATE

View file

@ -65,7 +65,7 @@ Orbital::Orbital(int l, int m) :
CreateVAO(); CreateVAO();
UpdateModel(); 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)); modelMatrix = glm::scale(modelMatrix, glm::vec3(3.0f));
} }

View file

@ -23,6 +23,5 @@ public:
int l, m; int l, m;
private: private:
static Shader* defaultShader; static Shader* defaultShader;
}; };

View file

@ -8,6 +8,7 @@
#include <backends/imgui_impl_opengl3.h> #include <backends/imgui_impl_opengl3.h>
#include "Orbital.hpp" #include "Orbital.hpp"
#include "Axis.hpp"
#include "Shader.hpp" #include "Shader.hpp"
#include "Camera.hpp" #include "Camera.hpp"
@ -74,6 +75,9 @@ int main(int argc, char** argv)
// Create some orbital and set up its transformation matrix // Create some orbital and set up its transformation matrix
// TODO: the matrix should probably be part of Model // TODO: the matrix should probably be part of Model
Orbital orbital(2, 1); 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); glm::mat4 modelMatrix = glm::mat4(1.0f);
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));
@ -123,6 +127,15 @@ int main(int argc, char** argv)
orbital.BindDefaultShader(camera); orbital.BindDefaultShader(camera);
orbital.Draw(); orbital.Draw();
axisX.BindDefaultShader(camera);
axisX.Draw();
axisY.BindDefaultShader(camera);
axisY.Draw();
axisZ.BindDefaultShader(camera);
axisZ.Draw();
ImGui::Begin("Orbital Settings"); ImGui::Begin("Orbital Settings");
if (ImGui::CollapsingHeader("Properties")) if (ImGui::CollapsingHeader("Properties"))