First observed bounce
This commit is contained in:
parent
48ef986f4f
commit
a1cc442885
3
Ball.cpp
3
Ball.cpp
|
@ -2,7 +2,8 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cmath>
|
#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),
|
Center(_center),
|
||||||
surface(_center, _p, _q),
|
surface(_center, _p, _q),
|
||||||
init_h(_center.z),
|
init_h(_center.z),
|
||||||
|
|
4
Ball.hpp
4
Ball.hpp
|
@ -32,11 +32,13 @@ class Ball {
|
||||||
void _compute_U_n();
|
void _compute_U_n();
|
||||||
void _compute_T_n();
|
void _compute_T_n();
|
||||||
public:
|
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);
|
void update_pos(double dt);
|
||||||
Point getCenter() const {return Center;}
|
Point getCenter() const {return Center;}
|
||||||
double accessT() const { return T;}
|
double accessT() const { return T;}
|
||||||
double access_crt_time() const { return crt_time;}
|
double access_crt_time() const { return crt_time;}
|
||||||
|
Spheroid* get_surface() { return &surface; }
|
||||||
};
|
};
|
||||||
|
|
||||||
std::ostream& operator << (std::ostream &out, Ball const& data);
|
std::ostream& operator << (std::ostream &out, Ball const& data);
|
||||||
|
|
1
Makefile
1
Makefile
|
@ -14,6 +14,7 @@ OBJS=Implicit.o \
|
||||||
util/ObjParser.o \
|
util/ObjParser.o \
|
||||||
MarchingCubes.o \
|
MarchingCubes.o \
|
||||||
_gen/marching_cubes_data.o \
|
_gen/marching_cubes_data.o \
|
||||||
|
periodic_updates.o \
|
||||||
tests/TestImplicitSphere.o \
|
tests/TestImplicitSphere.o \
|
||||||
render/GlutRender.o
|
render/GlutRender.o
|
||||||
|
|
||||||
|
|
24
main_bounce.cpp
Normal file
24
main_bounce.cpp
Normal 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
24
periodic_updates.cpp
Normal 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
6
periodic_updates.hpp
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Ball.hpp"
|
||||||
|
|
||||||
|
void init_periodic_static(Ball* ball);
|
||||||
|
void periodic_update();
|
|
@ -1,4 +1,5 @@
|
||||||
#include "GlutRender.hpp"
|
#include "GlutRender.hpp"
|
||||||
|
#include "../MarchingCubes.hpp"
|
||||||
|
|
||||||
#include <GL/gl.h>
|
#include <GL/gl.h>
|
||||||
#include <GL/glut.h>
|
#include <GL/glut.h>
|
||||||
|
@ -11,7 +12,11 @@ GlutRender& GlutRender::get_instance() {
|
||||||
return 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,
|
void GlutRender::init(int* argc, char** argv,
|
||||||
int wid, int hei, const char* win_name)
|
int wid, int hei, const char* win_name)
|
||||||
|
@ -44,10 +49,22 @@ void GlutRender::remove_mesh(Mesh* mesh) {
|
||||||
meshes.erase(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() {
|
void GlutRender::run() {
|
||||||
glutMainLoop();
|
glutMainLoop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GlutRender::set_idle_func(void (*func)(void)) {
|
||||||
|
glutIdleFunc(func);
|
||||||
|
}
|
||||||
|
|
||||||
void GlutRender::reshape(int wid, int hei) {
|
void GlutRender::reshape(int wid, int hei) {
|
||||||
if (hei == 0)
|
if (hei == 0)
|
||||||
hei = 1;
|
hei = 1;
|
||||||
|
@ -61,34 +78,42 @@ void GlutRender::reshape(int wid, int hei) {
|
||||||
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
|
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() {
|
void GlutRender::display() {
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
|
||||||
// Camera position and orientation
|
// Camera position and orientation
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glTranslatef(0., 0., -6.);
|
//glTranslatef(1., 2., -10.);
|
||||||
|
gluLookAt(0, 0, -10,
|
||||||
std::default_random_engine rand_engine(time(NULL));
|
0, 0, 10,
|
||||||
std::uniform_real_distribution<double> distribution;
|
0, 1, 0);
|
||||||
auto rand_color = std::bind(distribution, rand_engine);
|
|
||||||
|
|
||||||
for(Mesh* mesh: meshes) {
|
for(Mesh* mesh: meshes) {
|
||||||
const Point& mesh_center = mesh->get_center();
|
display_mesh(*mesh);
|
||||||
|
}
|
||||||
|
|
||||||
const std::vector<Point>& points = mesh->get_vertices();
|
for(const SurfaceDetails& surface: surfaces) {
|
||||||
|
Mesh mesh = MarchingCubes(*surface.surface, surface.box)();
|
||||||
glBegin(GL_TRIANGLES);
|
display_mesh(mesh);
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glutSwapBuffers();
|
glutSwapBuffers();
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "../Mesh.hpp"
|
#include "../Mesh.hpp"
|
||||||
|
#include "../Implicit.hpp"
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
class GlutRender {
|
class GlutRender {
|
||||||
|
@ -17,11 +18,17 @@ class GlutRender {
|
||||||
void cleanup();
|
void cleanup();
|
||||||
void add_mesh(Mesh* mesh);
|
void add_mesh(Mesh* mesh);
|
||||||
void remove_mesh(Mesh* mesh);
|
void remove_mesh(Mesh* mesh);
|
||||||
|
void add_surface(ImplicitSurface* surf, const Cuboid& box);
|
||||||
|
void remove_surface(ImplicitSurface* surf);
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
|
void set_idle_func(void (*func)(void));
|
||||||
|
|
||||||
private: //meth
|
private: //meth
|
||||||
GlutRender();
|
GlutRender();
|
||||||
|
|
||||||
|
void display_mesh(const Mesh& mesh) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void reshape(int wid, int hei);
|
void reshape(int wid, int hei);
|
||||||
void display();
|
void display();
|
||||||
|
@ -29,5 +36,20 @@ class GlutRender {
|
||||||
static void reshape_handle(int wid, int hei);
|
static void reshape_handle(int wid, int hei);
|
||||||
static void display_handle();
|
static void display_handle();
|
||||||
private:
|
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<Mesh*> meshes;
|
||||||
|
std::set<SurfaceDetails> surfaces;
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
#include "spheroid.hpp"
|
#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) {
|
ImplicitSurface(_center), init_p(_p), p(_p), q(_q), stiffness(0) {
|
||||||
_compute_volume();
|
_compute_volume();
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ const double PI = 3.141592653589793;
|
||||||
|
|
||||||
class Spheroid : public ImplicitSurface {
|
class Spheroid : public ImplicitSurface {
|
||||||
public:
|
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_center_pos(Point& _center);
|
||||||
void update_radius();
|
void update_radius();
|
||||||
void update_height();
|
void update_height();
|
||||||
|
@ -20,9 +20,9 @@ class Spheroid : public ImplicitSurface {
|
||||||
void check_horizontal_plan_collision(double& height);
|
void check_horizontal_plan_collision(double& height);
|
||||||
void check_vertical_plan_collision(double& abscissa);
|
void check_vertical_plan_collision(double& abscissa);
|
||||||
double operator() (double _x, double _y, double _z) const {
|
double operator() (double _x, double _y, double _z) const {
|
||||||
return (pow(_x - center.x, 2) / pow(q, 2)
|
return (pow(_x, 2) / pow(q, 2)
|
||||||
+ pow(_y - center.y, 2) / pow(q, 2)
|
+ pow(_y, 2) / pow(q, 2)
|
||||||
+ pow(_z - center.z, 2) / pow(p, 2) -1);
|
+ pow(_z, 2) / pow(p, 2) -1);
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue