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 <math.h>
#include <stdlib.h>
#include "vector.h"
#include "ray.h"
#include "scene.h"
#include "pgm.h"
#define TESTW 1000
#define TESTH 1000
char container[ CONTAINER_SIZE(5, 4, 1) ];
// Implement random
COORD_T ray_rand(void *seed)
{
return (COORD_T) rand_r( (int *)seed ) / RAND_MAX;
}
int main()
{
container_t *cont = (container_t *) container;
container_init(cont, 5, 4, 1);
// Init space_t
space_t *s = container_prepare_space(cont);;
// Set space options
color_set(&s->ambient, 0.09, 0.09, 0.09);
color_set(&s->back, 0.8, 0.8, 0.8);
color_set(&s->env_color, 0.13, 0.13, 0.13);
s->env_samples = 16;
// Set viewpoint options
vector_set(&s->view.position, 0, 16, 6);
vector_set(&s->view.target, 0, 0, 6);
s->view.width = TESTW;
s->view.height = TESTH;
// Create materials
material_t *m = add_material(cont);
vector_set(&m->color, 0.4, 0.4, 1);
m->defuse = 1;
m->specular = 0.0;
m->shine = 80;
m->reflective = 0.0;
material_t *m3 = add_material(cont);
vector_set(&m3->color, 1, 0.3, 0.1);
m3->defuse = 0.95;
m3->specular = 0.0;
m3->shine = 80;
m3->reflective = 0.05;
material_t *m2 = add_material(cont);
vector_set(&m2->color, 1, 1, 1);
m2->defuse = 0.0;
m2->specular = 0.5;
m2->shine = 80;
m2->reflective = 1;
material_t *mpl = add_material(cont);
//vector_set(&mpl.color, 0, 0.396, 0.7019);
vector_set(&mpl->color, 1, 1, 1);
mpl->defuse = 1;
mpl->specular = 0.0;
mpl->shine = 50;
mpl->reflective = 0.0;
viewpoint_init(&s->view);
object_t *o = add_object(cont, TYPE_SPHERE);
vector_set(&o->sph.center, 0, 4, 7);
o->sph.radius = 5;
o->m = m2;
o = add_object(cont, TYPE_SPHERE);
vector_set(&o->sph.center, 8, 8, 4);
o->sph.radius = 2;
o->m = m3;
o = add_object(cont, TYPE_SPHERE);
vector_set(&o->sph.center, -10, 9, 5);
o->sph.radius = 3;
o->m = m;
o = add_object(cont, TYPE_SPHERE);
vector_set(&o->sph.center, -10, -5, 5);
o->sph.radius = 3;
o->m = m;
o = add_object(cont, TYPE_PLANE);
vector_set(&o->pl.start, 0, 0, 2);
vector_set(&o->pl.norm, 0, 0, 1);
o->m = mpl;
light_t *l = add_light(cont);
vector_set(&l->pos, 20, 10, 30);
color_set(&l->defuse, 0.3, 0.3, 0.3);
color_set(&l->specular, 0.5, 0.5, 0.5);
pgm_write_header(stdout, TESTW, TESTH);
// Height percentage
unsigned percentstep = TESTH / 100;
unsigned percent = 0;
int seed;
for (int y = TESTH; y; y--) {
for (int x = TESTW; x; x--) {
// Random seed
seed = x * y;
color_t c;
ray_trace(&cont->space, x, y, 2, &c, &seed);
pgm_write_pixel(stdout, &c);
}
if (y % percentstep == 0) {
fprintf(stderr, "%d%\n", percent++);
}
}
}
|