From 5ff74be9fe22843b5ffb5dc1d7a802471253dca3 Mon Sep 17 00:00:00 2001 From: ktyl Date: Mon, 4 Mar 2024 07:57:42 +0000 Subject: [PATCH] feat: cycle animation with C key --- src/main.cpp | 42 ++++++++++++++++++++++++++++++++++++++---- src/orbiter.cpp | 23 ++++++++++++++++------- src/orbiter.hpp | 10 +++++++++- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2da5b2f..50bfd90 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -46,14 +46,41 @@ #include "orbiter.hpp" #include "widget.hpp" +// INPUT! +// +// what input do we even want in the first place? +// * camera controls - this is a rendering only concern which needn't affect the orbital +// * model validation - we want to modify orbits over time to confirm that our model is +// working properly +// +// TODO: input can only be directly handled by static methods, so we need to find a way to collect +// input from object instances and handle it at a higher level. in the case of this class, we want +// to determine when a configurable key is pressed. alternatively, we can split the behaviour into +// multiple sub-classes and run different loops at the top level? +// +// another idea is to process all input into a well-defined Input struct in the main loop, then +// pass this struct into objects' render() method. +// +// it's possible the first idea makes the most sense if the plan is to ultimately extract the orbit +// stuff to a library, since it keeps input a separate concern from the physics model +struct Input +{ + bool cycleAnimation; +} input; + void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { if (key == GLFW_KEY_C && action == GLFW_PRESS) { - std::cout << "lol!" << std::endl; + input.cycleAnimation = true; } } +void clearInput() +{ + input.cycleAnimation = false; +} + int main() { GLFWwindow* window = nullptr; @@ -78,22 +105,29 @@ int main() Orbiter orbiter(orbiterSphere, orbit, unlitProgram); // register input - // TODO: init objects with a reference to the window so that they can register - // their own key inputs glfwSetKeyCallback(window, keyCallback); // Main loop while (!glfwWindowShouldClose(window)) { - // TODO: receive input from GLFW + // receive input - key callback populates input struct defined further up, + // clearInput() needs to be called to clear input from previous frame + clearInput(); glfwPollEvents(); + // apply input + if (input.cycleAnimation) + { + orbiter.cycleAnimation(); + } + // rendering glClearColor(0.2, 0.3, 0.3, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); float time = glfwGetTime(); + planet.render(time); orbiter.render(time); glfwSwapBuffers(window); diff --git a/src/orbiter.cpp b/src/orbiter.cpp index ed53b31..6919843 100644 --- a/src/orbiter.cpp +++ b/src/orbiter.cpp @@ -1,13 +1,12 @@ #include "orbiter.hpp" -#include #include #include -Orbiter::Orbiter(Icosphere& sphere, Orbit& orbit, GLuint unlitShaderProgram) : +Orbiter::Orbiter(Icosphere& sphere, Orbit& orbit, GLuint shaderProgram) : _sphere(sphere), _orbit(orbit), - _widget(unlitShaderProgram) + _widget(shaderProgram) { } @@ -17,19 +16,29 @@ float Orbiter::getMeanAnomaly() return time; } +void Orbiter::cycleAnimation() +{ + _animation++; + + if (_animation > ANIM_ECCENTRICITY) + { + _animation = 0; + } +} + glm::vec3 Orbiter::getPosition(const float time) { std::vector keplerianElements(6); _orbit.getElements(keplerianElements); - int animation = (int)(time/ORBITAL_PERIOD) % 2 == 1; + //int animation = (int)(time/ORBITAL_PERIOD) % 2 == 1; - if (animation == ANIM_ORBITING) + if (_animation == ANIM_ORBITING) return _orbit.getPosition(time); // TODO: i want to modify the eccentricity of the orbit with a control, // not an automatic animation - if (animation == ANIM_ECCENTRICITY) + if (_animation == ANIM_ECCENTRICITY) { // TODO: what are these magic numbers float e = .25 + .2 * sin(time); @@ -40,7 +49,7 @@ glm::vec3 Orbiter::getPosition(const float time) return _orbit.getPosition(0); } - std::cerr << "unknown animation " << animation << std::endl; + std::cerr << "unknown animation " << _animation << std::endl; throw 1; } diff --git a/src/orbiter.hpp b/src/orbiter.hpp index afb7fda..bf48d6e 100644 --- a/src/orbiter.hpp +++ b/src/orbiter.hpp @@ -1,17 +1,22 @@ #pragma once +#include "gfx.hpp" #include "icosphere.hpp" #include "orbit.hpp" #include "widget.hpp" +#include + class Orbiter { public: - Orbiter(Icosphere& sphere, Orbit& orbit, GLuint unlitShaderProgram); + Orbiter(Icosphere& sphere, Orbit& orbit, GLuint shaderProgram); ~Orbiter(); void render(const float time); + void cycleAnimation(); + private: void updateModelMatrix(); float getMeanAnomaly(); @@ -24,6 +29,9 @@ private: const float ORBITAL_PERIOD = 6.284; + // TODO: convert these to an enum const int ANIM_ORBITING = 0; const int ANIM_ECCENTRICITY = 1; + + int _animation = ANIM_ORBITING; };