Fix clock being real and not user clock

This commit is contained in:
Théophile Bastian 2018-02-12 21:53:35 +01:00
parent 2f5c423cc7
commit 0bae04d802
4 changed files with 34 additions and 15 deletions

View file

@ -12,9 +12,14 @@ int main(int argc, char** argv) {
GlutRender& render = GlutRender::get_instance(); GlutRender& render = GlutRender::get_instance();
render.init(&argc, argv, 640, 480, "Bouncing stuff"); render.init(&argc, argv, 640, 480, "Bouncing stuff");
Ball ball(Point(0, 0, 10), 0, .25, 1, 1); Ball ball(Point(0, 10, 0), 0.25, 0.25, 0, 1, 1);
Cuboid bbox = ball.get_surface()->max_bounding_box();
printf("%.2lf %.2lf %.2lf | %.2lf %.2lf %.2lf\n",
bbox.low(0), bbox.low(1), bbox.low(2),
bbox.high(0), bbox.high(1), bbox.high(2));
render.add_surface(ball.get_surface(), render.add_surface(ball.get_surface(),
Cuboid(Point(-2, -2, -2), Point(2, 2, 2))); ball.get_surface()->max_bounding_box());
render.set_idle_func(periodic_update); render.set_idle_func(periodic_update);
init_periodic_static(&ball); init_periodic_static(&ball);

View file

@ -1,24 +1,38 @@
#include "periodic_updates.hpp" #include "periodic_updates.hpp"
#include <ctime> #include <ctime>
#include <chrono>
#include <GL/glut.h> #include <GL/glut.h>
static Ball* _ball = nullptr; static Ball* _ball = nullptr;
static clock_t _last_time = 0; static std::chrono::time_point<std::chrono::steady_clock>
_last_time, _init_clocks;
double time_of_clocks(clock_t time) {
return ((double)time) / ((double)CLOCKS_PER_SEC);
}
void init_periodic_static(Ball* ball) { void init_periodic_static(Ball* ball) {
_last_time = clock(); _last_time = std::chrono::steady_clock::now();
_init_clocks = std::chrono::steady_clock::now();
_ball = ball; _ball = ball;
} }
void periodic_update() { double ellapsed_double(
clock_t now = clock(); std::chrono::time_point<std::chrono::steady_clock> beg,
_ball->update_pos(time_of_clocks(now - _last_time)); std::chrono::time_point<std::chrono::steady_clock> end)
_last_time = now; {
std::chrono::duration<double> ellapsed_db = end - beg;
return ellapsed_db.count();
}
void periodic_update() {
auto now = std::chrono::steady_clock::now();
_ball->update_pos(ellapsed_double(_last_time, now));
fprintf(stderr, "dt = %lf, tot = %lf; center: %.2lf, %.2lf, %.2lf\n",
ellapsed_double(_last_time, now),
ellapsed_double(_init_clocks, now),
_ball->getCenter().x,
_ball->getCenter().y,
_ball->getCenter().z);
_last_time = now;
glutPostRedisplay(); glutPostRedisplay();
} }

View file

@ -103,8 +103,8 @@ void GlutRender::display() {
// Camera position and orientation // Camera position and orientation
glLoadIdentity(); glLoadIdentity();
//glTranslatef(1., 2., -10.); //glTranslatef(1., 2., -10.);
gluLookAt(0, 0, -10, gluLookAt(0, 5, -20,
0, 0, 10, 0, 5, 0,
0, 1, 0); 0, 1, 0);
for(Mesh* mesh: meshes) { for(Mesh* mesh: meshes) {

View file

@ -52,7 +52,7 @@ void Spheroid::check_vertical_plan_collision(double& abscissa) {
} }
Cuboid Spheroid::max_bounding_box() const { Cuboid Spheroid::max_bounding_box() const {
double max_radius = sqrt((3/4) * V / PI / min_p); double max_radius = sqrt((3./4.) * V / PI / min_p);
double max_height = init_p; double max_height = init_p;
Point _bd1(max_radius, max_radius, max_height); Point _bd1(max_radius, max_radius, max_height);
Point _bd2(-max_radius, -max_radius, -max_height); Point _bd2(-max_radius, -max_radius, -max_height);