cloth
view src/plane.cc @ 3:28a31079dcdf
disc
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 04 Jan 2016 10:52:15 +0200 |
parents | 2eac424f58b2 |
children |
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 : 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->dist = t;
38 col->pos = ray.origin + ray.dir * t;
39 col->normal = normal;
40 col->elast = 1.0;
41 return true;
42 }
44 Matrix4x4 Plane::calc_rot_matrix() const
45 {
46 Vector3 up = Vector3(0, 1, 0);
47 if(fabs(dot_product(up, normal)) - 1.0 <= 1e-5) {
48 up = Vector3(0, 0, 1);
49 }
51 Vector3 right = cross_product(up, normal).normalized();
52 up = cross_product(normal, right).normalized();
54 Matrix4x4 rot_matrix;
55 rot_matrix.set_column_vector(right, 0);
56 rot_matrix.set_column_vector(up, 1);
57 rot_matrix.set_column_vector(normal, 2);
58 rot_matrix.set_row_vector(Vector4(0, 0, 0, 1), 3);
59 return rot_matrix;
60 }
62 void Plane::draw() const
63 {
64 const float sz = 10.0f;
66 Matrix4x4 rot_matrix = calc_rot_matrix();
68 glMatrixMode(GL_MODELVIEW);
69 glPushMatrix();
70 glMultTransposeMatrixf(rot_matrix[0]);
72 glBegin(GL_QUADS);
73 glNormal3f(normal.x, normal.y, normal.z);
74 glVertex2f(-sz, -sz);
75 glVertex2f(sz, -sz);
76 glVertex2f(sz, sz);
77 glVertex2f(-sz, sz);
78 glEnd();
80 glPopMatrix();
81 }