forked from lagodiuk/raytracing-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.c
128 lines (104 loc) · 4.16 KB
/
example.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
#include <stdio.h>
#include <canvas.h>
#include <render.h>
#include <obj_loader.h>
#define CANVAS_W 400
#define CANVAS_H 400
// Boost by rendering in parallel
#define THREADS_NUM 4
#define BACKGROUND_COLOR rgb(255, 255, 255)
#define MAX_OBJECTS_NUMBER 10000
#define MAX_LIGHT_SOURCES_NUMBER 5
int
main(void) {
// Allocating scene
Scene * scene = new_scene(MAX_OBJECTS_NUMBER,
MAX_LIGHT_SOURCES_NUMBER,
BACKGROUND_COLOR);
// Allocating new sphere
Float radius = 100;
Point3d center = point3d(0, 0, 0);
Color sphere_color = rgb(250, 30, 30);
Material sphere_material = material(1, 5, 5, 10, 0, 10);
Object3d * sphere = new_sphere(center,
radius,
sphere_color,
sphere_material);
// Adding sphere to the scene
add_object(scene,
sphere);
// Allocating new triangle
Object3d * triangle = new_triangle(point3d(-700, -700, -130), // vertex 1
point3d( 700, -700, -130), // vertex 2
point3d( 0, 400, -130), // vertex 3
rgb(100, 255, 30), // color
material(1, 6, 0, 2, 0, 0) // surface params
);
// Adding triangle to the scene
add_object(scene,
triangle);
// Loading 3D model of cow from *.obj file
// defining transformations and parameters of 3D model
// TODO: must be refactored...
SceneFaceHandlerParams load_params =
new_scene_face_handler_params(scene,
// scale:
40,
// move dx, dy, dz:
-150, -100, 30,
// rotate around axises x, y, z:
0, 0, 0,
// color
rgb(200, 200, 50),
// surface params
material(2, 3, 0, 0, 0, 0)
);
load_obj("./demo/models/cow.obj",
// default handler which adding polygons of 3D model to scene:
scene_face_handler,
&load_params);
// This function is requried (bulding k-d tree of entire scene)
prepare_scene(scene);
printf("\nNumber of polygons: %i\n", scene->last_object_index + 1);
// Allocating new light source
Color light_source_color = rgb(255, 255, 255);
Point3d light_source_location = point3d(-300, 300, 300);
LightSource3d * light_source = new_light_source(light_source_location,
light_source_color);
// Adding light source to the scene
add_light_source(scene,
light_source);
// Adding fog
Float density = 0.002;
set_exponential_fog(scene, density);
// Allocating camera
// TODO: It's a pity, but quaternions are not implemented yet :(
Point3d camera_location = point3d(0, 500, 0);
Float focus = 320;
Float x_angle = -1.57;
Float y_angle = 0;
Float z_angle = 3.14;
Camera * camera = new_camera(camera_location,
x_angle,
y_angle,
z_angle,
focus);
// Rotate camera if needed
// rotate_camera(camera, d_x_angle, d_y_angle, d_z_angle);
// Move camera if needed
// move_camera(camera, vector3df(d_x, d_y, d_z));
// Alocate new canvas, to render scene on it
Canvas * canvas = new_canvas(CANVAS_W,
CANVAS_H);
render_scene(scene,
camera,
canvas,
THREADS_NUM);
// Saving rendered image in PNG format
write_png("example.png",
canvas);
release_canvas(canvas);
release_scene(scene);
release_camera(camera);
return 0;
}