-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrayTracer.c
690 lines (510 loc) · 14.2 KB
/
rayTracer.c
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
* rayTracer.c
*
* author: Nan Schaller
*
* This is a starter program for a ray tracer able to handle any
* number of spheres and one checked floor. (Programming Assignment
* 1)
*/
#define FAR_AWAY 10000.0
#include <stdio.h>
#include <stdlib.h> /* Needed for C++ on windows */
#include "rayTracer.h"
#include "math.h"
#include <GL/glut.h> /* glut.h includes gl.h and glu.h*/
/*
* The Scene
*/
struct Camera camera;
struct Vector cameraU, cameraV, cameraN;
struct Light light;
float worldWidth;
float worldHeight;
struct Color background;
struct Object object[3]; /* Array to hold objects in scene; set
for 2 spheres at the moment */
int numberOfObjects;
int screenWidth = 300;
int screenHeight = 300;
float pixelDX; /* Pixel width */
float pixelDY; /* Pixel height */
struct Point center; /* x, y, z at center of viewplane */
struct Point start; /* World location of lower left
corner of lower left pixel */
int numberOfLevels; /* Depth of raytracing */
char letter; /* For parcing input data */
/*
* Global Variables added for Tone Reproduction (Assignment 3)
*/
struct Color image[1024][1024]; /* a fudge because I'm not very adept at C */
int counter = 0; /* Initial ray tracing is 0, others >0 */
int whichTR = 0; /* which tone reproduction operator */
float lmax = 1.0; /* maximum luminance */
#include "reading.c"
/*
* Read Scene - parses provided input file
*/
void readScene( void ) {
int i;
/*
* Set the camera parameters
*/
readCamera();
/*
* Set the Light parameters
*/
readLight();
/*
* Set screen size and maximum ray tracing depth
*/
readParameters();
/*
* Set background color
*/
readBackground();
/*
* Read objects
*/
scanf( " %c", &letter );
i = 0;
while ( !feof( stdin ) ) {
if ( letter == 'S' ) {
/*
* Read sphere
*/
readSphere( i );
} else if ( letter == 'F' ) {
/*
* Read sphere
*/
readFloor( i );
} else {
printf( "Object %d is unknown, letter is %c\n", i, letter );
exit( 1 );
}
i++;
scanf(" %c", &letter);
}
numberOfObjects = i;
printf("The Scene has been read, there are %d objects\n\n", numberOfObjects );
fflush(stdout);
}
/*
* Calculate the dot product of Vector r and Vector s
*/
double dotProduct( struct Vector r, struct Vector s ) {
return r.dx * s.dx + r.dy * s.dy + r.dz * s.dz;
}
/*
* Calculate the dot product of Vector r and Point s
*/
double dotProductPoint( struct Vector r, struct Point s ) {
return r.dx * s.x + r.dy * s.y + r.dz * s.z;
}
/*
* Normalize the Vector r
*/
struct Vector normalize( struct Vector r) {
struct Vector normalizedVector;
double size = sqrt( r.dx * r.dx + r.dy * r.dy + r.dz * r.dz);
normalizedVector.dx = r.dx/size;
normalizedVector.dy = r.dy/size;
normalizedVector.dz = r.dz/size;
normalizedVector.start = r.start;
return normalizedVector;
}
/*
* Calculate normalized vector by subtracting two points
* Vector starts at point1
*/
struct Vector subtract( struct Point point1, struct Point point2 ) {
struct Vector vector;
vector.dx = point2.x - point1.x;
vector.dy = point2.y - point1.y;
vector.dz = point2.z - point1.z;
vector.start = point1;
vector = normalize( vector );
return vector;
}
/*
* Calculate cross product for left-handed coordinate system
*/
struct Vector crossProduct( struct Vector vector1, struct Vector vector2,
struct Point start ) {
struct Vector vector;
vector.start = start;
vector.dx = vector1.dz * vector2.dy - vector1.dy * vector2.dz;
vector.dy = vector1.dx * vector2.dz - vector1.dz * vector2.dx;
vector.dz = vector1.dy * vector2.dx - vector1.dx * vector2.dy;
vector = normalize( vector );
return vector;
}
/*
* Initialization setup for ray tracing
*/
void init( void ) {
/*
* Set up camera's left-handed coordinate system in terms of
* world coordinates
*/
cameraN = subtract( camera.position, camera.lookAt );
cameraU = crossProduct( cameraN, camera.up, center );
cameraV = crossProduct( cameraU, cameraN, center );
/*
* Find the center of the viewplane
*/
center.x = camera.position.x + camera.focalLength * cameraN.dx;
center.y = camera.position.y + camera.focalLength * cameraN.dy;
center.z = camera.position.z + camera.focalLength * cameraN.dz;
/*
* Calculate width of pixel in terms of world coordinates
*/
pixelDX = worldWidth / screenWidth;
pixelDY = worldHeight / screenHeight;
/*
* Calculate the center of the upper lefthand pixel of the
* viewplane
*/
start.x = center.x - ( ( worldWidth * cameraU.dx )
+ ( worldHeight * cameraV.dx ) ) / 2.0;
start.y = center.y - ( ( worldWidth * cameraU.dy )
+ ( worldHeight * cameraV.dy ) ) / 2.0;
start.z = center.z - ( ( worldWidth * cameraU.dz )
+ ( worldHeight * cameraV.dz ) ) / 2.0;
/* Debug print
printf("omega = %f\ncenter = %f, %f, %f\npixelDX, pixelDY = %f, %f\nstart = %
f, %f, %f\n",
omega, center.x, center.y, center.z, pixelDX, pixelDY,
start.x, start.y, start.y);
fflush(stdout);
*/
}
/*
* Find Sphere Intersection
*/
float sphereIntersection( int whichObject, struct Vector ray ) {
float omega = FAR_AWAY;
/* For part 1, your code goes here.
*
* Be sure to check BOTH omegas!
*
* The returned omega should place the intersection point
* BEYOND the viewplane from the camera.
*
* Minimally, it should be positive.
*/
return omega;
}
/*
* Find floor Intersection
*/
float floorIntersection( int whichObject, struct Vector ray) {
float omega = FAR_AWAY;
/* For part 1, your code goes here
*
* The returned omega should place the intersection point
* BEYOND the viewplane from the camera.
*
* Minimally, it should be positive.
*/
return omega;
}
/*
* Find object with the closest intersection point
*/
struct Hit intersection ( struct Vector view ) {
float smallestOmega = FAR_AWAY;
float omega; /* Distance to intersection point */
struct Hit hit; /* The closest object intersected */
int whichObject; /* Number of object intersected */
/*
* Initialize the object intersected to be the background
* and the distance from the eye point to be far away
*/
hit.which = numberOfObjects;
hit.omega = FAR_AWAY;
/*
* Loop through all of the objects
*/
for ( whichObject = 0; whichObject < numberOfObjects; whichObject++ ) {
if (object[whichObject].letter == 'S') {
/*
* See if the ray intersects with this sphere
*/
omega = sphereIntersection( whichObject, view);
/*
* Omega must be positive! Otherwise the intersection is "behind" the
* starting point.
*
* We really should be checking if the intersection point is on the
* otherside of the viewplane from the camera. I haven't figured out
* the math for this yet.
*
*/
if ( omega > 0.0 && omega < smallestOmega ) {
smallestOmega = omega;
hit.which = whichObject;
hit.omega = omega;
}
} else {
/*
* See if the ray intersects with the floor
*/
omega = floorIntersection( whichObject, view );
/*
* Omega must be positive! Otherwise the intersection is "behind" the
* starting point.
*
* We really should be checking if the intersection point is on the
* otherside of the viewplane from the camera. I haven't figured out
* the math for this yet.
*
*/
if ( omega > 0.0 && omega < smallestOmega ) {
smallestOmega = omega;
hit.which = whichObject;
hit.omega = omega;
}
}
}
/* debug print
if (hit.which != numberOfObjects ) {
printf( "In Intersection, hit.which %d, hit.omega = %f\n",
hit.which, hit.omega );
fflush( stdout );
}
*/
return hit;
}
struct Color trace( struct Vector view, int level ) {
struct Color color = background;
struct Hit hit;
/*
* find intersection with nearest object
*/
hit = intersection( view );
/*
* if no intersection
* color = background
* else
* color = object's ambient
*/
if ( hit.which == numberOfObjects ) {
color = background;
} else {
color = object[hit.which].ambient[0];
if (object[hit.which].letter == 'F') {
/* See if second color instead (part 1) */
color = object[hit.which].ambient[1];
}
/*
* If intersection point is lit (part 2)
* color = color + specular + diffuse
* (Don't count transmissive sphere as being in the way)
* (Might try multiplying lightedness by Kt?)
*/
/*
* If there are still more levels to do, i.e., level >0 AND
* object is reflective (part 2)
* color = color + Kr * color of traced reflection ray
* for level - 1
*/
/*
* If there are still more levels to do, i.e., level >0 AND
* object is transmissive (part 3)
* color = color + Kt * color of traced refracted ray
* for level - 1
*/
}
return color;
}
/*
* Tone Reproduction Operator Processing (Programming Assignment 3)
*/
void doTR( ) {
int i, j;
struct Color color;
/* debug print
printf( "whichTR, lmax = %d, %f\n", whichTR, lmax );
fflush( stdout );
*/
if (whichTR == 1) {
/*
* Clear window to the background color - just to
* check that we got here.
*/
glClearColor( 0.0, 0.0, 1.0, 0.0 );
glClear( GL_COLOR_BUFFER_BIT );
/* Do Reinhard */
for ( i = 0; i < screenHeight; i++ ) {
for ( j = 0; j < screenWidth; j++ ) {
color = image[j][i];
glColor3f( color.red, color.green, color.blue );
glRecti( j, i, j+1, i+1 );
}
}
glFlush();
} else if (whichTR == 2) {
/* Do Ward */
/*
* Clear window to the background color - just to
* check that we got here.
*/
glClearColor( 0.0, 1.0, 0.0, 0.0 );
glClear( GL_COLOR_BUFFER_BIT );
for ( i = 0; i < screenHeight; i++ ) {
for ( j = 0; j < screenWidth; j++ ) {
color = image[j][i];
glColor3f( color.red, color.green, color.blue );
glRecti( j, i, j+1, i+1 );
}
}
glFlush();
}
}
/*
* Display callback function - used for redisplay as well
*/
void display( void ) {
int i, j;
float x, y, z, zcol;
struct Color color;
struct Vector view;
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
gluOrtho2D( 0.0, 1.0*screenWidth, 0.0, 1.0*screenHeight );
glViewport( 0, 0, screenWidth, screenHeight );
/*
printf("counter = %d\n", counter);
fflush(stdout);
*/
/*
* Added to handle more efficient redisplay and tone reproduction
*/
if (counter == 0) {
/*
* Clear window to the background color
*/
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glClear( GL_COLOR_BUFFER_BIT );
for ( i = 0; i < screenHeight; i++ ) {
for ( j = 0; j < screenWidth; j++ ) {
x = start.x + ( i + 0.5 ) * cameraV.dx * pixelDY
+ ( j + 0.5 ) * cameraU.dx * pixelDX;
y = start.y + ( i + 0.5 ) * cameraV.dy * pixelDY
+ ( j + 0.5 ) * cameraU.dy * pixelDX;
z = start.z + ( i + 0.5 ) * cameraV.dz * pixelDY
+ ( j + 0.5 ) * cameraU.dz * pixelDX;
/*
* setup ray
*/
view.dx = x - camera.position.x;
view.dy = y - camera.position.y;
view.dz = z - camera.position.z;
view.start = camera.position;
view = normalize( view );
/*
* Note: trace is called using the input value
* of the number of levels to trace. This means
* that recursively you will need to DECREMENT,
* not increment!
*/
color = trace( view, numberOfLevels );
/*
* Draw the point
*/
glColor3f( color.red, color.green, color.blue );
glRecti( j, i, j+1, i+1 );
/*
* Save the color in an array for redisplay and
* for tone reproduction process
*/
image[j][i] = color;
}
}
/*
* Forces OpenGL commands to execute - moved for efficiency
*/
glFlush();
/*
* Added to handle more efficient redisplay and tone reproduction
*/
doTR( );
counter++;
} else {
/*
* Added to handle more efficient redisplay and tone reproduction
*/
if (whichTR == 0) {
/*
* On redisplay, don't recalculate, just display stored values
*/
for ( i = 0; i < screenHeight; i++ ) {
for ( j = 0; j < screenWidth; j++ ) {
color = image[j][i];
glColor3f( color.red, color.green, color.blue );
glRecti( j, i, j+1, i+1 );
}
}
glFlush();
} else {
/* Do Tone Reproduction */
doTR( );
}
}
}
/*
* Main routine - GLUT setup and initialization
*/
int main(int argc, char** argv) {
/*
* Read in the scene
*/
readScene();
/*
* Initialization setup for the raytracing
*/
init();
/*
* Initializes GLUT and should be used before any OpenGL functions
*/
glutInit( &argc, argv );
/* Debug print
printf("argc = %d\n", argc);
fflush( stdout );
*/
/*
* Process command line arguments - Lmax, whichTR
* Note: If done this way, must be done AFTER glutInit!
*/
if (argc == 3) {
/*
* Get tone reproduction arguments, if any
*/
whichTR = atoi( *++argv );
lmax = atof( *++argv );
} else if (argc != 1) {
printf(" Usage rayTracer <whichTR tone reproduction> <lmax> OR rayTracer\n")
;
fflush(stdout);
exit(1);
}
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize( screenWidth, screenHeight );
/*
* Creates window on screen with title in argument
*/
glutCreateWindow( "Nan's Ray Tracer" );
/*
* Callback function; causes "display()" to be called each time there
* is a display callback.
*/
glutDisplayFunc( display );
/*
* Causes program to enter an event-processing loop; should be last
* statement in main()
*/
glutMainLoop( );
return 0;
}