From fdabe9aadd90ee609413f666c8f1d81b859ad43f Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Sun, 6 Aug 2023 02:40:35 +0200 Subject: [PATCH] feat: draw tilted circle feat: generate orbit points at an angle --- CMakeLists.txt | 1 + src/hello.cpp | 7 +++++-- src/orbit.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/orbit.hpp | 19 +++++++++++++++++ 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/orbit.cpp create mode 100644 src/orbit.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4571f45..fb84799 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ add_executable(${PROJECT_NAME} src/io.cpp src/icosphere.cpp src/gfx.cpp + src/orbit.cpp ) if(WIN32) diff --git a/src/hello.cpp b/src/hello.cpp index 83d59ea..c125160 100644 --- a/src/hello.cpp +++ b/src/hello.cpp @@ -45,6 +45,7 @@ #include "gfx.hpp" #include "icosphere.hpp" +#include "orbit.hpp" #include #include "astro/twoBodyMethods.hpp" @@ -149,7 +150,8 @@ int main() return -1; GLuint shaderProgram = compileShaderProgram(); - Icosphere sphere(2); + //Icosphere sphere(0.5, 2); + Orbit orbit(30, glm::vec3(.5, .5, 0)); glEnable(GL_DEPTH_TEST); @@ -166,7 +168,8 @@ int main() updateModelViewProjectionMatrix(shaderProgram, glfwGetTime()); // Render objects - sphere.render(); + //sphere.render(); + orbit.render(); glfwSwapBuffers(window); glfwPollEvents(); diff --git a/src/orbit.cpp b/src/orbit.cpp new file mode 100644 index 0000000..6bee57a --- /dev/null +++ b/src/orbit.cpp @@ -0,0 +1,57 @@ +#include "orbit.hpp" + +#include "glm/gtx/quaternion.hpp" + +Orbit::Orbit(int vertexCount, glm::vec3 up) +{ + const float pi = 3.14159265359; + const glm::vec3 worldUp = glm::vec3(0.0, 1.0, 0.0); + + // Compute the rotation between world 'up' and our defined up + glm::quat rot = glm::rotation(worldUp, normalize(up)); + + for (int i = 0; i < vertexCount; i++) + { + float a = (float)i / (float)vertexCount; + a *= 2.0 * pi; + + glm::vec3 v = glm::vec3(cos(a), 0.0, sin(a)); + + // Rotate generated point to align with new up vector + v = rot * v; + + _vertices.push_back(v.x); + _vertices.push_back(v.y); + _vertices.push_back(v.z); + } + + glGenVertexArrays(1, &_vao); + glGenBuffers(1, &_vbo); + + glBindVertexArray(_vao); + + glBindBuffer(GL_ARRAY_BUFFER, _vbo); + size_t vboBufferSize = _vertices.size() * sizeof(float); + glBufferData(GL_ARRAY_BUFFER, vboBufferSize, &_vertices[0], GL_STATIC_DRAW); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); +} + +void Orbit::render() +{ + glBindVertexArray(_vao); + glBindBuffer(GL_ARRAY_BUFFER, _vbo); + + glDrawArrays(GL_LINE_LOOP, 0, _vertices.size() / 3); +} + +Orbit::~Orbit() +{ + glDeleteVertexArrays(1, &_vao); + glDeleteBuffers(1, &_vbo); +} + diff --git a/src/orbit.hpp b/src/orbit.hpp new file mode 100644 index 0000000..3bc15d1 --- /dev/null +++ b/src/orbit.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include +#include + +#include "glm/glm.hpp" + +class Orbit +{ +public: + Orbit(int vertexCount, glm::vec3 up); + void render(); + ~Orbit(); +private: + GLuint _vbo; + GLuint _vao; + + std::vector _vertices; +};