cloth

view src/plane.cc @ 0:92983e143a03

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 11 Feb 2013 19:40:36 +0200
parents
children 2eac424f58b2
line source
1 #include "opengl.h"
2 #include "plane.h"
3 #include "simworld.h"
5 Plane::Plane()
6 : pt(0, 0, 0), normal(0, 1, 0)
7 {
8 }
10 Plane::Plane(const Vector3 &pt, const Vector3 &norm)
11 this->pt(pt), normal(pt)
12 {
13 }
15 Plane::Plane(const Vector3 &norm, float dist)
16 : normal(norm)
17 {
18 pt = Vector3(0, 0, 0) - norm * dist;
19 }
21 bool Plane::collision(const Ray &ray, float rad, Collision *col) const
22 {
23 Vector3 pt = this->pt - normal * rad;
25 float ndotdir = dot_product(ray.dir, normal);
26 if(fabs(ndotdir) < 1e-6) {
27 return false;
28 }
30 float ndotptdir = dot_product((pt - ray.origin), normal);
32 float t = ndotptdir / ndotdir;
33 if(t < 1e-6 || t > (1.0 - 1e-6)) {
34 return false;
35 }
37 col->pos = ray.origin + ray.dir * t;
38 col->normal = normal;
39 col->elast = 1.0;
40 return true;
41 }
43 void Plane::draw(float sz) const
44 {
45 Vector3 up = Vector3(0, 1, 0);
46 if(fabs(dot_product(up, normal)) < 1e-6) {
47 up = Vector3(0, 0, 1);
48 }
50 Vector3 right = cross_product(up, normal);
51 up = cross_product(normal, right);
53 Matrix4x4 rot_matrix;
54 rot_matrix.set_column_vector(right, 0);
55 rot_matrix.set_column_vector(up, 1);
56 rot_matrix.set_column_vector(normal, 2);
57 rot_matrix.set_row_vector(Vector4(0, 0, 0, 1), 3);
59 glMatrixMode(GL_MODELVIEW);
60 glPushMatrix();
61 glMultTransposeMatrixf((float*)rot_matrix.m);
63 glBegin(GL_QUADS);
64 glNormal3f(normal.x, normal.y, normal.z);
65 glVertex2f(-sz, -sz);
66 glVertex2f(sz, -sz);
67 glVertex2f(sz, sz);
68 glVertex2f(-sz, sz);
69 glEnd();
71 glPopMatrix();
72 }