dbf-udg

view src/udg.cc @ 10:1120c069eb17

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 19 Feb 2013 23:46:44 +0200
parents 7056437a361b
children 5f99c4c7a9fe
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 dsys_start(demo);
146 return true;
147 }
149 void draw_backdrop()
150 {
151 glPushAttrib(GL_ENABLE_BIT);
152 glDisable(GL_DEPTH_TEST);
153 glDisable(GL_LIGHTING);
155 glMatrixMode(GL_MODELVIEW);
156 glPushMatrix();
157 glLoadIdentity();
158 glMatrixMode(GL_PROJECTION);
159 glPushMatrix();
160 glLoadIdentity();
162 glBegin(GL_QUADS);
163 glColor3f(0, 0, 0);
164 glVertex2f(-1, -1);
165 glVertex2f(1, -1);
166 glColor3f(1, 1, 1);
167 glVertex2f(1, 1);
168 glVertex2f(-1, 1);
169 glEnd();
171 glPopMatrix();
172 glMatrixMode(GL_MODELVIEW);
173 glPopMatrix();
175 glPopAttrib();
176 /*draw_scroller(glutGet(GLUT_ELAPSED_TIME) / 1000.0); */
177 }
179 void disp()
180 {
181 float ldir[] = {-1, 1, 2, 0};
182 float ldir2[] = {0.0, 0.35, -0.9, 0};
184 float lcol[] = {1, 1, 1, 1};
185 float lcol2[] = {0.35, 0.3, 0.15, 1};
187 dsys_update(demo, dsys_msec_to_dtime(glutGet(GLUT_ELAPSED_TIME)));
189 float sec = dsys_dtime_to_sec(dsys_time(demo));
190 float auto_angle = sec * 10.0;
192 int xres, yres;
193 if(opt_highres) {
194 xres = xsz;
195 yres = ysz;
196 } else {
197 xres = xsz / DITHER_SZ;
198 yres = ysz / DITHER_SZ;
199 }
201 if(!rtarg) {
202 printf("(re)creating render target (%dx%d)\n", xres, yres);
203 if(!(rtarg = create_rtarg(xres, yres))) {
204 exit(0);
205 }
206 }
208 if(!opt_regular_render) {
209 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
210 }
211 glViewport(0, 0, xres, yres);
213 glClearColor(1, 1, 1, 1);
214 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
216 draw_backdrop();
218 glMatrixMode(GL_MODELVIEW);
219 glLoadIdentity();
221 glTranslatef(0, 0.8, -cam_dist);
222 glRotatef(cam_phi, 1, 0, 0);
223 glRotatef(opt_autorot ? auto_angle : cam_theta, 0, 1, 0);
225 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
226 glLightfv(GL_LIGHT0, GL_DIFFUSE, lcol);
228 glLightfv(GL_LIGHT1, GL_POSITION, ldir2);
229 glLightfv(GL_LIGHT1, GL_DIFFUSE, lcol2);
231 const float blue[] = {0.4, 0.45, 1.0, 1};
232 const float white[] = {1, 1, 1, 1};
233 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
234 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
235 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
237 bind_program(phong_prog);
238 mball_render(sec);
239 bind_program(0);
242 if(!opt_regular_render) {
243 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
244 glViewport(0, 0, xsz, ysz);
246 glClear(GL_COLOR_BUFFER_BIT);
248 glMatrixMode(GL_PROJECTION);
249 glPushMatrix();
250 glLoadIdentity();
251 glMatrixMode(GL_MODELVIEW);
252 glLoadIdentity();
253 glPushMatrix();
255 glPushAttrib(GL_ENABLE_BIT);
256 glDisable(GL_DEPTH_TEST);
258 bind_program(post_prog);
259 set_uniform_int(post_prog, "framebuf", 0);
260 set_uniform_int(post_prog, "dither_tex", 1);
261 set_uniform_int(post_prog, "dither_levels", DITHER_LEVELS);
262 set_uniform_int(post_prog, "dither_size", DITHER_SZ);
264 glActiveTextureARB(GL_TEXTURE0);
265 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
266 glEnable(GL_TEXTURE_2D);
267 glActiveTextureARB(GL_TEXTURE1);
268 glBindTexture(GL_TEXTURE_2D, dither_tex);
269 glEnable(GL_TEXTURE_2D);
271 glBegin(GL_QUADS);
272 glColor3f(0, 1, 0);
273 glTexCoord2f(0, 0); glVertex2f(-1, -1);
274 glTexCoord2f(1, 0); glVertex2f(1, -1);
275 glTexCoord2f(1, 1); glVertex2f(1, 1);
276 glTexCoord2f(0, 1); glVertex2f(-1, 1);
277 glEnd();
279 glActiveTextureARB(GL_TEXTURE1);
280 glDisable(GL_TEXTURE_2D);
281 glActiveTextureARB(GL_TEXTURE0);
282 glDisable(GL_TEXTURE_2D);
284 bind_program(0);
286 glPopAttrib();
288 glMatrixMode(GL_PROJECTION);
289 glPopMatrix();
290 glMatrixMode(GL_MODELVIEW);
291 glPopMatrix();
292 }
294 glutSwapBuffers();
295 assert(glGetError() == GL_NO_ERROR);
296 }
298 void idle()
299 {
300 glutPostRedisplay();
301 }
303 void reshape(int x, int y)
304 {
305 glViewport(0, 0, x, y);
307 glMatrixMode(GL_PROJECTION);
308 glLoadIdentity();
309 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
311 if(x != xsz || y != ysz) {
312 destroy_rtarg(rtarg);
313 rtarg = 0;
314 xsz = x;
315 ysz = y;
316 }
317 }
319 void keyb(unsigned char key, int x, int y)
320 {
321 switch(key) {
322 case 27:
323 exit(0);
325 case 'a':
326 opt_autorot = !opt_autorot;
327 break;
329 case 'f':
330 {
331 static bool fullscreen;
332 static int orig_x, orig_y;
334 fullscreen = !fullscreen;
335 if(fullscreen) {
336 orig_x = xsz;
337 orig_y = ysz;
338 glutFullScreen();
339 } else {
340 glutReshapeWindow(orig_x, orig_y);
341 }
342 }
343 break;
345 case 'r':
346 opt_regular_render = !opt_regular_render;
347 break;
349 case 'h':
350 opt_highres = !opt_highres;
351 if(rtarg) {
352 destroy_rtarg(rtarg);
353 rtarg = 0;
354 }
355 break;
356 }
357 }
359 bool bnstate[16];
360 int prev_x, prev_y;
362 void mouse(int bn, int state, int x, int y)
363 {
364 int idx = bn - GLUT_LEFT_BUTTON;
366 if(idx < (int)(sizeof bnstate / sizeof *bnstate)) {
367 bnstate[idx] = state == GLUT_DOWN;
368 }
369 prev_x = x;
370 prev_y = y;
371 }
373 void motion(int x, int y)
374 {
375 int dx = x - prev_x;
376 int dy = y - prev_y;
377 prev_x = x;
378 prev_y = y;
380 if(bnstate[0]) {
381 cam_theta = fmod(cam_theta + dx * 0.5, 360.0);
382 cam_phi += dy * 0.5;
383 if(cam_phi < -90) {
384 cam_phi = -90;
385 }
386 if(cam_phi > 90) {
387 cam_phi = 90;
388 }
389 }
390 if(bnstate[2]) {
391 cam_dist += dy * 0.1;
392 if(cam_dist < 0) {
393 cam_dist = 0;
394 }
395 }
396 }
398 struct render_target *create_rtarg(int xsz, int ysz)
399 {
400 struct render_target *rt = new render_target;
402 glGenFramebuffersEXT(1, &rt->fbo);
403 glBindFramebufferEXT(GL_FRAMEBUFFER, rt->fbo);
405 // create the render target texture
406 glGenTextures(1, &rt->color_tex);
407 glBindTexture(GL_TEXTURE_2D, rt->color_tex);
408 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
409 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
410 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
411 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
412 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
414 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color_tex, 0);
416 // create depth buffer
417 glGenRenderbuffersEXT(1, &rt->depth_buf);
418 glBindRenderbufferEXT(GL_RENDERBUFFER, rt->depth_buf);
419 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xsz, ysz);
421 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth_buf);
423 if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
424 fprintf(stderr, "incomplete fbo\n");
425 return 0;
426 }
428 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
429 return rt;
430 }
432 void destroy_rtarg(struct render_target *rt)
433 {
434 if(!rt) {
435 return;
436 }
437 glDeleteFramebuffersEXT(1, &rt->fbo);
438 glDeleteTextures(1, &rt->color_tex);
439 glDeleteRenderbuffersEXT(1, &rt->depth_buf);
440 delete rt;
441 }