erebus

view 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
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 if(!ctx->scn->load(fname)) {
207 return -1;
208 }
209 return 0;
210 }
212 bool erb_input_keyboard(struct erebus *ctx, int key, bool pressed)
213 {
214 if(!ctx) return false;
215 if((int)ctx->keystate.size() <= key) {
216 ctx->keystate.resize(key < 256 ? 256 : key + 1);
217 }
219 ctx->keystate[key] = pressed;
221 if(pressed) {
222 switch(key) {
223 case '.':
224 {
225 int node_count = ctx->scn->get_node_count();
226 if(node_count && ++ctx->dbg_nodesel >= node_count) {
227 ctx->dbg_nodesel = 0;
228 }
229 printf("selected node: %d\n", ctx->dbg_nodesel);
230 }
231 break;
233 case ',':
234 {
235 int node_count = ctx->scn->get_node_count();
236 if(node_count && --ctx->dbg_nodesel < 0) {
237 ctx->dbg_nodesel = node_count - 1;
238 }
239 printf("selected node: %d\n", ctx->dbg_nodesel);
240 }
241 break;
243 case '=':
244 case '-':
245 case '0':
246 if(ctx->dbg_nodesel != -1) {
247 SceneNode *node = ctx->scn->get_node(ctx->dbg_nodesel);
248 Vector3 s = node->get_scaling();
249 switch(key) {
250 case '=':
251 node->set_scaling(s * 1.1);
252 break;
253 case '-':
254 node->set_scaling(s * 0.9);
255 break;
256 case '0':
257 node->set_scaling(Vector3(1, 1, 1));
258 break;
259 }
260 }
261 erb_begin_frame(ctx, 0);
262 return true;
263 }
264 }
265 return false;
266 }
268 bool erb_input_mouse_button(struct erebus *ctx, int bn, bool pressed, int x, int y)
269 {
270 if(!ctx) return false;
271 if((int)ctx->bnstate.size() <= bn) {
272 ctx->bnstate.resize(bn < 32 ? 32 : bn + 1);
273 }
275 ctx->bnstate[bn] = pressed;
276 ctx->mouse_pos[0] = x;
277 ctx->mouse_pos[1] = y;
278 return false;
279 }
281 bool erb_input_mouse_motion(struct erebus *ctx, int x, int y)
282 {
283 bool res = false;
285 if(!ctx) return res;
287 int dx = x - ctx->mouse_pos[0];
288 int dy = y - ctx->mouse_pos[1];
290 if(dx || dy) {
291 TargetCamera *cam = (TargetCamera*)ctx->scn->get_active_camera();
292 if(cam && ctx->bnstate[0]) {
293 Vector3 cpos = cam->get_position();
294 float mag = cpos.length();
296 float theta = atan2(cpos.z / mag, cpos.x / mag) - DEG_TO_RAD(dx * 0.5);
297 float phi = acos(cpos.y / mag) - DEG_TO_RAD(dy * 0.5);
299 if(phi < 0) phi = 0;
300 if(phi > M_PI) phi = M_PI;
302 cpos.x = cos(theta) * sin(phi) * mag;
303 cpos.y = cos(phi) * mag;
304 cpos.z = sin(theta) * sin(phi) * mag;
305 cam->set_position(cpos);
307 erb_begin_frame(ctx, 0);
308 res = true;
309 }
310 }
312 ctx->mouse_pos[0] = x;
313 ctx->mouse_pos[1] = y;
314 return res;
315 }
317 bool erb_input_6dof_button(struct erebus *ctx, int bn, bool pressed)
318 {
319 if(!ctx) return false;
320 return false;
321 }
323 bool erb_input_6dof_motion(struct erebus *ctx, float x, float y, float z)
324 {
325 if(!ctx) return false;
326 return false;
327 }
330 } // extern "C"
332 float randf(float low, float high)
333 {
334 std::uniform_real_distribution<float> unirnd(low, high);
335 return unirnd(rnd_gen);
336 }
338 static void render_pixel(struct erebus *ctx, int x, int y, int sample)
339 {
340 Camera *cam = ctx->scn->get_active_camera();
341 if(!cam) return;
343 int xsz = ctx->fbimg.get_width();
344 int ysz = ctx->fbimg.get_height();
345 int offs = (y * xsz + x) * 4;
347 float *pix = ctx->fbimg.get_pixels() + offs;
348 float *accum = ctx->accum.get_pixels() + offs;
350 Ray ray = cam->get_primary_ray(x, y, xsz, ysz, sample);
351 Color c = ray_trace(ctx, ray, 0);
352 accum[0] += c.x;
353 accum[1] += c.y;
354 accum[2] += c.z;
355 accum[3] += c.w;
357 float inv_samples = 1.0f / (float)(sample + 1);
358 pix[0] = pow(accum[0] * inv_samples, ctx->inv_gamma);
359 pix[1] = pow(accum[1] * inv_samples, ctx->inv_gamma);
360 pix[2] = pow(accum[2] * inv_samples, ctx->inv_gamma);
361 pix[3] = accum[3] * inv_samples;
362 }
364 bool Rect::operator ==(const Rect &r) const
365 {
366 return memcmp(this, &r, sizeof r) == 0;
367 }
369 bool Rect::operator !=(const Rect &r) const
370 {
371 return memcmp(this, &r, sizeof r) != 0;
372 }