feat: cycle animation with C key
This commit is contained in:
		
							parent
							
								
									0b4e84323c
								
							
						
					
					
						commit
						5ff74be9fe
					
				
							
								
								
									
										42
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								src/main.cpp
									
									
									
									
									
								
							@ -46,14 +46,41 @@
 | 
				
			|||||||
#include "orbiter.hpp"
 | 
					#include "orbiter.hpp"
 | 
				
			||||||
#include "widget.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)
 | 
					void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    if (key == GLFW_KEY_C && action == GLFW_PRESS)
 | 
					    if (key == GLFW_KEY_C && action == GLFW_PRESS)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        std::cout << "lol!" << std::endl;
 | 
					        input.cycleAnimation = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void clearInput()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    input.cycleAnimation = false;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main()
 | 
					int main()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    GLFWwindow* window = nullptr;
 | 
					    GLFWwindow* window = nullptr;
 | 
				
			||||||
@ -78,22 +105,29 @@ int main()
 | 
				
			|||||||
    Orbiter orbiter(orbiterSphere, orbit, unlitProgram);
 | 
					    Orbiter orbiter(orbiterSphere, orbit, unlitProgram);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // register input
 | 
					    // register input
 | 
				
			||||||
    // TODO: init objects with a reference to the window so that they can register
 | 
					 | 
				
			||||||
    // their own key inputs
 | 
					 | 
				
			||||||
    glfwSetKeyCallback(window, keyCallback);
 | 
					    glfwSetKeyCallback(window, keyCallback);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Main loop
 | 
					    // Main loop
 | 
				
			||||||
    while (!glfwWindowShouldClose(window))
 | 
					    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();
 | 
					        glfwPollEvents();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // apply input
 | 
				
			||||||
 | 
					        if (input.cycleAnimation)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            orbiter.cycleAnimation();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // rendering
 | 
					        // rendering
 | 
				
			||||||
        glClearColor(0.2, 0.3, 0.3, 1.0);
 | 
					        glClearColor(0.2, 0.3, 0.3, 1.0);
 | 
				
			||||||
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 | 
					        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        float time = glfwGetTime();
 | 
					        float time = glfwGetTime();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        planet.render(time);
 | 
				
			||||||
        orbiter.render(time);
 | 
					        orbiter.render(time);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        glfwSwapBuffers(window);
 | 
					        glfwSwapBuffers(window);
 | 
				
			||||||
 | 
				
			|||||||
@ -1,13 +1,12 @@
 | 
				
			|||||||
#include "orbiter.hpp"
 | 
					#include "orbiter.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <GLFW/glfw3.h>
 | 
					 | 
				
			||||||
#include <iostream>
 | 
					#include <iostream>
 | 
				
			||||||
#include <astro/stateVectorIndices.hpp>
 | 
					#include <astro/stateVectorIndices.hpp>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Orbiter::Orbiter(Icosphere& sphere, Orbit& orbit, GLuint unlitShaderProgram) :
 | 
					Orbiter::Orbiter(Icosphere& sphere, Orbit& orbit, GLuint shaderProgram) :
 | 
				
			||||||
    _sphere(sphere),
 | 
					    _sphere(sphere),
 | 
				
			||||||
    _orbit(orbit),
 | 
					    _orbit(orbit),
 | 
				
			||||||
    _widget(unlitShaderProgram)
 | 
					    _widget(shaderProgram)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -17,19 +16,29 @@ float Orbiter::getMeanAnomaly()
 | 
				
			|||||||
    return time;
 | 
					    return time;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Orbiter::cycleAnimation()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    _animation++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (_animation > ANIM_ECCENTRICITY)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        _animation = 0;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
glm::vec3 Orbiter::getPosition(const float time)
 | 
					glm::vec3 Orbiter::getPosition(const float time)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    std::vector<float> keplerianElements(6);
 | 
					    std::vector<float> keplerianElements(6);
 | 
				
			||||||
    _orbit.getElements(keplerianElements);
 | 
					    _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);
 | 
					        return _orbit.getPosition(time);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO: i want to modify the eccentricity of the orbit with a control,
 | 
					    // TODO: i want to modify the eccentricity of the orbit with a control,
 | 
				
			||||||
    // not an automatic animation
 | 
					    // not an automatic animation
 | 
				
			||||||
    if (animation == ANIM_ECCENTRICITY)
 | 
					    if (_animation == ANIM_ECCENTRICITY)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        // TODO: what are these magic numbers
 | 
					        // TODO: what are these magic numbers
 | 
				
			||||||
        float e = .25 + .2 * sin(time);
 | 
					        float e = .25 + .2 * sin(time);
 | 
				
			||||||
@ -40,7 +49,7 @@ glm::vec3 Orbiter::getPosition(const float time)
 | 
				
			|||||||
        return _orbit.getPosition(0);
 | 
					        return _orbit.getPosition(0);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    std::cerr << "unknown animation " << animation << std::endl;
 | 
					    std::cerr << "unknown animation " << _animation << std::endl;
 | 
				
			||||||
    throw 1;
 | 
					    throw 1;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,17 +1,22 @@
 | 
				
			|||||||
#pragma once
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "gfx.hpp"
 | 
				
			||||||
#include "icosphere.hpp"
 | 
					#include "icosphere.hpp"
 | 
				
			||||||
#include "orbit.hpp"
 | 
					#include "orbit.hpp"
 | 
				
			||||||
#include "widget.hpp"
 | 
					#include "widget.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <GLFW/glfw3.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Orbiter
 | 
					class Orbiter
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    Orbiter(Icosphere& sphere, Orbit& orbit, GLuint unlitShaderProgram);
 | 
					    Orbiter(Icosphere& sphere, Orbit& orbit, GLuint shaderProgram);
 | 
				
			||||||
    ~Orbiter();
 | 
					    ~Orbiter();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void render(const float time);
 | 
					    void render(const float time);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    void cycleAnimation();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    void updateModelMatrix();
 | 
					    void updateModelMatrix();
 | 
				
			||||||
    float getMeanAnomaly();
 | 
					    float getMeanAnomaly();
 | 
				
			||||||
@ -24,6 +29,9 @@ private:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const float ORBITAL_PERIOD = 6.284;
 | 
					    const float ORBITAL_PERIOD = 6.284;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // TODO: convert these to an enum
 | 
				
			||||||
    const int ANIM_ORBITING     = 0;
 | 
					    const int ANIM_ORBITING     = 0;
 | 
				
			||||||
    const int ANIM_ECCENTRICITY = 1;
 | 
					    const int ANIM_ECCENTRICITY = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    int _animation = ANIM_ORBITING;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user