-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
639 lines (491 loc) · 18 KB
/
main.cpp
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
#include <limits>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "Vect.h"
#include "Ray.h"
#include "Camera.h"
#include "Color.h"
#include "Light.h"
#include "Source.h"
#include "Object.h"
#include "Sphere.h"
#include "Plane.h"
#include "Triangle.h"
#include "Cylinder.h"
using namespace std;
struct RGBType{
double r;
double g;
double b;
};
void savebmp(const char *filename, int w, int h, int dpi, RGBType *data){
FILE * f;
int k = w*h;
int s = 4*k;
int filesize = 54 + s;
double factor = 39.375;
int m = static_cast<int>(factor);
int ppm = dpi * m;
unsigned char bmpfileheader[14] = { 'B','M', 0,0,0,0, 0,0,0,0, 54,0,0,0};
unsigned char bmpinfoheader[40] = { 40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0,24,0};
bmpfileheader[ 2] = (unsigned char) (filesize);
bmpfileheader[ 3] = (unsigned char) (filesize>>8);
bmpfileheader[ 4] = (unsigned char) (filesize>>16);
bmpfileheader[ 5] = (unsigned char) (filesize>>24);
bmpinfoheader[ 4] = (unsigned char) (w);
bmpinfoheader[ 5] = (unsigned char) (w>>8);
bmpinfoheader[ 6] = (unsigned char) (w>>16);
bmpinfoheader[ 7] = (unsigned char) (w>>24);
bmpinfoheader[ 8] = (unsigned char) (h);
bmpinfoheader[ 9] = (unsigned char) (h>>8);
bmpinfoheader[10] = (unsigned char) (h>>16);
bmpinfoheader[11] = (unsigned char) (h>>24);
bmpinfoheader[21] = (unsigned char) (s);
bmpinfoheader[22] = (unsigned char) (s>>8);
bmpinfoheader[23] = (unsigned char) (s>>16);
bmpinfoheader[24] = (unsigned char) (s>>24);
bmpinfoheader[25] = (unsigned char) (ppm);
bmpinfoheader[26] = (unsigned char) (ppm>>8);
bmpinfoheader[27] = (unsigned char) (ppm>>16);
bmpinfoheader[28] = (unsigned char) (ppm>>24);
bmpinfoheader[29] = (unsigned char) (ppm);
bmpinfoheader[30] = (unsigned char) (ppm>>8);
bmpinfoheader[31] = (unsigned char) (ppm>>16);
bmpinfoheader[32] = (unsigned char) (ppm>>24);
f = fopen(filename,"wb");
fwrite(bmpfileheader,1,14,f);
fwrite(bmpinfoheader,1,40,f);
for (int i = 0; i < k; i++){
RGBType rgb = data[i];
double red = (data[i].r) * 255;
double green = (data[i].g) * 255;
double blue = (data[i].b) * 255;
unsigned char color[3] = {(int)floor(blue), (int)floor(green), (int)floor(red)};
fwrite(color,1,3,f);
}
fclose(f);
}
int winningObjectIndex(vector<double> object_intersections){
//return the index of the winning intersection
int index_of_minimum_value;
// prevent unnecessary calculations
if (object_intersections.size() == 0) {
// if there are no intersections
return -1;
}
else if (object_intersections.size() == 1){
if ( object_intersections.at(0) > 0){
// if that intersection is greater than zero then its
// our index of min value
return 0;
}
else{
// otherwise the only intersection value is negative
return -1;
}
}
else {
// otherwise there is more than one intersection
// first find the maximum value
double max = 0;
for (int i=0; i < object_intersections.size(); i ++){
if ( max < object_intersections.at(i)) {
max = object_intersections.at(i);
}
}
// then starting from the maximum value find the minimum
// positive value
if (max > 0) {
// we only want positive intersections
for (int index = 0; index < object_intersections.size(); index++){
if (object_intersections.at(index) > 0 && object_intersections.at(index) <= max){
max = object_intersections.at(index);
index_of_minimum_value = index;
}
}
return index_of_minimum_value;
}
else {
// all the intersections were negative
return -1;
}
}
}
Color getColorAt(Vect intersection_position, Vect intersecting_ray_direction, vector<Object*>scene_objects, int index_of_winning_object, vector<Source*> light_sources, double accuracy, double ambientLight)
{
Color winning_object_color = scene_objects.at(index_of_winning_object)->getColor();
Vect winning_object_normal = scene_objects.at(index_of_winning_object)->getNormalAt(intersection_position);
if (winning_object_color.getColorSpecial()==2)
{
// checkered/tile floor pattern
int square = (int)floor(intersection_position.getVectX())
+ (int)floor(intersection_position.getVectZ());
if ((square % 2 )==0){
//black tile
winning_object_color.setColorRed(0.2);
winning_object_color.setColorGreen(0.2);
winning_object_color.setColorBlue(0.2);
}
else
{
// white tile
winning_object_color.setColorRed(0.4);
winning_object_color.setColorGreen(0.4);
winning_object_color.setColorBlue(0.4);
}
}
Color final_color = winning_object_color.colorScalar(ambientLight);
// bw 0 and 1 refers to shinyness
if (winning_object_color.getColorSpecial() > 0 &&
winning_object_color.getColorSpecial() <= 1)
{
// reflection from objects with specular intensity
double dot1 = winning_object_normal.dotProduct(intersecting_ray_direction.negative());
Vect scalar1 = winning_object_normal.vectMult(dot1);
Vect add1 = scalar1.vectAdd(intersecting_ray_direction);
Vect scalar2 = add1.vectMult(2);
Vect add2 = intersecting_ray_direction.negative().vectAdd(scalar2);
Vect reflection_direction = add2.normalize();
Ray reflection_ray (intersection_position, reflection_direction);
// determine what the ray intersects with first
vector<double> reflection_intersections;
for (int reflection_index = 0; reflection_index < scene_objects.size(); reflection_index ++)
{
reflection_intersections.push_back(scene_objects.at(reflection_index)->findIntersection(reflection_ray));
}
int index_of_winning_object_with_reflection = winningObjectIndex(reflection_intersections);
if (index_of_winning_object_with_reflection != -1)
{
// reflection ray missed everything else
if (reflection_intersections.at(index_of_winning_object_with_reflection) > accuracy)
{
// determine the position and direction at the point of intersection with
// the ray only affects the color if it reflected off something
Vect reflection_intersection_position = intersection_position.vectAdd(reflection_direction.vectMult(reflection_intersections.at(index_of_winning_object_with_reflection)));
Vect reflection_intersection_ray_direction = reflection_direction;
Color reflection_intersection_color = getColorAt(reflection_intersection_position, reflection_intersection_ray_direction, scene_objects, index_of_winning_object_with_reflection, light_sources, accuracy, ambientLight);
final_color = final_color.colorAdd(reflection_intersection_color.colorScalar(winning_object_color.getColorSpecial()));
}
}
}
for (int light_index = 0; light_index < light_sources.size(); light_index++)
{
Vect light_direction = light_sources.at(light_index)->getLightPosition().vectAdd(intersection_position.negative()).normalize();
float cosine_angle = winning_object_normal.dotProduct(light_direction);
if (cosine_angle > 0 ){
// test for shadows
bool shadowed = false;
Vect distance_to_light = light_sources.at(light_index)->getLightPosition().vectAdd(intersection_position.negative()).normalize();
float distance_to_light_magnitude = distance_to_light.magnitude();
// from direction of light source
Ray shadow_ray (intersection_position, light_sources.at(light_index)->getLightPosition().vectAdd(intersection_position.negative()).normalize());
vector<double> secondary_intersections;
// boolean test for shadowed.. why?
for (int object_index = 0; object_index< scene_objects.size() && shadowed == false; object_index++)
{
secondary_intersections.push_back(scene_objects.at(object_index)->findIntersection(shadow_ray));
}
// paused here at 1 hour and 1 minute
// if we find a secondary intersection less than the distance to the light source the pixel is now shadowed
for (int c = 0; c < secondary_intersections.size(); c++)
{
if(secondary_intersections.at(c) > accuracy)
{
if (secondary_intersections.at(c) <= distance_to_light_magnitude)
{
shadowed = true;
}
//break;
}
}
if(shadowed == false)
{
final_color = final_color.colorAdd(winning_object_color.colorMultiply(light_sources.at(light_index)->getLightColor()).colorScalar(cosine_angle));
}
}
}
return final_color.clip();
}
vector<Object*> scene_objects;
void makeCube (Vect corner1, Vect corner2, Color color){
// corner1
double c1x = corner1.getVectX();
double c1y = corner1.getVectY();
double c1z = corner1.getVectZ();
// corner2
double c2x = corner2.getVectX();
double c2y = corner2.getVectY();
double c2z = corner2.getVectZ();
Vect A (c2x, c1y, c1z);
Vect B (c2x, c1y, c2z);
Vect C (c1x, c1y, c2z);
Vect D (c2x, c2y, c1z);
Vect E (c1x, c2y, c1z);
Vect F (c1x, c2y, c2z);
// left side
scene_objects.push_back( new Triangle ( D, A, corner1, color ));
scene_objects.push_back( new Triangle ( corner1, E, D, color ));
// far side
scene_objects.push_back( new Triangle ( corner2, B, A, color ));
scene_objects.push_back( new Triangle ( A, D, corner2, color ));
// right side
scene_objects.push_back( new Triangle ( F, C, B, color ));
scene_objects.push_back( new Triangle ( B, corner2, F, color ));
// front
scene_objects.push_back( new Triangle ( E, corner1, C, color ));
scene_objects.push_back( new Triangle ( C, F, E, color ));
// bottom
scene_objects.push_back( new Triangle ( D, E, F, color ));
scene_objects.push_back( new Triangle ( F, corner2, D, color ));
// top
scene_objects.push_back( new Triangle ( corner1, A, B, color ));
scene_objects.push_back( new Triangle ( B, C, corner1, color ));
}
int main(int argc, char *argv[])
{
cout << "rendering..." << endl;
clock_t t1, t2;
t1 = clock();
int dpi = 72;
int width = 640;
int height = 480;
int n = width * height;
RGBType *pixels = new RGBType[n];
int aadepth = 2 ;
double aathreshold = 0.1;
double aspectRatio = (double)width/(double)height;
double ambientLight = 0.2;
double accuracy = 0.00000001;
Vect O (0,0,0);
Vect X (1,0,0);
Vect Y (0,1,0);
Vect Z (0,0,1);
Vect new_sphere_location (2.5,0,0);
Vect campos(3, 1.5, -4);
Vect look_at (0,0,0);
Vect diff_btw ( campos.getVectX() - look_at.getVectX(),
campos.getVectY() - look_at.getVectY(),
campos.getVectZ() - look_at.getVectZ() );
Vect camdir = diff_btw.negative().normalize();
Vect camright = Y.crossProduct(camdir).normalize();
Vect camdown = camright.crossProduct(camdir);
Camera scene_cam (campos, camdir, camright, camdown);
Color white_light (1.0, 1.0, 1.0, 0);
Color pretty_white (1.0, 1.0, 1.0, 0.3);
Color pretty_green (0.5, 1.0, 0.5, 0.3);
Color pretty_red (1.0, 0.5, 0.5, 0.3);
Color pretty_blue (0.5, 0.5, 1.0, 0.3);
Color maroon (0.5, 0.25, 0.25, 0.95);
Color tile_floor(1,1,1,2);
Color gray (0.5, 0.5, 0.5, 0);
Color black (0.0,0.0,0.0, 0);
Color orange (0.94, 0.75, 0.31, 0);
Vect light_position (-7,10, -10);
Light scene_light ( light_position, white_light);
vector<Source*> light_sources;
light_sources.push_back(dynamic_cast<Source*>(&scene_light));
// read object description from file, generate objects
// and add to the scene...
// file line is of format /objtype/ /x/ /y/ /z/ /radius/ /color/
// basic scene plane
Plane scene_plane (Y, 0, tile_floor);
scene_objects.push_back(dynamic_cast<Object*>(&scene_plane));
// basis vector indicators
/*
Sphere oball (O, .1, pretty_white);
Sphere xball (X, .1, pretty_red);
Sphere yball (Y, .1, pretty_green);
Sphere zball (Z, .1, pretty_blue);
scene_objects.push_back(dynamic_cast<Object*>(&oball));
scene_objects.push_back(dynamic_cast<Object*>(&xball));
scene_objects.push_back(dynamic_cast<Object*>(&yball));
scene_objects.push_back(dynamic_cast<Object*>(&zball));
*/
cout << "input file: " << argv[1] << endl;
string line;
ifstream myfile (argv[1]);
//scene_objects.push_back(dynamic_cast<Object*>(&zball));
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
string arr[10];
int i = 0;
cout << line << endl;
stringstream ssin(line);
while (ssin.good() && i < 10){
ssin >> arr[i];
++i;
}
if (arr[0] == "sphere")
{
//cout << " - - - - - - " << endl;
cout << "sphere found" << endl;
Vect * newVect = new Vect( atof(arr[1].c_str()), atof(arr[2].c_str()), atof(arr[3].c_str()) );
double radius = atof(arr[4].c_str());
//cout << "radius: " << radius << endl;
double redColor = atof(arr[5].c_str());
double greenColor = atof(arr[6].c_str());
double blueColor = atof(arr[7].c_str());
cout << "red: " << redColor << endl;
cout << "green: " << greenColor << endl;
cout << "blue: " << blueColor << endl;
//cout << " - - - - - " << endl;
Color * newColor = new Color( atof(arr[5].c_str()), atof(arr[6].c_str()), atof(arr[7].c_str()), atof(arr[8].c_str()) );
//Sphere testBall ( testVect, atof(argv[6]), pretty_green);
/*
cout << double(arr[5]) << endl;
double radius = double(arr[5]);
cout << "radius: " << radius << endl;
*/
//Sphere zball (&testVect, 1.0, pretty_blue);
scene_objects.push_back( new Sphere (*newVect, radius , *newColor) );
//cout << "sphere added" << endl;
}
for (i = 1; i < 7; i++){
cout << i+1 << " " << arr[i] << endl;
}
}
myfile.close();
}
//Sphere scene_sphere (O, 1, pretty_green);
//Cylinder scene_cylinder (O, 1.0, 1.5, 0.25, orange);
/*
Triangle scene_triangle( Vect(3,0,0),
Vect(0,3,0),
Vect(0,0,3), orange);
*/
/*
//scene_objects.push_back(dynamic_cast<Object*>(&scene_sphere));
*/
//scene_objects.push_back(dynamic_cast<Object*>(&scene_cylinder));
//scene_objects.push_back(dynamic_cast<Object*>(&scene_triangle));
//makeCube( Vect( 1 , 1, 1 ), Vect( -1, -1, -1), orange );
int thisone, aa_index;
double xamnt, yamnt;
double tempRed, tempGreen, tempBlue;
for (int x=0; x < width; x++){
for (int y=0; y<height; y++){
thisone = y * width + x;
// start with a blank pixel
double tempRed[aadepth*aadepth];
double tempGreen[aadepth*aadepth];
double tempBlue[aadepth*aadepth];
for (int aax = 0; aax < aadepth; aax++)
{
for(int aay = 0; aay < aadepth; aay++)
{
aa_index = aay*aadepth + aax;
srand(time(0));
// crate the ray from the camera to this pixel.
if(aadepth == 1){
// start with no anti-aliasing
// creates centered rectangular image plane
if (width > height) {
// the image is wider than it is tall
xamnt = ( (x + 0.5)/width) * aspectRatio - (((width-height)/(double)height)/2);
yamnt = ( (height - y ) + 0.5) /height;
}
else if (height > width){
// the image is taller than it is wide
xamnt = (x + 0.5)/width;
yamnt = (((height - y) + 0.5 )/ height)/aspectRatio - (((height - width)/(double)width)/2);
}
else
{
// the image is square
xamnt = (x + 0.5)/width;
yamnt = (( height - y ) + 0.5)/height;
}
}
else
{
// anti-aliasing
if (width > height) {
// the image is wider than it is tall
xamnt = ( (x + (double)aax/((double)aadepth - 1))/width) * aspectRatio - (((width-height)/(double)height)/2);
yamnt = ( (height - y ) + (double)aax/((double)aadepth - 1)) /height;
}
else if (height > width){
// the image is taller than it is wide
xamnt = (x + (double)aax/((double)aadepth - 1))/width;
yamnt = (((height - y) + (double)aax/((double)aadepth - 1) )/ height)/aspectRatio - (((height - width)/(double)width)/2);
}
else
{
// the image is square
xamnt = (x + (double)aax/((double)aadepth - 1))/width;
yamnt = (( height - y ) + (double)aax/((double)aadepth - 1))/height;
}
}
Vect cam_ray_origin = scene_cam.getCameraPosition(); // returns camera's origin
Vect cam_ray_direction = camdir.vectAdd(camright.vectMult(xamnt - 0.5).vectAdd(camdown.vectMult(yamnt - 0.5))).normalize();
Ray cam_ray (cam_ray_origin, cam_ray_direction);
// goes through specific x,y pixel into scene to look for intersection
vector<double> intersections;
for (int index = 0; index < scene_objects.size(); index++){
intersections.push_back(scene_objects.at(index)->findIntersection(cam_ray));
}
int index_of_winning_object = winningObjectIndex(intersections);
if ( index_of_winning_object == -1 ){
// set the background black
tempRed[aa_index] = 0;
tempGreen[aa_index] = 0;
tempBlue[aa_index] = 0;
}
else
{
// index corresponds to an object in our scene
if (intersections.at(index_of_winning_object) > accuracy )
{
// determine the position and direction vector at the
// point of intersection
Vect intersection_position = cam_ray_origin.vectAdd(cam_ray_direction.vectMult(intersections.at(index_of_winning_object)));
Vect intersecting_ray_direction = cam_ray_direction;
// get color at intersection
Color intersection_color = getColorAt(intersection_position, intersecting_ray_direction, scene_objects, index_of_winning_object, light_sources, accuracy, ambientLight);
tempRed[aa_index] = intersection_color.getColorRed();
tempGreen[aa_index] = intersection_color.getColorGreen();
tempBlue[aa_index] = intersection_color.getColorBlue();
}
}
}
}
// average the pixel color
double totalRed = 0;
double totalGreen = 0;
double totalBlue = 0;
for (int iRed = 0; iRed < aadepth * aadepth; iRed++)
{
totalRed = totalRed + tempRed[iRed];
}
for (int iGreen = 0; iGreen < aadepth * aadepth; iGreen++)
{
totalGreen = totalGreen + tempGreen[iGreen];
}
for (int iBlue = 0; iBlue < aadepth * aadepth; iBlue++)
{
totalBlue = totalBlue + tempBlue[iBlue];
}
double avgRed = totalRed / (aadepth * aadepth);
double avgGreen = totalGreen / (aadepth * aadepth);
double avgBlue = totalBlue / (aadepth * aadepth);
pixels[thisone].r = avgRed;
pixels[thisone].g = avgGreen;
pixels[thisone].b = avgBlue;
}
}
savebmp("scene_anti-aliased.bmp",width,height,dpi,pixels);
delete pixels, tempRed, tempGreen, tempBlue;
t2 = clock();
float diff = ((float)t2 - (float)t1)/1000;
cout << diff << " seconds" << endl;
return 0;
}