erebus

view liberebus/src/erebus.cc @ 32:b1fc96c71bcc

- lambert BRDF importance sampling - UI + commandline arguments - font rendering for showing status/progress
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 07 Jun 2014 13:36:36 +0300
parents 53a98c148bf8
children d15ee526daa6
line source
1 #include <string.h>
2 #include <limits.h>
3 #include <algorithm>
4 #include <chrono>
5 #include <random>
6 #include "erebus.h"
7 #include "erebus_impl.h"
8 #include "scene.h"
9 #include "geomobj.h"
10 #include "rt.h"
12 #define INF_SAMPLES (INT_MAX / 2)
14 using namespace std::chrono;
16 static void render_block(struct erebus *ctx, Block blk);
17 static void render_pixel(struct erebus *ctx, int x, int y, int sample);
19 static std::mt19937 rnd_gen;
21 extern "C" {
23 struct erebus *erb_init(void)
24 {
25 struct erebus *ctx = 0;
26 try {
27 ctx = new struct erebus;
28 }
29 catch(...) {
30 return 0;
31 }
33 rnd_gen.seed(time(0));
35 ctx->scn = 0;
36 ctx->cur_time = 0;
37 ctx->cur_frame = 0;
38 ctx->tpool = 0;
40 erb_setoptf(ctx, ERB_OPT_GAMMA, 2.2);
41 erb_setopti(ctx, ERB_OPT_MAX_ITER, 6);
42 erb_setopti(ctx, ERB_OPT_MAX_SAMPLES, INF_SAMPLES);
43 erb_setopti(ctx, ERB_OPT_NUM_THREADS, -1);
45 ctx->dbg_nodesel = -1;
46 return ctx;
47 }
49 void erb_destroy(struct erebus *ctx)
50 {
51 if(ctx) {
52 // make sure the threadpool stops BEFORE destroying the framebuffers etc in ctx
53 delete ctx->tpool;
54 delete ctx;
55 }
56 }
58 void erb_setopti(struct erebus *ctx, enum erb_option opt, int val)
59 {
60 ctx->options[opt].ival = val;
61 ctx->options[opt].type = Option::Type::INT;
62 }
64 void erb_setoptf(struct erebus *ctx, enum erb_option opt, float val)
65 {
66 ctx->options[opt].fval = val;
67 ctx->options[opt].type = Option::Type::FLOAT;
68 }
70 void erb_setoptfv(struct erebus *ctx, enum erb_option opt, float *vec)
71 {
72 for(int i=0; i<4; i++) {
73 ctx->options[opt].vval[i] = vec[i];
74 }
75 ctx->options[opt].type = Option::Type::VEC;
76 }
78 int erb_getopti(struct erebus *ctx, enum erb_option opt)
79 {
80 switch(ctx->options[opt].type) {
81 case Option::Type::INT:
82 return ctx->options[opt].ival;
83 case Option::Type::FLOAT:
84 return (int)ctx->options[opt].fval;
85 case Option::Type::VEC:
86 return (int)ctx->options[opt].vval.x;
87 }
88 return 0; // can't happen
89 }
91 float erb_getoptf(struct erebus *ctx, enum erb_option opt)
92 {
93 switch(ctx->options[opt].type) {
94 case Option::Type::INT:
95 return (float)ctx->options[opt].ival;
96 case Option::Type::FLOAT:
97 return ctx->options[opt].fval;
98 case Option::Type::VEC:
99 return ctx->options[opt].vval.x;
100 }
101 return 0.0f; // can't happen
102 }
104 float *erb_getoptfv(struct erebus *ctx, enum erb_option opt)
105 {
106 switch(ctx->options[opt].type) {
107 case Option::Type::INT:
108 {
109 int ival = ctx->options[opt].ival;
110 ctx->options[opt].vval = Vector4(ival, ival, ival, ival);
111 }
112 break;
113 case Option::Type::FLOAT:
114 {
115 float fval = ctx->options[opt].fval;
116 ctx->options[opt].vval = Vector4(fval, fval, fval, fval);
117 }
118 default:
119 break;
120 }
122 return &ctx->options[opt].vval.x;
123 }
125 float *erb_get_framebuffer(struct erebus *ctx)
126 {
127 return ctx->fbimg.get_pixels();
128 }
130 void erb_begin_frame(struct erebus *ctx, long ms)
131 {
132 if(!ctx->tpool) {
133 int num_threads = erb_getopti(ctx, ERB_OPT_NUM_THREADS);
134 ctx->tpool = new ThreadPool(num_threads);
135 }
137 ++ctx->cur_frame;
138 ctx->cur_sample = 0;
139 ctx->cur_time = ms;
141 int xsz = erb_getopti(ctx, ERB_OPT_WIDTH);
142 int ysz = erb_getopti(ctx, ERB_OPT_HEIGHT);
144 if(!ctx->fbimg.get_pixels() || ctx->fbimg.get_width() != xsz || ctx->fbimg.get_height() < ysz) {
145 ctx->fbimg.create(xsz, ysz);
146 ctx->accum.create(xsz, ysz);
147 } else {
148 ctx->fbimg.clear();
149 ctx->accum.clear();
150 }
152 ctx->inv_gamma = 1.0f / erb_getoptf(ctx, ERB_OPT_GAMMA);
154 ctx->scn->update(ctx->cur_time);
155 }
157 int erb_render(struct erebus *ctx, long timeout)
158 {
159 return erb_render_rect(ctx, 0, 0, ctx->fbimg.get_width(), ctx->fbimg.get_height(), timeout);
160 }
162 #define BLKSZ 32
164 int erb_render_rect(struct erebus *ctx, int x, int y, int width, int height, long timeout)
165 {
166 while(ctx->tpool->pending()) {
167 if(timeout > 0) {
168 long wait_interval = ctx->tpool->wait(timeout);
169 timeout -= wait_interval;
170 } else {
171 return 1;
172 }
173 }
175 if(!width || !height) return -1;
177 int startx = x;
178 int endx = x + width;
179 int endy = y + height;
181 while(y < endy) {
182 x = startx;
183 while(x < endx) {
184 Block blk;
185 blk.x = x;
186 blk.y = y;
187 blk.width = std::min(BLKSZ, endx - x);
188 blk.height = std::min(BLKSZ, endy - y);
189 blk.sample = ctx->cur_sample;
190 blk.frame = ctx->cur_frame;
192 ctx->tpool->add_work(std::bind(render_block, ctx, blk));
194 x += BLKSZ;
195 }
196 y += BLKSZ;
197 }
199 ++ctx->cur_sample;
200 ctx->tpool->wait(timeout); // wait for completion
201 return ctx->cur_sample > erb_getopti(ctx, ERB_OPT_MAX_SAMPLES) ? 0 : 1;
202 }
205 int erb_get_progress(struct erebus *ctx)
206 {
207 struct erb_render_status st;
208 if(erb_get_status(ctx, &st) == -1) {
209 return 0;
210 }
211 return st.progress_percent;
212 }
214 int erb_get_status(struct erebus *ctx, struct erb_render_status *stat)
215 {
216 long pending = ctx->tpool->pending();
217 if(!pending) {
218 return -1;
219 }
220 int xsz = ctx->fbimg.get_width();
221 int ysz = ctx->fbimg.get_height();
222 int xblocks = (xsz + BLKSZ - 1) / BLKSZ;
223 int yblocks = (ysz + BLKSZ - 1) / BLKSZ;
224 long num_blocks = xblocks * yblocks;
226 stat->frames = stat->max_frames = 0; // TODO
228 stat->blocks = num_blocks - pending;
229 stat->max_blocks = num_blocks;
231 stat->samples = ctx->cur_sample ? ctx->cur_sample - 1 : 0;
232 if((stat->max_samples = erb_getopti(ctx, ERB_OPT_MAX_SAMPLES)) == INF_SAMPLES) {
233 stat->max_samples = stat->samples;
235 stat->progress_percent = 100 * stat->blocks / stat->max_blocks;
236 } else {
237 stat->progress_percent = 100 * stat->samples / stat->max_samples;
238 }
239 return 0;
240 }
242 int erb_load_scene(struct erebus *ctx, const char *fname)
243 {
244 delete ctx->scn;
245 ctx->scn = new Scene;
247 if(!ctx->scn->load(fname)) {
248 return -1;
249 }
250 return 0;
251 }
253 bool erb_input_keyboard(struct erebus *ctx, int key, bool pressed)
254 {
255 if(!ctx) return false;
256 if((int)ctx->keystate.size() <= key) {
257 ctx->keystate.resize(key < 256 ? 256 : key + 1);
258 }
260 ctx->keystate[key] = pressed;
262 if(pressed) {
263 switch(key) {
264 case '.':
265 {
266 int node_count = ctx->scn->get_node_count();
267 if(node_count && ++ctx->dbg_nodesel >= node_count) {
268 ctx->dbg_nodesel = 0;
269 }
270 printf("selected node: %d\n", ctx->dbg_nodesel);
271 }
272 break;
274 case ',':
275 {
276 int node_count = ctx->scn->get_node_count();
277 if(node_count && --ctx->dbg_nodesel < 0) {
278 ctx->dbg_nodesel = node_count - 1;
279 }
280 printf("selected node: %d\n", ctx->dbg_nodesel);
281 }
282 break;
284 case '=':
285 case '-':
286 case '0':
287 if(ctx->dbg_nodesel != -1) {
288 SceneNode *node = ctx->scn->get_node(ctx->dbg_nodesel);
289 Vector3 s = node->get_scaling();
290 switch(key) {
291 case '=':
292 node->set_scaling(s * 1.1);
293 break;
294 case '-':
295 node->set_scaling(s * 0.9);
296 break;
297 case '0':
298 node->set_scaling(Vector3(1, 1, 1));
299 break;
300 }
301 }
302 erb_begin_frame(ctx, 0);
303 return true;
304 }
305 }
306 return false;
307 }
309 bool erb_input_mouse_button(struct erebus *ctx, int bn, bool pressed, int x, int y)
310 {
311 if(!ctx) return false;
312 if((int)ctx->bnstate.size() <= bn) {
313 ctx->bnstate.resize(bn < 32 ? 32 : bn + 1);
314 }
316 ctx->bnstate[bn] = pressed;
317 ctx->mouse_pos[0] = x;
318 ctx->mouse_pos[1] = y;
319 return false;
320 }
322 bool erb_input_mouse_motion(struct erebus *ctx, int x, int y)
323 {
324 bool res = false;
326 if(!ctx) return res;
328 int dx = x - ctx->mouse_pos[0];
329 int dy = y - ctx->mouse_pos[1];
331 if(dx || dy) {
332 TargetCamera *cam = (TargetCamera*)ctx->scn->get_active_camera();
333 if(cam && ctx->bnstate[0]) {
334 Vector3 cpos = cam->get_position();
335 float mag = cpos.length();
337 float theta = atan2(cpos.z / mag, cpos.x / mag) - DEG_TO_RAD(dx * 0.5);
338 float phi = acos(cpos.y / mag) - DEG_TO_RAD(dy * 0.5);
340 if(phi < 0) phi = 0;
341 if(phi > M_PI) phi = M_PI;
343 cpos.x = cos(theta) * sin(phi) * mag;
344 cpos.y = cos(phi) * mag;
345 cpos.z = sin(theta) * sin(phi) * mag;
346 cam->set_position(cpos);
348 erb_begin_frame(ctx, 0);
349 res = true;
350 }
351 }
353 ctx->mouse_pos[0] = x;
354 ctx->mouse_pos[1] = y;
355 return res;
356 }
358 bool erb_input_6dof_button(struct erebus *ctx, int bn, bool pressed)
359 {
360 if(!ctx) return false;
361 return false;
362 }
364 bool erb_input_6dof_motion(struct erebus *ctx, float x, float y, float z)
365 {
366 if(!ctx) return false;
367 return false;
368 }
371 } // extern "C"
373 float randf(float low, float high)
374 {
375 std::uniform_real_distribution<float> unirnd(low, high);
376 return unirnd(rnd_gen);
377 }
379 static void render_block(struct erebus *ctx, Block blk)
380 {
381 if(blk.frame < ctx->cur_frame) {
382 return; // skip stale blocks
383 }
385 for(int i=0; i<blk.height; i++) {
386 for(int j=0; j<blk.width; j++) {
387 render_pixel(ctx, blk.x + j, blk.y + i, blk.sample);
388 }
389 }
390 }
392 static void render_pixel(struct erebus *ctx, int x, int y, int sample)
393 {
394 Camera *cam = ctx->scn->get_active_camera();
395 if(!cam) return;
397 int xsz = ctx->fbimg.get_width();
398 int ysz = ctx->fbimg.get_height();
399 int offs = (y * xsz + x) * 4;
401 float *pix = ctx->fbimg.get_pixels() + offs;
402 float *accum = ctx->accum.get_pixels() + offs;
404 Ray ray = cam->get_primary_ray(x, y, xsz, ysz, sample);
405 Color c = ray_trace(ctx, ray, 0);
406 accum[0] += c.x;
407 accum[1] += c.y;
408 accum[2] += c.z;
409 accum[3] += c.w;
411 float inv_samples = 1.0f / (float)(sample + 1);
412 pix[0] = pow(accum[0] * inv_samples, ctx->inv_gamma);
413 pix[1] = pow(accum[1] * inv_samples, ctx->inv_gamma);
414 pix[2] = pow(accum[2] * inv_samples, ctx->inv_gamma);
415 pix[3] = accum[3] * inv_samples;
416 }