feat(orbit-animation): update vertices over time

This commit is contained in:
ktyl 2023-10-07 23:53:01 +01:00
parent 2384028542
commit 3a71a4899d
3 changed files with 42 additions and 15 deletions

View File

@ -37,6 +37,7 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <astro/stateVectorIndices.hpp>
#include "gfx.hpp" #include "gfx.hpp"
#include "icosphere.hpp" #include "icosphere.hpp"
@ -54,7 +55,14 @@ int main()
Icosphere planet(0.2, 3, litProgram); Icosphere planet(0.2, 3, litProgram);
Icosphere orbiter(0.07, 2, litProgram); Icosphere orbiter(0.07, 2, litProgram);
Orbit orbit(100);
std::vector<float> keplerianElements(6);
keplerianElements[astro::semiMajorAxisIndex] = .75;
keplerianElements[astro::eccentricityIndex] = .5;
keplerianElements[astro::inclinationIndex] = 3.142 / 2.0 + 1;
keplerianElements[astro::argumentOfPeriapsisIndex] = 2.0;
keplerianElements[astro::longitudeOfAscendingNodeIndex] = 0;
Orbit orbit(keplerianElements);
Widget widget(orbit, unlitProgram); Widget widget(orbit, unlitProgram);
@ -66,6 +74,10 @@ int main()
float time = glfwGetTime(); float time = glfwGetTime();
float e = .25 + .2 * sin(time);
keplerianElements[astro::eccentricityIndex] = e;
orbit.setElements(keplerianElements);
glm::vec3 pos = orbit.getPosition(time); glm::vec3 pos = orbit.getPosition(time);
orbiter.setPosition(pos); orbiter.setPosition(pos);

View File

@ -3,18 +3,22 @@
#include "astro/stateVectorIndices.hpp" #include "astro/stateVectorIndices.hpp"
#include "astro/orbitalElementConversions.hpp" #include "astro/orbitalElementConversions.hpp"
Orbit::Orbit(int vertexCount) : Orbit::Orbit(Vector6 keplerianElements) :
_keplerianElements(std::vector<float>(6)) _keplerianElements(keplerianElements)
{ {
_keplerianElements[astro::semiMajorAxisIndex] = .75; glGenVertexArrays(1, &_vao);
_keplerianElements[astro::eccentricityIndex] = .5; glGenBuffers(1, &_vbo);
_keplerianElements[astro::inclinationIndex] = _pi / 2.0 + 1;
_keplerianElements[astro::argumentOfPeriapsisIndex] = 2.0;
_keplerianElements[astro::longitudeOfAscendingNodeIndex] = 0;
for (int i = 0; i < vertexCount; i++) regenerateVertices();
}
void Orbit::regenerateVertices()
{ {
float t = (float)i / (float)vertexCount * 2.0 * _pi; _vertices.clear();
for (int i = 0; i < _vertexCount; i++)
{
float t = (float)i / (float)_vertexCount * 2.0 * _pi;
glm::vec3 pos = getPosition(t); glm::vec3 pos = getPosition(t);
_vertices.push_back(pos.x); _vertices.push_back(pos.x);
@ -22,9 +26,6 @@ Orbit::Orbit(int vertexCount) :
_vertices.push_back(pos.z); _vertices.push_back(pos.z);
} }
glGenVertexArrays(1, &_vao);
glGenBuffers(1, &_vbo);
glBindVertexArray(_vao); glBindVertexArray(_vao);
glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBindBuffer(GL_ARRAY_BUFFER, _vbo);
@ -38,6 +39,12 @@ Orbit::Orbit(int vertexCount) :
glBindVertexArray(0); glBindVertexArray(0);
} }
void Orbit::setElements(Vector6 keplerianElements)
{
_keplerianElements = keplerianElements;
regenerateVertices();
}
// Interpolate a position around the orbit. // Interpolate a position around the orbit.
// t is in range 0..1 and wraps. // t is in range 0..1 and wraps.
glm::vec3 Orbit::getPosition(const float meanAnomaly) glm::vec3 Orbit::getPosition(const float meanAnomaly)

View File

@ -5,22 +5,30 @@
#include "glm/glm.hpp" #include "glm/glm.hpp"
typedef std::vector<float> Vector6;
class Orbit class Orbit
{ {
public: public:
Orbit(int vertexCount); Orbit(Vector6 keplerianElements);
void render(); void render();
glm::vec3 getPosition(const float meanAnomaly); glm::vec3 getPosition(const float meanAnomaly);
glm::vec3 getVelocity(const float meanAnomaly);
glm::vec3 getTangent(const float meanAnomaly); glm::vec3 getTangent(const float meanAnomaly);
void setElements(Vector6 keplerianElements);
~Orbit(); ~Orbit();
private: private:
const float _pi = 3.14159265359; const float _pi = 3.14159265359;
const int _vertexCount = 100;
GLuint _vbo; GLuint _vbo;
GLuint _vao; GLuint _vao;
std::vector<float> _vertices; std::vector<float> _vertices;
std::vector<float> _keplerianElements; Vector6 _keplerianElements;
void regenerateVertices();
}; };