erebus

annotate liberebus/src/erebus.cc @ 37:db8a90307386

implemented console and rudimentary commandline parser
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 09 Jun 2014 16:01:00 +0300
parents d15ee526daa6
children 5e27c85e79ca
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@37 6 #ifndef _MSC_VER
nuclear@37 7 #include <alloca.h>
nuclear@37 8 #else
nuclear@37 9 #include <malloc.h>
nuclear@37 10 #endif
nuclear@2 11 #include "erebus.h"
nuclear@17 12 #include "erebus_impl.h"
nuclear@4 13 #include "scene.h"
nuclear@4 14 #include "geomobj.h"
nuclear@5 15 #include "rt.h"
nuclear@2 16
nuclear@8 17 #define INF_SAMPLES (INT_MAX / 2)
nuclear@8 18
nuclear@2 19 using namespace std::chrono;
nuclear@2 20
nuclear@26 21 static void render_block(struct erebus *ctx, Block blk);
nuclear@8 22 static void render_pixel(struct erebus *ctx, int x, int y, int sample);
nuclear@2 23
nuclear@2 24 static std::mt19937 rnd_gen;
nuclear@2 25
nuclear@2 26 extern "C" {
nuclear@2 27
nuclear@2 28 struct erebus *erb_init(void)
nuclear@2 29 {
nuclear@31 30 struct erebus *ctx = 0;
nuclear@2 31 try {
nuclear@2 32 ctx = new struct erebus;
nuclear@2 33 }
nuclear@2 34 catch(...) {
nuclear@2 35 return 0;
nuclear@2 36 }
nuclear@2 37
nuclear@10 38 rnd_gen.seed(time(0));
nuclear@10 39
nuclear@4 40 ctx->scn = 0;
nuclear@2 41 ctx->cur_time = 0;
nuclear@26 42 ctx->cur_frame = 0;
nuclear@32 43 ctx->tpool = 0;
nuclear@8 44
nuclear@17 45 erb_setoptf(ctx, ERB_OPT_GAMMA, 2.2);
nuclear@17 46 erb_setopti(ctx, ERB_OPT_MAX_ITER, 6);
nuclear@17 47 erb_setopti(ctx, ERB_OPT_MAX_SAMPLES, INF_SAMPLES);
nuclear@17 48 erb_setopti(ctx, ERB_OPT_NUM_THREADS, -1);
nuclear@15 49
nuclear@15 50 ctx->dbg_nodesel = -1;
nuclear@2 51 return ctx;
nuclear@2 52 }
nuclear@2 53
nuclear@2 54 void erb_destroy(struct erebus *ctx)
nuclear@2 55 {
nuclear@31 56 if(ctx) {
nuclear@31 57 // make sure the threadpool stops BEFORE destroying the framebuffers etc in ctx
nuclear@31 58 delete ctx->tpool;
nuclear@31 59 delete ctx;
nuclear@31 60 }
nuclear@2 61 }
nuclear@2 62
nuclear@2 63 void erb_setopti(struct erebus *ctx, enum erb_option opt, int val)
nuclear@2 64 {
nuclear@17 65 ctx->options[opt].ival = val;
nuclear@17 66 ctx->options[opt].type = Option::Type::INT;
nuclear@2 67 }
nuclear@17 68
nuclear@2 69 void erb_setoptf(struct erebus *ctx, enum erb_option opt, float val)
nuclear@2 70 {
nuclear@17 71 ctx->options[opt].fval = val;
nuclear@17 72 ctx->options[opt].type = Option::Type::FLOAT;
nuclear@2 73 }
nuclear@17 74
nuclear@2 75 void erb_setoptfv(struct erebus *ctx, enum erb_option opt, float *vec)
nuclear@2 76 {
nuclear@2 77 for(int i=0; i<4; i++) {
nuclear@17 78 ctx->options[opt].vval[i] = vec[i];
nuclear@2 79 }
nuclear@17 80 ctx->options[opt].type = Option::Type::VEC;
nuclear@2 81 }
nuclear@2 82
nuclear@2 83 int erb_getopti(struct erebus *ctx, enum erb_option opt)
nuclear@2 84 {
nuclear@17 85 switch(ctx->options[opt].type) {
nuclear@17 86 case Option::Type::INT:
nuclear@17 87 return ctx->options[opt].ival;
nuclear@17 88 case Option::Type::FLOAT:
nuclear@17 89 return (int)ctx->options[opt].fval;
nuclear@17 90 case Option::Type::VEC:
nuclear@17 91 return (int)ctx->options[opt].vval.x;
nuclear@17 92 }
nuclear@17 93 return 0; // can't happen
nuclear@2 94 }
nuclear@17 95
nuclear@2 96 float erb_getoptf(struct erebus *ctx, enum erb_option opt)
nuclear@2 97 {
nuclear@17 98 switch(ctx->options[opt].type) {
nuclear@17 99 case Option::Type::INT:
nuclear@17 100 return (float)ctx->options[opt].ival;
nuclear@17 101 case Option::Type::FLOAT:
nuclear@17 102 return ctx->options[opt].fval;
nuclear@17 103 case Option::Type::VEC:
nuclear@17 104 return ctx->options[opt].vval.x;
nuclear@17 105 }
nuclear@17 106 return 0.0f; // can't happen
nuclear@2 107 }
nuclear@17 108
nuclear@2 109 float *erb_getoptfv(struct erebus *ctx, enum erb_option opt)
nuclear@2 110 {
nuclear@17 111 switch(ctx->options[opt].type) {
nuclear@17 112 case Option::Type::INT:
nuclear@17 113 {
nuclear@17 114 int ival = ctx->options[opt].ival;
nuclear@17 115 ctx->options[opt].vval = Vector4(ival, ival, ival, ival);
nuclear@17 116 }
nuclear@17 117 break;
nuclear@17 118 case Option::Type::FLOAT:
nuclear@17 119 {
nuclear@17 120 float fval = ctx->options[opt].fval;
nuclear@17 121 ctx->options[opt].vval = Vector4(fval, fval, fval, fval);
nuclear@17 122 }
nuclear@17 123 default:
nuclear@17 124 break;
nuclear@17 125 }
nuclear@17 126
nuclear@17 127 return &ctx->options[opt].vval.x;
nuclear@2 128 }
nuclear@2 129
nuclear@2 130 float *erb_get_framebuffer(struct erebus *ctx)
nuclear@2 131 {
nuclear@2 132 return ctx->fbimg.get_pixels();
nuclear@2 133 }
nuclear@2 134
nuclear@2 135 void erb_begin_frame(struct erebus *ctx, long ms)
nuclear@2 136 {
nuclear@32 137 if(!ctx->tpool) {
nuclear@32 138 int num_threads = erb_getopti(ctx, ERB_OPT_NUM_THREADS);
nuclear@32 139 ctx->tpool = new ThreadPool(num_threads);
nuclear@32 140 }
nuclear@34 141 ctx->tpool->clear_work(); // remove any previously pending jobs
nuclear@32 142
nuclear@26 143 ++ctx->cur_frame;
nuclear@26 144 ctx->cur_sample = 0;
nuclear@2 145 ctx->cur_time = ms;
nuclear@4 146
nuclear@17 147 int xsz = erb_getopti(ctx, ERB_OPT_WIDTH);
nuclear@17 148 int ysz = erb_getopti(ctx, ERB_OPT_HEIGHT);
nuclear@4 149
nuclear@26 150 if(!ctx->fbimg.get_pixels() || ctx->fbimg.get_width() != xsz || ctx->fbimg.get_height() < ysz) {
nuclear@26 151 ctx->fbimg.create(xsz, ysz);
nuclear@26 152 ctx->accum.create(xsz, ysz);
nuclear@26 153 } else {
nuclear@26 154 ctx->fbimg.clear();
nuclear@26 155 ctx->accum.clear();
nuclear@26 156 }
nuclear@15 157
nuclear@17 158 ctx->inv_gamma = 1.0f / erb_getoptf(ctx, ERB_OPT_GAMMA);
nuclear@26 159
nuclear@26 160 ctx->scn->update(ctx->cur_time);
nuclear@2 161 }
nuclear@2 162
nuclear@2 163 int erb_render(struct erebus *ctx, long timeout)
nuclear@2 164 {
nuclear@2 165 return erb_render_rect(ctx, 0, 0, ctx->fbimg.get_width(), ctx->fbimg.get_height(), timeout);
nuclear@2 166 }
nuclear@2 167
nuclear@26 168 #define BLKSZ 32
nuclear@26 169
nuclear@2 170 int erb_render_rect(struct erebus *ctx, int x, int y, int width, int height, long timeout)
nuclear@2 171 {
nuclear@31 172 while(ctx->tpool->pending()) {
nuclear@26 173 if(timeout > 0) {
nuclear@31 174 long wait_interval = ctx->tpool->wait(timeout);
nuclear@26 175 timeout -= wait_interval;
nuclear@26 176 } else {
nuclear@26 177 return 1;
nuclear@26 178 }
nuclear@26 179 }
nuclear@26 180
nuclear@2 181 if(!width || !height) return -1;
nuclear@2 182
nuclear@26 183 int startx = x;
nuclear@26 184 int endx = x + width;
nuclear@26 185 int endy = y + height;
nuclear@26 186
nuclear@26 187 while(y < endy) {
nuclear@26 188 x = startx;
nuclear@26 189 while(x < endx) {
nuclear@26 190 Block blk;
nuclear@26 191 blk.x = x;
nuclear@26 192 blk.y = y;
nuclear@26 193 blk.width = std::min(BLKSZ, endx - x);
nuclear@26 194 blk.height = std::min(BLKSZ, endy - y);
nuclear@26 195 blk.sample = ctx->cur_sample;
nuclear@26 196 blk.frame = ctx->cur_frame;
nuclear@26 197
nuclear@31 198 ctx->tpool->add_work(std::bind(render_block, ctx, blk));
nuclear@26 199
nuclear@26 200 x += BLKSZ;
nuclear@26 201 }
nuclear@26 202 y += BLKSZ;
nuclear@2 203 }
nuclear@2 204
nuclear@26 205 ++ctx->cur_sample;
nuclear@31 206 ctx->tpool->wait(timeout); // wait for completion
nuclear@26 207 return ctx->cur_sample > erb_getopti(ctx, ERB_OPT_MAX_SAMPLES) ? 0 : 1;
nuclear@26 208 }
nuclear@4 209
nuclear@2 210
nuclear@2 211 int erb_get_progress(struct erebus *ctx)
nuclear@2 212 {
nuclear@32 213 struct erb_render_status st;
nuclear@32 214 if(erb_get_status(ctx, &st) == -1) {
nuclear@32 215 return 0;
nuclear@32 216 }
nuclear@32 217 return st.progress_percent;
nuclear@32 218 }
nuclear@32 219
nuclear@32 220 int erb_get_status(struct erebus *ctx, struct erb_render_status *stat)
nuclear@32 221 {
nuclear@32 222 long pending = ctx->tpool->pending();
nuclear@34 223
nuclear@32 224 int xsz = ctx->fbimg.get_width();
nuclear@32 225 int ysz = ctx->fbimg.get_height();
nuclear@32 226 int xblocks = (xsz + BLKSZ - 1) / BLKSZ;
nuclear@32 227 int yblocks = (ysz + BLKSZ - 1) / BLKSZ;
nuclear@32 228 long num_blocks = xblocks * yblocks;
nuclear@32 229
nuclear@32 230 stat->frames = stat->max_frames = 0; // TODO
nuclear@32 231
nuclear@32 232 stat->blocks = num_blocks - pending;
nuclear@32 233 stat->max_blocks = num_blocks;
nuclear@32 234
nuclear@32 235 stat->samples = ctx->cur_sample ? ctx->cur_sample - 1 : 0;
nuclear@32 236 if((stat->max_samples = erb_getopti(ctx, ERB_OPT_MAX_SAMPLES)) == INF_SAMPLES) {
nuclear@32 237 stat->max_samples = stat->samples;
nuclear@32 238
nuclear@34 239 if(stat->max_blocks) {
nuclear@34 240 stat->progress_percent = 100 * stat->blocks / stat->max_blocks;
nuclear@34 241 } else {
nuclear@34 242 stat->progress_percent = 0;
nuclear@34 243 }
nuclear@32 244 } else {
nuclear@34 245 if(stat->max_samples) {
nuclear@34 246 stat->progress_percent = 100 * stat->samples / stat->max_samples;
nuclear@34 247 } else {
nuclear@34 248 stat->progress_percent = 0;
nuclear@34 249 }
nuclear@32 250 }
nuclear@32 251 return 0;
nuclear@2 252 }
nuclear@2 253
nuclear@2 254 int erb_load_scene(struct erebus *ctx, const char *fname)
nuclear@2 255 {
nuclear@37 256 if(!ctx->scn) {
nuclear@37 257 ctx->scn = new Scene;
nuclear@37 258 }
nuclear@2 259
nuclear@19 260 if(!ctx->scn->load(fname)) {
nuclear@19 261 return -1;
nuclear@19 262 }
nuclear@4 263 return 0;
nuclear@2 264 }
nuclear@2 265
nuclear@37 266 int erb_proc_cmd(struct erebus *ctx, const char *cmd)
nuclear@37 267 {
nuclear@37 268 if(!ctx->scn) {
nuclear@37 269 ctx->scn = new Scene;
nuclear@37 270 }
nuclear@37 271
nuclear@37 272 char *buf = (char*)alloca(strlen(cmd) + 1);
nuclear@37 273 strcpy(buf, cmd);
nuclear@37 274
nuclear@37 275 std::vector<char*> args;
nuclear@37 276 char *tok;
nuclear@37 277 while((tok = strtok(buf, " \t\v\n\r"))) {
nuclear@37 278 buf = 0;
nuclear@37 279 args.push_back(tok);
nuclear@37 280 }
nuclear@37 281 args.push_back(0);
nuclear@37 282
nuclear@37 283 if(!ctx->scn->proc_cmd(args.size() - 1, &args[0])) {
nuclear@37 284 return -1;
nuclear@37 285 }
nuclear@37 286 return 0;
nuclear@37 287 }
nuclear@37 288
nuclear@9 289 bool erb_input_keyboard(struct erebus *ctx, int key, bool pressed)
nuclear@9 290 {
nuclear@9 291 if(!ctx) return false;
nuclear@15 292 if((int)ctx->keystate.size() <= key) {
nuclear@15 293 ctx->keystate.resize(key < 256 ? 256 : key + 1);
nuclear@15 294 }
nuclear@9 295
nuclear@9 296 ctx->keystate[key] = pressed;
nuclear@10 297
nuclear@10 298 if(pressed) {
nuclear@10 299 switch(key) {
nuclear@15 300 case '.':
nuclear@15 301 {
nuclear@15 302 int node_count = ctx->scn->get_node_count();
nuclear@15 303 if(node_count && ++ctx->dbg_nodesel >= node_count) {
nuclear@15 304 ctx->dbg_nodesel = 0;
nuclear@15 305 }
nuclear@15 306 printf("selected node: %d\n", ctx->dbg_nodesel);
nuclear@15 307 }
nuclear@15 308 break;
nuclear@15 309
nuclear@15 310 case ',':
nuclear@15 311 {
nuclear@15 312 int node_count = ctx->scn->get_node_count();
nuclear@15 313 if(node_count && --ctx->dbg_nodesel < 0) {
nuclear@15 314 ctx->dbg_nodesel = node_count - 1;
nuclear@15 315 }
nuclear@15 316 printf("selected node: %d\n", ctx->dbg_nodesel);
nuclear@15 317 }
nuclear@15 318 break;
nuclear@15 319
nuclear@10 320 case '=':
nuclear@10 321 case '-':
nuclear@15 322 case '0':
nuclear@15 323 if(ctx->dbg_nodesel != -1) {
nuclear@15 324 SceneNode *node = ctx->scn->get_node(ctx->dbg_nodesel);
nuclear@15 325 Vector3 s = node->get_scaling();
nuclear@15 326 switch(key) {
nuclear@15 327 case '=':
nuclear@15 328 node->set_scaling(s * 1.1);
nuclear@15 329 break;
nuclear@15 330 case '-':
nuclear@15 331 node->set_scaling(s * 0.9);
nuclear@15 332 break;
nuclear@15 333 case '0':
nuclear@15 334 node->set_scaling(Vector3(1, 1, 1));
nuclear@15 335 break;
nuclear@15 336 }
nuclear@15 337 }
nuclear@15 338 erb_begin_frame(ctx, 0);
nuclear@15 339 return true;
nuclear@10 340 }
nuclear@10 341 }
nuclear@9 342 return false;
nuclear@9 343 }
nuclear@9 344
nuclear@9 345 bool erb_input_mouse_button(struct erebus *ctx, int bn, bool pressed, int x, int y)
nuclear@9 346 {
nuclear@9 347 if(!ctx) return false;
nuclear@15 348 if((int)ctx->bnstate.size() <= bn) {
nuclear@15 349 ctx->bnstate.resize(bn < 32 ? 32 : bn + 1);
nuclear@15 350 }
nuclear@9 351
nuclear@9 352 ctx->bnstate[bn] = pressed;
nuclear@9 353 ctx->mouse_pos[0] = x;
nuclear@9 354 ctx->mouse_pos[1] = y;
nuclear@10 355 return false;
nuclear@9 356 }
nuclear@9 357
nuclear@10 358 bool erb_input_mouse_motion(struct erebus *ctx, int x, int y)
nuclear@10 359 {
nuclear@18 360 bool res = false;
nuclear@18 361
nuclear@18 362 if(!ctx) return res;
nuclear@18 363
nuclear@18 364 int dx = x - ctx->mouse_pos[0];
nuclear@18 365 int dy = y - ctx->mouse_pos[1];
nuclear@18 366
nuclear@18 367 if(dx || dy) {
nuclear@18 368 TargetCamera *cam = (TargetCamera*)ctx->scn->get_active_camera();
nuclear@18 369 if(cam && ctx->bnstate[0]) {
nuclear@18 370 Vector3 cpos = cam->get_position();
nuclear@18 371 float mag = cpos.length();
nuclear@18 372
nuclear@19 373 float theta = atan2(cpos.z / mag, cpos.x / mag) - DEG_TO_RAD(dx * 0.5);
nuclear@19 374 float phi = acos(cpos.y / mag) - DEG_TO_RAD(dy * 0.5);
nuclear@18 375
nuclear@18 376 if(phi < 0) phi = 0;
nuclear@18 377 if(phi > M_PI) phi = M_PI;
nuclear@18 378
nuclear@18 379 cpos.x = cos(theta) * sin(phi) * mag;
nuclear@18 380 cpos.y = cos(phi) * mag;
nuclear@18 381 cpos.z = sin(theta) * sin(phi) * mag;
nuclear@18 382 cam->set_position(cpos);
nuclear@18 383
nuclear@18 384 erb_begin_frame(ctx, 0);
nuclear@18 385 res = true;
nuclear@18 386 }
nuclear@18 387 }
nuclear@10 388
nuclear@10 389 ctx->mouse_pos[0] = x;
nuclear@10 390 ctx->mouse_pos[1] = y;
nuclear@18 391 return res;
nuclear@10 392 }
nuclear@10 393
nuclear@10 394 bool erb_input_6dof_button(struct erebus *ctx, int bn, bool pressed)
nuclear@10 395 {
nuclear@10 396 if(!ctx) return false;
nuclear@10 397 return false;
nuclear@10 398 }
nuclear@10 399
nuclear@10 400 bool erb_input_6dof_motion(struct erebus *ctx, float x, float y, float z)
nuclear@10 401 {
nuclear@10 402 if(!ctx) return false;
nuclear@10 403 return false;
nuclear@10 404 }
nuclear@9 405
nuclear@9 406
nuclear@2 407 } // extern "C"
nuclear@2 408
nuclear@2 409 float randf(float low, float high)
nuclear@2 410 {
nuclear@2 411 std::uniform_real_distribution<float> unirnd(low, high);
nuclear@2 412 return unirnd(rnd_gen);
nuclear@2 413 }
nuclear@2 414
nuclear@26 415 static void render_block(struct erebus *ctx, Block blk)
nuclear@26 416 {
nuclear@26 417 if(blk.frame < ctx->cur_frame) {
nuclear@26 418 return; // skip stale blocks
nuclear@26 419 }
nuclear@26 420
nuclear@26 421 for(int i=0; i<blk.height; i++) {
nuclear@26 422 for(int j=0; j<blk.width; j++) {
nuclear@26 423 render_pixel(ctx, blk.x + j, blk.y + i, blk.sample);
nuclear@26 424 }
nuclear@26 425 }
nuclear@26 426 }
nuclear@26 427
nuclear@8 428 static void render_pixel(struct erebus *ctx, int x, int y, int sample)
nuclear@2 429 {
nuclear@5 430 Camera *cam = ctx->scn->get_active_camera();
nuclear@5 431 if(!cam) return;
nuclear@5 432
nuclear@5 433 int xsz = ctx->fbimg.get_width();
nuclear@5 434 int ysz = ctx->fbimg.get_height();
nuclear@8 435 int offs = (y * xsz + x) * 4;
nuclear@5 436
nuclear@8 437 float *pix = ctx->fbimg.get_pixels() + offs;
nuclear@8 438 float *accum = ctx->accum.get_pixels() + offs;
nuclear@8 439
nuclear@8 440 Ray ray = cam->get_primary_ray(x, y, xsz, ysz, sample);
nuclear@17 441 Color c = ray_trace(ctx, ray, 0);
nuclear@8 442 accum[0] += c.x;
nuclear@8 443 accum[1] += c.y;
nuclear@8 444 accum[2] += c.z;
nuclear@8 445 accum[3] += c.w;
nuclear@8 446
nuclear@8 447 float inv_samples = 1.0f / (float)(sample + 1);
nuclear@17 448 pix[0] = pow(accum[0] * inv_samples, ctx->inv_gamma);
nuclear@17 449 pix[1] = pow(accum[1] * inv_samples, ctx->inv_gamma);
nuclear@17 450 pix[2] = pow(accum[2] * inv_samples, ctx->inv_gamma);
nuclear@8 451 pix[3] = accum[3] * inv_samples;
nuclear@2 452 }