cloth

changeset 3:28a31079dcdf tip

disc
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 04 Jan 2016 10:52:15 +0200
parents 2eac424f58b2
children
files src/disc.cc src/disc.h src/main.cc src/object.h src/plane.cc src/plane.h src/simworld.cc src/simworld.h
diffstat 8 files changed, 122 insertions(+), 8 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/disc.cc	Mon Jan 04 10:52:15 2016 +0200
     1.3 @@ -0,0 +1,51 @@
     1.4 +#include "disc.h"
     1.5 +#include "opengl.h"
     1.6 +#include "simworld.h"
     1.7 +
     1.8 +Disc::Disc()
     1.9 +{
    1.10 +	radius = 1.0f;
    1.11 +}
    1.12 +
    1.13 +Disc::Disc(const Vector3 &pt, const Vector3 &norm, float rad)
    1.14 +	: Plane(pt, norm)
    1.15 +{
    1.16 +	radius = rad;
    1.17 +}
    1.18 +
    1.19 +bool Disc::collision(const Ray &ray, float rad, Collision *col) const
    1.20 +{
    1.21 +	if(!Plane::collision(ray, rad, col)) {
    1.22 +		return false;
    1.23 +	}
    1.24 +	Vector3 pt = this->pt - normal * rad;
    1.25 +	float dist_sq = (col->pos - pt).length_sq();
    1.26 +	if(dist_sq > radius * radius) {
    1.27 +		return false;
    1.28 +	}
    1.29 +	return true;
    1.30 +}
    1.31 +
    1.32 +void Disc::draw() const
    1.33 +{
    1.34 +	Matrix4x4 rot_matrix = calc_rot_matrix();
    1.35 +
    1.36 +	glMatrixMode(GL_MODELVIEW);
    1.37 +	glPushMatrix();
    1.38 +	glMultTransposeMatrixf(rot_matrix[0]);
    1.39 +
    1.40 +	glBegin(GL_TRIANGLE_FAN);
    1.41 +	glNormal3f(normal.x, normal.y, normal.z);
    1.42 +	glVertex2f(0, 0);
    1.43 +	const int segm = 42;
    1.44 +	for(int i=0; i<segm; i++) {
    1.45 +		float u = (float)i / (float)(segm - 1);
    1.46 +		float theta = u * M_PI * 2.0;
    1.47 +		float x = cos(theta) * radius;
    1.48 +		float y = sin(theta) * radius;
    1.49 +		glVertex2f(x, y);
    1.50 +	}
    1.51 +	glEnd();
    1.52 +
    1.53 +	glPopMatrix();
    1.54 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/disc.h	Mon Jan 04 10:52:15 2016 +0200
     2.3 @@ -0,0 +1,17 @@
     2.4 +#ifndef DISC_H_
     2.5 +#define DISC_H_
     2.6 +
     2.7 +#include "plane.h"
     2.8 +
     2.9 +class Disc : public Plane {
    2.10 +public:
    2.11 +	float radius;
    2.12 +
    2.13 +	Disc();
    2.14 +	Disc(const Vector3 &pt, const Vector3 &norm, float rad);
    2.15 +
    2.16 +	bool collision(const Ray &ray, float rad, Collision *col) const;
    2.17 +	void draw() const;
    2.18 +};
    2.19 +
    2.20 +#endif	/* DISC_H_ */
     3.1 --- a/src/main.cc	Tue Feb 12 00:23:40 2013 +0200
     3.2 +++ b/src/main.cc	Mon Jan 04 10:52:15 2016 +0200
     3.3 @@ -1,6 +1,11 @@
     3.4  #include <stdio.h>
     3.5  #include <stdlib.h>
     3.6 +#include <vector>
     3.7  #include "opengl.h"
     3.8 +#include "plane.h"
     3.9 +#include "disc.h"
    3.10 +#include "particle.h"
    3.11 +#include "simworld.h"
    3.12  
    3.13  static bool init();
    3.14  static void cleanup();
    3.15 @@ -15,6 +20,8 @@
    3.16  
    3.17  static bool moving_cloth;
    3.18  
    3.19 +static SimWorld simworld;
    3.20 +
    3.21  
    3.22  int main(int argc, char **argv)
    3.23  {
    3.24 @@ -49,6 +56,10 @@
    3.25  	glEnable(GL_DEPTH_TEST);
    3.26  	glEnable(GL_CULL_FACE);
    3.27  
    3.28 +	Disc *disc = new Disc;
    3.29 +	disc->normal = Vector3(0, 1, 0);
    3.30 +	simworld.add_object(disc);
    3.31 +
    3.32  	return true;
    3.33  }
    3.34  
    3.35 @@ -68,7 +79,10 @@
    3.36  
    3.37  	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    3.38  
    3.39 -	glutSolidTeapot(1.0);
    3.40 +	for(int i=0; i<simworld.get_object_count(); i++) {
    3.41 +		Object *obj = simworld.get_object(i);
    3.42 +		obj->draw();
    3.43 +	}
    3.44  
    3.45  	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    3.46  
     4.1 --- a/src/object.h	Tue Feb 12 00:23:40 2013 +0200
     4.2 +++ b/src/object.h	Mon Jan 04 10:52:15 2016 +0200
     4.3 @@ -8,6 +8,7 @@
     4.4  	virtual ~Object() = default;
     4.5  
     4.6  	virtual bool collision(const Ray &ray, float rad, Collision *col) const = 0;
     4.7 +	virtual void draw() const = 0;
     4.8  };
     4.9  
    4.10  #endif	// OBJECT_H_
     5.1 --- a/src/plane.cc	Tue Feb 12 00:23:40 2013 +0200
     5.2 +++ b/src/plane.cc	Mon Jan 04 10:52:15 2016 +0200
     5.3 @@ -41,25 +41,33 @@
     5.4  	return true;
     5.5  }
     5.6  
     5.7 -void Plane::draw(float sz) const
     5.8 +Matrix4x4 Plane::calc_rot_matrix() const
     5.9  {
    5.10  	Vector3 up = Vector3(0, 1, 0);
    5.11 -	if(fabs(dot_product(up, normal)) < 1e-6) {
    5.12 +	if(fabs(dot_product(up, normal)) - 1.0 <= 1e-5) {
    5.13  		up = Vector3(0, 0, 1);
    5.14  	}
    5.15  
    5.16 -	Vector3 right = cross_product(up, normal);
    5.17 -	up = cross_product(normal, right);
    5.18 +	Vector3 right = cross_product(up, normal).normalized();
    5.19 +	up = cross_product(normal, right).normalized();
    5.20  
    5.21  	Matrix4x4 rot_matrix;
    5.22  	rot_matrix.set_column_vector(right, 0);
    5.23  	rot_matrix.set_column_vector(up, 1);
    5.24  	rot_matrix.set_column_vector(normal, 2);
    5.25  	rot_matrix.set_row_vector(Vector4(0, 0, 0, 1), 3);
    5.26 +	return rot_matrix;
    5.27 +}
    5.28 +
    5.29 +void Plane::draw() const
    5.30 +{
    5.31 +	const float sz = 10.0f;
    5.32 +
    5.33 +	Matrix4x4 rot_matrix = calc_rot_matrix();
    5.34  
    5.35  	glMatrixMode(GL_MODELVIEW);
    5.36  	glPushMatrix();
    5.37 -	glMultTransposeMatrixf((float*)rot_matrix.m);
    5.38 +	glMultTransposeMatrixf(rot_matrix[0]);
    5.39  
    5.40  	glBegin(GL_QUADS);
    5.41  	glNormal3f(normal.x, normal.y, normal.z);
     6.1 --- a/src/plane.h	Tue Feb 12 00:23:40 2013 +0200
     6.2 +++ b/src/plane.h	Mon Jan 04 10:52:15 2016 +0200
     6.3 @@ -2,10 +2,14 @@
     6.4  #define PLANE_H_
     6.5  
     6.6  #include <vmath/vmath.h>
     6.7 +#include "object.h"
     6.8  
     6.9  struct Collision;
    6.10  
    6.11 -class Plane {
    6.12 +class Plane : public Object {
    6.13 +protected:
    6.14 +	Matrix4x4 calc_rot_matrix() const;
    6.15 +
    6.16  public:
    6.17  	Vector3 pt;
    6.18  	Vector3 normal;
    6.19 @@ -16,7 +20,7 @@
    6.20  
    6.21  	bool collision(const Ray &ray, float rad, Collision *col) const;
    6.22  
    6.23 -	void draw(float sz) const;
    6.24 +	void draw() const;
    6.25  };
    6.26  
    6.27  #endif	// PLANE_H_
     7.1 --- a/src/simworld.cc	Tue Feb 12 00:23:40 2013 +0200
     7.2 +++ b/src/simworld.cc	Mon Jan 04 10:52:15 2016 +0200
     7.3 @@ -7,6 +7,21 @@
     7.4  	damping = 0.99;
     7.5  }
     7.6  
     7.7 +void SimWorld::add_object(Object *o)
     7.8 +{
     7.9 +	objects.push_back(o);
    7.10 +}
    7.11 +
    7.12 +int SimWorld::get_object_count() const
    7.13 +{
    7.14 +	return (int)objects.size();
    7.15 +}
    7.16 +
    7.17 +Object *SimWorld::get_object(int idx) const
    7.18 +{
    7.19 +	return objects[idx];
    7.20 +}
    7.21 +
    7.22  void SimWorld::add_particle(Particle *p)
    7.23  {
    7.24  	part.push_back(p);
     8.1 --- a/src/simworld.h	Tue Feb 12 00:23:40 2013 +0200
     8.2 +++ b/src/simworld.h	Mon Jan 04 10:52:15 2016 +0200
     8.3 @@ -25,6 +25,10 @@
     8.4  
     8.5  	void set_gravity(const Vector3 &f);
     8.6  
     8.7 +	void add_object(Object *o);
     8.8 +	int get_object_count() const;
     8.9 +	Object *get_object(int idx) const;
    8.10 +
    8.11  	void add_particle(Particle *p);
    8.12  
    8.13  	bool collision(const Ray &ray, float rad, Collision *col) const;