-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shapes.h
executable file
·168 lines (116 loc) · 3.27 KB
/
Shapes.h
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Shape.h
* RayTracer
*
* Shape classes
*
* Created by Lita Cho on 9/23/08.
*
*/
#ifndef SHAPESH
#define SHAPESH
//Forward declarations
class Ray;
#include "algebra3.h"
#include "Ray.h"
#include <vector>
using namespace std;
// Forward declarations
class BoundingBox;
typedef struct intersect_record_struct IntersectRecord;
class Shape {
public:
// Default Constructor
Shape();
// Default Destructor
virtual ~Shape();
// If object intersects with a ray, return true.
virtual bool intersect(Ray& ray, IntersectRecord* rec) = 0;
// Get the bounding box for this shape
virtual BoundingBox getBoundingBox() = 0;
// Get the texture coordinate for this shape given a point on the shape.
virtual vec2 getTextureCoordinate(const vec3& point) = 0;
};
class BoundingBox : public Shape {
public:
BoundingBox();
BoundingBox(const vec3& min, const vec3& max);
bool intersect(Ray& ray, IntersectRecord* rec);
BoundingBox getBoundingBox();
vec2 getTextureCoordinate(const vec3& point);
void transform(const mat4& transformMatrix);
double minCoordinate(int axis);
double maxCoordinate(int axis);
static BoundingBox combine(const BoundingBox& box1, const BoundingBox& box2);
private:
// The min point is going to be stored in bounds[0] and the max is stored
// at bounds[1].
vec3 bounds[2];
};
class Sphere : public Shape {
public:
Sphere(double radius, const vec3& center);
bool intersect(Ray& ray, IntersectRecord* rec);
BoundingBox getBoundingBox();
vec2 getTextureCoordinate(const vec3& point);
static Sphere unitSphere;
private:
double getDiscriminant(const vec3& origin, const vec3& direction);
void getNormal(Ray& ray, IntersectRecord* rec);
private:
vec3 center;
double radius;
};
class TransformedShape : public Shape {
public:
TransformedShape(Shape* shape, const mat4& transform);
bool intersect(Ray& ray, IntersectRecord* rec);
BoundingBox getBoundingBox();
vec2 getTextureCoordinate(const vec3& point);
protected:
vec3 transformNormal(const vec3& normal);
Shape* shape;
mat4 inverseTransform;
};
class Ellipsoid : public TransformedShape {
public:
Ellipsoid(const mat4& transform);
};
class Triangle : public Shape {
public:
Triangle (const vec3& a, const vec3& b, const vec3& c);
bool intersect(Ray& ray, IntersectRecord* rec);
BoundingBox getBoundingBox();
vec2 getTextureCoordinate(const vec3& point);
private:
void getNormal(Ray& ray, IntersectRecord* rec);
vec3 a;
vec3 b;
vec3 c;
mat3 barycentricFinder;
};
struct Mesh {
vector<vec3> vertices;
vector<vec3> normals;
vector<vec2> textures;
};
class MeshTriangle : public Shape {
public:
MeshTriangle(Mesh* mesh, int vertI[], int normI[], int texI[]);
virtual bool intersect(Ray& ray, IntersectRecord* rec);
BoundingBox getBoundingBox();
vec2 getTextureCoordinate(const vec3& point);
protected:
void getNormal(Ray& ray, IntersectRecord* rec);
Mesh* mesh;
int vertI[3];
int normI[3];
int texI[3];
mat3 barycentricFinder;
};
class WireframeTriangle : public MeshTriangle {
public:
WireframeTriangle(Mesh* mesh, int vertI[], int normI[], int texI[]);
bool intersect(Ray& ray, IntersectRecord* rec);
};
#endif