-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.cpp
More file actions
237 lines (186 loc) · 5.47 KB
/
object.cpp
File metadata and controls
237 lines (186 loc) · 5.47 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <cmath>
#include "object.h"
float Object::intersect(IntersectionInfo* info, Ray ray)
{
printf("just an object... no intersection");
}
int Object::getTextureColor(IntersectionInfo *info)
{
return 0;
}
Object::Object()
{
//printf("constructed obj");
}
Object::~Object()
{
}
Sphere::Sphere(float x, float y, float z, float radius)
{
center[X_AXIS] = x;
center[Y_AXIS] = y;
center[Z_AXIS] = z;
this->radius = radius;
}
int Sphere::getTextureColor(IntersectionInfo* info)
{
if((int)this->texture == 0)
{
return 0;
}
return 0;
}
float Sphere::intersect(IntersectionInfo* info, Ray ray)
{
info->mat.color = {0,0,0};
float dx = ray.direction[X_AXIS];
float dy = ray.direction[Y_AXIS];
float dz = ray.direction[Z_AXIS];
float xo = ray.origin[X_AXIS];
float yo = ray.origin[Y_AXIS];
float zo = ray.origin[Z_AXIS];
float A = pow(dx, 2) + pow(dy, 2) + pow(dz, 2);
if(A < 0.99 || A > 1.01)
{
printf("ray was not normalized");
}
float B = 2* ( dx*(xo - center[X_AXIS]) + dy*(yo-center[Y_AXIS]) + dz*(zo - center[Z_AXIS]));
float C = pow((xo - center[X_AXIS]), 2) + pow(yo - center[Y_AXIS], 2) + pow(zo - center[Z_AXIS], 2) - pow(radius, 2);
float root = pow(B, 2) - 4*C;
if(root < 0)
{
return -1;
}
//calculate root
info->mat = this->mat;
info->mat.color = this->mat.color;
float posDist = (-B+sqrt(root))/2;
float negDist = (-B-sqrt(root))/2;
//use smallest dist
float dist = posDist > negDist ? negDist : posDist;
//calculate intersection point and store
info->intersectionLocation[X_AXIS] = ray.origin[X_AXIS] + ray.direction[X_AXIS]*dist;
info->intersectionLocation[Y_AXIS] = ray.origin[Y_AXIS] + ray.direction[Y_AXIS]*dist;
info->intersectionLocation[Z_AXIS] = ray.origin[Z_AXIS] + ray.direction[Z_AXIS]*dist;
//calculate normal and store
float normal [3] = {};
for(int i = 0; i < 3; i++)
{
normal[i] = info->intersectionLocation[i] - center[i];
}
//normalize normal
float normalMag = sqrt(pow(normal[X_AXIS], 2) + pow(normal[Y_AXIS], 2) + pow(normal[Z_AXIS], 2));
for(int i = 0; i < 3; i++)
{
float x = normal[i]/normalMag;
normal[i] = normal[i]/normalMag;
}
std::copy(std::begin(normal), std::end(normal), info->normal);
info->id = this->id;
return dist;
}
float Triangle::intersect(IntersectionInfo* info, Ray ray)
{
constexpr float epsilon = std::numeric_limits<float>::epsilon();
info->mat.color = {255,0,0};
float edge1 [3] = {0, 0, 0};
float edge2 [3] = {0, 0, 0};
float T [3] = {0,0,0};
float P [3] = {0,0,0};
float Q [3] = {0,0,0};
for(int i = 0; i < 3; i++)
{
edge1[i] = point1[i] - point0[i];
edge2[i] = point2[i] - point0[i];
T[i] = ray.origin[i] - point0[i];
}
//calculate normal
crossProduct(edge1, edge2, info->normal);
//normalize normal
float normalMag = sqrt(pow(info->normal[X_AXIS], 2) + pow(info->normal[Y_AXIS], 2) + pow(info->normal[Z_AXIS], 2));
for(int i = 0; i < 3; i++)
{
info->normal[i] = info->normal[i]/normalMag;
}
// P = cross product D x e2
crossProduct(ray.direction, edge2, P);
//cross product T and e1
crossProduct(T, edge1, Q);
//denominator
float denominator = dotProduct(edge1, P);
if(denominator > -epsilon && denominator < epsilon)
{
return -1; //parallel to triangle
}
//this causes backface culling
if (denominator < epsilon)
{
return -1;
}
float w = dotProduct(Q, edge2)/denominator;
float u = dotProduct(P,T)/denominator;
float v = dotProduct(Q, ray.direction)/denominator;
if(u < 0 || v < 0 || u + v > 1 || u > 1)
{
return -1;
}
info->mat = this->mat;
info->mat.color = this->mat.color;
//calculate intersection point and store
info->intersectionLocation[X_AXIS] = ray.origin[X_AXIS] + ray.direction[X_AXIS]*w;
info->intersectionLocation[Y_AXIS] = ray.origin[Y_AXIS] + ray.direction[Y_AXIS]*w;
info->intersectionLocation[Z_AXIS] = ray.origin[Z_AXIS] + ray.direction[Z_AXIS]*w;
info->id = this->id;
return w;
}
int Triangle::getTextureColor(IntersectionInfo *info)
{
if((int)this->texture == 0)
{
//if texture is none, return base color
return 0;
}
//TODO: add rotation to get flattened coords first
//note: in order for two checkerboards to align, their p0s need to be at the same z
// this is because checker size is not fitted to z length, so can overflow (board can have 2.5 sq in y direction)
if(this->texture == TextureEnum::CHECKER)
{
//get checker color from position
//checker assumes that 0 is the 90 degree corner of the triangle
//we'll set the checker squares to be n*n size, where n is x width/3 on p1->p2
float checkerWidth = 0;
if(point0[X_AXIS] != point1[X_AXIS]){
checkerWidth = fabs(point0[X_AXIS] - point1[X_AXIS]);
}
else{
checkerWidth = fabs(point0[X_AXIS] - point2[X_AXIS]);
}
checkerWidth = checkerWidth/15;
//check u,v position against width, where 0,0 in local coords is point0
//0,0 starts with a red square
float x = info->intersectionLocation[X_AXIS];
float z = info->intersectionLocation[Z_AXIS];
//if difference between x and point0[x] / checker width % 2 == 0 (even), red
int xMod = (int)std::floor(std::fabs(x - point0[X_AXIS])/checkerWidth)%2;
int zMod = (int)std::floor(std::fabs(z - point0[Z_AXIS])/checkerWidth)%2;
if(xMod == zMod)
{
info->mat.color = {100, 200, 100};
}
else{
info->mat.color = {50, 150, 50};
}
}
return 0;
}
Light::Light(float x, float y, float z, float radius)
{
center[X_AXIS] = x;
center[Y_AXIS] = y;
center[Z_AXIS] = z;
this->radius = radius;
}
float Light::intersect(IntersectionInfo* info, Ray ray)
{
return -1;
}