rev |
line source |
nuclear@2
|
1 #include <string.h>
|
nuclear@2
|
2 #include <limits.h>
|
nuclear@27
|
3 #include <algorithm>
|
nuclear@2
|
4 #include <chrono>
|
nuclear@2
|
5 #include <random>
|
nuclear@2
|
6 #include "erebus.h"
|
nuclear@17
|
7 #include "erebus_impl.h"
|
nuclear@4
|
8 #include "scene.h"
|
nuclear@4
|
9 #include "geomobj.h"
|
nuclear@5
|
10 #include "rt.h"
|
nuclear@2
|
11
|
nuclear@8
|
12 #define INF_SAMPLES (INT_MAX / 2)
|
nuclear@8
|
13
|
nuclear@2
|
14 using namespace std::chrono;
|
nuclear@2
|
15
|
nuclear@26
|
16 static void render_block(struct erebus *ctx, Block blk);
|
nuclear@8
|
17 static void render_pixel(struct erebus *ctx, int x, int y, int sample);
|
nuclear@2
|
18
|
nuclear@2
|
19 static std::mt19937 rnd_gen;
|
nuclear@2
|
20
|
nuclear@2
|
21 extern "C" {
|
nuclear@2
|
22
|
nuclear@2
|
23 struct erebus *erb_init(void)
|
nuclear@2
|
24 {
|
nuclear@2
|
25 struct erebus *ctx;
|
nuclear@2
|
26 try {
|
nuclear@2
|
27 ctx = new struct erebus;
|
nuclear@2
|
28 }
|
nuclear@2
|
29 catch(...) {
|
nuclear@2
|
30 return 0;
|
nuclear@2
|
31 }
|
nuclear@2
|
32
|
nuclear@10
|
33 rnd_gen.seed(time(0));
|
nuclear@10
|
34
|
nuclear@4
|
35 ctx->scn = 0;
|
nuclear@2
|
36 ctx->cur_time = 0;
|
nuclear@26
|
37 ctx->cur_frame = 0;
|
nuclear@8
|
38
|
nuclear@17
|
39 erb_setoptf(ctx, ERB_OPT_GAMMA, 2.2);
|
nuclear@17
|
40 erb_setopti(ctx, ERB_OPT_MAX_ITER, 6);
|
nuclear@17
|
41 erb_setopti(ctx, ERB_OPT_MAX_SAMPLES, INF_SAMPLES);
|
nuclear@17
|
42 erb_setopti(ctx, ERB_OPT_NUM_THREADS, -1);
|
nuclear@15
|
43
|
nuclear@15
|
44 ctx->dbg_nodesel = -1;
|
nuclear@2
|
45 return ctx;
|
nuclear@2
|
46 }
|
nuclear@2
|
47
|
nuclear@2
|
48 void erb_destroy(struct erebus *ctx)
|
nuclear@2
|
49 {
|
nuclear@2
|
50 delete ctx;
|
nuclear@2
|
51 }
|
nuclear@2
|
52
|
nuclear@2
|
53 void erb_setopti(struct erebus *ctx, enum erb_option opt, int val)
|
nuclear@2
|
54 {
|
nuclear@17
|
55 ctx->options[opt].ival = val;
|
nuclear@17
|
56 ctx->options[opt].type = Option::Type::INT;
|
nuclear@2
|
57 }
|
nuclear@17
|
58
|
nuclear@2
|
59 void erb_setoptf(struct erebus *ctx, enum erb_option opt, float val)
|
nuclear@2
|
60 {
|
nuclear@17
|
61 ctx->options[opt].fval = val;
|
nuclear@17
|
62 ctx->options[opt].type = Option::Type::FLOAT;
|
nuclear@2
|
63 }
|
nuclear@17
|
64
|
nuclear@2
|
65 void erb_setoptfv(struct erebus *ctx, enum erb_option opt, float *vec)
|
nuclear@2
|
66 {
|
nuclear@2
|
67 for(int i=0; i<4; i++) {
|
nuclear@17
|
68 ctx->options[opt].vval[i] = vec[i];
|
nuclear@2
|
69 }
|
nuclear@17
|
70 ctx->options[opt].type = Option::Type::VEC;
|
nuclear@2
|
71 }
|
nuclear@2
|
72
|
nuclear@2
|
73 int erb_getopti(struct erebus *ctx, enum erb_option opt)
|
nuclear@2
|
74 {
|
nuclear@17
|
75 switch(ctx->options[opt].type) {
|
nuclear@17
|
76 case Option::Type::INT:
|
nuclear@17
|
77 return ctx->options[opt].ival;
|
nuclear@17
|
78 case Option::Type::FLOAT:
|
nuclear@17
|
79 return (int)ctx->options[opt].fval;
|
nuclear@17
|
80 case Option::Type::VEC:
|
nuclear@17
|
81 return (int)ctx->options[opt].vval.x;
|
nuclear@17
|
82 }
|
nuclear@17
|
83 return 0; // can't happen
|
nuclear@2
|
84 }
|
nuclear@17
|
85
|
nuclear@2
|
86 float erb_getoptf(struct erebus *ctx, enum erb_option opt)
|
nuclear@2
|
87 {
|
nuclear@17
|
88 switch(ctx->options[opt].type) {
|
nuclear@17
|
89 case Option::Type::INT:
|
nuclear@17
|
90 return (float)ctx->options[opt].ival;
|
nuclear@17
|
91 case Option::Type::FLOAT:
|
nuclear@17
|
92 return ctx->options[opt].fval;
|
nuclear@17
|
93 case Option::Type::VEC:
|
nuclear@17
|
94 return ctx->options[opt].vval.x;
|
nuclear@17
|
95 }
|
nuclear@17
|
96 return 0.0f; // can't happen
|
nuclear@2
|
97 }
|
nuclear@17
|
98
|
nuclear@2
|
99 float *erb_getoptfv(struct erebus *ctx, enum erb_option opt)
|
nuclear@2
|
100 {
|
nuclear@17
|
101 switch(ctx->options[opt].type) {
|
nuclear@17
|
102 case Option::Type::INT:
|
nuclear@17
|
103 {
|
nuclear@17
|
104 int ival = ctx->options[opt].ival;
|
nuclear@17
|
105 ctx->options[opt].vval = Vector4(ival, ival, ival, ival);
|
nuclear@17
|
106 }
|
nuclear@17
|
107 break;
|
nuclear@17
|
108 case Option::Type::FLOAT:
|
nuclear@17
|
109 {
|
nuclear@17
|
110 float fval = ctx->options[opt].fval;
|
nuclear@17
|
111 ctx->options[opt].vval = Vector4(fval, fval, fval, fval);
|
nuclear@17
|
112 }
|
nuclear@17
|
113 default:
|
nuclear@17
|
114 break;
|
nuclear@17
|
115 }
|
nuclear@17
|
116
|
nuclear@17
|
117 return &ctx->options[opt].vval.x;
|
nuclear@2
|
118 }
|
nuclear@2
|
119
|
nuclear@2
|
120 float *erb_get_framebuffer(struct erebus *ctx)
|
nuclear@2
|
121 {
|
nuclear@2
|
122 return ctx->fbimg.get_pixels();
|
nuclear@2
|
123 }
|
nuclear@2
|
124
|
nuclear@2
|
125 void erb_begin_frame(struct erebus *ctx, long ms)
|
nuclear@2
|
126 {
|
nuclear@8
|
127 printf("starting new frame...\n");
|
nuclear@26
|
128 ++ctx->cur_frame;
|
nuclear@26
|
129 ctx->cur_sample = 0;
|
nuclear@2
|
130 ctx->cur_time = ms;
|
nuclear@4
|
131
|
nuclear@17
|
132 int xsz = erb_getopti(ctx, ERB_OPT_WIDTH);
|
nuclear@17
|
133 int ysz = erb_getopti(ctx, ERB_OPT_HEIGHT);
|
nuclear@4
|
134
|
nuclear@26
|
135 if(!ctx->fbimg.get_pixels() || ctx->fbimg.get_width() != xsz || ctx->fbimg.get_height() < ysz) {
|
nuclear@26
|
136 ctx->fbimg.create(xsz, ysz);
|
nuclear@26
|
137 ctx->accum.create(xsz, ysz);
|
nuclear@26
|
138 } else {
|
nuclear@26
|
139 ctx->fbimg.clear();
|
nuclear@26
|
140 ctx->accum.clear();
|
nuclear@26
|
141 }
|
nuclear@15
|
142
|
nuclear@17
|
143 ctx->inv_gamma = 1.0f / erb_getoptf(ctx, ERB_OPT_GAMMA);
|
nuclear@26
|
144
|
nuclear@26
|
145 ctx->scn->update(ctx->cur_time);
|
nuclear@2
|
146 }
|
nuclear@2
|
147
|
nuclear@2
|
148 int erb_render(struct erebus *ctx, long timeout)
|
nuclear@2
|
149 {
|
nuclear@2
|
150 return erb_render_rect(ctx, 0, 0, ctx->fbimg.get_width(), ctx->fbimg.get_height(), timeout);
|
nuclear@2
|
151 }
|
nuclear@2
|
152
|
nuclear@26
|
153 #define BLKSZ 32
|
nuclear@26
|
154
|
nuclear@2
|
155 int erb_render_rect(struct erebus *ctx, int x, int y, int width, int height, long timeout)
|
nuclear@2
|
156 {
|
nuclear@26
|
157 while(ctx->tpool.pending()) {
|
nuclear@26
|
158 if(timeout > 0) {
|
nuclear@26
|
159 long wait_interval = ctx->tpool.wait(timeout);
|
nuclear@26
|
160 timeout -= wait_interval;
|
nuclear@26
|
161 } else {
|
nuclear@26
|
162 return 1;
|
nuclear@26
|
163 }
|
nuclear@26
|
164 }
|
nuclear@26
|
165
|
nuclear@2
|
166 if(!width || !height) return -1;
|
nuclear@2
|
167
|
nuclear@26
|
168 int startx = x;
|
nuclear@26
|
169 int endx = x + width;
|
nuclear@26
|
170 int endy = y + height;
|
nuclear@26
|
171
|
nuclear@26
|
172 while(y < endy) {
|
nuclear@26
|
173 x = startx;
|
nuclear@26
|
174 while(x < endx) {
|
nuclear@26
|
175 Block blk;
|
nuclear@26
|
176 blk.x = x;
|
nuclear@26
|
177 blk.y = y;
|
nuclear@26
|
178 blk.width = std::min(BLKSZ, endx - x);
|
nuclear@26
|
179 blk.height = std::min(BLKSZ, endy - y);
|
nuclear@26
|
180 blk.sample = ctx->cur_sample;
|
nuclear@26
|
181 blk.frame = ctx->cur_frame;
|
nuclear@26
|
182
|
nuclear@26
|
183 ctx->tpool.add_work(std::bind(render_block, ctx, blk));
|
nuclear@26
|
184
|
nuclear@26
|
185 x += BLKSZ;
|
nuclear@26
|
186 }
|
nuclear@26
|
187 y += BLKSZ;
|
nuclear@2
|
188 }
|
nuclear@2
|
189
|
nuclear@26
|
190 ++ctx->cur_sample;
|
nuclear@26
|
191 ctx->tpool.wait(timeout); // wait for completion
|
nuclear@26
|
192 return ctx->cur_sample > erb_getopti(ctx, ERB_OPT_MAX_SAMPLES) ? 0 : 1;
|
nuclear@26
|
193 }
|
nuclear@4
|
194
|
nuclear@2
|
195
|
nuclear@2
|
196 int erb_get_progress(struct erebus *ctx)
|
nuclear@2
|
197 {
|
nuclear@2
|
198 return 0; // TODO
|
nuclear@2
|
199 }
|
nuclear@2
|
200
|
nuclear@2
|
201 int erb_load_scene(struct erebus *ctx, const char *fname)
|
nuclear@2
|
202 {
|
nuclear@4
|
203 delete ctx->scn;
|
nuclear@4
|
204 ctx->scn = new Scene;
|
nuclear@2
|
205
|
nuclear@19
|
206 if(!ctx->scn->load(fname)) {
|
nuclear@19
|
207 return -1;
|
nuclear@19
|
208 }
|
nuclear@4
|
209 return 0;
|
nuclear@2
|
210 }
|
nuclear@2
|
211
|
nuclear@9
|
212 bool erb_input_keyboard(struct erebus *ctx, int key, bool pressed)
|
nuclear@9
|
213 {
|
nuclear@9
|
214 if(!ctx) return false;
|
nuclear@15
|
215 if((int)ctx->keystate.size() <= key) {
|
nuclear@15
|
216 ctx->keystate.resize(key < 256 ? 256 : key + 1);
|
nuclear@15
|
217 }
|
nuclear@9
|
218
|
nuclear@9
|
219 ctx->keystate[key] = pressed;
|
nuclear@10
|
220
|
nuclear@10
|
221 if(pressed) {
|
nuclear@10
|
222 switch(key) {
|
nuclear@15
|
223 case '.':
|
nuclear@15
|
224 {
|
nuclear@15
|
225 int node_count = ctx->scn->get_node_count();
|
nuclear@15
|
226 if(node_count && ++ctx->dbg_nodesel >= node_count) {
|
nuclear@15
|
227 ctx->dbg_nodesel = 0;
|
nuclear@15
|
228 }
|
nuclear@15
|
229 printf("selected node: %d\n", ctx->dbg_nodesel);
|
nuclear@15
|
230 }
|
nuclear@15
|
231 break;
|
nuclear@15
|
232
|
nuclear@15
|
233 case ',':
|
nuclear@15
|
234 {
|
nuclear@15
|
235 int node_count = ctx->scn->get_node_count();
|
nuclear@15
|
236 if(node_count && --ctx->dbg_nodesel < 0) {
|
nuclear@15
|
237 ctx->dbg_nodesel = node_count - 1;
|
nuclear@15
|
238 }
|
nuclear@15
|
239 printf("selected node: %d\n", ctx->dbg_nodesel);
|
nuclear@15
|
240 }
|
nuclear@15
|
241 break;
|
nuclear@15
|
242
|
nuclear@10
|
243 case '=':
|
nuclear@10
|
244 case '-':
|
nuclear@15
|
245 case '0':
|
nuclear@15
|
246 if(ctx->dbg_nodesel != -1) {
|
nuclear@15
|
247 SceneNode *node = ctx->scn->get_node(ctx->dbg_nodesel);
|
nuclear@15
|
248 Vector3 s = node->get_scaling();
|
nuclear@15
|
249 switch(key) {
|
nuclear@15
|
250 case '=':
|
nuclear@15
|
251 node->set_scaling(s * 1.1);
|
nuclear@15
|
252 break;
|
nuclear@15
|
253 case '-':
|
nuclear@15
|
254 node->set_scaling(s * 0.9);
|
nuclear@15
|
255 break;
|
nuclear@15
|
256 case '0':
|
nuclear@15
|
257 node->set_scaling(Vector3(1, 1, 1));
|
nuclear@15
|
258 break;
|
nuclear@15
|
259 }
|
nuclear@15
|
260 }
|
nuclear@15
|
261 erb_begin_frame(ctx, 0);
|
nuclear@15
|
262 return true;
|
nuclear@10
|
263 }
|
nuclear@10
|
264 }
|
nuclear@9
|
265 return false;
|
nuclear@9
|
266 }
|
nuclear@9
|
267
|
nuclear@9
|
268 bool erb_input_mouse_button(struct erebus *ctx, int bn, bool pressed, int x, int y)
|
nuclear@9
|
269 {
|
nuclear@9
|
270 if(!ctx) return false;
|
nuclear@15
|
271 if((int)ctx->bnstate.size() <= bn) {
|
nuclear@15
|
272 ctx->bnstate.resize(bn < 32 ? 32 : bn + 1);
|
nuclear@15
|
273 }
|
nuclear@9
|
274
|
nuclear@9
|
275 ctx->bnstate[bn] = pressed;
|
nuclear@9
|
276 ctx->mouse_pos[0] = x;
|
nuclear@9
|
277 ctx->mouse_pos[1] = y;
|
nuclear@10
|
278 return false;
|
nuclear@9
|
279 }
|
nuclear@9
|
280
|
nuclear@10
|
281 bool erb_input_mouse_motion(struct erebus *ctx, int x, int y)
|
nuclear@10
|
282 {
|
nuclear@18
|
283 bool res = false;
|
nuclear@18
|
284
|
nuclear@18
|
285 if(!ctx) return res;
|
nuclear@18
|
286
|
nuclear@18
|
287 int dx = x - ctx->mouse_pos[0];
|
nuclear@18
|
288 int dy = y - ctx->mouse_pos[1];
|
nuclear@18
|
289
|
nuclear@18
|
290 if(dx || dy) {
|
nuclear@18
|
291 TargetCamera *cam = (TargetCamera*)ctx->scn->get_active_camera();
|
nuclear@18
|
292 if(cam && ctx->bnstate[0]) {
|
nuclear@18
|
293 Vector3 cpos = cam->get_position();
|
nuclear@18
|
294 float mag = cpos.length();
|
nuclear@18
|
295
|
nuclear@19
|
296 float theta = atan2(cpos.z / mag, cpos.x / mag) - DEG_TO_RAD(dx * 0.5);
|
nuclear@19
|
297 float phi = acos(cpos.y / mag) - DEG_TO_RAD(dy * 0.5);
|
nuclear@18
|
298
|
nuclear@18
|
299 if(phi < 0) phi = 0;
|
nuclear@18
|
300 if(phi > M_PI) phi = M_PI;
|
nuclear@18
|
301
|
nuclear@18
|
302 cpos.x = cos(theta) * sin(phi) * mag;
|
nuclear@18
|
303 cpos.y = cos(phi) * mag;
|
nuclear@18
|
304 cpos.z = sin(theta) * sin(phi) * mag;
|
nuclear@18
|
305 cam->set_position(cpos);
|
nuclear@18
|
306
|
nuclear@18
|
307 erb_begin_frame(ctx, 0);
|
nuclear@18
|
308 res = true;
|
nuclear@18
|
309 }
|
nuclear@18
|
310 }
|
nuclear@10
|
311
|
nuclear@10
|
312 ctx->mouse_pos[0] = x;
|
nuclear@10
|
313 ctx->mouse_pos[1] = y;
|
nuclear@18
|
314 return res;
|
nuclear@10
|
315 }
|
nuclear@10
|
316
|
nuclear@10
|
317 bool erb_input_6dof_button(struct erebus *ctx, int bn, bool pressed)
|
nuclear@10
|
318 {
|
nuclear@10
|
319 if(!ctx) return false;
|
nuclear@10
|
320 return false;
|
nuclear@10
|
321 }
|
nuclear@10
|
322
|
nuclear@10
|
323 bool erb_input_6dof_motion(struct erebus *ctx, float x, float y, float z)
|
nuclear@10
|
324 {
|
nuclear@10
|
325 if(!ctx) return false;
|
nuclear@10
|
326 return false;
|
nuclear@10
|
327 }
|
nuclear@9
|
328
|
nuclear@9
|
329
|
nuclear@2
|
330 } // extern "C"
|
nuclear@2
|
331
|
nuclear@2
|
332 float randf(float low, float high)
|
nuclear@2
|
333 {
|
nuclear@2
|
334 std::uniform_real_distribution<float> unirnd(low, high);
|
nuclear@2
|
335 return unirnd(rnd_gen);
|
nuclear@2
|
336 }
|
nuclear@2
|
337
|
nuclear@26
|
338 static void render_block(struct erebus *ctx, Block blk)
|
nuclear@26
|
339 {
|
nuclear@26
|
340 if(blk.frame < ctx->cur_frame) {
|
nuclear@26
|
341 return; // skip stale blocks
|
nuclear@26
|
342 }
|
nuclear@26
|
343
|
nuclear@26
|
344 for(int i=0; i<blk.height; i++) {
|
nuclear@26
|
345 for(int j=0; j<blk.width; j++) {
|
nuclear@26
|
346 render_pixel(ctx, blk.x + j, blk.y + i, blk.sample);
|
nuclear@26
|
347 }
|
nuclear@26
|
348 }
|
nuclear@26
|
349 }
|
nuclear@26
|
350
|
nuclear@8
|
351 static void render_pixel(struct erebus *ctx, int x, int y, int sample)
|
nuclear@2
|
352 {
|
nuclear@5
|
353 Camera *cam = ctx->scn->get_active_camera();
|
nuclear@5
|
354 if(!cam) return;
|
nuclear@5
|
355
|
nuclear@5
|
356 int xsz = ctx->fbimg.get_width();
|
nuclear@5
|
357 int ysz = ctx->fbimg.get_height();
|
nuclear@8
|
358 int offs = (y * xsz + x) * 4;
|
nuclear@5
|
359
|
nuclear@8
|
360 float *pix = ctx->fbimg.get_pixels() + offs;
|
nuclear@8
|
361 float *accum = ctx->accum.get_pixels() + offs;
|
nuclear@8
|
362
|
nuclear@8
|
363 Ray ray = cam->get_primary_ray(x, y, xsz, ysz, sample);
|
nuclear@17
|
364 Color c = ray_trace(ctx, ray, 0);
|
nuclear@8
|
365 accum[0] += c.x;
|
nuclear@8
|
366 accum[1] += c.y;
|
nuclear@8
|
367 accum[2] += c.z;
|
nuclear@8
|
368 accum[3] += c.w;
|
nuclear@8
|
369
|
nuclear@8
|
370 float inv_samples = 1.0f / (float)(sample + 1);
|
nuclear@17
|
371 pix[0] = pow(accum[0] * inv_samples, ctx->inv_gamma);
|
nuclear@17
|
372 pix[1] = pow(accum[1] * inv_samples, ctx->inv_gamma);
|
nuclear@17
|
373 pix[2] = pow(accum[2] * inv_samples, ctx->inv_gamma);
|
nuclear@8
|
374 pix[3] = accum[3] * inv_samples;
|
nuclear@2
|
375 }
|