dbf-udg

view src/udg.cc @ 9:7056437a361b

added demosys
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 19 Feb 2013 18:17:17 +0200
parents f0a47f46ee45
children 1120c069eb17
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <assert.h>
5 #include "opengl.h"
6 #include "sdr.h"
7 #include "dither_matrix.h"
8 #include "scroller.h"
9 #include "mballs.h"
10 #include "dsys.h"
12 #define DITHER_SZ 8
13 #define DITHER_LEVELS 16
15 #if DITHER_SZ == 4
16 #define dither_matrix dither_matrix4
17 #elif DITHER_SZ == 8
18 #define dither_matrix halftone_matrix8
19 #else
20 #error "invalid dither size"
21 #endif
23 struct render_target {
24 unsigned int fbo;
25 unsigned int color_tex, depth_buf;
26 };
28 bool init();
29 void cleanup();
30 void disp();
31 void idle();
32 void reshape(int x, int y);
33 void keyb(unsigned char key, int x, int y);
34 void mouse(int bn, int state, int x, int y);
35 void motion(int x, int y);
36 struct render_target *create_rtarg(int xsz, int ysz);
37 void destroy_rtarg(struct render_target *rt);
39 int xsz, ysz;
40 float cam_theta, cam_phi = 25, cam_dist = 11;
41 unsigned int dither_tex;
42 struct render_target *rtarg;
43 unsigned int post_prog, phong_prog;
45 int opt_highres, opt_regular_render;
46 bool opt_autorot = true;
48 struct dsys_demo *demo;
51 int main(int argc, char **argv)
52 {
53 glutInit(&argc, argv);
54 glutInitWindowSize(1024, 768);
55 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
56 glutCreateWindow("DBF UDG compo entry by Nuclear");
58 glutDisplayFunc(disp);
59 glutIdleFunc(idle);
60 glutReshapeFunc(reshape);
61 glutKeyboardFunc(keyb);
62 glutMouseFunc(mouse);
63 glutMotionFunc(motion);
65 glewInit();
67 if(!init()) {
68 return 1;
69 }
71 glutMainLoop();
72 return 0;
73 }
75 bool init()
76 {
77 FILE *fp = fopen("udg.ppm", "wb");
78 if(fp) {
79 fprintf(fp, "P6\n%d %d\n255\n", DITHER_SZ, DITHER_SZ * DITHER_LEVELS);
80 }
82 unsigned char *img = new unsigned char[DITHER_SZ * DITHER_SZ * DITHER_LEVELS];
83 unsigned char *ptr = img;
85 for(int i=0; i<DITHER_LEVELS; i++) {
86 float val = (float)i / (float)(DITHER_LEVELS - 1);
87 for(int y=0; y<DITHER_SZ; y++) {
88 for(int x=0; x<DITHER_SZ; x++) {
89 /* (1 + M) / (1 + MxN) */
90 float thres = (1.0 + dither_matrix[x][y]) / (1.0 + DITHER_SZ * DITHER_SZ);
91 *ptr++ = val >= thres ? 255 : 0;
93 if(fp) {
94 int r = ptr[-1] ? 246 : 10;
95 int g = ptr[-1] ? 198 : 72;
96 int b = ptr[-1] ? 141 : 85;
97 fputc(r, fp);
98 fputc(g, fp);
99 fputc(b, fp);
100 }
101 }
102 }
103 }
105 if(fp) {
106 fclose(fp);
107 }
109 if(!(phong_prog = create_program_load("sdr/phong.v.glsl", "sdr/phong.p.glsl"))) {
110 return false;
111 }
113 if(!(post_prog = create_program_load("sdr/dither.v.glsl", "sdr/dither.p.glsl"))) {
114 return false;
115 }
117 glGenTextures(1, &dither_tex);
118 glBindTexture(GL_TEXTURE_2D, dither_tex);
119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
120 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
122 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
123 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, DITHER_SZ, DITHER_SZ * DITHER_LEVELS, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, img);
125 if(!init_scroller()) {
126 return false;
127 }
129 if(!mball_init()) {
130 return false;
131 }
133 if(!(demo = dsys_open("demoscript"))) {
134 return false;
135 }
137 glEnable(GL_CULL_FACE);
138 glEnable(GL_DEPTH_TEST);
139 glEnable(GL_LIGHTING);
140 glEnable(GL_LIGHT0);
141 glEnable(GL_LIGHT1);
142 glEnable(GL_NORMALIZE);
144 return true;
145 }
147 void draw_backdrop()
148 {
149 glPushAttrib(GL_ENABLE_BIT);
150 glDisable(GL_DEPTH_TEST);
151 glDisable(GL_LIGHTING);
153 glMatrixMode(GL_MODELVIEW);
154 glPushMatrix();
155 glLoadIdentity();
156 glMatrixMode(GL_PROJECTION);
157 glPushMatrix();
158 glLoadIdentity();
160 glBegin(GL_QUADS);
161 glColor3f(0, 0, 0);
162 glVertex2f(-1, -1);
163 glVertex2f(1, -1);
164 glColor3f(1, 1, 1);
165 glVertex2f(1, 1);
166 glVertex2f(-1, 1);
167 glEnd();
169 glPopMatrix();
170 glMatrixMode(GL_MODELVIEW);
171 glPopMatrix();
173 glPopAttrib();
174 /*draw_scroller(glutGet(GLUT_ELAPSED_TIME) / 1000.0); */
175 }
177 void disp()
178 {
179 float ldir[] = {-1, 1, 2, 0};
180 float ldir2[] = {0.0, 0.35, -0.9, 0};
182 float lcol[] = {1, 1, 1, 1};
183 float lcol2[] = {0.35, 0.3, 0.15, 1};
185 dsys_update(demo, dsys_msec_to_dtime(glutGet(GLUT_ELAPSED_TIME)));
187 float sec = dsys_dtime_to_sec(dsys_time(demo));
188 float auto_angle = sec * 10.0;
190 int xres, yres;
191 if(opt_highres) {
192 xres = xsz;
193 yres = ysz;
194 } else {
195 xres = xsz / DITHER_SZ;
196 yres = ysz / DITHER_SZ;
197 }
199 if(!rtarg) {
200 printf("(re)creating render target (%dx%d)\n", xres, yres);
201 if(!(rtarg = create_rtarg(xres, yres))) {
202 exit(0);
203 }
204 }
206 if(!opt_regular_render) {
207 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
208 }
209 glViewport(0, 0, xres, yres);
211 glClearColor(1, 1, 1, 1);
212 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
214 draw_backdrop();
216 glMatrixMode(GL_MODELVIEW);
217 glLoadIdentity();
219 glTranslatef(0, 0.8, -cam_dist);
220 glRotatef(cam_phi, 1, 0, 0);
221 glRotatef(opt_autorot ? auto_angle : cam_theta, 0, 1, 0);
223 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
224 glLightfv(GL_LIGHT0, GL_DIFFUSE, lcol);
226 glLightfv(GL_LIGHT1, GL_POSITION, ldir2);
227 glLightfv(GL_LIGHT1, GL_DIFFUSE, lcol2);
229 const float blue[] = {0.4, 0.45, 1.0, 1};
230 const float white[] = {1, 1, 1, 1};
231 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
232 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
233 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
235 bind_program(phong_prog);
236 mball_render(sec);
237 bind_program(0);
240 if(!opt_regular_render) {
241 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
242 glViewport(0, 0, xsz, ysz);
244 glClear(GL_COLOR_BUFFER_BIT);
246 glMatrixMode(GL_PROJECTION);
247 glPushMatrix();
248 glLoadIdentity();
249 glMatrixMode(GL_MODELVIEW);
250 glLoadIdentity();
251 glPushMatrix();
253 glPushAttrib(GL_ENABLE_BIT);
254 glDisable(GL_DEPTH_TEST);
256 bind_program(post_prog);
257 set_uniform_int(post_prog, "framebuf", 0);
258 set_uniform_int(post_prog, "dither_tex", 1);
259 set_uniform_int(post_prog, "dither_levels", DITHER_LEVELS);
260 set_uniform_int(post_prog, "dither_size", DITHER_SZ);
262 glActiveTextureARB(GL_TEXTURE0);
263 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
264 glEnable(GL_TEXTURE_2D);
265 glActiveTextureARB(GL_TEXTURE1);
266 glBindTexture(GL_TEXTURE_2D, dither_tex);
267 glEnable(GL_TEXTURE_2D);
269 glBegin(GL_QUADS);
270 glColor3f(0, 1, 0);
271 glTexCoord2f(0, 0); glVertex2f(-1, -1);
272 glTexCoord2f(1, 0); glVertex2f(1, -1);
273 glTexCoord2f(1, 1); glVertex2f(1, 1);
274 glTexCoord2f(0, 1); glVertex2f(-1, 1);
275 glEnd();
277 glActiveTextureARB(GL_TEXTURE1);
278 glDisable(GL_TEXTURE_2D);
279 glActiveTextureARB(GL_TEXTURE0);
280 glDisable(GL_TEXTURE_2D);
282 bind_program(0);
284 glPopAttrib();
286 glMatrixMode(GL_PROJECTION);
287 glPopMatrix();
288 glMatrixMode(GL_MODELVIEW);
289 glPopMatrix();
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 }