erebus
view liberebus/src/erebus.cc @ 6:bb006fb96f1b
forgot to add two files
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 24 May 2014 02:22:14 +0300 |
parents | 93894c232d65 |
children | e2d9bf168a41 |
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 using namespace std::chrono;
14 struct Rect {
15 int x, y, width, height;
17 bool operator ==(const Rect &r) { return memcmp(this, &r, sizeof r) == 0; }
18 bool operator !=(const Rect &r) { return memcmp(this, &r, sizeof r) != 0; }
19 };
21 #define INVALID_RECT Rect{0, 0, 0, 0}
23 struct erebus {
24 Scene *scn;
26 Image<float> fbimg;
27 Vector4 options[ERB_NUM_OPTIONS];
29 // render state
30 long cur_time;
31 int cur_pixel_x, cur_pixel_y;
32 Rect cur_rect;
33 };
35 static void render_pixel(struct erebus *ctx, int x, int y);
37 static std::mt19937 rnd_gen;
39 extern "C" {
41 struct erebus *erb_init(void)
42 {
43 struct erebus *ctx;
44 try {
45 ctx = new struct erebus;
46 }
47 catch(...) {
48 return 0;
49 }
51 ctx->scn = 0;
52 ctx->cur_time = 0;
53 ctx->cur_rect = INVALID_RECT;
54 return ctx;
55 }
57 void erb_destroy(struct erebus *ctx)
58 {
59 delete ctx;
60 }
62 void erb_setopti(struct erebus *ctx, enum erb_option opt, int val)
63 {
64 ctx->options[opt].x = val;
65 }
66 void erb_setoptf(struct erebus *ctx, enum erb_option opt, float val)
67 {
68 ctx->options[opt].x = val;
69 }
70 void erb_setoptfv(struct erebus *ctx, enum erb_option opt, float *vec)
71 {
72 for(int i=0; i<4; i++) {
73 ctx->options[opt][i] = vec[i];
74 }
75 }
77 int erb_getopti(struct erebus *ctx, enum erb_option opt)
78 {
79 return ctx->options[opt].x;
80 }
81 float erb_getoptf(struct erebus *ctx, enum erb_option opt)
82 {
83 return ctx->options[opt].x;
84 }
85 float *erb_getoptfv(struct erebus *ctx, enum erb_option opt)
86 {
87 return &ctx->options[opt].x;
88 }
90 float *erb_get_framebuffer(struct erebus *ctx)
91 {
92 return ctx->fbimg.get_pixels();
93 }
95 void erb_begin_frame(struct erebus *ctx, long ms)
96 {
97 ctx->cur_time = ms;
99 int xsz = ctx->options[ERB_OPT_WIDTH].x;
100 int ysz = ctx->options[ERB_OPT_HEIGHT].x;
102 ctx->fbimg.create(xsz, ysz);
103 }
105 int erb_render(struct erebus *ctx, long timeout)
106 {
107 return erb_render_rect(ctx, 0, 0, ctx->fbimg.get_width(), ctx->fbimg.get_height(), timeout);
108 }
110 int erb_render_rect(struct erebus *ctx, int x, int y, int width, int height, long timeout)
111 {
112 if(!width || !height) return -1;
114 Rect rect{x, y, width, height};
115 if(ctx->cur_rect != rect) {
116 ctx->cur_rect = rect;
117 ctx->cur_pixel_x = x;
118 ctx->cur_pixel_y = y;
119 }
121 ctx->scn->update();
123 if(timeout > 0) {
124 auto start_time = steady_clock::now();
125 while(duration_cast<milliseconds>(steady_clock::now() - start_time).count() < timeout) {
126 render_pixel(ctx, ctx->cur_pixel_x, ctx->cur_pixel_y);
128 if(++ctx->cur_pixel_x >= ctx->cur_rect.width) {
129 if(++ctx->cur_pixel_y >= ctx->cur_rect.height) {
130 ctx->cur_rect = INVALID_RECT;
131 return 0;
132 }
133 }
134 }
135 return 1;
136 }
138 for(int i=0; i<height; i++) {
139 for(int j=0; j<width; j++) {
140 render_pixel(ctx, j, i);
141 }
142 }
143 return 0;
144 }
146 int erb_get_progress(struct erebus *ctx)
147 {
148 return 0; // TODO
149 }
151 int erb_load_scene(struct erebus *ctx, const char *fname)
152 {
153 delete ctx->scn;
154 ctx->scn = new Scene;
156 // XXX for now just create a test scene here
157 Sphere *sph = new Sphere;
158 SceneNode *sph_node = new SceneNode(sph);
159 ctx->scn->add_object(sph);
160 ctx->scn->add_node(sph_node);
162 TargetCamera *cam = new TargetCamera(Vector3(0, 0, -10), Vector3(0, 0, 0));
163 //ctx->scn->add_object(cam);
164 ctx->scn->use_camera(cam);
166 return 0;
167 }
169 } // extern "C"
171 float randf(float low, float high)
172 {
173 std::uniform_real_distribution<float> unirnd(low, high);
174 return unirnd(rnd_gen);
175 }
177 static void render_pixel(struct erebus *ctx, int x, int y)
178 {
179 Camera *cam = ctx->scn->get_active_camera();
180 if(!cam) return;
182 int xsz = ctx->fbimg.get_width();
183 int ysz = ctx->fbimg.get_height();
184 float *pix = ctx->fbimg.get_pixels() + (y * xsz + x) * 4;
186 Ray ray = cam->get_primary_ray(x, y, xsz, ysz, 0);
187 //Color c = ray_trace(ray, ctx->scn, 0);
188 Color c = ray.dir.normalized() * 0.5 + Vector3(0.5, 0.5, 0.5);
189 pix[0] = 1.0;//c.x;
190 pix[1] = c.y;
191 pix[2] = c.z;
192 pix[3] = c.w;
193 }