First observed bounce

This commit is contained in:
Théophile Bastian 2018-02-12 18:40:05 +01:00
parent 48ef986f4f
commit a1cc442885
10 changed files with 132 additions and 27 deletions

View file

@ -2,7 +2,8 @@
#include <iostream>
#include <cmath>
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),

View file

@ -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);

View file

@ -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

24
main_bounce.cpp Normal file
View file

@ -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;
}

24
periodic_updates.cpp Normal file
View file

@ -0,0 +1,24 @@
#include "periodic_updates.hpp"
#include <ctime>
#include <GL/glut.h>
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();
}

6
periodic_updates.hpp Normal file
View file

@ -0,0 +1,6 @@
#pragma once
#include "Ball.hpp"
void init_periodic_static(Ball* ball);
void periodic_update();

View file

@ -1,4 +1,5 @@
#include "GlutRender.hpp"
#include "../MarchingCubes.hpp"
#include <GL/gl.h>
#include <GL/glut.h>
@ -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<double> 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<Point>& 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<double> 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<Point>& 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();

View file

@ -3,6 +3,7 @@
#pragma once
#include "../Mesh.hpp"
#include "../Implicit.hpp"
#include <set>
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<double()> rand_color;
std::set<Mesh*> meshes;
std::set<SurfaceDetails> surfaces;
};

View file

@ -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();
}

View file

@ -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:
/**