From a1cc442885c8bc737b2569c6b7186125d2932e26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Mon, 12 Feb 2018 18:40:05 +0100 Subject: [PATCH] First observed bounce --- Ball.cpp | 3 +- Ball.hpp | 4 ++- Makefile | 1 + main_bounce.cpp | 24 ++++++++++++++++ periodic_updates.cpp | 24 ++++++++++++++++ periodic_updates.hpp | 6 ++++ render/GlutRender.cpp | 65 ++++++++++++++++++++++++++++++------------- render/GlutRender.hpp | 22 +++++++++++++++ spheroid.cpp | 2 +- spheroid.hpp | 8 +++--- 10 files changed, 132 insertions(+), 27 deletions(-) create mode 100644 main_bounce.cpp create mode 100644 periodic_updates.cpp create mode 100644 periodic_updates.hpp diff --git a/Ball.cpp b/Ball.cpp index 82338a4..809e812 100644 --- a/Ball.cpp +++ b/Ball.cpp @@ -2,7 +2,8 @@ #include #include -Ball::Ball(Point& _center, double _min_height, double _v_x, double _p, double _q) : +Ball::Ball(const Point& _center, double _min_height, double _v_x, + double _p, double _q) : Center(_center), surface(_center, _p, _q), init_h(_center.z), diff --git a/Ball.hpp b/Ball.hpp index 0ab1a86..b065e3d 100644 --- a/Ball.hpp +++ b/Ball.hpp @@ -32,11 +32,13 @@ class Ball { void _compute_U_n(); void _compute_T_n(); public: - Ball(Point& _center, double _min_height, double _v_x, double p, double q); + Ball(const Point& _center, double _min_height, double _v_x, + double p, double q); void update_pos(double dt); Point getCenter() const {return Center;} double accessT() const { return T;} double access_crt_time() const { return crt_time;} + Spheroid* get_surface() { return &surface; } }; std::ostream& operator << (std::ostream &out, Ball const& data); diff --git a/Makefile b/Makefile index c816215..b5d37ec 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ OBJS=Implicit.o \ util/ObjParser.o \ MarchingCubes.o \ _gen/marching_cubes_data.o \ + periodic_updates.o \ tests/TestImplicitSphere.o \ render/GlutRender.o diff --git a/main_bounce.cpp b/main_bounce.cpp new file mode 100644 index 0000000..c4fc771 --- /dev/null +++ b/main_bounce.cpp @@ -0,0 +1,24 @@ +/** An entry-point file using render/GlutRender as a renderer, displaying the + * bouncing implicit-surface defined sphere. + **/ + +#include "render/GlutRender.hpp" +#include "Ball.hpp" +#include "MarchingCubes.hpp" + +#include "periodic_updates.hpp" + +int main(int argc, char** argv) { + GlutRender& render = GlutRender::get_instance(); + render.init(&argc, argv, 640, 480, "Bouncing stuff"); + + Ball ball(Point(0, 0, 10), 0, .25, 1, 1); + render.add_surface(ball.get_surface(), + Cuboid(Point(-2, -2, -2), Point(2, 2, 2))); + + render.set_idle_func(periodic_update); + init_periodic_static(&ball); + render.run(); + + return 0; +} diff --git a/periodic_updates.cpp b/periodic_updates.cpp new file mode 100644 index 0000000..b38b54e --- /dev/null +++ b/periodic_updates.cpp @@ -0,0 +1,24 @@ +#include "periodic_updates.hpp" + +#include +#include + +static Ball* _ball = nullptr; +static clock_t _last_time = 0; + +double time_of_clocks(clock_t time) { + return ((double)time) / ((double)CLOCKS_PER_SEC); +} + +void init_periodic_static(Ball* ball) { + _last_time = clock(); + _ball = ball; +} + +void periodic_update() { + clock_t now = clock(); + _ball->update_pos(time_of_clocks(now - _last_time)); + _last_time = now; + + glutPostRedisplay(); +} diff --git a/periodic_updates.hpp b/periodic_updates.hpp new file mode 100644 index 0000000..761977d --- /dev/null +++ b/periodic_updates.hpp @@ -0,0 +1,6 @@ +#pragma once + +#include "Ball.hpp" + +void init_periodic_static(Ball* ball); +void periodic_update(); diff --git a/render/GlutRender.cpp b/render/GlutRender.cpp index 31268d6..7c05f27 100644 --- a/render/GlutRender.cpp +++ b/render/GlutRender.cpp @@ -1,4 +1,5 @@ #include "GlutRender.hpp" +#include "../MarchingCubes.hpp" #include #include @@ -11,7 +12,11 @@ GlutRender& GlutRender::get_instance() { return instance; } -GlutRender::GlutRender() { } +GlutRender::GlutRender() { + std::default_random_engine rand_engine(time(NULL)); + std::uniform_real_distribution distribution; + rand_color = std::bind(distribution, rand_engine); +} void GlutRender::init(int* argc, char** argv, int wid, int hei, const char* win_name) @@ -44,10 +49,22 @@ void GlutRender::remove_mesh(Mesh* mesh) { meshes.erase(mesh); } +void GlutRender::add_surface(ImplicitSurface* surf, const Cuboid& box) { + surfaces.insert(SurfaceDetails(surf, box)); +} + +void GlutRender::remove_surface(ImplicitSurface* surf) { + surfaces.erase(SurfaceDetails(surf, Cuboid::empty())); +} + void GlutRender::run() { glutMainLoop(); } +void GlutRender::set_idle_func(void (*func)(void)) { + glutIdleFunc(func); +} + void GlutRender::reshape(int wid, int hei) { if (hei == 0) hei = 1; @@ -61,34 +78,42 @@ void GlutRender::reshape(int wid, int hei) { gluPerspective(45.0f, aspect, 0.1f, 100.0f); } +void GlutRender::display_mesh(const Mesh& mesh) const { + const Point& mesh_center = mesh.get_center(); + + const std::vector& points = mesh.get_vertices(); + + glBegin(GL_TRIANGLES); + for(const Face& face: mesh.get_faces()) { + Point p0 = face.pt(0, points) + mesh_center, + p1 = face.pt(1, points) + mesh_center, + p2 = face.pt(2, points) + mesh_center; + glColor3f(rand_color(), rand_color(), rand_color()); + glVertex3f(p0[0], p0[1], p0[2]); + glVertex3f(p1[0], p1[1], p1[2]); + glVertex3f(p2[0], p2[1], p2[2]); + } + glEnd(); +} + void GlutRender::display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); // Camera position and orientation glLoadIdentity(); - glTranslatef(0., 0., -6.); - - std::default_random_engine rand_engine(time(NULL)); - std::uniform_real_distribution distribution; - auto rand_color = std::bind(distribution, rand_engine); + //glTranslatef(1., 2., -10.); + gluLookAt(0, 0, -10, + 0, 0, 10, + 0, 1, 0); for(Mesh* mesh: meshes) { - const Point& mesh_center = mesh->get_center(); + display_mesh(*mesh); + } - const std::vector& points = mesh->get_vertices(); - - glBegin(GL_TRIANGLES); - for(const Face& face: mesh->get_faces()) { - Point p0 = face.pt(0, points) + mesh_center, - p1 = face.pt(1, points) + mesh_center, - p2 = face.pt(2, points) + mesh_center; - glColor3f(rand_color(), rand_color(), rand_color()); - glVertex3f(p0[0], p0[1], p0[2]); - glVertex3f(p1[0], p1[1], p1[2]); - glVertex3f(p2[0], p2[1], p2[2]); - } - glEnd(); + for(const SurfaceDetails& surface: surfaces) { + Mesh mesh = MarchingCubes(*surface.surface, surface.box)(); + display_mesh(mesh); } glutSwapBuffers(); diff --git a/render/GlutRender.hpp b/render/GlutRender.hpp index eb85680..1ca1ba0 100644 --- a/render/GlutRender.hpp +++ b/render/GlutRender.hpp @@ -3,6 +3,7 @@ #pragma once #include "../Mesh.hpp" +#include "../Implicit.hpp" #include class GlutRender { @@ -17,11 +18,17 @@ class GlutRender { void cleanup(); void add_mesh(Mesh* mesh); void remove_mesh(Mesh* mesh); + void add_surface(ImplicitSurface* surf, const Cuboid& box); + void remove_surface(ImplicitSurface* surf); void run(); + void set_idle_func(void (*func)(void)); + private: //meth GlutRender(); + void display_mesh(const Mesh& mesh) const; + protected: void reshape(int wid, int hei); void display(); @@ -29,5 +36,20 @@ class GlutRender { static void reshape_handle(int wid, int hei); static void display_handle(); private: + struct SurfaceDetails { + SurfaceDetails(ImplicitSurface* surf, const Cuboid& box): + surface(surf), box(box) {} + + ImplicitSurface* surface; + Cuboid box; + + bool operator<(const SurfaceDetails& oth) const { + return surface < oth.surface; + } + }; + + std::function rand_color; + std::set meshes; + std::set surfaces; }; diff --git a/spheroid.cpp b/spheroid.cpp index ef9f381..4ac9fc6 100644 --- a/spheroid.cpp +++ b/spheroid.cpp @@ -4,7 +4,7 @@ #include "spheroid.hpp" -Spheroid::Spheroid(Point& _center, size_t _p, size_t _q) : +Spheroid::Spheroid(const Point& _center, double _p, double _q) : ImplicitSurface(_center), init_p(_p), p(_p), q(_q), stiffness(0) { _compute_volume(); } diff --git a/spheroid.hpp b/spheroid.hpp index 66b2aec..8eb8ead 100644 --- a/spheroid.hpp +++ b/spheroid.hpp @@ -12,7 +12,7 @@ const double PI = 3.141592653589793; class Spheroid : public ImplicitSurface { public: - Spheroid(Point& _center, size_t _p, size_t _q); + Spheroid(const Point& _center, double _p, double _q); void update_center_pos(Point& _center); void update_radius(); void update_height(); @@ -20,9 +20,9 @@ class Spheroid : public ImplicitSurface { void check_horizontal_plan_collision(double& height); void check_vertical_plan_collision(double& abscissa); double operator() (double _x, double _y, double _z) const { - return (pow(_x - center.x, 2) / pow(q, 2) - + pow(_y - center.y, 2) / pow(q, 2) - + pow(_z - center.z, 2) / pow(p, 2) -1); + return (pow(_x, 2) / pow(q, 2) + + pow(_y, 2) / pow(q, 2) + + pow(_z, 2) / pow(p, 2) -1); } private: /**