-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
94 lines (80 loc) · 2.52 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
#include <stdio.h>
#include <stdlib.h>
#include "raytracing.h"
void write_to_file(int ***img, int img_w, int img_h) {
FILE *img_file;
img_file = fopen("image.ppm", "wb");
fprintf(img_file, "P3\n");
fprintf(img_file, "%d %d\n", img_w, img_h);
fprintf(img_file, "255\n");
for (int i = 0; i < img_h; i++) {
for (int j = 0; j < img_w; j++) {
fprintf(img_file, "%d %d %d ", img[i][j][0], img[i][j][1], img[i][j][2]);
}
fprintf(img_file, "\n");
}
fclose(img_file);
}
int main() {
int img_w = 1920;
int img_h = 1080;
int ***img = (int ***)malloc(img_h * sizeof(int **));
for (int i = 0; i < img_h; i++) {
img[i] = (int **)malloc(img_w * sizeof(int *));
for (int j = 0; j < img_w; j++) {
img[i][j] = (int *)malloc(3 * sizeof(int));
}
}
plane_t plane = {
{0.0, 0.0, 0.0}, // Position
{0.0, 1.0, 0.0}, // Normal
{1.0, 1.0, 1.0}, // Color 1 (RGB)
{0.0, 0.0, 0.0}, // Color 2 (RGB)
0.5, // Diffuse
0.25, // Specular
0.35 // Reflection
};
sphere_t sphere_0 = {
{-0.5, 0.6, -2.0}, // Position
{0.20, 0.59, 0.86}, // Color (RGB)
0.6, // Radius
1.0, // Diffuse
0.5, // Specular
0.15 // Reflection
};
sphere_t sphere_1 = {
{0.2, 0.2, -2.0}, // Position
{0.9, 0.30, 0.23}, // Color (RGB)
0.2, // Radius
1.0, // Diffuse
0.5, // Specular
0.15 // Reflection
};
sphere_t sphere_2 = {
{-0.65, 0.3, -1.0}, // Position
{0.18, 0.80, 0.44}, // Color (RGB)
0.3, // Radius
1.0, // Diffuse
0.5, // Specular
0.15 // Reflection
};
scenario_t scenario = {
0.00, // Ambient Light
100, // Specular light power
{0.75, 0.4, 2.0}, // Camera Position
{0.0, 0.5}, // Camera Rotation
{-1.0, 3.0, 1.0}, // Light position
{1.0, 1.0, 1.0}, // Light color
plane, // Plane info
{sphere_0, sphere_1, sphere_2} // Spheres info
};
render_scene(img, img_w, img_h, scenario);
write_to_file(img, img_w, img_h);
for (int i = 0; i < img_h; i++) {
for (int j = 0; j < img_w; j++) {
free(img[i][j]);
}
free(img[i]);
}
free(img);
}