erebus
view liberebus/src/erebus.cc @ 11:788f74d597a2
added README.md
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 24 May 2014 21:44:25 +0300 |
parents | d38e13d6063c |
children | 20d6c05529f1 |
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 rnd_gen.seed(time(0));
62 ctx->scn = 0;
63 ctx->cur_time = 0;
64 ctx->cur_rect = INVALID_RECT;
66 ctx->options[ERB_OPT_MAX_SAMPLES].x = (float)INF_SAMPLES;
67 return ctx;
68 }
70 void erb_destroy(struct erebus *ctx)
71 {
72 delete ctx;
73 }
75 void erb_setopti(struct erebus *ctx, enum erb_option opt, int val)
76 {
77 ctx->options[opt].x = val;
78 }
79 void erb_setoptf(struct erebus *ctx, enum erb_option opt, float val)
80 {
81 ctx->options[opt].x = val;
82 }
83 void erb_setoptfv(struct erebus *ctx, enum erb_option opt, float *vec)
84 {
85 for(int i=0; i<4; i++) {
86 ctx->options[opt][i] = vec[i];
87 }
88 }
90 int erb_getopti(struct erebus *ctx, enum erb_option opt)
91 {
92 return ctx->options[opt].x;
93 }
94 float erb_getoptf(struct erebus *ctx, enum erb_option opt)
95 {
96 return ctx->options[opt].x;
97 }
98 float *erb_getoptfv(struct erebus *ctx, enum erb_option opt)
99 {
100 return &ctx->options[opt].x;
101 }
103 float *erb_get_framebuffer(struct erebus *ctx)
104 {
105 return ctx->fbimg.get_pixels();
106 }
108 void erb_begin_frame(struct erebus *ctx, long ms)
109 {
110 printf("starting new frame...\n");
111 ctx->cur_time = ms;
113 int xsz = ctx->options[ERB_OPT_WIDTH].x;
114 int ysz = ctx->options[ERB_OPT_HEIGHT].x;
116 ctx->fbimg.create(xsz, ysz);
117 ctx->accum.create(xsz, ysz);
118 }
120 int erb_render(struct erebus *ctx, long timeout)
121 {
122 return erb_render_rect(ctx, 0, 0, ctx->fbimg.get_width(), ctx->fbimg.get_height(), timeout);
123 }
125 int erb_render_rect(struct erebus *ctx, int x, int y, int width, int height, long timeout)
126 {
127 if(!width || !height) return -1;
129 Rect rect{x, y, width, height};
130 if(ctx->cur_rect != rect) {
131 // starting a new rendering apparently
132 ctx->cur_rect = rect;
133 ctx->cur_pixel_x = x;
134 ctx->cur_pixel_y = y;
135 ctx->cur_sample = 0;
136 }
138 ctx->scn->update();
140 int max_samples = ctx->options[ERB_OPT_MAX_SAMPLES].x;
142 if(timeout > 0) {
143 auto start_time = steady_clock::now();
144 while(duration_cast<milliseconds>(steady_clock::now() - start_time).count() < timeout) {
145 render_pixel(ctx, ctx->cur_pixel_x, ctx->cur_pixel_y, ctx->cur_sample);
147 if(++ctx->cur_pixel_x >= ctx->cur_rect.width) {
148 ctx->cur_pixel_x = ctx->cur_rect.x;
149 if(++ctx->cur_pixel_y >= ctx->cur_rect.height) {
150 ctx->cur_pixel_y = ctx->cur_rect.y;
151 if(++ctx->cur_sample >= max_samples) {
152 ctx->cur_rect = INVALID_RECT;
153 return 0;
154 }
155 }
156 }
157 }
158 return 1;
159 }
161 if(ctx->options[ERB_OPT_MAX_SAMPLES].x == (float)INF_SAMPLES) {
162 max_samples = 128;
163 }
165 for(int i=0; i<height; i++) {
166 for(int j=0; j<width; j++) {
167 for(int k=0; k<max_samples; k++) {
168 render_pixel(ctx, j, i, k);
169 }
170 }
171 }
172 return 0;
173 }
175 int erb_get_progress(struct erebus *ctx)
176 {
177 return 0; // TODO
178 }
180 int erb_load_scene(struct erebus *ctx, const char *fname)
181 {
182 delete ctx->scn;
183 ctx->scn = new Scene;
185 // XXX for now just create a test scene here
186 Sphere *sph = new Sphere;
187 sph->mtl.set_attrib("albedo", Color(1.0, 0.3, 0.2));
188 SceneNode *sph_node = new SceneNode(sph);
189 ctx->scn->add_object(sph);
190 ctx->scn->add_node(sph_node);
192 sph = new Sphere;
193 sph->mtl.set_attrib("albedo", Color(0.3, 0.4, 1.0));
194 sph_node = new SceneNode(sph);
195 sph_node->set_position(Vector3(0, -3.0, 0));
196 //sph_node->set_scaling(Vector3(4.0, 4.0, 4.0) * 0.3);
197 ctx->scn->add_object(sph);
198 ctx->scn->add_node(sph_node);
200 Sphere *lt = new Sphere;
201 lt->mtl.set_attrib("emissive", Color(10, 10, 10));
202 SceneNode *lt_node = new SceneNode(lt);
203 lt_node->set_position(Vector3(-15, 15, -10));
204 lt_node->set_scaling(Vector3(5, 5, 5));
205 ctx->scn->add_object(lt);
206 ctx->scn->add_node(lt_node);
208 TargetCamera *cam = new TargetCamera(Vector3(0, 4, -8), Vector3(0, 0, 0));
209 //ctx->scn->add_object(cam);
210 ctx->scn->use_camera(cam);
212 return 0;
213 }
215 bool erb_input_keyboard(struct erebus *ctx, int key, bool pressed)
216 {
217 if(!ctx) return false;
218 if(ctx->keystate.size() <= key) ctx->keystate.resize(key < 256 ? 256 : key + 1);
220 ctx->keystate[key] = pressed;
222 if(pressed) {
223 switch(key) {
224 case '=':
225 case '-':
226 }
227 }
228 return false;
229 }
231 bool erb_input_mouse_button(struct erebus *ctx, int bn, bool pressed, int x, int y)
232 {
233 if(!ctx) return false;
234 if(ctx->bnstate.size() <= bn) ctx->bnstate.resize(bn < 32 ? 32 : bn + 1);
236 ctx->bnstate[bn] = pressed;
237 ctx->mouse_pos[0] = x;
238 ctx->mouse_pos[1] = y;
239 return false;
240 }
242 bool erb_input_mouse_motion(struct erebus *ctx, int x, int y)
243 {
244 if(!ctx) return false;
246 ctx->mouse_pos[0] = x;
247 ctx->mouse_pos[1] = y;
248 return false;
249 }
251 bool erb_input_6dof_button(struct erebus *ctx, int bn, bool pressed)
252 {
253 if(!ctx) return false;
254 return false;
255 }
257 bool erb_input_6dof_motion(struct erebus *ctx, float x, float y, float z)
258 {
259 if(!ctx) return false;
260 return false;
261 }
264 } // extern "C"
266 float randf(float low, float high)
267 {
268 std::uniform_real_distribution<float> unirnd(low, high);
269 return unirnd(rnd_gen);
270 }
272 static void render_pixel(struct erebus *ctx, int x, int y, int sample)
273 {
274 Camera *cam = ctx->scn->get_active_camera();
275 if(!cam) return;
277 int xsz = ctx->fbimg.get_width();
278 int ysz = ctx->fbimg.get_height();
279 int offs = (y * xsz + x) * 4;
281 float *pix = ctx->fbimg.get_pixels() + offs;
282 float *accum = ctx->accum.get_pixels() + offs;
284 Ray ray = cam->get_primary_ray(x, y, xsz, ysz, sample);
285 Color c = ray_trace(ray, ctx->scn, 0);
286 accum[0] += c.x;
287 accum[1] += c.y;
288 accum[2] += c.z;
289 accum[3] += c.w;
291 float inv_samples = 1.0f / (float)(sample + 1);
292 pix[0] = accum[0] * inv_samples;
293 pix[1] = accum[1] * inv_samples;
294 pix[2] = accum[2] * inv_samples;
295 pix[3] = accum[3] * inv_samples;
296 }