erebus

view src/main.cc @ 32:b1fc96c71bcc

- lambert BRDF importance sampling - UI + commandline arguments - font rendering for showing status/progress
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 07 Jun 2014 13:36:36 +0300
parents fb20e3855740
children d15ee526daa6
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <signal.h>
5 #include <vector>
6 #include <chrono>
7 #include <imago2.h>
8 #include <drawtext.h>
9 #include "opengl.h"
10 #include "erebus.h"
12 using namespace std::chrono;
14 static bool init();
15 static void cleanup();
16 static void begin_frame(long tm);
17 static void end_frame();
18 static void resize_rtarget(int xsz, int ysz);
19 static void update_rect(int x, int y, int xsz, int ysz, float *pixels);
20 static void idle();
21 static void display();
22 static void display_statusbar(const erb_render_status &status);
23 static void save_image(const char *fname = 0);
24 static void reshape(int x, int y);
25 static void keyb(unsigned char key, int x, int y);
26 static void keyb_up(unsigned char key, int x, int y);
27 static void mouse(int bn, int st, int x, int y);
28 static void motion(int x, int y);
29 static void sball_button(int bn, int st);
30 static void sball_motion(int x, int y, int z);
31 static int next_pow2(int x);
32 static void sighandler(int s);
33 static bool parse_args(int argc, char **argv);
35 static int win_width, win_height, width, height, rtex_width, rtex_height;
36 static unsigned int rtex;
38 static int opt_samples = -1;
39 static int opt_iter = -1;
40 static int opt_threads = -1;
41 static float opt_imgscale = 2.0f;
43 static erebus *erb;
44 static bool render_pending;
45 static bool show_status = true;
46 static steady_clock::time_point start_time;
48 static std::vector<char*> sfiles;
50 static dtx_font *font;
52 int main(int argc, char **argv)
53 {
54 glutInitWindowSize(1024, 600);
55 glutInit(&argc, argv);
57 if(!parse_args(argc, argv)) {
58 return 1;
59 }
61 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
62 glutCreateWindow("erebus OpenGL frontend");
64 glutDisplayFunc(display);
65 glutReshapeFunc(reshape);
66 glutKeyboardFunc(keyb);
67 glutKeyboardUpFunc(keyb_up);
68 glutMouseFunc(mouse);
69 glutMotionFunc(motion);
70 glutSpaceballButtonFunc(sball_button);
71 glutSpaceballMotionFunc(sball_motion);
73 if(!init()) {
74 return 1;
75 }
76 atexit(cleanup);
77 signal(SIGINT, sighandler);
78 signal(SIGSEGV, sighandler);
79 signal(SIGILL, sighandler);
80 signal(SIGTERM, sighandler);
81 signal(SIGFPE, sighandler);
83 glutMainLoop();
84 }
86 static bool init()
87 {
88 width = glutGet(GLUT_WINDOW_WIDTH) / opt_imgscale;
89 height = glutGet(GLUT_WINDOW_HEIGHT) / opt_imgscale;
91 if(!(font = dtx_open_font_glyphmap("data/serif.glyphmap"))) {
92 fprintf(stderr, "warning: failed to load font!\n");
93 }
94 dtx_use_font(font, 24);
96 if(!(erb = erb_init())) {
97 return false;
98 }
99 erb_setopti(erb, ERB_OPT_WIDTH, width);
100 erb_setopti(erb, ERB_OPT_HEIGHT, height);
102 if(opt_samples != -1) {
103 erb_setopti(erb, ERB_OPT_MAX_SAMPLES, opt_samples);
104 }
105 if(opt_iter != -1) {
106 erb_setopti(erb, ERB_OPT_MAX_ITER, opt_iter);
107 }
108 if(opt_threads != -1) {
109 erb_setopti(erb, ERB_OPT_NUM_THREADS, opt_threads);
110 }
112 for(size_t i=0; i<sfiles.size(); i++) {
113 printf("loading scene file: %s\n", sfiles[i]);
114 if(erb_load_scene(erb, sfiles[i]) == -1) {
115 return false;
116 }
117 }
119 if(!sfiles.empty()) {
120 begin_frame(0);
121 }
123 glEnable(GL_TEXTURE_2D);
124 return true;
125 }
127 static void cleanup()
128 {
129 save_image("final.png");
130 erb_destroy(erb);
131 }
133 static void begin_frame(long tm)
134 {
135 printf("rendering frame (t=%ld) ... ", tm);
136 fflush(stdout);
138 render_pending = true;
139 glutIdleFunc(idle);
140 erb_begin_frame(erb, 0);
142 start_time = steady_clock::now();
143 }
145 static void end_frame()
146 {
147 if(!render_pending) return;
149 auto dur = steady_clock::now() - start_time;
150 long full_msec = duration_cast<milliseconds>(dur).count();
151 long msec, sec, min, hr, days;
153 msec = full_msec;
154 printf("done in ");
155 if((sec = msec / 1000) > 0) {
156 msec %= 1000;
157 if((min = sec / 60) > 0) {
158 sec %= 60;
159 if((hr = min / 60) > 0) {
160 min %= 60;
161 if((days = hr / 24) > 0) {
162 hr %= 24;
163 printf("%ld days ", days);
164 }
165 printf("%ld hours ", hr);
166 }
167 printf("%ld min ", min);
168 }
169 printf("%ld sec ", sec);
170 }
171 printf("%ld ms (%ld total msec)\n", msec, full_msec);
173 render_pending = false;
174 glutIdleFunc(0);
175 }
177 static void resize_rtarget(int xsz, int ysz)
178 {
179 static unsigned char *defpix;
181 win_width = xsz;
182 win_height = ysz;
184 width = xsz / opt_imgscale;
185 height = ysz / opt_imgscale;
187 if(width <= rtex_width && height <= rtex_height) {
188 return;
189 }
190 rtex_width = next_pow2(width);
191 rtex_height = next_pow2(height);
193 printf("resizing framebuffer texture: %dx%d\n", rtex_width, rtex_height);
195 if(!rtex) {
196 glGenTextures(1, &rtex);
197 }
199 delete [] defpix;
200 defpix = new unsigned char[rtex_width * rtex_height * 4];
201 unsigned char *ptr = defpix;
202 for(int i=0; i<rtex_height; i++) {
203 for(int j=0; j<rtex_width; j++) {
204 bool chess = ((i >> 4) & 1) == ((j >> 4) & 1);
206 int val = chess ? 64 : 48;
208 *ptr++ = val;
209 *ptr++ = val;
210 *ptr++ = val;
211 *ptr++ = 255;
212 }
213 }
215 glBindTexture(GL_TEXTURE_2D, rtex);
216 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
217 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
218 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, rtex_width, rtex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, defpix);
219 }
221 static void update_rect(int x, int y, int xsz, int ysz, float *pixels)
222 {
223 glBindTexture(GL_TEXTURE_2D, rtex);
224 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, xsz, ysz, GL_RGBA, GL_FLOAT, pixels);
225 }
227 static void idle()
228 {
229 glutPostRedisplay();
230 }
232 static void display()
233 {
234 static struct erb_render_status status;
236 if(render_pending) {
237 if(erb_render(erb, 64) == 0) {
238 end_frame();
239 }
240 update_rect(0, 0, width, height, erb_get_framebuffer(erb));
241 erb_get_status(erb, &status);
242 }
244 float maxu = (float)width / (float)rtex_width;
245 float maxv = (float)height / (float)rtex_height;
247 glBegin(GL_QUADS);
248 glColor4f(1, 1, 1, 1);
249 glTexCoord2f(0, maxv); glVertex2f(-1, -1);
250 glTexCoord2f(maxu, maxv); glVertex2f(1, -1);
251 glTexCoord2f(maxu, 0); glVertex2f(1, 1);
252 glTexCoord2f(0, 0); glVertex2f(-1, 1);
253 glEnd();
255 // draw progress information etc...
256 if(show_status) {
257 display_statusbar(status);
258 }
260 glutSwapBuffers();
261 assert(glGetError() == GL_NO_ERROR);
262 }
264 static void display_statusbar(const erb_render_status &status)
265 {
266 if(!font) return;
268 bool show_progress = opt_samples > 0;
270 glPushAttrib(GL_ENABLE_BIT);
271 glDisable(GL_TEXTURE_2D);
273 glMatrixMode(GL_PROJECTION);
274 glPushMatrix();
275 glLoadIdentity();
276 glOrtho(0, win_width, 0, win_height, -1, 1);
278 glMatrixMode(GL_MODELVIEW);
279 glPushMatrix();
280 glLoadIdentity();
282 int font_height = dtx_glyph_height('Q');
283 int prog_width = show_progress ? status.progress_percent * win_width / 100 : 0;
285 glBegin(GL_QUADS);
286 glColor4f(0, 0, 0, 1);
287 glVertex2f(prog_width, 0);
288 glVertex2f(win_width, 0);
289 glVertex2f(win_width, font_height);
290 glVertex2f(prog_width, font_height);
292 glColor4f(0.25, 0, 0, 1);
293 glVertex2f(0, 0);
294 glVertex2f(prog_width, 0);
295 glVertex2f(prog_width, font_height);
296 glVertex2f(0, font_height);
297 glEnd();
299 glTranslatef(5, 5, 0);
301 glColor4f(1, 1, 1, 1);
303 if(opt_samples > 0) {
304 dtx_printf("samples: %d / %d\n", status.samples, status.max_samples);
306 glTranslatef(win_width - dtx_string_width("progress: 100%") - 5, 0, 0);
307 dtx_printf("progress: %d%%\n", status.progress_percent);
308 } else {
309 dtx_printf("samples: %d\n", status.samples);
310 }
311 glPopMatrix();
312 glMatrixMode(GL_PROJECTION);
313 glPopMatrix();
315 glPopAttrib();
316 }
318 static void save_image(const char *fname)
319 {
320 float *fb = erb_get_framebuffer(erb);
322 if(img_save_pixels(fname ? fname : "output.png", fb, width, height, IMG_FMT_RGBAF) == -1) {
323 fprintf(stderr, "failed to save image\n");
324 }
325 }
327 static void reshape(int x, int y)
328 {
329 glViewport(0, 0, x, y);
330 resize_rtarget(x, y);
332 erb_setopti(erb, ERB_OPT_WIDTH, width);
333 erb_setopti(erb, ERB_OPT_HEIGHT, height);
334 }
336 static void keyb(unsigned char key, int x, int y)
337 {
338 switch(key) {
339 case 27:
340 end_frame();
341 exit(0);
343 case ' ':
344 begin_frame(0);
345 break;
347 case '`':
348 printf("saving image.\n");
349 save_image();
350 break;
352 case 'p':
353 show_status = !show_status;
354 glutPostRedisplay();
355 break;
356 }
358 if(erb_input_keyboard(erb, key, true)) {
359 glutPostRedisplay();
360 }
361 }
363 static void keyb_up(unsigned char key, int x, int y)
364 {
365 if(erb_input_keyboard(erb, key, false)) {
366 glutPostRedisplay();
367 }
368 }
370 static void mouse(int bn, int st, int x, int y)
371 {
372 if(erb_input_mouse_button(erb, bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y)) {
373 glutPostRedisplay();
374 }
375 }
377 static void motion(int x, int y)
378 {
379 if(erb_input_mouse_motion(erb, x, y)) {
380 glutPostRedisplay();
381 }
382 }
384 static void sball_button(int bn, int state)
385 {
386 if(erb_input_6dof_button(erb, bn, state == GLUT_DOWN)) {
387 glutPostRedisplay();
388 }
389 }
391 static void sball_motion(int x, int y, int z)
392 {
393 if(erb_input_6dof_motion(erb, x / 65536.0, y / 65536.0, z / 65536.0)) {
394 glutPostRedisplay();
395 }
396 }
398 static int next_pow2(int x)
399 {
400 int res = 2;
401 while(res < x) {
402 res <<= 1;
403 }
404 return res;
405 }
407 static void sighandler(int s)
408 {
409 exit(0);
410 }
412 static bool parse_args(int argc, char **argv)
413 {
414 for(int i=1; i<argc; i++) {
415 if(argv[i][0] == '-') {
416 if(strcmp(argv[i], "-samples") == 0) {
417 opt_samples = atoi(argv[++i]);
418 if(opt_samples <= 0) {
419 fprintf(stderr, "invalid -samples option: %s\n", argv[i]);
420 return false;
421 }
423 } else if(strcmp(argv[i], "-iter") == 0) {
424 opt_iter = atoi(argv[++i]);
425 if(opt_iter <= 0) {
426 fprintf(stderr, "invalid -iter option: %s\n", argv[i]);
427 return false;
428 }
430 } else if(strcmp(argv[i], "-threads") == 0) {
431 opt_threads = atoi(argv[++i]);
432 if(opt_threads <= 0) {
433 fprintf(stderr, "invalid -threads option: %s\n", argv[i]);
434 return false;
435 }
437 } else if(strcmp(argv[i], "-scale") == 0) {
438 opt_imgscale = atof(argv[++i]);
439 if(opt_imgscale <= 0.0f) {
440 fprintf(stderr, "invalid -scale option: %s\n", argv[i]);
441 return false;
442 }
444 } else {
445 fprintf(stderr, "invalid option: %s\n", argv[i]);
446 return false;
447 }
448 } else {
449 sfiles.push_back(argv[i]);
450 }
451 }
453 return true;