-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.cpp
More file actions
executable file
·324 lines (249 loc) · 9 KB
/
Copy pathobject.cpp
File metadata and controls
executable file
·324 lines (249 loc) · 9 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <cstdlib>
#include <cmath>
#include <cstdio>
// GLM lib for matrix calculation
#include "include/glm/glm.hpp"
#include "include/glm/gtc/matrix_transform.hpp"
#include "include/glm/gtc/type_ptr.hpp"
#include "object.h"
extern Object* scene;
ostream& operator << (ostream& os, glm::vec3& vec){
printf("(%.2f,%.2f,%.2f)",vec.x,vec.y,vec.z);
return os;
}
const float precision = 0.00001;
//---------------------------------------------------------------------------------
/**********************************************************************
* This function intersects a ray with a given sphere 'sph'.
* The function returns the distance from the eye to the intersection point,
* which will be compared with others to determine which intersection
* is closest. The value -1.0 is returned if there is no intersection
*
* If there is an intersection, the point of intersection will be
* stored in the "hit" variable
**********************************************************************/
float Sphere::Intersect(glm::vec3 eye, glm::vec3 ray, glm::vec3 *hit, bool near) {
glm::vec3 fromCtr = eye - center ;
float a = glm::dot(ray,ray);
float b = 2 * glm::dot(ray, fromCtr);
float c = glm::dot(fromCtr,fromCtr) - radius * radius;
float delta = b*b/4 - a*c;
if(delta < -precision) // no intersection
return -1.0;
else if(-precision <= delta && delta <= precision){ // only one intersection
float len = -b / 2 ;
if(len < -precision) // wrong direction
return -1.0;
else{ // right direction
*hit = eye + ray * len;
return len ;
}
}
else{ // two intersections
float len1 = -b/2 + sqrt(delta);
float len2 = -b/2 - sqrt(delta);
if(len1 < -precision) // both wrong direction
return -1.0;
else if(len2 < -precision) { // only one right direction
*hit = eye + ray * len1;
return len1 ;
}
else{ // both right direction
if(near){
*hit = eye + ray * len2;
return len2 ;
}
else{
*hit = eye + ray * len1;
return len1;
}
}
}
}
// set *outRay and return true if not total reflection; return false other wise
bool Object::GetRefractRay(glm::vec3 inRay, glm::vec3 inPoint, glm::vec3 *outRay){
glm::vec3 normal = GetNormal(inPoint);
inRay = glm::normalize(-inRay);
float ratio;
if(glm::dot(normal, inRay) > 0 ){ // inward ray
ratio = 1 / refractivity ;
}
else{ // outward ray
normal = -normal;
ratio = refractivity;
}
float root = 1 - pow(ratio,2) * ( 1 - pow( glm::dot(normal,inRay), 2) );
if( root < precision ) return false;
*outRay = normal * ( ratio * glm::dot(normal,inRay) - sqrt(root) ) - ratio * inRay;
return true;
}
// set *outRay, *outPoint and return true if success
// otherwise return false
// refract can fail out of precision error
bool Sphere::Refract(glm::vec3 inRay, glm::vec3 inPoint, glm::vec3 *outRay, glm::vec3 *outPoint){
glm::vec3 midRay, retRay;
glm::vec3 retPoint;
if(! GetRefractRay(inRay,inPoint, &midRay) ) return false;
float dist = Intersect(inPoint, midRay, &retPoint, false);
if(dist < precision) return false;
if(! GetRefractRay(midRay,retPoint,&retRay) ) return false;
*outRay = retRay;
*outPoint = retPoint;
return true;
}
// return -1.0 when no intersection, and do nothing to *hit
// else return distance from eye to hit point (positive only), and set *hit
float Plane::Intersect(glm::vec3 eye, glm::vec3 ray, glm::vec3 *hit){
/*
Explanation on how to calculate intersection point.
Define:
dist : distance from eye to hit point (if there is)
the intersection point of a line and a plane can be calculated by following :
dist = dot( (plane.center - eye), plane.normal ) / dot( ray, plane.normal )
hit = dist * ray + eye;
*/
ray = glm::normalize(ray);
float divident = glm::dot((center - eye) , normal);
float divisor = glm::dot(ray , normal);
if(-precision < divisor && divisor < precision) return -1.0;
float dist = divident / divisor;
if(dist < 0) return -1.0;
glm::vec3 point = dist * ray + eye;
glm::vec3 CtoP = point - center;
if( fabs(glm::dot(CtoP, Xaxis)) > Xlen ) return -1.0;
if( fabs(glm::dot(CtoP, Yaxis)) > Ylen ) return -1.0;
*hit = point;
return dist;
}
float Triangle::Intersect(glm::vec3 eye, glm::vec3 ray, glm::vec3 *hit){
ray = glm::normalize(ray);
float divident = glm::dot((vertex[0] - eye) , normal);
float divisor = glm::dot(ray , normal);
/*
printf("check if divisor == 0\n");
printf("-------------\n");
cout << "vertex[0] : " << vertex[0] << "\n";
cout << "eye : " << eye << "\n";
cout << "normal : " << normal << "\n";
cout << "ray : " << ray << "\n";
cout << "divident : " << divident << "\n";
cout << "divisor : " << divisor << "\n";
printf("-------------\n");
*/
if(-precision < divisor && divisor < precision) return -1.0;
//printf("divisor != 0\n");
float dist = divident / divisor;
if(dist < 0) return -1.0;
//printf("dist >= 0\n");
glm::vec3 point = dist * ray + eye;
bool inside = true;
for(int i=0;i<3;i++){
glm::vec3 edge = vertex[(i+1) % 3] - vertex[i];
glm::vec3 line = point - vertex[i];
if( glm::dot( normal , glm::cross(edge,line) ) < -precision ){
inside = false;
break;
}
}
if(! inside) return -1.0;
//printf("! inside\n");
*hit = point;
return dist;
}
/*********************************************************************
* This function returns a pointer to the Object that the
* ray intersects first; NULL if no intersection.
**********************************************************************/
Object* intersectScene(glm::vec3 eye, glm::vec3 ray, glm::vec3 *hit, int ignore) {
float len;
bool len_set = false;
glm::vec3 ret_hit ;
Object* ret_obj = NULL;
for(Object* s = scene; s!=NULL; s = s->next){
if(s->index == ignore) continue;
glm::vec3 tmp_hit;
float tmp_len;
tmp_len = s->Intersect(eye,ray,&tmp_hit);
//printf("tmp_len = %f \n",tmp_len);
if(tmp_len > precision){
if(!len_set || tmp_len < len ){
len_set = true;
len = tmp_len;
ret_hit = tmp_hit;
ret_obj = s;
}
}
}
if(len_set){
*hit = ret_hit;
}
//else printf("No Intersection : len = %f \n",len);
return ret_obj;
}
/*****************************************************
* This function adds an object into the scene list
*****************************************************/
void addObject(Object* obj){
if(scene==NULL)
scene = obj;
else{
obj->next = scene;
scene = obj;
}
}
// This function adds a sphere into the scene list
void addSphere(int id, glm::vec3 amb, glm::vec3 dif, glm::vec3 spe, float shine, float refl,
glm::vec3 ctr, float rad){
Sphere* sph = new Sphere;
(*sph) = Sphere(id,amb,dif,spe,shine,refl,ctr,rad);
addObject(sph);
}
//bool refr = false, float refrty = 2, float refrce = 0
void addSphere(int id, glm::vec3 amb, glm::vec3 dif, glm::vec3 spe, float shine, float refl,
glm::vec3 ctr, float rad, bool refr, float refrty, float refrce){
Sphere* sph = new Sphere;
(*sph) = Sphere(id,amb,dif,spe,shine,refl,ctr,rad,refr,refrty, refrce);
addObject(sph);
}
// This function adds a plane into the scene list
void addPlane(int id,glm::vec3 amb, glm::vec3 dif, glm::vec3 spe, float shine, float refl,
glm::vec3 ctr, glm::vec3 norm, glm::vec3 Xax, int Xl, int Yl){
Plane* pla = new Plane;
(*pla) = Plane(id,amb,dif,spe,shine,refl,ctr,norm,Xax,Xl,Yl);
addObject(pla);
}
void addTriangle(int id, glm::vec3 amb, glm::vec3 dif, glm::vec3 spe, float shine, float refl,
glm::vec3 p0,glm::vec3 p1, glm::vec3 p2){
Triangle *tri = new Triangle;
*tri = Triangle(id, amb, dif, spe, shine, refl, p0, p1, p2);
addObject(tri);
}
void addTriangle(int id, glm::vec3 amb, glm::vec3 dif, glm::vec3 spe, float shine, float refl,
glm::vec3 p0,glm::vec3 p1, glm::vec3 p2, bool refr, float refrty, float refrce){
Triangle *tri = new Triangle;
*tri = Triangle(id, amb, dif, spe, shine, refl, p0, p1, p2, refr, refrty, refrce);
addObject(tri);
}
// print all objects in the scene list
void printObjects(){
for(Object* obj=scene; obj; obj=obj->next){
//obj->PrintInfo();
obj->PrintShape();
/*
glm::vec3 point(0,5,0);
glm::vec3 normal = obj->GetNormal(point);
cout << normal << "\n";
*/
}
}
// free space allocated for scene
void freeObjects(){
if(scene == NULL) return;
Object* now = scene;
Object* next;
while(now != NULL){
next = now->next;
delete now;
now = next;
}
}