2018-02-12 18:40:05 +01:00
|
|
|
#include "periodic_updates.hpp"
|
|
|
|
|
|
|
|
#include <ctime>
|
2018-02-14 14:06:35 +01:00
|
|
|
#include <cmath>
|
2018-02-12 21:53:35 +01:00
|
|
|
#include <chrono>
|
2018-02-12 18:40:05 +01:00
|
|
|
#include <GL/glut.h>
|
|
|
|
|
2018-02-14 14:06:35 +01:00
|
|
|
struct Movement {
|
|
|
|
Movement() :
|
|
|
|
fwd(false),
|
|
|
|
bck(false),
|
|
|
|
left(false),
|
|
|
|
right(false),
|
|
|
|
turn_l(false),
|
|
|
|
turn_r(false),
|
2018-02-14 14:16:13 +01:00
|
|
|
sight_angle(3.14159)
|
2018-02-14 14:06:35 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fwd,
|
|
|
|
bck,
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
turn_l,
|
|
|
|
turn_r;
|
|
|
|
|
|
|
|
double sight_angle;
|
|
|
|
|
|
|
|
void tick() {
|
|
|
|
sight_angle += (turn_l - turn_r) * .05;
|
|
|
|
}
|
|
|
|
|
|
|
|
Point movement() {
|
|
|
|
Point front_dir = Point(-sin(sight_angle), 0, -cos(sight_angle)),
|
|
|
|
left_dir = Point(-cos(sight_angle), 0, sin(sight_angle));
|
|
|
|
return (fwd - bck) * front_dir
|
|
|
|
+ (left - right) * left_dir;
|
|
|
|
}
|
|
|
|
Point sight() {
|
|
|
|
return Point(-sin(sight_angle), 0., -cos(sight_angle));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-12 18:40:05 +01:00
|
|
|
static Ball* _ball = nullptr;
|
2018-02-12 21:53:35 +01:00
|
|
|
static std::chrono::time_point<std::chrono::steady_clock>
|
|
|
|
_last_time, _init_clocks;
|
2018-02-12 18:40:05 +01:00
|
|
|
|
2018-02-14 11:18:36 +01:00
|
|
|
static bool is_paused = false;
|
|
|
|
static double speed_factor = 1.;
|
2018-02-14 14:06:35 +01:00
|
|
|
static KeyMappings _keymap = KeyMappings::qwerty();
|
|
|
|
static Movement _movement;
|
|
|
|
static Point _position(0., 2.5, -10.);
|
|
|
|
static GlutRender* _render = nullptr;
|
2018-02-14 11:18:36 +01:00
|
|
|
|
2018-02-14 14:06:35 +01:00
|
|
|
void init_periodic_static(Ball* ball, GlutRender* render) {
|
2018-02-12 21:53:35 +01:00
|
|
|
_last_time = std::chrono::steady_clock::now();
|
|
|
|
_init_clocks = std::chrono::steady_clock::now();
|
2018-02-12 18:40:05 +01:00
|
|
|
_ball = ball;
|
2018-02-14 14:06:35 +01:00
|
|
|
_render = render;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_key_mapping(const KeyMappings& mapping) {
|
|
|
|
_keymap = mapping;
|
2018-02-12 18:40:05 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 21:53:35 +01:00
|
|
|
double ellapsed_double(
|
|
|
|
std::chrono::time_point<std::chrono::steady_clock> beg,
|
|
|
|
std::chrono::time_point<std::chrono::steady_clock> end)
|
|
|
|
{
|
|
|
|
std::chrono::duration<double> ellapsed_db = end - beg;
|
|
|
|
return ellapsed_db.count();
|
|
|
|
}
|
|
|
|
|
2018-02-12 18:40:05 +01:00
|
|
|
void periodic_update() {
|
2018-02-12 21:53:35 +01:00
|
|
|
auto now = std::chrono::steady_clock::now();
|
2018-02-14 14:06:35 +01:00
|
|
|
|
|
|
|
if(!is_paused)
|
|
|
|
_ball->update_pos(speed_factor * ellapsed_double(_last_time, now));
|
|
|
|
|
|
|
|
_movement.tick();
|
|
|
|
_position += _movement.movement();
|
2018-02-14 14:16:13 +01:00
|
|
|
Point look_at = _position + _movement.sight();
|
2018-02-14 14:06:35 +01:00
|
|
|
|
2018-02-14 14:16:13 +01:00
|
|
|
_render->set_camera(_position, look_at);
|
2018-02-12 21:53:35 +01:00
|
|
|
|
|
|
|
_last_time = now;
|
2018-02-12 18:40:05 +01:00
|
|
|
glutPostRedisplay();
|
|
|
|
}
|
2018-02-14 11:18:36 +01:00
|
|
|
|
2018-02-14 14:06:35 +01:00
|
|
|
void mvt_keys_update(bool up, unsigned key) {
|
|
|
|
if(key == _keymap.mv_fwd)
|
|
|
|
_movement.fwd = !up;
|
|
|
|
else if(key == _keymap.mv_bck)
|
|
|
|
_movement.bck = !up;
|
|
|
|
else if(key == _keymap.mv_left)
|
|
|
|
_movement.left = !up;
|
|
|
|
else if(key == _keymap.mv_right)
|
|
|
|
_movement.right = !up;
|
|
|
|
else if(key == _keymap.turn_left)
|
|
|
|
_movement.turn_l = !up;
|
|
|
|
else if(key == _keymap.turn_right)
|
|
|
|
_movement.turn_r = !up;
|
|
|
|
}
|
|
|
|
|
2018-02-14 11:18:36 +01:00
|
|
|
void periodic_kb_handler(unsigned char key, int, int) {
|
2018-02-14 14:06:35 +01:00
|
|
|
if(key == ' ')
|
2018-02-14 11:18:36 +01:00
|
|
|
is_paused = !is_paused;
|
|
|
|
else if(key == '<') {
|
|
|
|
speed_factor -= .1;
|
|
|
|
if(speed_factor <= 0.05)
|
|
|
|
speed_factor = .1;
|
|
|
|
}
|
|
|
|
else if(key == '>') {
|
|
|
|
speed_factor += .1;
|
|
|
|
}
|
|
|
|
else if(key == '0') {
|
|
|
|
speed_factor = 1.;
|
|
|
|
}
|
2018-02-14 14:06:35 +01:00
|
|
|
mvt_keys_update(false, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void periodic_kb_up_handler(unsigned char key, int, int) {
|
|
|
|
mvt_keys_update(true, key);
|
2018-02-14 11:18:36 +01:00
|
|
|
}
|