forked from embedded2016/raytracing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
68 lines (58 loc) · 1.83 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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include "primitives.h"
#include "raytracing.h"
#define OUT_FILENAME "out.ppm"
#define ROWS 512
#define COLS 512
static void write_to_ppm(FILE *outfile, uint8_t *pixels,
int width, int height)
{
fprintf(outfile, "P6\n%d %d\n%d\n", width, height, 255);
fwrite(pixels, 1, height * width * 3, outfile);
}
static double diff_in_second(struct timespec t1, struct timespec t2)
{
struct timespec diff;
if (t2.tv_nsec-t1.tv_nsec < 0) {
diff.tv_sec = t2.tv_sec - t1.tv_sec - 1;
diff.tv_nsec = t2.tv_nsec - t1.tv_nsec + 1000000000;
} else {
diff.tv_sec = t2.tv_sec - t1.tv_sec;
diff.tv_nsec = t2.tv_nsec - t1.tv_nsec;
}
return (diff.tv_sec + diff.tv_nsec / 1000000000.0);
}
int main()
{
uint8_t *pixels;
light_node lights = NULL;
rectangular_node rectangulars = NULL;
sphere_node spheres = NULL;
color background = { 0.0, 0.1, 0.1 };
struct timespec start, end;
#include "use-models.h"
/* allocate by the given resolution */
pixels = malloc(sizeof(unsigned char) * ROWS * COLS * 3);
if (!pixels) exit(-1);
printf("# Rendering scene\n");
/* do the ray tracing with the given geometry */
clock_gettime(CLOCK_REALTIME, &start);
raytracing(pixels, background,
rectangulars, spheres, lights, &view, ROWS, COLS);
clock_gettime(CLOCK_REALTIME, &end);
{
FILE *outfile = fopen(OUT_FILENAME, "wb");
write_to_ppm(outfile, pixels, ROWS, COLS);
fclose(outfile);
}
delete_rectangular_list(&rectangulars);
delete_sphere_list(&spheres);
delete_light_list(&lights);
free(pixels);
printf("Done!\n");
printf("Execution time of raytracing() : %lf sec\n", diff_in_second(start, end));
return 0;
}