erebus

view liberebus/src/erebus.cc @ 18:09028848f276

- implemented Box object intersection - implemented interactive camera manipulation
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 26 May 2014 23:34:12 +0300
parents e9da2916bc79
children 6204e4d3f445
line source
1 #include <string.h>
2 #include <limits.h>
3 #include <chrono>
4 #include <random>
5 #include "erebus.h"
6 #include "erebus_impl.h"
7 #include "scene.h"
8 #include "geomobj.h"
9 #include "rt.h"
11 #define INF_SAMPLES (INT_MAX / 2)
13 using namespace std::chrono;
15 #define INVALID_RECT Rect{0, 0, 0, 0}
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;
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_rect = INVALID_RECT;
39 erb_setoptf(ctx, ERB_OPT_GAMMA, 2.2);
40 erb_setopti(ctx, ERB_OPT_MAX_ITER, 6);
41 erb_setopti(ctx, ERB_OPT_MAX_SAMPLES, INF_SAMPLES);
42 erb_setopti(ctx, ERB_OPT_NUM_THREADS, -1);
44 ctx->dbg_nodesel = -1;
45 return ctx;
46 }
48 void erb_destroy(struct erebus *ctx)
49 {
50 delete ctx;
51 }
53 void erb_setopti(struct erebus *ctx, enum erb_option opt, int val)
54 {
55 ctx->options[opt].ival = val;
56 ctx->options[opt].type = Option::Type::INT;
57 }
59 void erb_setoptf(struct erebus *ctx, enum erb_option opt, float val)
60 {
61 ctx->options[opt].fval = val;
62 ctx->options[opt].type = Option::Type::FLOAT;
63 }
65 void erb_setoptfv(struct erebus *ctx, enum erb_option opt, float *vec)
66 {
67 for(int i=0; i<4; i++) {
68 ctx->options[opt].vval[i] = vec[i];
69 }
70 ctx->options[opt].type = Option::Type::VEC;
71 }
73 int erb_getopti(struct erebus *ctx, enum erb_option opt)
74 {
75 switch(ctx->options[opt].type) {
76 case Option::Type::INT:
77 return ctx->options[opt].ival;
78 case Option::Type::FLOAT:
79 return (int)ctx->options[opt].fval;
80 case Option::Type::VEC:
81 return (int)ctx->options[opt].vval.x;
82 }
83 return 0; // can't happen
84 }
86 float erb_getoptf(struct erebus *ctx, enum erb_option opt)
87 {
88 switch(ctx->options[opt].type) {
89 case Option::Type::INT:
90 return (float)ctx->options[opt].ival;
91 case Option::Type::FLOAT:
92 return ctx->options[opt].fval;
93 case Option::Type::VEC:
94 return ctx->options[opt].vval.x;
95 }
96 return 0.0f; // can't happen
97 }
99 float *erb_getoptfv(struct erebus *ctx, enum erb_option opt)
100 {
101 switch(ctx->options[opt].type) {
102 case Option::Type::INT:
103 {
104 int ival = ctx->options[opt].ival;
105 ctx->options[opt].vval = Vector4(ival, ival, ival, ival);
106 }
107 break;
108 case Option::Type::FLOAT:
109 {
110 float fval = ctx->options[opt].fval;
111 ctx->options[opt].vval = Vector4(fval, fval, fval, fval);
112 }
113 default:
114 break;
115 }
117 return &ctx->options[opt].vval.x;
118 }
120 float *erb_get_framebuffer(struct erebus *ctx)
121 {
122 return ctx->fbimg.get_pixels();
123 }
125 void erb_begin_frame(struct erebus *ctx, long ms)
126 {
127 printf("starting new frame...\n");
128 ctx->cur_time = ms;
130 int xsz = erb_getopti(ctx, ERB_OPT_WIDTH);
131 int ysz = erb_getopti(ctx, ERB_OPT_HEIGHT);
133 ctx->fbimg.create(xsz, ysz);
134 ctx->accum.create(xsz, ysz);
136 ctx->cur_rect = INVALID_RECT;
137 ctx->inv_gamma = 1.0f / erb_getoptf(ctx, ERB_OPT_GAMMA);
138 }
140 int erb_render(struct erebus *ctx, long timeout)
141 {
142 return erb_render_rect(ctx, 0, 0, ctx->fbimg.get_width(), ctx->fbimg.get_height(), timeout);
143 }
145 int erb_render_rect(struct erebus *ctx, int x, int y, int width, int height, long timeout)
146 {
147 if(!width || !height) return -1;
149 Rect rect{x, y, width, height};
150 if(ctx->cur_rect != rect) {
151 // starting a new rendering apparently
152 ctx->cur_rect = rect;
153 ctx->cur_pixel_x = x;
154 ctx->cur_pixel_y = y;
155 ctx->cur_sample = 0;
156 }
158 ctx->scn->update();
160 int max_samples = erb_getopti(ctx, ERB_OPT_MAX_SAMPLES);
162 if(timeout > 0) {
163 auto start_time = steady_clock::now();
164 while(duration_cast<milliseconds>(steady_clock::now() - start_time).count() < timeout) {
165 render_pixel(ctx, ctx->cur_pixel_x, ctx->cur_pixel_y, ctx->cur_sample);
167 if(++ctx->cur_pixel_x >= ctx->cur_rect.width) {
168 ctx->cur_pixel_x = ctx->cur_rect.x;
169 if(++ctx->cur_pixel_y >= ctx->cur_rect.height) {
170 ctx->cur_pixel_y = ctx->cur_rect.y;
171 if(++ctx->cur_sample >= max_samples) {
172 ctx->cur_rect = INVALID_RECT;
173 return 0;
174 }
175 }
176 }
177 }
178 return 1;
179 }
181 if(max_samples == INF_SAMPLES) {
182 // don't allow infinite samples when rendering non-progressively
183 max_samples = 128;
184 }
186 for(int i=0; i<height; i++) {
187 for(int j=0; j<width; j++) {
188 for(int k=0; k<max_samples; k++) {
189 render_pixel(ctx, j, i, k);
190 }
191 }
192 }
193 return 0;
194 }
196 int erb_get_progress(struct erebus *ctx)
197 {
198 return 0; // TODO
199 }
201 int erb_load_scene(struct erebus *ctx, const char *fname)
202 {
203 delete ctx->scn;
204 ctx->scn = new Scene;
206 // XXX for now just create a test scene here
207 Sphere *sph = new Sphere;
208 sph->mtl.set_attrib("diffuse", Color(1.0, 0.3, 0.2));
209 SceneNode *sph_node = new SceneNode(sph);
210 ctx->scn->add_object(sph);
211 ctx->scn->add_node(sph_node);
213 Box *box = new Box;
214 box->mtl.set_attrib("diffuse", Color(0.9, 0.8, 0.75));
215 SceneNode *box_node = new SceneNode(box);
216 box_node->set_position(Vector3(0, -1.25, 0));
217 box_node->set_scaling(Vector3(5, 0.5, 5));
218 ctx->scn->add_object(box);
219 ctx->scn->add_node(box_node);
221 Sphere *lt = new Sphere;
222 lt->mtl.set_attrib("emissive", Color(10, 10, 10));
223 SceneNode *lt_node = new SceneNode(lt);
224 lt_node->set_position(Vector3(-15, 15, -10));
225 lt_node->set_scaling(Vector3(8, 8, 8));
226 ctx->scn->add_object(lt);
227 ctx->scn->add_node(lt_node);
229 TargetCamera *cam = new TargetCamera(Vector3(0, 4, -8), Vector3(0, 0, 0));
230 //ctx->scn->add_object(cam);
231 ctx->scn->use_camera(cam);
233 return 0;
234 }
236 bool erb_input_keyboard(struct erebus *ctx, int key, bool pressed)
237 {
238 if(!ctx) return false;
239 if((int)ctx->keystate.size() <= key) {
240 ctx->keystate.resize(key < 256 ? 256 : key + 1);
241 }
243 ctx->keystate[key] = pressed;
245 if(pressed) {
246 switch(key) {
247 case '.':
248 {
249 int node_count = ctx->scn->get_node_count();
250 if(node_count && ++ctx->dbg_nodesel >= node_count) {
251 ctx->dbg_nodesel = 0;
252 }
253 printf("selected node: %d\n", ctx->dbg_nodesel);
254 }
255 break;
257 case ',':
258 {
259 int node_count = ctx->scn->get_node_count();
260 if(node_count && --ctx->dbg_nodesel < 0) {
261 ctx->dbg_nodesel = node_count - 1;
262 }
263 printf("selected node: %d\n", ctx->dbg_nodesel);
264 }
265 break;
267 case '=':
268 case '-':
269 case '0':
270 if(ctx->dbg_nodesel != -1) {
271 SceneNode *node = ctx->scn->get_node(ctx->dbg_nodesel);
272 Vector3 s = node->get_scaling();
273 switch(key) {
274 case '=':
275 node->set_scaling(s * 1.1);
276 break;
277 case '-':
278 node->set_scaling(s * 0.9);
279 break;
280 case '0':
281 node->set_scaling(Vector3(1, 1, 1));
282 break;
283 }
284 }
285 erb_begin_frame(ctx, 0);
286 return true;
287 }
288 }
289 return false;
290 }
292 bool erb_input_mouse_button(struct erebus *ctx, int bn, bool pressed, int x, int y)
293 {
294 if(!ctx) return false;
295 if((int)ctx->bnstate.size() <= bn) {
296 ctx->bnstate.resize(bn < 32 ? 32 : bn + 1);
297 }
299 ctx->bnstate[bn] = pressed;
300 ctx->mouse_pos[0] = x;
301 ctx->mouse_pos[1] = y;
302 return false;
303 }
305 bool erb_input_mouse_motion(struct erebus *ctx, int x, int y)
306 {
307 bool res = false;
309 if(!ctx) return res;
311 int dx = x - ctx->mouse_pos[0];
312 int dy = y - ctx->mouse_pos[1];
314 if(dx || dy) {
315 TargetCamera *cam = (TargetCamera*)ctx->scn->get_active_camera();
316 if(cam && ctx->bnstate[0]) {
317 Vector3 cpos = cam->get_position();
318 float mag = cpos.length();
320 float theta = atan2(cpos.z / mag, cpos.x / mag) + DEG_TO_RAD(dx * 0.5);
321 float phi = acos(cpos.y / mag) + DEG_TO_RAD(dy * 0.5);
323 if(phi < 0) phi = 0;
324 if(phi > M_PI) phi = M_PI;
326 cpos.x = cos(theta) * sin(phi) * mag;
327 cpos.y = cos(phi) * mag;
328 cpos.z = sin(theta) * sin(phi) * mag;
329 cam->set_position(cpos);
331 erb_begin_frame(ctx, 0);
332 res = true;
333 }
334 }
336 ctx->mouse_pos[0] = x;
337 ctx->mouse_pos[1] = y;
338 return res;
339 }
341 bool erb_input_6dof_button(struct erebus *ctx, int bn, bool pressed)
342 {
343 if(!ctx) return false;
344 return false;
345 }
347 bool erb_input_6dof_motion(struct erebus *ctx, float x, float y, float z)
348 {
349 if(!ctx) return false;
350 return false;
351 }
354 } // extern "C"
356 float randf(float low, float high)
357 {
358 std::uniform_real_distribution<float> unirnd(low, high);
359 return unirnd(rnd_gen);
360 }
362 static void render_pixel(struct erebus *ctx, int x, int y, int sample)
363 {
364 Camera *cam = ctx->scn->get_active_camera();
365 if(!cam) return;
367 int xsz = ctx->fbimg.get_width();
368 int ysz = ctx->fbimg.get_height();
369 int offs = (y * xsz + x) * 4;
371 float *pix = ctx->fbimg.get_pixels() + offs;
372 float *accum = ctx->accum.get_pixels() + offs;
374 Ray ray = cam->get_primary_ray(x, y, xsz, ysz, sample);
375 Color c = ray_trace(ctx, ray, 0);
376 accum[0] += c.x;
377 accum[1] += c.y;
378 accum[2] += c.z;
379 accum[3] += c.w;
381 float inv_samples = 1.0f / (float)(sample + 1);
382 pix[0] = pow(accum[0] * inv_samples, ctx->inv_gamma);
383 pix[1] = pow(accum[1] * inv_samples, ctx->inv_gamma);
384 pix[2] = pow(accum[2] * inv_samples, ctx->inv_gamma);
385 pix[3] = accum[3] * inv_samples;
386 }
388 bool Rect::operator ==(const Rect &r) const
389 {
390 return memcmp(this, &r, sizeof r) == 0;
391 }
393 bool Rect::operator !=(const Rect &r) const
394 {
395 return memcmp(this, &r, sizeof r) != 0;
396 }