-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShapes.h
168 lines (141 loc) · 3.08 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
#ifndef SHAPES_H
#define SHAPES_H
#include <math.h>
#include <memory.h>
//#include <assert.h>
#include <algorithm>
typedef unsigned char BYTE;
struct Vec
{
float x, y, z;
Vec() {};
Vec(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {};
float Dot(const Vec& v2) { return (x * v2.x) + (y * v2.y) + (z * v2.z); }
Vec operator-(const Vec& v)
{
return Vec(this->x - v.x, this->y - v.y, this->z - v.z);
}
Vec operator+(const Vec& v)
{
return Vec(this->x + v.x, this->y + v.y, this->z + v.z);
}
Vec operator*(const float& f)
{
return Vec(this->x * f, this->y * f, this->z * f);
}
// useful when comparing lengths! (faster)
inline float Length2()
{
return (x*x + y*y + z*z);
}
float Length()
{
return sqrt(Length2());
}
void Normalize()
{
float l = Length();
x /= l;
y /= l;
z /= l;
}
};
struct Ray
{
Vec o;
Vec d;
Ray(Vec origin, Vec direction) : o(origin), d(direction) { };
};
struct Color
{
BYTE r;
BYTE g;
BYTE b;
Color() { memset(&r, 0, 3); }
Color(BYTE red, BYTE green, BYTE blue) : r(red), g(green), b(blue) {};
const Color& operator+(const Color& plus) {
unsigned int rr, gg, bb;
rr = plus.r + r;
if (rr > 255)
rr = 255;
gg = plus.g + g;
if (gg > 255)
gg= 255;
bb = plus.b + b;
if (bb > 255)
bb = 255;
return Color(rr,gg,bb);
}
};
class Shape;
struct HitData
{
float t;
Color color;
Shape* lastShape = nullptr;
Vec lastNormal;
// hit data is -1 (not hit) and black (default color for background)
HitData() : t(-1.0f), color(0, 0, 0) { };
};
class Shape
{
public:
Color c;
// each subclass, has to implement these two methods!
virtual void test(Ray& ray, HitData& hit) = 0;
virtual Vec normal(Vec &point) = 0;
// Light with (0.5,0.5,0.5) intensity for ambient, diffuse and specular
// This method is the same for all clases, so implement only for Shape
virtual Color shade(Vec& light, const Vec& cam, Ray& r, HitData& h);
};
// for each class, add the necessary data that describes the shape,
// implement a constructor, and implement the test function
// EACH CLASS SHOULD INHERIT FROM SHAPE,
class MPlane : public Shape
{
Vec n;
float d;
public:
void test(Ray& ray, HitData& hit);
Vec normal(Vec &point);
MPlane(Vec normal, float _d, Color color);
};
class MSphere : public Shape
{
Vec center;
float radius;
float radius2;
public:
void test(Ray& ray, HitData& hit);
Vec normal(Vec &point);
MSphere(Vec _center, float _radius, Color _color);
};
class MTriangle : public Shape
{
Vec p1, p2, p3, nor;
Vec edge0, edge1;
public:
void test(Ray& ray, HitData& hit);
Vec normal(Vec &point) { return nor; }
MTriangle(Vec _p1, Vec _p2, Vec _p3, Color _color);
};
class MOBB : public Shape
{
public:
Vec Bcenter;
Vec Bu;
Vec Bv;
Vec Bw;
Vec Pu,Puo;
Vec Pv,Pvo;
Vec Pw,Pwo;
float halfBu;
float halfBv;
float halfBw;
void test(Ray& ray, HitData& hit);
Vec normal(Vec& point);
// Center point, lenght U vector, length V vector, length W vector, color
MOBB(Vec b, Vec b1, Vec b2, Vec b3, float Hu, float Hv, float Hw, Color _color);
MOBB(Vec b, float Hu, float Hv, float Hw, Color _color);
};
#endif