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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include "vector.h"
#include "ray.h"
#include "scene.h"
#include "pgm.h"
#define TESTW 500
#define TESTH 500
#define PERCENTSTEP (TESTH / 100)
#define WORKERS 4
void *worker_func(void *arg);
typedef struct {
unsigned id;
color_t *image;
} office_t;
// Height percentage, workers take turn at printing to it. This maaaay be a bad idea
unsigned percent = 0;
pthread_mutex_t percentlock;
#define OBJECTS 10
#define MATERIALS 6
#define LIGHTS 1
char container[ CONTAINER_SIZE(OBJECTS, MATERIALS, LIGHTS) ];
container_t *cont = (container_t *) container;
settings_t gfx_low = {
.samples = 10,
.depth = 15,
};
// Implement random
COORD_T ray_rand(void *seed)
{
return (COORD_T) rand_r( (unsigned *)seed ) / RAND_MAX;
}
int main()
{
container_init(cont, OBJECTS, MATERIALS, LIGHTS);
// 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->back, 0.0, 0.0, 0.0);
color_set(&s->env_color, 0.13, 0.13, 0.13);
s->env_enabled = false;
s->gfx = &gfx_low;
// Set viewpoint options
vector_set(&s->view.position, 0, 5, 4);
vector_set(&s->view.target, 0, 5, 0);
s->view.width = TESTW;
s->view.height = TESTH;
// Create materials
material_t *m = add_material(cont);
vector_set(&m->color, 1, 1, 1);
m->defuse = 1;
m->specular = 0;
m->shine = 80;
m->reflective = 0.0;
material_t *ml = add_material(cont);
vector_set(&ml->color, 1, 1, 1);
ml->emissive = 8;
material_t *m2 = add_material(cont);
vector_set(&m2->color, 1, 1, 1);
m2->defuse = 0;
m2->specular = 0.1;
m2->shine = 1;
m2->reflective = 1;
material_t *mpl = add_material(cont);
vector_set(&mpl->color, 1, 1, 1);
mpl->defuse = 1;
mpl->specular = 0.0;
mpl->shine = 50;
mpl->reflective = 0;
material_t *mplgreen = add_material(cont);
memcpy(mplgreen, mpl, sizeof(material_t));
vector_set(&mplgreen->color, 0.3, 0.3, 1);
//mplgreen->reflective = 1;
//mplgreen->defuse = 0;
material_t *mplred = add_material(cont);
memcpy(mplred, mplgreen, sizeof(material_t));
vector_set(&mplred->color, 1, 0.3, 0.3);
viewpoint_init(&s->view);
object_t *o;
o = add_object(cont, TYPE_SPHERE);
vector_set(&o->sph.center, 2, 6, -1);
o->sph.radius = 1;
o->m = m2;
o = add_object(cont, TYPE_SPHERE);
vector_set(&o->sph.center, 0, 4, -1);
o->sph.radius = 1.3;
o->m = m;
o = add_object(cont, TYPE_SPHERE);
vector_set(&o->sph.center, -2, 5, -2);
o->sph.radius = 1.3;
o->m = m;
o = add_object(cont, TYPE_PLANE);
vector_set(&o->pl.start, 0, 0, 0);
vector_set(&o->pl.norm, 0, 1, 0);
o->m = mpl;
o = add_object(cont, TYPE_PLANE);
vector_set(&o->pl.start, 0, 10, 0);
vector_set(&o->pl.norm, 0, 1, 0);
o->m = mpl;
o = add_object(cont, TYPE_PLANE);
vector_set(&o->pl.start, 0, 0, -5);
vector_set(&o->pl.norm, 0, 0, 1);
o->m = mpl;
o = add_object(cont, TYPE_PLANE);
vector_set(&o->pl.start, 0, 0, 10);
vector_set(&o->pl.norm, 0, 0, 1);
o->m = mpl;
o = add_object(cont, TYPE_PLANE);
vector_set(&o->pl.start, -5, 0, 0);
vector_set(&o->pl.norm, 1, 0, 0);
o->m = mplgreen;
o = add_object(cont, TYPE_PLANE);
vector_set(&o->pl.start, 5, 0, 0);
vector_set(&o->pl.norm, 1, 0, 0);
o->m = mplred;
// light ball
o = add_object(cont, TYPE_SPHERE);
vector_set(&o->sph.center, 0, 7, 0);
o->sph.radius = 0.5;
o->m = ml;
/*
light_t *l = add_light(cont, TYPE_L_POINT);
vector_set(&l->point.pos, 0, 7, 0);
color_set(&l->color, 1, 1, 1);
l->radiance = 5;
*/
pgm_write_header(stdout, TESTW, TESTH);
// Create image array. Not as memory efficient but much simpler when multiprocessing
color_t *image = malloc( sizeof(color_t) * TESTW * TESTH);
if (!image) {
fprintf(stderr, "Could not allocate image array");
exit(1);
}
if (pthread_mutex_init(&percentlock, NULL)) {
fprintf(stderr, "Could not percent lock\n");
exit(1);
}
// Hire the workers
pthread_t workers[WORKERS];
for (int i = 0; i < WORKERS; i++) {
office_t *office = malloc(sizeof(office_t));
office->id = i;
office->image = image;
// Start them and show them their office(chunk in the array in this case :-D)
int rc = pthread_create(&workers[i], NULL, worker_func, office);
if (rc) {
fprintf(stderr, "Could not create worker %d\nsorry\n", i);
exit(1);
}
}
// Wait for the threads to finish and print as we go
for (int i = 0; i < WORKERS; i++) {
pthread_join(workers[i], NULL);
}
// Print the stuff the worker was responsable for.
for (int y = 0; y < TESTH; y++) {
for(int x = 0; x < TESTW; x++) {
pgm_write_pixel(stdout, &image[ y * TESTW + x]);
}
}
free(image);
}
void *worker_func(void *arg) {
// Organize our office
office_t *office = (office_t *) arg;
int seed;
// Start working
for (int y = office->id; y < TESTH; y += WORKERS) {
// Start time
for (int x = 0; x < TESTW; x++) {
color_t *c = &office->image[ y * TESTW + x ];
//color_t c;
seed = x * y;
path_trace(&cont->space, TESTW - x, TESTH - y, c, &seed);
color_clamp(c);
}
if (y % PERCENTSTEP == 0) {
// Unlock the thingy
pthread_mutex_lock(&percentlock);
fprintf(stderr, "%d%%\n", percent++);
pthread_mutex_unlock(&percentlock);
}
}
//free(office);
pthread_exit(NULL);
}
|