-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplane.cpp
70 lines (53 loc) · 1.92 KB
/
plane.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "platform.h"
//FIXME: Store origin?!
class Plane {
private:
glm::vec3 normal;
glm::vec3 origin;
public:
//FIXME: Normalize normal!
Plane(glm::vec3 normal, glm::vec3 origin) : normal(normal), origin(origin) {}
Plane(glm::vec3 normal, float distance = 0.0f) : normal(normal) {
origin = normal * distance;
}
glm::vec3 getOrigin() const {
return origin;
}
glm::vec3 getNormal() const {
return normal;
}
glm::vec3 project(glm::vec3 point, float distance = 0.0f) const {
return point + (getDistance(point) - distance) * normal;
}
float getDistance(glm::vec3 point = glm::vec3(0.0f, 0.0f, 0.0f)) const {
return dot(normal, point - origin);
}
// Gets position on ray
float collideRay(glm::vec3 origin, glm::vec3 direction) const {
float projection = dot(direction, normal);
return -getDistance(origin) / projection;
}
bool inFront(glm::vec3 point) const {
return getDistance(point) > 0.0f;
}
//FIXME: Maybe just draw this more stupidly [axis aligned] and instead use a matrix?!
//FIXME!!
void draw(float gap = 1.0f, unsigned int segmentsX = 3, unsigned int segmentsY = 3, glm::vec3 direction = glm::vec3(1.0f, 0.0f, 0.0f)) {
//FIXME: Project direction onto plane
glm::vec3 directionOnPlane = project(origin + direction) - origin;
directionOnPlane = glm::normalize(directionOnPlane);
glm::vec3 directionOnPlaneY = glm::normalize(glm::cross(directionOnPlane, normal));
float halfWidth = gap * segmentsX / 2.0f;
glm::vec3 lineOrigin = origin - directionOnPlaneY * halfWidth;
for(unsigned int i = 0; i <= segmentsX; i++) {
glm::vec3 a = lineOrigin - directionOnPlane * halfWidth;
glm::vec3 b = lineOrigin + directionOnPlane * halfWidth;
lineOrigin += directionOnPlaneY * gap;
Platform::drawLine(a, b);
}
// Draw the second axis
if (segmentsY > 1) {
draw(gap, segmentsY, 1, directionOnPlaneY);
}
}
};