-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
239 lines (191 loc) · 6.05 KB
/
Copy pathmain.cpp
File metadata and controls
239 lines (191 loc) · 6.05 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
#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<Object.h>
#include<Sphere.h>
#include<Plane.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 minimum value
return 0;
}
else{
// otherwise the only intersection value is negative
return -1;
}
}
else {
//otherwise there are 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 inersections
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 are negative
return -1;
}
}
}
int thisone;
int main (int argc, char *argv[]){
cout << "rendering ... " << endl;
int dpi = 72;
int width = 640;
int height = 480;
int n = width*height;
RGBType *pixels = new RGBType[n];
double aspectratio = (double) width/(double) height;
Vect O (0,0,0);
Vect X (1,0,0);
Vect Y (0,1,0);
Vect Z (0,0,1);
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,1,1,0);
Color pretty_green(0.5,1.0,0.5,0.3);
Color maroon (0.5,0.25,0.25,0);
Color gray(0.5,0.5,0.5,0);
Color black(0,0,0,0);
Vect light_position (-7,10,-10);
Light scene_light (light_position, white_light);
// scene object
Sphere scene_sphere(O, 1, pretty_green);
Plane scene_plane(Y, -1, maroon);
vector<Object*> scene_objects;
scene_objects.push_back(dynamic_cast<Object*>(&scene_sphere));
scene_objects.push_back(dynamic_cast<Object*>(&scene_plane));
double xamnt, yamnt;
for(int x=0;x<width;x++){
for (int y=0;y<height;y++){
thisone = y*width + x;
// start with no anti-aliasing
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;
}
Vect cam_ray_origin = scene_cam.getCameraPosition();
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);
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((x > 200 && x < 440) && (y > 200 && y < 280)){
pixels[thisone].r = 0.5;
pixels[thisone].g = 0.5;
pixels[thisone].b = 0.5;
}
else{
pixels[thisone].r = 0;
pixels[thisone].g = 0;
pixels[thisone].b = 0;
}
}
}
savebmp("scene.bmp",width,height,dpi,pixels);
return 0;
}