-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
154 lines (137 loc) · 3.71 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#define epsilon 0.000000001
typedef struct {
double x;
double y;
double z;
} Vector;
typedef Vector Point;
typedef Vector Color;
typedef struct {
Point origin;
Vector direction;
} Ray;
typedef struct {
Vector normal;
double distance;
Color color;
} Plane;
typedef struct {
Point point;
double lambda;
Plane* plane;
} Intersection;
Ray getRay(int x, int y) {
Ray r;
r.origin.x = 0;
r.origin.y = 0;
r.origin.z = 0;
r.direction.x = -320 + x + 0.5;
r.direction.y = 240 - y + 0.5;
r.direction.z = -120;
return r;
}
Plane* createBox() {
Plane* box = (Plane*)malloc(5*sizeof(Plane));
int size = 5;
Plane p0 = {-1, 0, 0, size, 255, 0, 100};//links
box[0] = p0;
Plane p1 = {1, 0, 0, size, 0, 255, 0};//rechts
box[1] = p1;
Plane p2 = {0, 0, -1, size, 255,255,255};//hinten
box[2] = p2;
Plane p3 = {0, 1, 0, size, 255, 255, 255};//oben
box[3] = p3;
Plane p4 = {0, -1, 0, size, 255, 255, 255};//unten
box[4] = p4;
return box;
}
double vectorLength(Vector v) {
return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
}
Vector pointDifference(Point p1, Point p2) {
Vector v = {p1.x - p2.x, p1.y - p2.y, p1.z - p2.z};
return v;
}
double scalarProduct(Vector* v1, Vector* v2) {
return v1->x*v2->x + v1->y*v2->y + v1->z*v2->z;
}
Intersection rayHitsPlane(Ray* r, Plane* p) {
double gamma = scalarProduct(&(r->direction), &(p->normal));
double side = p->distance - scalarProduct(&(p->normal), &(r->origin));
Intersection intersect = {0, 0, 0, 0, p};
if( gamma * side > epsilon) {
double lambda = side / gamma;
intersect.point.x = r->origin.x + r->direction.x * lambda;
intersect.point.y = r->origin.y + r->direction.y * lambda;
intersect.point.z = r->origin.z + r->direction.z * lambda;
intersect.lambda = lambda;
} else {
}
return intersect;
}
Intersection getFirstIntersection(Ray* ray, Plane* box) {
Intersection nearestIntersect; double minDouble = DBL_MAX;
int planeCounter;
for(planeCounter = 0; planeCounter < 5; planeCounter++) {
Intersection intersect = rayHitsPlane(ray, &box[planeCounter]);
if(intersect.lambda > epsilon && intersect.lambda < minDouble) {
nearestIntersect = intersect;
minDouble = intersect.lambda;
}
}
return nearestIntersect;
}
Color traceRay(Ray* ray, Plane* box) {
Color color = {0, 0, 0};
Point light = {0.5, 2, -3.5};
Intersection intersect = getFirstIntersection(ray, box);
if(intersect.lambda > epsilon) {
Vector directionToLight = pointDifference(light, intersect.point);
Ray rayToLight = {intersect.point, directionToLight};
Intersection tempIntersect = getFirstIntersection(&rayToLight, box);
if(tempIntersect.lambda >= 1 || tempIntersect.lambda == 0) {
double length = vectorLength(directionToLight);
double scale = 5 / (length * length);
color = intersect.plane->color;
color.x *= scale;
color.y *= scale;
color.z *= scale;
}
}
return color;
}
void normalizeColor(Color* color) {
double factor = 255/fmax(color->x, fmax(color->y, color->z));
if(factor < 1) {
color->x = color->x*factor;
color->y = color->y*factor;
color->z = color->z*factor;
}
}
extern int write_bmp(const char* filename, int width, int height, char* rgb);
int main(int argc, char* argv[]) {
Plane* box = createBox();
char* rgb = (char*)malloc(3*640*480*sizeof(char));
int x,y;
for(x=0; x<640; x++) {
for(y=0; y<480; y++) {
Ray r = getRay(x, y);
Color c = traceRay(&r, box);
normalizeColor(&c);
int ipos = 3*(640*y+x);
double red = c.z;
double green = c.y;
double blue = c.x;
rgb[ipos + 2] = (unsigned char)red;
rgb[ipos + 1] = (unsigned char)green;
rgb[ipos] = (unsigned char)blue;
}
}
write_bmp("test.bmp", 640, 480, rgb);
free(box);free(rgb);
return 0;
}