# HG changeset patch # User John Tsiombikas # Date 1451897535 -7200 # Node ID 28a31079dcdfe08f2e2d019a503815d1cdee2442 # Parent 2eac424f58b2e63b7ba65aa7d10d52f7ef35ce97 disc diff -r 2eac424f58b2 -r 28a31079dcdf src/disc.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/disc.cc Mon Jan 04 10:52:15 2016 +0200 @@ -0,0 +1,51 @@ +#include "disc.h" +#include "opengl.h" +#include "simworld.h" + +Disc::Disc() +{ + radius = 1.0f; +} + +Disc::Disc(const Vector3 &pt, const Vector3 &norm, float rad) + : Plane(pt, norm) +{ + radius = rad; +} + +bool Disc::collision(const Ray &ray, float rad, Collision *col) const +{ + if(!Plane::collision(ray, rad, col)) { + return false; + } + Vector3 pt = this->pt - normal * rad; + float dist_sq = (col->pos - pt).length_sq(); + if(dist_sq > radius * radius) { + return false; + } + return true; +} + +void Disc::draw() const +{ + Matrix4x4 rot_matrix = calc_rot_matrix(); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glMultTransposeMatrixf(rot_matrix[0]); + + glBegin(GL_TRIANGLE_FAN); + glNormal3f(normal.x, normal.y, normal.z); + glVertex2f(0, 0); + const int segm = 42; + for(int i=0; i #include +#include #include "opengl.h" +#include "plane.h" +#include "disc.h" +#include "particle.h" +#include "simworld.h" static bool init(); static void cleanup(); @@ -15,6 +20,8 @@ static bool moving_cloth; +static SimWorld simworld; + int main(int argc, char **argv) { @@ -49,6 +56,10 @@ glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); + Disc *disc = new Disc; + disc->normal = Vector3(0, 1, 0); + simworld.add_object(disc); + return true; } @@ -68,7 +79,10 @@ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - glutSolidTeapot(1.0); + for(int i=0; idraw(); + } glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); diff -r 2eac424f58b2 -r 28a31079dcdf src/object.h --- a/src/object.h Tue Feb 12 00:23:40 2013 +0200 +++ b/src/object.h Mon Jan 04 10:52:15 2016 +0200 @@ -8,6 +8,7 @@ virtual ~Object() = default; virtual bool collision(const Ray &ray, float rad, Collision *col) const = 0; + virtual void draw() const = 0; }; #endif // OBJECT_H_ diff -r 2eac424f58b2 -r 28a31079dcdf src/plane.cc --- a/src/plane.cc Tue Feb 12 00:23:40 2013 +0200 +++ b/src/plane.cc Mon Jan 04 10:52:15 2016 +0200 @@ -41,25 +41,33 @@ return true; } -void Plane::draw(float sz) const +Matrix4x4 Plane::calc_rot_matrix() const { Vector3 up = Vector3(0, 1, 0); - if(fabs(dot_product(up, normal)) < 1e-6) { + if(fabs(dot_product(up, normal)) - 1.0 <= 1e-5) { up = Vector3(0, 0, 1); } - Vector3 right = cross_product(up, normal); - up = cross_product(normal, right); + Vector3 right = cross_product(up, normal).normalized(); + up = cross_product(normal, right).normalized(); Matrix4x4 rot_matrix; rot_matrix.set_column_vector(right, 0); rot_matrix.set_column_vector(up, 1); rot_matrix.set_column_vector(normal, 2); rot_matrix.set_row_vector(Vector4(0, 0, 0, 1), 3); + return rot_matrix; +} + +void Plane::draw() const +{ + const float sz = 10.0f; + + Matrix4x4 rot_matrix = calc_rot_matrix(); glMatrixMode(GL_MODELVIEW); glPushMatrix(); - glMultTransposeMatrixf((float*)rot_matrix.m); + glMultTransposeMatrixf(rot_matrix[0]); glBegin(GL_QUADS); glNormal3f(normal.x, normal.y, normal.z); diff -r 2eac424f58b2 -r 28a31079dcdf src/plane.h --- a/src/plane.h Tue Feb 12 00:23:40 2013 +0200 +++ b/src/plane.h Mon Jan 04 10:52:15 2016 +0200 @@ -2,10 +2,14 @@ #define PLANE_H_ #include +#include "object.h" struct Collision; -class Plane { +class Plane : public Object { +protected: + Matrix4x4 calc_rot_matrix() const; + public: Vector3 pt; Vector3 normal; @@ -16,7 +20,7 @@ bool collision(const Ray &ray, float rad, Collision *col) const; - void draw(float sz) const; + void draw() const; }; #endif // PLANE_H_ diff -r 2eac424f58b2 -r 28a31079dcdf src/simworld.cc --- a/src/simworld.cc Tue Feb 12 00:23:40 2013 +0200 +++ b/src/simworld.cc Mon Jan 04 10:52:15 2016 +0200 @@ -7,6 +7,21 @@ damping = 0.99; } +void SimWorld::add_object(Object *o) +{ + objects.push_back(o); +} + +int SimWorld::get_object_count() const +{ + return (int)objects.size(); +} + +Object *SimWorld::get_object(int idx) const +{ + return objects[idx]; +} + void SimWorld::add_particle(Particle *p) { part.push_back(p); diff -r 2eac424f58b2 -r 28a31079dcdf src/simworld.h --- a/src/simworld.h Tue Feb 12 00:23:40 2013 +0200 +++ b/src/simworld.h Mon Jan 04 10:52:15 2016 +0200 @@ -25,6 +25,10 @@ void set_gravity(const Vector3 &f); + void add_object(Object *o); + int get_object_count() const; + Object *get_object(int idx) const; + void add_particle(Particle *p); bool collision(const Ray &ray, float rad, Collision *col) const;