dbf-udg
view src/udg.cc @ 14:5be16862d1b5
minor makefile modifications
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 20 Feb 2013 05:54:36 +0200 |
parents | 1abbed71e9c9 |
children |
line source
1 /*
2 Printblobs - halftoning display hack
3 Copyright (C) 2013 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <math.h>
22 #include <assert.h>
23 #include "opengl.h"
24 #include "udg.h"
25 #include "sdr.h"
26 #include "dither_matrix.h"
27 #include "mballs.h"
28 #include "dsys.h"
29 #include "post.h"
31 #define DITHER_SZ 8
32 #define DITHER_LEVELS 16
34 #if DITHER_SZ == 4
35 #define dither_matrix dither_matrix4
36 #elif DITHER_SZ == 8
37 #define dither_matrix halftone_matrix8
38 #else
39 #error "invalid dither size"
40 #endif
42 struct render_target {
43 unsigned int fbo;
44 unsigned int color_tex, depth_buf;
45 };
47 bool init();
48 void cleanup();
49 void disp();
50 void idle();
51 void reshape(int x, int y);
52 void keyb(unsigned char key, int x, int y);
53 void mouse(int bn, int state, int x, int y);
54 void motion(int x, int y);
55 struct render_target *create_rtarg(int xsz, int ysz);
56 void destroy_rtarg(struct render_target *rt);
57 bool parse_args(int argc, char **argv);
59 int xsz, ysz;
60 float cam_theta, cam_phi = 25, cam_dist = 11;
61 unsigned int dither_tex;
62 struct render_target *rtarg;
63 unsigned int post_prog, phong_prog;
65 int opt_blocky = true, opt_regular_render;
66 bool opt_autorot = true;
68 struct dsys_demo *demo;
69 struct dsys_event *evfadein;
71 int main(int argc, char **argv)
72 {
73 glutInitWindowSize(1024, 768);
74 glutInit(&argc, argv);
76 if(!parse_args(argc, argv)) {
77 return 1;
78 }
80 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
81 glutCreateWindow("DBF UDG compo entry by Nuclear");
83 glutDisplayFunc(disp);
84 glutIdleFunc(idle);
85 glutReshapeFunc(reshape);
86 glutKeyboardFunc(keyb);
87 glutMouseFunc(mouse);
88 glutMotionFunc(motion);
90 glewInit();
92 if(!init()) {
93 return 1;
94 }
96 glutMainLoop();
97 return 0;
98 }
100 bool init()
101 {
102 if(!(demo = dsys_open("demoscript"))) {
103 return false;
104 }
105 evfadein = dsys_event(demo, "fade_in");
106 dsys_set_event_eval(evfadein, dsys_eval_sigmoid);
108 // dump the tile image
109 FILE *fp = fopen("udg.ppm", "wb");
110 if(fp) {
111 fprintf(fp, "P6\n%d %d\n255\n", DITHER_SZ, DITHER_SZ * DITHER_LEVELS);
112 }
114 unsigned char *img = new unsigned char[DITHER_SZ * DITHER_SZ * DITHER_LEVELS];
115 unsigned char *ptr = img;
117 for(int i=0; i<DITHER_LEVELS; i++) {
118 float val = (float)i / (float)(DITHER_LEVELS - 1);
119 for(int y=0; y<DITHER_SZ; y++) {
120 for(int x=0; x<DITHER_SZ; x++) {
121 /* (1 + M) / (1 + MxN) */
122 float thres = (1.0 + dither_matrix[x][y]) / (1.0 + DITHER_SZ * DITHER_SZ);
123 *ptr++ = val >= thres ? 255 : 0;
125 if(fp) {
126 int r = ptr[-1] ? 246 : 10;
127 int g = ptr[-1] ? 198 : 72;
128 int b = ptr[-1] ? 141 : 85;
129 fputc(r, fp);
130 fputc(g, fp);
131 fputc(b, fp);
132 }
133 }
134 }
135 }
137 if(fp) {
138 fclose(fp);
139 }
141 if(!(phong_prog = create_program_load("sdr/phong.v.glsl", "sdr/phong.p.glsl"))) {
142 return false;
143 }
145 if(!(post_prog = create_program_load("sdr/dither.v.glsl", "sdr/dither.p.glsl"))) {
146 return false;
147 }
149 glGenTextures(1, &dither_tex);
150 glBindTexture(GL_TEXTURE_2D, dither_tex);
151 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
152 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
153 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
154 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
155 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, DITHER_SZ, DITHER_SZ * DITHER_LEVELS, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, img);
157 if(!mball_init()) {
158 return false;
159 }
161 glEnable(GL_CULL_FACE);
162 glEnable(GL_DEPTH_TEST);
163 glEnable(GL_LIGHTING);
164 glEnable(GL_LIGHT0);
165 glEnable(GL_LIGHT1);
166 glEnable(GL_NORMALIZE);
169 reshape(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
170 return true;
171 }
173 void draw_backdrop()
174 {
175 glPushAttrib(GL_ENABLE_BIT);
176 glDisable(GL_DEPTH_TEST);
177 glDisable(GL_LIGHTING);
179 glMatrixMode(GL_MODELVIEW);
180 glPushMatrix();
181 glLoadIdentity();
182 glMatrixMode(GL_PROJECTION);
183 glPushMatrix();
184 glLoadIdentity();
186 glBegin(GL_QUADS);
187 glColor3f(0, 0, 0);
188 glVertex2f(-1, -1);
189 glVertex2f(1, -1);
190 glColor3f(1, 1, 1);
191 glVertex2f(1, 1);
192 glVertex2f(-1, 1);
193 glEnd();
195 glPopMatrix();
196 glMatrixMode(GL_MODELVIEW);
197 glPopMatrix();
199 glPopAttrib();
200 }
202 void disp()
203 {
204 float ldir[] = {-1, 1, 2, 0};
205 float ldir2[] = {0.0, 0.35, -0.9, 0};
207 float lcol[] = {1, 1, 1, 1};
208 float lcol2[] = {0.35, 0.3, 0.15, 1};
210 dsys_update(demo, dsys_msec_to_dtime(glutGet(GLUT_ELAPSED_TIME)));
212 float sec = dsys_dtime_to_sec(dsys_time(demo));
213 float auto_angle = sec * 10.0;
215 int xres, yres;
216 if(!opt_blocky) {
217 xres = xsz;
218 yres = ysz;
219 } else {
220 xres = xsz / DITHER_SZ;
221 yres = ysz / DITHER_SZ;
222 }
224 if(!rtarg) {
225 printf("(re)creating render target (%dx%d)\n", xres, yres);
226 if(!(rtarg = create_rtarg(xres, yres))) {
227 exit(0);
228 }
229 }
231 if(!opt_regular_render) {
232 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
233 }
234 glViewport(0, 0, xres, yres);
236 glClearColor(1, 1, 1, 1);
237 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
239 draw_backdrop();
241 glMatrixMode(GL_MODELVIEW);
242 glLoadIdentity();
244 glTranslatef(0, 0.8, -cam_dist);
245 glRotatef(cam_phi, 1, 0, 0);
246 glRotatef(opt_autorot ? auto_angle : cam_theta, 0, 1, 0);
248 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
249 glLightfv(GL_LIGHT0, GL_DIFFUSE, lcol);
251 glLightfv(GL_LIGHT1, GL_POSITION, ldir2);
252 glLightfv(GL_LIGHT1, GL_DIFFUSE, lcol2);
254 const float blue[] = {0.4, 0.45, 1.0, 1};
255 const float white[] = {1, 1, 1, 1};
256 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
257 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
258 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
260 bind_program(phong_prog);
261 mball_render(sec);
262 bind_program(0);
264 float tfadein = evfadein ? dsys_event_value(evfadein) : 1.0;
265 /*if(tfadein < 1.0) {
266 glEnable(GL_BLEND);
267 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
268 overlay(0, 0, 0, 1.0 - tfadein);
269 glDisable(GL_BLEND);
270 }*/
273 if(!opt_regular_render) {
274 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
275 glViewport(0, 0, xsz, ysz);
277 glClear(GL_COLOR_BUFFER_BIT);
279 bind_program(post_prog);
280 set_uniform_int(post_prog, "framebuf", 0);
281 set_uniform_int(post_prog, "dither_tex", 1);
282 set_uniform_int(post_prog, "dither_levels", DITHER_LEVELS);
283 set_uniform_int(post_prog, "dither_size", DITHER_SZ);
284 set_uniform_float(post_prog, "tfadein", tfadein);
286 glActiveTextureARB(GL_TEXTURE0);
287 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
288 glEnable(GL_TEXTURE_2D);
289 glActiveTextureARB(GL_TEXTURE1);
290 glBindTexture(GL_TEXTURE_2D, dither_tex);
291 glEnable(GL_TEXTURE_2D);
293 overlay(1, 1, 1, 1);
295 glActiveTextureARB(GL_TEXTURE1);
296 glDisable(GL_TEXTURE_2D);
297 glActiveTextureARB(GL_TEXTURE0);
298 glDisable(GL_TEXTURE_2D);
300 bind_program(0);
301 }
303 glutSwapBuffers();
304 assert(glGetError() == GL_NO_ERROR);
305 }
307 void idle()
308 {
309 glutPostRedisplay();
310 }
312 void reshape(int x, int y)
313 {
314 glViewport(0, 0, x, y);
316 glMatrixMode(GL_PROJECTION);
317 glLoadIdentity();
318 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
320 if(x != xsz || y != ysz) {
321 destroy_rtarg(rtarg);
322 rtarg = 0;
323 xsz = x;
324 ysz = y;
325 }
326 }
328 void keyb(unsigned char key, int x, int y)
329 {
330 switch(key) {
331 case 27:
332 exit(0);
334 case 'a':
335 opt_autorot = !opt_autorot;
336 break;
338 case 'f':
339 {
340 static bool fullscreen;
341 static int orig_x, orig_y;
343 fullscreen = !fullscreen;
344 if(fullscreen) {
345 orig_x = xsz;
346 orig_y = ysz;
347 glutFullScreen();
348 } else {
349 glutReshapeWindow(orig_x, orig_y);
350 }
351 }
352 break;
354 case 'r':
355 opt_regular_render = !opt_regular_render;
356 break;
358 case 'b':
359 opt_blocky = !opt_blocky;
360 if(rtarg) {
361 destroy_rtarg(rtarg);
362 rtarg = 0;
363 }
364 break;
366 case ' ':
367 if(dsys_is_running(demo)) {
368 dsys_stop(demo);
369 } else {
370 dsys_start(demo);
371 }
372 break;
373 }
374 }
376 bool bnstate[16];
377 int prev_x, prev_y;
379 void mouse(int bn, int state, int x, int y)
380 {
381 int idx = bn - GLUT_LEFT_BUTTON;
383 if(idx < (int)(sizeof bnstate / sizeof *bnstate)) {
384 bnstate[idx] = state == GLUT_DOWN;
385 }
386 prev_x = x;
387 prev_y = y;
388 }
390 void motion(int x, int y)
391 {
392 int dx = x - prev_x;
393 int dy = y - prev_y;
394 prev_x = x;
395 prev_y = y;
397 if(bnstate[0]) {
398 cam_theta = fmod(cam_theta + dx * 0.5, 360.0);
399 cam_phi += dy * 0.5;
400 if(cam_phi < -90) {
401 cam_phi = -90;
402 }
403 if(cam_phi > 90) {
404 cam_phi = 90;
405 }
406 }
407 if(bnstate[2]) {
408 cam_dist += dy * 0.1;
409 if(cam_dist < 0) {
410 cam_dist = 0;
411 }
412 }
413 }
415 struct render_target *create_rtarg(int xsz, int ysz)
416 {
417 struct render_target *rt = new render_target;
419 glGenFramebuffersEXT(1, &rt->fbo);
420 glBindFramebufferEXT(GL_FRAMEBUFFER, rt->fbo);
422 // create the render target texture
423 glGenTextures(1, &rt->color_tex);
424 glBindTexture(GL_TEXTURE_2D, rt->color_tex);
425 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
426 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
427 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
428 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
429 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
431 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color_tex, 0);
433 // create depth buffer
434 glGenRenderbuffersEXT(1, &rt->depth_buf);
435 glBindRenderbufferEXT(GL_RENDERBUFFER, rt->depth_buf);
436 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xsz, ysz);
438 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth_buf);
440 if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
441 fprintf(stderr, "incomplete fbo\n");
442 return 0;
443 }
445 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
446 return rt;
447 }
449 void destroy_rtarg(struct render_target *rt)
450 {
451 if(!rt) {
452 return;
453 }
454 glDeleteFramebuffersEXT(1, &rt->fbo);
455 glDeleteTextures(1, &rt->color_tex);
456 glDeleteRenderbuffersEXT(1, &rt->depth_buf);
457 delete rt;
458 }
460 bool parse_args(int argc, char **argv)
461 {
462 for(int i=1; i<argc; i++) {
463 if(strcmp(argv[i], "-noblocky") == 0) {
464 opt_blocky = false;
465 } else if(strcmp(argv[i], "-blocky") == 0) {
466 opt_blocky = true;
467 } else if(strcmp(argv[i], "-nodither") == 0) {
468 opt_regular_render = true;
469 } else {
470 fprintf(stderr, "invalid argument: %s\n", argv[i]);
471 return false;
472 }
473 }
474 return true;
475 }