-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBox.h
More file actions
65 lines (55 loc) · 1.98 KB
/
Box.h
File metadata and controls
65 lines (55 loc) · 1.98 KB
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
#ifndef BOX_H
#define BOX_H
#include "Geometry.h"
#include "GeometryUtil.h"
#include "Util.h"
#include "Material.h"
#include "glm/glm.hpp"
#include <cfloat>
class Box : public Geometry {
public:
__device__ Box(const glm::vec3 p1, const glm::vec3 p2,
const Material &mat, const glm::mat4 trans,
const glm::mat4 & invTrans) :
Geometry(mat, trans, invTrans) {
minP = getSmallestBoxCorner(p1, p2);
maxP = getBiggestBoxCorner(p1, p2);
}
__device__ virtual BoundingBox getBoundingBox() const {
return generateBoundingBox(minP, maxP, trans);
}
__device__ virtual glm::vec2 UVAt(const Ray &r, float param) const {
printf("Boxes do not currently support textures\n");
return glm::vec2(0.0f);
}
__device__ virtual glm::vec3 getCenter() const {
glm::vec3 c = (minP + maxP) / 2.0f;
glm::vec4 objSpaceCenter(c.x, c.y, c.z, 1.0f);
return glm::vec3(trans * objSpaceCenter);
}
private:
__device__ virtual float intersects(const Ray &ray) const {
return boxIntersect(ray, minP, maxP);
}
// Precondition: The given position is on the box
__device__ virtual glm::vec3 getNormalAt(const Ray &ray, float param) const {
glm::vec3 intersectPoint = ray.transform(invTrans).getPoint(param);
glm::vec3 norm;
if (isFloatEqual(intersectPoint.x, minP.x)) {
norm = glm::vec3(-1.0f, 0.0f, 0.0f);
} else if (isFloatEqual(intersectPoint.x, maxP.x)) {
norm = glm::vec3(1.0f, 0.0f, 0.0f);
} else if (isFloatEqual(intersectPoint.y, minP.y)) {
norm = glm::vec3(0.0f, -1.0f, 0.0f);
} else if (isFloatEqual(intersectPoint.y, maxP.y)) {
norm = glm::vec3(0.0f, 1.0f, 0.0f);
} else if (isFloatEqual(intersectPoint.z, minP.z)) {
norm = glm::vec3(0.0f, 0.0f, -1.0f);
} else {
norm = glm::vec3(0.0f, 0.0f, 1.0f);
}
return glm::vec3(trans * glm::vec4(norm.x, norm.y, norm.z, 0.0f));
}
glm::vec3 minP, maxP;
};
#endif //BOX_H