erebus

annotate liberebus/src/erebus.cc @ 19:6204e4d3f445

scene loading
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 27 May 2014 07:43:55 +0300
parents 09028848f276
children c8a6fb04fefa
rev   line source
nuclear@2 1 #include <string.h>
nuclear@2 2 #include <limits.h>
nuclear@2 3 #include <chrono>
nuclear@2 4 #include <random>
nuclear@2 5 #include "erebus.h"
nuclear@17 6 #include "erebus_impl.h"
nuclear@4 7 #include "scene.h"
nuclear@4 8 #include "geomobj.h"
nuclear@5 9 #include "rt.h"
nuclear@2 10
nuclear@8 11 #define INF_SAMPLES (INT_MAX / 2)
nuclear@8 12
nuclear@2 13 using namespace std::chrono;
nuclear@2 14
nuclear@2 15 #define INVALID_RECT Rect{0, 0, 0, 0}
nuclear@2 16
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@2 37 ctx->cur_rect = INVALID_RECT;
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@2 128 ctx->cur_time = ms;
nuclear@4 129
nuclear@17 130 int xsz = erb_getopti(ctx, ERB_OPT_WIDTH);
nuclear@17 131 int ysz = erb_getopti(ctx, ERB_OPT_HEIGHT);
nuclear@4 132
nuclear@4 133 ctx->fbimg.create(xsz, ysz);
nuclear@8 134 ctx->accum.create(xsz, ysz);
nuclear@15 135
nuclear@15 136 ctx->cur_rect = INVALID_RECT;
nuclear@17 137 ctx->inv_gamma = 1.0f / erb_getoptf(ctx, ERB_OPT_GAMMA);
nuclear@2 138 }
nuclear@2 139
nuclear@2 140 int erb_render(struct erebus *ctx, long timeout)
nuclear@2 141 {
nuclear@2 142 return erb_render_rect(ctx, 0, 0, ctx->fbimg.get_width(), ctx->fbimg.get_height(), timeout);
nuclear@2 143 }
nuclear@2 144
nuclear@2 145 int erb_render_rect(struct erebus *ctx, int x, int y, int width, int height, long timeout)
nuclear@2 146 {
nuclear@2 147 if(!width || !height) return -1;
nuclear@2 148
nuclear@2 149 Rect rect{x, y, width, height};
nuclear@2 150 if(ctx->cur_rect != rect) {
nuclear@8 151 // starting a new rendering apparently
nuclear@2 152 ctx->cur_rect = rect;
nuclear@2 153 ctx->cur_pixel_x = x;
nuclear@2 154 ctx->cur_pixel_y = y;
nuclear@8 155 ctx->cur_sample = 0;
nuclear@2 156 }
nuclear@2 157
nuclear@4 158 ctx->scn->update();
nuclear@4 159
nuclear@17 160 int max_samples = erb_getopti(ctx, ERB_OPT_MAX_SAMPLES);
nuclear@8 161
nuclear@2 162 if(timeout > 0) {
nuclear@2 163 auto start_time = steady_clock::now();
nuclear@2 164 while(duration_cast<milliseconds>(steady_clock::now() - start_time).count() < timeout) {
nuclear@8 165 render_pixel(ctx, ctx->cur_pixel_x, ctx->cur_pixel_y, ctx->cur_sample);
nuclear@2 166
nuclear@2 167 if(++ctx->cur_pixel_x >= ctx->cur_rect.width) {
nuclear@8 168 ctx->cur_pixel_x = ctx->cur_rect.x;
nuclear@2 169 if(++ctx->cur_pixel_y >= ctx->cur_rect.height) {
nuclear@8 170 ctx->cur_pixel_y = ctx->cur_rect.y;
nuclear@8 171 if(++ctx->cur_sample >= max_samples) {
nuclear@8 172 ctx->cur_rect = INVALID_RECT;
nuclear@8 173 return 0;
nuclear@8 174 }
nuclear@2 175 }
nuclear@2 176 }
nuclear@2 177 }
nuclear@2 178 return 1;
nuclear@2 179 }
nuclear@2 180
nuclear@17 181 if(max_samples == INF_SAMPLES) {
nuclear@17 182 // don't allow infinite samples when rendering non-progressively
nuclear@8 183 max_samples = 128;
nuclear@8 184 }
nuclear@8 185
nuclear@2 186 for(int i=0; i<height; i++) {
nuclear@2 187 for(int j=0; j<width; j++) {
nuclear@8 188 for(int k=0; k<max_samples; k++) {
nuclear@8 189 render_pixel(ctx, j, i, k);
nuclear@8 190 }
nuclear@2 191 }
nuclear@2 192 }
nuclear@2 193 return 0;
nuclear@2 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@8 338 static void render_pixel(struct erebus *ctx, int x, int y, int sample)
nuclear@2 339 {
nuclear@5 340 Camera *cam = ctx->scn->get_active_camera();
nuclear@5 341 if(!cam) return;
nuclear@5 342
nuclear@5 343 int xsz = ctx->fbimg.get_width();
nuclear@5 344 int ysz = ctx->fbimg.get_height();
nuclear@8 345 int offs = (y * xsz + x) * 4;
nuclear@5 346
nuclear@8 347 float *pix = ctx->fbimg.get_pixels() + offs;
nuclear@8 348 float *accum = ctx->accum.get_pixels() + offs;
nuclear@8 349
nuclear@8 350 Ray ray = cam->get_primary_ray(x, y, xsz, ysz, sample);
nuclear@17 351 Color c = ray_trace(ctx, ray, 0);
nuclear@8 352 accum[0] += c.x;
nuclear@8 353 accum[1] += c.y;
nuclear@8 354 accum[2] += c.z;
nuclear@8 355 accum[3] += c.w;
nuclear@8 356
nuclear@8 357 float inv_samples = 1.0f / (float)(sample + 1);
nuclear@17 358 pix[0] = pow(accum[0] * inv_samples, ctx->inv_gamma);
nuclear@17 359 pix[1] = pow(accum[1] * inv_samples, ctx->inv_gamma);
nuclear@17 360 pix[2] = pow(accum[2] * inv_samples, ctx->inv_gamma);
nuclear@8 361 pix[3] = accum[3] * inv_samples;
nuclear@2 362 }
nuclear@17 363
nuclear@17 364 bool Rect::operator ==(const Rect &r) const
nuclear@17 365 {
nuclear@17 366 return memcmp(this, &r, sizeof r) == 0;
nuclear@17 367 }
nuclear@17 368
nuclear@17 369 bool Rect::operator !=(const Rect &r) const
nuclear@17 370 {
nuclear@17 371 return memcmp(this, &r, sizeof r) != 0;
nuclear@17 372 }
nuclear@17 373