dbf-udg

view src/udg.cc @ 11:5f99c4c7a9fe

now it looks pretty much ok
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 20 Feb 2013 04:55:03 +0200
parents 1120c069eb17
children 1abbed71e9c9
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6 #include "opengl.h"
7 #include "udg.h"
8 #include "sdr.h"
9 #include "dither_matrix.h"
10 #include "scroller.h"
11 #include "mballs.h"
12 #include "dsys.h"
13 #include "post.h"
15 #define DITHER_SZ 8
16 #define DITHER_LEVELS 16
18 #if DITHER_SZ == 4
19 #define dither_matrix dither_matrix4
20 #elif DITHER_SZ == 8
21 #define dither_matrix halftone_matrix8
22 #else
23 #error "invalid dither size"
24 #endif
26 struct render_target {
27 unsigned int fbo;
28 unsigned int color_tex, depth_buf;
29 };
31 bool init();
32 void cleanup();
33 void disp();
34 void idle();
35 void reshape(int x, int y);
36 void keyb(unsigned char key, int x, int y);
37 void mouse(int bn, int state, int x, int y);
38 void motion(int x, int y);
39 struct render_target *create_rtarg(int xsz, int ysz);
40 void destroy_rtarg(struct render_target *rt);
41 bool parse_args(int argc, char **argv);
43 int xsz, ysz;
44 float cam_theta, cam_phi = 25, cam_dist = 11;
45 unsigned int dither_tex;
46 struct render_target *rtarg;
47 unsigned int post_prog, phong_prog;
49 int opt_highres, opt_regular_render;
50 bool opt_autorot = true;
52 struct dsys_demo *demo;
53 struct dsys_event *evfadein;
55 int main(int argc, char **argv)
56 {
57 glutInitWindowSize(1024, 768);
58 glutInit(&argc, argv);
60 if(!parse_args(argc, argv)) {
61 return 1;
62 }
64 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
65 glutCreateWindow("DBF UDG compo entry by Nuclear");
67 glutDisplayFunc(disp);
68 glutIdleFunc(idle);
69 glutReshapeFunc(reshape);
70 glutKeyboardFunc(keyb);
71 glutMouseFunc(mouse);
72 glutMotionFunc(motion);
74 glewInit();
76 if(!init()) {
77 return 1;
78 }
80 glutMainLoop();
81 return 0;
82 }
84 bool init()
85 {
86 if(!(demo = dsys_open("demoscript"))) {
87 return false;
88 }
89 evfadein = dsys_event(demo, "fade_in");
90 dsys_set_event_eval(evfadein, dsys_eval_sigmoid);
92 // dump the tile image
93 FILE *fp = fopen("udg.ppm", "wb");
94 if(fp) {
95 fprintf(fp, "P6\n%d %d\n255\n", DITHER_SZ, DITHER_SZ * DITHER_LEVELS);
96 }
98 unsigned char *img = new unsigned char[DITHER_SZ * DITHER_SZ * DITHER_LEVELS];
99 unsigned char *ptr = img;
101 for(int i=0; i<DITHER_LEVELS; i++) {
102 float val = (float)i / (float)(DITHER_LEVELS - 1);
103 for(int y=0; y<DITHER_SZ; y++) {
104 for(int x=0; x<DITHER_SZ; x++) {
105 /* (1 + M) / (1 + MxN) */
106 float thres = (1.0 + dither_matrix[x][y]) / (1.0 + DITHER_SZ * DITHER_SZ);
107 *ptr++ = val >= thres ? 255 : 0;
109 if(fp) {
110 int r = ptr[-1] ? 246 : 10;
111 int g = ptr[-1] ? 198 : 72;
112 int b = ptr[-1] ? 141 : 85;
113 fputc(r, fp);
114 fputc(g, fp);
115 fputc(b, fp);
116 }
117 }
118 }
119 }
121 if(fp) {
122 fclose(fp);
123 }
125 if(!(phong_prog = create_program_load("sdr/phong.v.glsl", "sdr/phong.p.glsl"))) {
126 return false;
127 }
129 if(!(post_prog = create_program_load("sdr/dither.v.glsl", "sdr/dither.p.glsl"))) {
130 return false;
131 }
133 glGenTextures(1, &dither_tex);
134 glBindTexture(GL_TEXTURE_2D, dither_tex);
135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
137 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
138 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
139 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, DITHER_SZ, DITHER_SZ * DITHER_LEVELS, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, img);
141 if(!init_scroller()) {
142 return false;
143 }
145 if(!mball_init()) {
146 return false;
147 }
149 glEnable(GL_CULL_FACE);
150 glEnable(GL_DEPTH_TEST);
151 glEnable(GL_LIGHTING);
152 glEnable(GL_LIGHT0);
153 glEnable(GL_LIGHT1);
154 glEnable(GL_NORMALIZE);
156 //dsys_start(demo);
158 return true;
159 }
161 void draw_backdrop()
162 {
163 glPushAttrib(GL_ENABLE_BIT);
164 glDisable(GL_DEPTH_TEST);
165 glDisable(GL_LIGHTING);
167 glMatrixMode(GL_MODELVIEW);
168 glPushMatrix();
169 glLoadIdentity();
170 glMatrixMode(GL_PROJECTION);
171 glPushMatrix();
172 glLoadIdentity();
174 glBegin(GL_QUADS);
175 glColor3f(0, 0, 0);
176 glVertex2f(-1, -1);
177 glVertex2f(1, -1);
178 glColor3f(1, 1, 1);
179 glVertex2f(1, 1);
180 glVertex2f(-1, 1);
181 glEnd();
183 glPopMatrix();
184 glMatrixMode(GL_MODELVIEW);
185 glPopMatrix();
187 glPopAttrib();
188 /*draw_scroller(glutGet(GLUT_ELAPSED_TIME) / 1000.0); */
189 }
191 void disp()
192 {
193 float ldir[] = {-1, 1, 2, 0};
194 float ldir2[] = {0.0, 0.35, -0.9, 0};
196 float lcol[] = {1, 1, 1, 1};
197 float lcol2[] = {0.35, 0.3, 0.15, 1};
199 dsys_update(demo, dsys_msec_to_dtime(glutGet(GLUT_ELAPSED_TIME)));
201 float sec = dsys_dtime_to_sec(dsys_time(demo));
202 float auto_angle = sec * 10.0;
204 int xres, yres;
205 if(opt_highres) {
206 xres = xsz;
207 yres = ysz;
208 } else {
209 xres = xsz / DITHER_SZ;
210 yres = ysz / DITHER_SZ;
211 }
213 if(!rtarg) {
214 printf("(re)creating render target (%dx%d)\n", xres, yres);
215 if(!(rtarg = create_rtarg(xres, yres))) {
216 exit(0);
217 }
218 }
220 if(!opt_regular_render) {
221 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
222 }
223 glViewport(0, 0, xres, yres);
225 glClearColor(1, 1, 1, 1);
226 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
228 draw_backdrop();
230 glMatrixMode(GL_MODELVIEW);
231 glLoadIdentity();
233 glTranslatef(0, 0.8, -cam_dist);
234 glRotatef(cam_phi, 1, 0, 0);
235 glRotatef(opt_autorot ? auto_angle : cam_theta, 0, 1, 0);
237 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
238 glLightfv(GL_LIGHT0, GL_DIFFUSE, lcol);
240 glLightfv(GL_LIGHT1, GL_POSITION, ldir2);
241 glLightfv(GL_LIGHT1, GL_DIFFUSE, lcol2);
243 const float blue[] = {0.4, 0.45, 1.0, 1};
244 const float white[] = {1, 1, 1, 1};
245 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
246 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
247 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
249 bind_program(phong_prog);
250 mball_render(sec);
251 bind_program(0);
253 float tfadein = evfadein ? dsys_event_value(evfadein) : 1.0;
254 /*if(tfadein < 1.0) {
255 glEnable(GL_BLEND);
256 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
257 overlay(0, 0, 0, 1.0 - tfadein);
258 glDisable(GL_BLEND);
259 }*/
262 if(!opt_regular_render) {
263 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
264 glViewport(0, 0, xsz, ysz);
266 glClear(GL_COLOR_BUFFER_BIT);
268 bind_program(post_prog);
269 set_uniform_int(post_prog, "framebuf", 0);
270 set_uniform_int(post_prog, "dither_tex", 1);
271 set_uniform_int(post_prog, "dither_levels", DITHER_LEVELS);
272 set_uniform_int(post_prog, "dither_size", DITHER_SZ);
273 set_uniform_float(post_prog, "tfadein", tfadein);
275 glActiveTextureARB(GL_TEXTURE0);
276 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
277 glEnable(GL_TEXTURE_2D);
278 glActiveTextureARB(GL_TEXTURE1);
279 glBindTexture(GL_TEXTURE_2D, dither_tex);
280 glEnable(GL_TEXTURE_2D);
282 overlay(1, 1, 1, 1);
284 glActiveTextureARB(GL_TEXTURE1);
285 glDisable(GL_TEXTURE_2D);
286 glActiveTextureARB(GL_TEXTURE0);
287 glDisable(GL_TEXTURE_2D);
289 bind_program(0);
290 }
292 glutSwapBuffers();
293 assert(glGetError() == GL_NO_ERROR);
294 }
296 void idle()
297 {
298 glutPostRedisplay();
299 }
301 void reshape(int x, int y)
302 {
303 glViewport(0, 0, x, y);
305 glMatrixMode(GL_PROJECTION);
306 glLoadIdentity();
307 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
309 if(x != xsz || y != ysz) {
310 destroy_rtarg(rtarg);
311 rtarg = 0;
312 xsz = x;
313 ysz = y;
314 }
315 }
317 void keyb(unsigned char key, int x, int y)
318 {
319 switch(key) {
320 case 27:
321 exit(0);
323 case 'a':
324 opt_autorot = !opt_autorot;
325 break;
327 case 'f':
328 {
329 static bool fullscreen;
330 static int orig_x, orig_y;
332 fullscreen = !fullscreen;
333 if(fullscreen) {
334 orig_x = xsz;
335 orig_y = ysz;
336 glutFullScreen();
337 } else {
338 glutReshapeWindow(orig_x, orig_y);
339 }
340 }
341 break;
343 case 'r':
344 opt_regular_render = !opt_regular_render;
345 break;
347 case 'h':
348 opt_highres = !opt_highres;
349 if(rtarg) {
350 destroy_rtarg(rtarg);
351 rtarg = 0;
352 }
353 break;
354 }
355 }
357 bool bnstate[16];
358 int prev_x, prev_y;
360 void mouse(int bn, int state, int x, int y)
361 {
362 int idx = bn - GLUT_LEFT_BUTTON;
364 if(idx < (int)(sizeof bnstate / sizeof *bnstate)) {
365 bnstate[idx] = state == GLUT_DOWN;
366 }
367 prev_x = x;
368 prev_y = y;
369 }
371 void motion(int x, int y)
372 {
373 int dx = x - prev_x;
374 int dy = y - prev_y;
375 prev_x = x;
376 prev_y = y;
378 if(bnstate[0]) {
379 cam_theta = fmod(cam_theta + dx * 0.5, 360.0);
380 cam_phi += dy * 0.5;
381 if(cam_phi < -90) {
382 cam_phi = -90;
383 }
384 if(cam_phi > 90) {
385 cam_phi = 90;
386 }
387 }
388 if(bnstate[2]) {
389 cam_dist += dy * 0.1;
390 if(cam_dist < 0) {
391 cam_dist = 0;
392 }
393 }
394 }
396 struct render_target *create_rtarg(int xsz, int ysz)
397 {
398 struct render_target *rt = new render_target;
400 glGenFramebuffersEXT(1, &rt->fbo);
401 glBindFramebufferEXT(GL_FRAMEBUFFER, rt->fbo);
403 // create the render target texture
404 glGenTextures(1, &rt->color_tex);
405 glBindTexture(GL_TEXTURE_2D, rt->color_tex);
406 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
407 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
408 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
409 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
410 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
412 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color_tex, 0);
414 // create depth buffer
415 glGenRenderbuffersEXT(1, &rt->depth_buf);
416 glBindRenderbufferEXT(GL_RENDERBUFFER, rt->depth_buf);
417 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xsz, ysz);
419 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth_buf);
421 if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
422 fprintf(stderr, "incomplete fbo\n");
423 return 0;
424 }
426 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
427 return rt;
428 }
430 void destroy_rtarg(struct render_target *rt)
431 {
432 if(!rt) {
433 return;
434 }
435 glDeleteFramebuffersEXT(1, &rt->fbo);
436 glDeleteTextures(1, &rt->color_tex);
437 glDeleteRenderbuffersEXT(1, &rt->depth_buf);
438 delete rt;
439 }
441 bool parse_args(int argc, char **argv)
442 {
443 for(int i=1; i<argc; i++) {
444 if(strcmp(argv[i], "-noblock") == 0) {
445 opt_highres = true;
446 } else if(strcmp(argv[i], "-nodither") == 0) {
447 opt_regular_render = true;
448 } else {
449 fprintf(stderr, "invalid argument: %s\n", argv[i]);
450 return false;
451 }
452 }
453 return true;
454 }