feat: draw tilted circle

feat: generate orbit points at an angle
This commit is contained in:
Cat Flynn 2023-08-06 02:40:35 +02:00
parent c774045c92
commit fdabe9aadd
4 changed files with 82 additions and 2 deletions

View File

@ -29,6 +29,7 @@ add_executable(${PROJECT_NAME}
src/io.cpp src/io.cpp
src/icosphere.cpp src/icosphere.cpp
src/gfx.cpp src/gfx.cpp
src/orbit.cpp
) )
if(WIN32) if(WIN32)

View File

@ -45,6 +45,7 @@
#include "gfx.hpp" #include "gfx.hpp"
#include "icosphere.hpp" #include "icosphere.hpp"
#include "orbit.hpp"
#include <cmath> #include <cmath>
#include "astro/twoBodyMethods.hpp" #include "astro/twoBodyMethods.hpp"
@ -149,7 +150,8 @@ int main()
return -1; return -1;
GLuint shaderProgram = compileShaderProgram(); GLuint shaderProgram = compileShaderProgram();
Icosphere sphere(2); //Icosphere sphere(0.5, 2);
Orbit orbit(30, glm::vec3(.5, .5, 0));
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
@ -166,7 +168,8 @@ int main()
updateModelViewProjectionMatrix(shaderProgram, glfwGetTime()); updateModelViewProjectionMatrix(shaderProgram, glfwGetTime());
// Render objects // Render objects
sphere.render(); //sphere.render();
orbit.render();
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();

57
src/orbit.cpp Normal file
View File

@ -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);
}

19
src/orbit.hpp Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <GL/glew.h>
#include <vector>
#include "glm/glm.hpp"
class Orbit
{
public:
Orbit(int vertexCount, glm::vec3 up);
void render();
~Orbit();
private:
GLuint _vbo;
GLuint _vao;
std::vector<float> _vertices;
};