erebus

view liberebus/src/erebus.cc @ 9:d38e13d6063c

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 24 May 2014 17:22:53 +0300
parents e2d9bf168a41
children 506e114b7ca2
line source
1 #include <string.h>
2 #include <limits.h>
3 #include <chrono>
4 #include <random>
5 #include "erebus.h"
6 #include "vmath/vector.h"
7 #include "image.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 struct Rect {
17 int x, y, width, height;
19 bool operator ==(const Rect &r) { return memcmp(this, &r, sizeof r) == 0; }
20 bool operator !=(const Rect &r) { return memcmp(this, &r, sizeof r) != 0; }
21 };
23 #define INVALID_RECT Rect{0, 0, 0, 0}
25 struct erebus {
26 Scene *scn;
28 Image<float> fbimg;
29 Image<float> accum; // sample accumulator per pixel
30 Vector4 options[ERB_NUM_OPTIONS];
32 // render state
33 long cur_time;
34 int cur_pixel_x, cur_pixel_y;
35 Rect cur_rect;
36 int cur_sample;
38 // interactive input
39 std::vector<bool> keystate;
40 std::vector<bool> bnstate;
41 int mouse_pos[2];
42 };
44 static void render_pixel(struct erebus *ctx, int x, int y, int sample);
46 static std::mt19937 rnd_gen;
48 extern "C" {
50 struct erebus *erb_init(void)
51 {
52 struct erebus *ctx;
53 try {
54 ctx = new struct erebus;
55 }
56 catch(...) {
57 return 0;
58 }
60 ctx->scn = 0;
61 ctx->cur_time = 0;
62 ctx->cur_rect = INVALID_RECT;
64 ctx->options[ERB_OPT_MAX_SAMPLES].x = (float)INF_SAMPLES;
65 return ctx;
66 }
68 void erb_destroy(struct erebus *ctx)
69 {
70 delete ctx;
71 }
73 void erb_setopti(struct erebus *ctx, enum erb_option opt, int val)
74 {
75 ctx->options[opt].x = val;
76 }
77 void erb_setoptf(struct erebus *ctx, enum erb_option opt, float val)
78 {
79 ctx->options[opt].x = val;
80 }
81 void erb_setoptfv(struct erebus *ctx, enum erb_option opt, float *vec)
82 {
83 for(int i=0; i<4; i++) {
84 ctx->options[opt][i] = vec[i];
85 }
86 }
88 int erb_getopti(struct erebus *ctx, enum erb_option opt)
89 {
90 return ctx->options[opt].x;
91 }
92 float erb_getoptf(struct erebus *ctx, enum erb_option opt)
93 {
94 return ctx->options[opt].x;
95 }
96 float *erb_getoptfv(struct erebus *ctx, enum erb_option opt)
97 {
98 return &ctx->options[opt].x;
99 }
101 float *erb_get_framebuffer(struct erebus *ctx)
102 {
103 return ctx->fbimg.get_pixels();
104 }
106 void erb_begin_frame(struct erebus *ctx, long ms)
107 {
108 printf("starting new frame...\n");
109 ctx->cur_time = ms;
111 int xsz = ctx->options[ERB_OPT_WIDTH].x;
112 int ysz = ctx->options[ERB_OPT_HEIGHT].x;
114 ctx->fbimg.create(xsz, ysz);
115 ctx->accum.create(xsz, ysz);
116 }
118 int erb_render(struct erebus *ctx, long timeout)
119 {
120 return erb_render_rect(ctx, 0, 0, ctx->fbimg.get_width(), ctx->fbimg.get_height(), timeout);
121 }
123 int erb_render_rect(struct erebus *ctx, int x, int y, int width, int height, long timeout)
124 {
125 if(!width || !height) return -1;
127 Rect rect{x, y, width, height};
128 if(ctx->cur_rect != rect) {
129 // starting a new rendering apparently
130 ctx->cur_rect = rect;
131 ctx->cur_pixel_x = x;
132 ctx->cur_pixel_y = y;
133 ctx->cur_sample = 0;
134 }
136 ctx->scn->update();
138 int max_samples = ctx->options[ERB_OPT_MAX_SAMPLES].x;
140 if(timeout > 0) {
141 auto start_time = steady_clock::now();
142 while(duration_cast<milliseconds>(steady_clock::now() - start_time).count() < timeout) {
143 render_pixel(ctx, ctx->cur_pixel_x, ctx->cur_pixel_y, ctx->cur_sample);
145 if(++ctx->cur_pixel_x >= ctx->cur_rect.width) {
146 ctx->cur_pixel_x = ctx->cur_rect.x;
147 if(++ctx->cur_pixel_y >= ctx->cur_rect.height) {
148 ctx->cur_pixel_y = ctx->cur_rect.y;
149 if(++ctx->cur_sample >= max_samples) {
150 ctx->cur_rect = INVALID_RECT;
151 return 0;
152 }
153 }
154 }
155 }
156 return 1;
157 }
159 if(ctx->options[ERB_OPT_MAX_SAMPLES].x == (float)INF_SAMPLES) {
160 max_samples = 128;
161 }
163 for(int i=0; i<height; i++) {
164 for(int j=0; j<width; j++) {
165 for(int k=0; k<max_samples; k++) {
166 render_pixel(ctx, j, i, k);
167 }
168 }
169 }
170 return 0;
171 }
173 int erb_get_progress(struct erebus *ctx)
174 {
175 return 0; // TODO
176 }
178 int erb_load_scene(struct erebus *ctx, const char *fname)
179 {
180 delete ctx->scn;
181 ctx->scn = new Scene;
183 // XXX for now just create a test scene here
184 Sphere *sph = new Sphere;
185 sph->mtl.set_attrib("albedo", Color(1.0, 0.3, 0.2));
186 SceneNode *sph_node = new SceneNode(sph);
187 ctx->scn->add_object(sph);
188 ctx->scn->add_node(sph_node);
190 sph = new Sphere;
191 sph->mtl.set_attrib("albedo", Color(0.3, 0.4, 1.0));
192 sph_node = new SceneNode(sph);
193 sph_node->set_position(Vector3(0, -3.0, 0));
194 //sph_node->set_scaling(Vector3(4.0, 4.0, 4.0) * 0.3);
195 ctx->scn->add_object(sph);
196 ctx->scn->add_node(sph_node);
198 Sphere *lt = new Sphere;
199 lt->mtl.set_attrib("emissive", Color(10, 10, 10));
200 SceneNode *lt_node = new SceneNode(lt);
201 lt_node->set_position(Vector3(-15, 15, -10));
202 lt_node->set_scaling(Vector3(5, 5, 5));
203 ctx->scn->add_object(lt);
204 ctx->scn->add_node(lt_node);
206 TargetCamera *cam = new TargetCamera(Vector3(0, 4, -8), Vector3(0, 0, 0));
207 //ctx->scn->add_object(cam);
208 ctx->scn->use_camera(cam);
210 return 0;
211 }
213 bool erb_input_keyboard(struct erebus *ctx, int key, bool pressed)
214 {
215 if(!ctx) return false;
216 if(ctx->keystate.size() <= key) ctx->keystate.resize(key < 256 ? 256 : key + 1);
218 ctx->keystate[key] = pressed;
219 return false;
220 }
222 bool erb_input_mouse_button(struct erebus *ctx, int bn, bool pressed, int x, int y)
223 {
224 if(!ctx) return false;
225 if(ctx->bnstate.size() <= bn) ctx->bnstate.resize(bn < 32 ? 32 : bn + 1);
227 ctx->bnstate[bn] = pressed;
228 ctx->mouse_pos[0] = x;
229 ctx->mouse_pos[1] = y;
230 }
232 bool erb_input_mouse_motion(struct erebus *ctx, int x, int y);
233 bool erb_input_6dof_button(struct erebus *ctx, int bn, bool pressed);
234 bool erb_input_6dof_motion(struct erebus *ctx, float x, float y, float z);
237 } // extern "C"
239 float randf(float low, float high)
240 {
241 std::uniform_real_distribution<float> unirnd(low, high);
242 return unirnd(rnd_gen);
243 }
245 static void render_pixel(struct erebus *ctx, int x, int y, int sample)
246 {
247 Camera *cam = ctx->scn->get_active_camera();
248 if(!cam) return;
250 int xsz = ctx->fbimg.get_width();
251 int ysz = ctx->fbimg.get_height();
252 int offs = (y * xsz + x) * 4;
254 float *pix = ctx->fbimg.get_pixels() + offs;
255 float *accum = ctx->accum.get_pixels() + offs;
257 Ray ray = cam->get_primary_ray(x, y, xsz, ysz, sample);
258 Color c = ray_trace(ray, ctx->scn, 0);
259 accum[0] += c.x;
260 accum[1] += c.y;
261 accum[2] += c.z;
262 accum[3] += c.w;
264 float inv_samples = 1.0f / (float)(sample + 1);
265 pix[0] = accum[0] * inv_samples;
266 pix[1] = accum[1] * inv_samples;
267 pix[2] = accum[2] * inv_samples;
268 pix[3] = accum[3] * inv_samples;
269 }