-
Notifications
You must be signed in to change notification settings - Fork 0
/
raytracer.c
176 lines (148 loc) · 6.21 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
/*
Copyright 2012 Shiben Bhattacharjee & Naveen Kumar
licensed under the apache license, version 2.0 (the "license");
you may not use this file except in compliance with the license.
you may obtain a copy of the license at
http://www.apache.org/licenses/license-2.0
unless required by applicable law or agreed to in writing, software
distributed under the license is distributed on an "as is" basis,
without warranties or conditions of any kind, either express or implied.
see the license for the specific language governing permissions and
limitations under the license.
*/
#include "types.h"
#include "scene.h"
#include "ray.h"
#include "shade.h"
#include "image.h"
#define FILENAME "output.ppm"
#define ISBINARY 1
#define WIDTH 160
#define HEIGHT 120
#define FOV 45.0f
#define RECURSION 2
#define CONE_DIVISIONS 32
#define CYLINDER_DIVISIONS 32
#define SPHERE_SUBDIVISIONS 3
void createScene(Scene *scene)
{
Object cone, cylinder, cube, sphere, plane_base, plane_right, plane_left, plane_back;
Material mtl_matt1, mtl_matt2, mtl_matt3, mtl_matt4, mtl_glass, mtl_shiny1, mtl_shiny2;
Vector red, blue, green, purple, white;
/* lets collect a few colors */
setVector(&red, 1.0f, 0.0f, 0.0f);
setVector(&green, 0.0f, 1.0f, 0.0f);
setVector(&blue, 0.0f, 0.0f, 1.0f);
setVector(&purple, 1.0f, 0.0f, 1.0f);
setVector(&white, 1.0f, 1.0f, 1.0f);
/* Create some materials */
/* setMaterial(&mtl, color, kambient, kdiffuse, kspecular, shininess, refl, refr, ir) */
setMaterial(&mtl_shiny1, white, 0.1f, 0.5f, 0.4f, 2.0f, 0.4f, 0.2f, 1.4f);
setMaterial(&mtl_shiny2, blue, 0.1f, 0.5f, 0.4f, 3.0f, 0.2f, 0.0f, 1.4f);
setMaterial(&mtl_matt1, red, 0.1f, 0.5f, 0.4f, 32.0f, 0.0f, 0.0f, 1.4f);
setMaterial(&mtl_matt2, blue, 0.1f, 0.5f, 0.4f, 2.0f, 0.0f, 0.0f, 1.4f);
setMaterial(&mtl_matt3, green, 0.1f, 0.5f, 0.4f, 2.0f, 0.0f, 0.0f, 1.4f);
setMaterial(&mtl_matt4, purple, 0.1f, 0.5f, 0.4f, 2.0f, 0.0f, 0.0f, 1.4f);
setMaterial(&mtl_glass, white, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.9f, 1.4f);
/* create some objects, attach created materials to them */
createCone( &cone, mtl_matt4, 1.0f, 1.5f, CONE_DIVISIONS);
createSphere( &sphere, mtl_matt1, 0.9f, SPHERE_SUBDIVISIONS);
createCylinder( &cylinder, mtl_shiny2, 0.75f, 1.0f, CYLINDER_DIVISIONS);
createCube( &cube, mtl_glass, 1.0f);
createPlaneXZ( &plane_base, mtl_shiny1, 10.0f);
createPlaneXZ( &plane_left, mtl_matt1, 10.0f);
createPlaneXZ( &plane_right, mtl_matt2, 10.0f);
createPlaneXZ( &plane_back, mtl_matt3, 10.0f);
/* arrange them in the scene */
transformObject(&cone, translate(2.0f, 0.0f, -2.8f));
transformObject(&cube, matMatMult(translate( 2.0f, 0.5f, -1.0f), rotateY(45.0f)));
transformObject(&cylinder, translate(-0.0f, 0.0f, -1.0f));
transformObject(&sphere, translate(-0.0f, 0.9f, -2.7f));
transformObject(&plane_base, translate( 1.0f, 0.0f, -4.0f));
transformObject(&plane_left, matMatMult(translate( -2.0f, 0.0f, -4.0f), rotateZ(-90.0)));
transformObject(&plane_right, matMatMult(translate( 4.0f, 0.0f, -4.0f), rotateZ(90.0)));
transformObject(&plane_back, matMatMult(translate( 1.0f, 0.0f, -6.0f), rotateX(90.0)));
/* add them to the scene */
initScene(scene, 8);
addObjectToScene(scene, cone);
addObjectToScene(scene, cube);
addObjectToScene(scene, cylinder);
addObjectToScene(scene, sphere);
addObjectToScene(scene, plane_base);
addObjectToScene(scene, plane_right);
addObjectToScene(scene, plane_left);
addObjectToScene(scene, plane_back);
}
Vector trace(Ray ray, Scene scene, Light light, int recur)
{
Hit hit;
Vector output, reflColor, refrColor;
float krefl, krefr;
/* default return color */
setVector(&output, 0.0f, 0.0f, 0.0f);
hit = intersectScene(ray, scene);
if(hit.objid >= 0)
{
/* current object color */
output = add(ambient(hit, scene, light), add(diffuse(hit, scene, light), specular(hit, scene, light)));
if(recur > 0)
{
/* collect color captured by reflected ray */
reflColor = trace(reflectRay(hit), scene, light, recur - 1);
krefl = scene.obj[hit.objid].material.reflectivity;
output = add(output, floatVecMult(krefl, reflColor));
/* collect color captured by refracted ray */
refrColor = trace(refractRay(hit, scene.obj[hit.objid].material.ir), scene, light, recur - 1);
krefr = scene.obj[hit.objid].material.translucency;
output = add(output, floatVecMult(krefr, refrColor));
}
/* reduce color by the shadow factor of the light */
return floatVecMult(1.0f - traceShadow(hit, scene, light), output);
}
return output;
}
int main(int argc, char **argv)
{
int i, j;
Vector lcolor, lpos, camPos, lookat;
Light light;
Camera cam;
Hit hit;
Ray ray;
Color outcolor;
Image image;
Scene scene;
/* setup scene */
createScene(&scene);
/* setup light */
setVector(&lcolor, 1.0f, 1.0f, 1.0f);
setVector(&lpos, -1.0f, 4.0f, 4.0f);
setLight(&light, lpos, lcolor, 0.3f);
/* setup camera */
setVector(&camPos, 1.0f, 2.0f, 4.0f);
setVector(&lookat, 1.0f, 0.0f, -6.0f);
setCamera(&cam, camPos, lookat, FOV, WIDTH, HEIGHT);
/* setup image */
initImage(&image, WIDTH, HEIGHT);
printf("Image: %s, Size: %dx%d, Binary: %s\n", FILENAME, WIDTH, HEIGHT, ISBINARY? "Yes": "No");
printf("Recursion Depth: %d, Initial Ray Count: %d, Triangle Count: %d\n", RECURSION, WIDTH * HEIGHT, getTriangleCount(scene));
printf("Raytracing...\n");
/* tracing */
for(j = 0; j < HEIGHT; j++)
for(i = 0; i < WIDTH; i++)
{
ray = generateRay(i, j, cam);
outcolor = vector2color(trace(ray, scene, light, RECURSION));
setPixel(&image, i, j, outcolor);
}
/* dumping image */
printf("Done\nWriting image...\n");
if(ISBINARY)
writeImageBinary(image, FILENAME);
else
writeImageAscii(image, FILENAME);
/* clean up */
cleanScene(&scene);
printf("Done\n");
return 0;
}