dbf-udg

view src/udg.cc @ 6:57ea4988a9f2

added code to dump the ditherblock image
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 18 Feb 2013 04:41:26 +0200
parents e09cbb2e9d4f
children 603656331514
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"
11 #define DITHER_SZ 8
12 #define DITHER_LEVELS 16
14 #if DITHER_SZ == 4
15 #define dither_matrix dither_matrix4
16 #elif DITHER_SZ == 8
17 #define dither_matrix halftone_matrix8
18 #else
19 #error "invalid dither size"
20 #endif
22 struct render_target {
23 unsigned int fbo;
24 unsigned int color_tex, depth_buf;
25 };
27 bool init();
28 void cleanup();
29 void disp();
30 void idle();
31 void reshape(int x, int y);
32 void keyb(unsigned char key, int x, int y);
33 void mouse(int bn, int state, int x, int y);
34 void motion(int x, int y);
35 struct render_target *create_rtarg(int xsz, int ysz);
36 void destroy_rtarg(struct render_target *rt);
38 int xsz, ysz;
39 float cam_theta, cam_phi = 25, cam_dist = 9;
40 unsigned int dither_tex;
41 struct render_target *rtarg;
42 unsigned int prog;
44 int opt_highres, opt_regular_render;
47 int main(int argc, char **argv)
48 {
49 glutInit(&argc, argv);
50 glutInitWindowSize(1024, 768);
51 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
52 glutCreateWindow("DBF UDG compo entry by Nuclear");
54 glutDisplayFunc(disp);
55 glutIdleFunc(idle);
56 glutReshapeFunc(reshape);
57 glutKeyboardFunc(keyb);
58 glutMouseFunc(mouse);
59 glutMotionFunc(motion);
61 glewInit();
63 if(!init()) {
64 return 1;
65 }
67 glutMainLoop();
68 return 0;
69 }
71 bool init()
72 {
73 FILE *fp = fopen("udg.ppm", "wb");
74 if(fp) {
75 fprintf(fp, "P6\n%d %d\n255\n", DITHER_SZ, DITHER_SZ * DITHER_LEVELS);
76 }
78 unsigned char *img = new unsigned char[DITHER_SZ * DITHER_SZ * DITHER_LEVELS];
79 unsigned char *ptr = img;
81 for(int i=0; i<DITHER_LEVELS; i++) {
82 float val = (float)i / (float)(DITHER_LEVELS - 1);
83 for(int y=0; y<DITHER_SZ; y++) {
84 for(int x=0; x<DITHER_SZ; x++) {
85 /* (1 + M) / (1 + MxN) */
86 float thres = (1.0 + dither_matrix[x][y]) / (1.0 + DITHER_SZ * DITHER_SZ);
87 *ptr++ = val >= thres ? 255 : 0;
89 if(fp) {
90 int r = ptr[-1] ? 246 : 10;
91 int g = ptr[-1] ? 198 : 72;
92 int b = ptr[-1] ? 141 : 85;
93 fputc(r, fp);
94 fputc(g, fp);
95 fputc(b, fp);
96 }
97 }
98 }
99 }
101 if(fp) {
102 fclose(fp);
103 }
105 if(!(prog = create_program_load("sdr/dither.v.glsl", "sdr/dither.p.glsl"))) {
106 return false;
107 }
109 glGenTextures(1, &dither_tex);
110 glBindTexture(GL_TEXTURE_2D, dither_tex);
111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
115 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, DITHER_SZ, DITHER_SZ * DITHER_LEVELS, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, img);
117 if(!init_scroller()) {
118 return false;
119 }
121 if(!mball_init()) {
122 return false;
123 }
125 glEnable(GL_CULL_FACE);
126 glEnable(GL_DEPTH_TEST);
127 glEnable(GL_LIGHTING);
128 glEnable(GL_LIGHT0);
129 glEnable(GL_NORMALIZE);
131 return true;
132 }
134 void draw_backdrop()
135 {
136 glPushAttrib(GL_ENABLE_BIT);
137 glDisable(GL_DEPTH_TEST);
138 glDisable(GL_LIGHTING);
140 glMatrixMode(GL_MODELVIEW);
141 glPushMatrix();
142 glLoadIdentity();
143 glMatrixMode(GL_PROJECTION);
144 glPushMatrix();
145 glLoadIdentity();
147 glBegin(GL_QUADS);
148 glColor3f(0, 0, 0);
149 glVertex2f(-1, -1);
150 glVertex2f(1, -1);
151 glColor3f(1, 1, 1);
152 glVertex2f(1, 1);
153 glVertex2f(-1, 1);
154 glEnd();
156 glPopMatrix();
157 glMatrixMode(GL_MODELVIEW);
158 glPopMatrix();
160 glPopAttrib();
161 /*draw_scroller(glutGet(GLUT_ELAPSED_TIME) / 1000.0); */
162 }
164 void disp()
165 {
166 float ldir[] = {-1, 1, 2, 0};
167 int xres, yres;
169 if(opt_highres) {
170 xres = xsz;
171 yres = ysz;
172 } else {
173 xres = xsz / DITHER_SZ;
174 yres = ysz / DITHER_SZ;
175 }
177 if(!rtarg) {
178 printf("(re)creating render target (%dx%d)\n", xres, yres);
179 if(!(rtarg = create_rtarg(xres, yres))) {
180 exit(0);
181 }
182 }
184 if(!opt_regular_render) {
185 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
186 }
187 glViewport(0, 0, xres, yres);
189 glClearColor(1, 1, 1, 1);
190 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
192 draw_backdrop();
194 glMatrixMode(GL_MODELVIEW);
195 glLoadIdentity();
197 glTranslatef(0, 0, -cam_dist);
198 glRotatef(cam_phi, 1, 0, 0);
199 glRotatef(cam_theta, 0, 1, 0);
201 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
203 const float blue[] = {0.4, 0.45, 1.0, 1};
204 const float white[] = {1, 1, 1, 1};
205 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
206 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
207 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
209 mball_render();
212 if(!opt_regular_render) {
213 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
214 glViewport(0, 0, xsz, ysz);
216 glClear(GL_COLOR_BUFFER_BIT);
218 glMatrixMode(GL_PROJECTION);
219 glPushMatrix();
220 glLoadIdentity();
221 glMatrixMode(GL_MODELVIEW);
222 glLoadIdentity();
223 glPushMatrix();
225 glPushAttrib(GL_ENABLE_BIT);
226 glDisable(GL_DEPTH_TEST);
228 bind_program(prog);
229 set_uniform_int(prog, "framebuf", 0);
230 set_uniform_int(prog, "dither_tex", 1);
231 set_uniform_int(prog, "dither_levels", DITHER_LEVELS);
232 set_uniform_int(prog, "dither_size", DITHER_SZ);
234 glActiveTextureARB(GL_TEXTURE0);
235 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
236 glEnable(GL_TEXTURE_2D);
237 glActiveTextureARB(GL_TEXTURE1);
238 glBindTexture(GL_TEXTURE_2D, dither_tex);
239 glEnable(GL_TEXTURE_2D);
241 glBegin(GL_QUADS);
242 glColor3f(0, 1, 0);
243 glTexCoord2f(0, 0); glVertex2f(-1, -1);
244 glTexCoord2f(1, 0); glVertex2f(1, -1);
245 glTexCoord2f(1, 1); glVertex2f(1, 1);
246 glTexCoord2f(0, 1); glVertex2f(-1, 1);
247 glEnd();
249 glActiveTextureARB(GL_TEXTURE1);
250 glDisable(GL_TEXTURE_2D);
251 glActiveTextureARB(GL_TEXTURE0);
252 glDisable(GL_TEXTURE_2D);
254 bind_program(0);
256 glPopAttrib();
258 glMatrixMode(GL_PROJECTION);
259 glPopMatrix();
260 glMatrixMode(GL_MODELVIEW);
261 glPopMatrix();
262 }
264 glutSwapBuffers();
265 assert(glGetError() == GL_NO_ERROR);
266 }
268 void idle()
269 {
270 glutPostRedisplay();
271 }
273 void reshape(int x, int y)
274 {
275 glViewport(0, 0, x, y);
277 glMatrixMode(GL_PROJECTION);
278 glLoadIdentity();
279 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
281 if(x != xsz || y != ysz) {
282 destroy_rtarg(rtarg);
283 rtarg = 0;
284 xsz = x;
285 ysz = y;
286 }
287 }
289 void keyb(unsigned char key, int x, int y)
290 {
291 switch(key) {
292 case 27:
293 exit(0);
295 case 'f':
296 {
297 static bool fullscreen;
298 static int orig_x, orig_y;
300 fullscreen = !fullscreen;
301 if(fullscreen) {
302 orig_x = xsz;
303 orig_y = ysz;
304 glutFullScreen();
305 } else {
306 glutReshapeWindow(orig_x, orig_y);
307 }
308 }
309 break;
311 case 'r':
312 opt_regular_render = !opt_regular_render;
313 break;
315 case 'h':
316 opt_highres = !opt_highres;
317 if(rtarg) {
318 destroy_rtarg(rtarg);
319 rtarg = 0;
320 }
321 break;
322 }
323 }
325 bool bnstate[16];
326 int prev_x, prev_y;
328 void mouse(int bn, int state, int x, int y)
329 {
330 int idx = bn - GLUT_LEFT_BUTTON;
332 if(idx < (int)(sizeof bnstate / sizeof *bnstate)) {
333 bnstate[idx] = state == GLUT_DOWN;
334 }
335 prev_x = x;
336 prev_y = y;
337 }
339 void motion(int x, int y)
340 {
341 int dx = x - prev_x;
342 int dy = y - prev_y;
343 prev_x = x;
344 prev_y = y;
346 if(bnstate[0]) {
347 cam_theta = fmod(cam_theta + dx * 0.5, 360.0);
348 cam_phi += dy * 0.5;
349 if(cam_phi < -90) {
350 cam_phi = -90;
351 }
352 if(cam_phi > 90) {
353 cam_phi = 90;
354 }
355 }
356 if(bnstate[2]) {
357 cam_dist += dy * 0.1;
358 if(cam_dist < 0) {
359 cam_dist = 0;
360 }
361 }
362 }
364 struct render_target *create_rtarg(int xsz, int ysz)
365 {
366 struct render_target *rt = new render_target;
368 glGenFramebuffersEXT(1, &rt->fbo);
369 glBindFramebufferEXT(GL_FRAMEBUFFER, rt->fbo);
371 // create the render target texture
372 glGenTextures(1, &rt->color_tex);
373 glBindTexture(GL_TEXTURE_2D, rt->color_tex);
374 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
375 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
376 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
377 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
378 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
380 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color_tex, 0);
382 // create depth buffer
383 glGenRenderbuffersEXT(1, &rt->depth_buf);
384 glBindRenderbufferEXT(GL_RENDERBUFFER, rt->depth_buf);
385 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xsz, ysz);
387 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth_buf);
389 if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
390 fprintf(stderr, "incomplete fbo\n");
391 return 0;
392 }
394 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
395 return rt;
396 }
398 void destroy_rtarg(struct render_target *rt)
399 {
400 if(!rt) {
401 return;
402 }
403 glDeleteFramebuffersEXT(1, &rt->fbo);
404 glDeleteTextures(1, &rt->color_tex);
405 glDeleteRenderbuffersEXT(1, &rt->depth_buf);
406 delete rt;
407 }