dbf-udg

view src/udg.cc @ 7:603656331514

phong blobs
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 18 Feb 2013 05:44:17 +0200
parents 57ea4988a9f2
children f0a47f46ee45
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 post_prog, phong_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(!(phong_prog = create_program_load("sdr/phong.v.glsl", "sdr/phong.p.glsl"))) {
106 return false;
107 }
109 if(!(post_prog = create_program_load("sdr/dither.v.glsl", "sdr/dither.p.glsl"))) {
110 return false;
111 }
113 glGenTextures(1, &dither_tex);
114 glBindTexture(GL_TEXTURE_2D, dither_tex);
115 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
119 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, DITHER_SZ, DITHER_SZ * DITHER_LEVELS, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, img);
121 if(!init_scroller()) {
122 return false;
123 }
125 if(!mball_init()) {
126 return false;
127 }
129 glEnable(GL_CULL_FACE);
130 glEnable(GL_DEPTH_TEST);
131 glEnable(GL_LIGHTING);
132 glEnable(GL_LIGHT0);
133 glEnable(GL_NORMALIZE);
135 return true;
136 }
138 void draw_backdrop()
139 {
140 glPushAttrib(GL_ENABLE_BIT);
141 glDisable(GL_DEPTH_TEST);
142 glDisable(GL_LIGHTING);
144 glMatrixMode(GL_MODELVIEW);
145 glPushMatrix();
146 glLoadIdentity();
147 glMatrixMode(GL_PROJECTION);
148 glPushMatrix();
149 glLoadIdentity();
151 glBegin(GL_QUADS);
152 glColor3f(0, 0, 0);
153 glVertex2f(-1, -1);
154 glVertex2f(1, -1);
155 glColor3f(1, 1, 1);
156 glVertex2f(1, 1);
157 glVertex2f(-1, 1);
158 glEnd();
160 glPopMatrix();
161 glMatrixMode(GL_MODELVIEW);
162 glPopMatrix();
164 glPopAttrib();
165 /*draw_scroller(glutGet(GLUT_ELAPSED_TIME) / 1000.0); */
166 }
168 void disp()
169 {
170 float ldir[] = {-1, 1, 2, 0};
171 int xres, yres;
173 if(opt_highres) {
174 xres = xsz;
175 yres = ysz;
176 } else {
177 xres = xsz / DITHER_SZ;
178 yres = ysz / DITHER_SZ;
179 }
181 if(!rtarg) {
182 printf("(re)creating render target (%dx%d)\n", xres, yres);
183 if(!(rtarg = create_rtarg(xres, yres))) {
184 exit(0);
185 }
186 }
188 if(!opt_regular_render) {
189 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
190 }
191 glViewport(0, 0, xres, yres);
193 glClearColor(1, 1, 1, 1);
194 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
196 draw_backdrop();
198 glMatrixMode(GL_MODELVIEW);
199 glLoadIdentity();
201 glTranslatef(0, 0, -cam_dist);
202 glRotatef(cam_phi, 1, 0, 0);
203 glRotatef(cam_theta, 0, 1, 0);
205 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
207 const float blue[] = {0.4, 0.45, 1.0, 1};
208 const float white[] = {1, 1, 1, 1};
209 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
210 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
211 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
213 bind_program(phong_prog);
214 mball_render();
215 bind_program(0);
218 if(!opt_regular_render) {
219 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
220 glViewport(0, 0, xsz, ysz);
222 glClear(GL_COLOR_BUFFER_BIT);
224 glMatrixMode(GL_PROJECTION);
225 glPushMatrix();
226 glLoadIdentity();
227 glMatrixMode(GL_MODELVIEW);
228 glLoadIdentity();
229 glPushMatrix();
231 glPushAttrib(GL_ENABLE_BIT);
232 glDisable(GL_DEPTH_TEST);
234 bind_program(post_prog);
235 set_uniform_int(post_prog, "framebuf", 0);
236 set_uniform_int(post_prog, "dither_tex", 1);
237 set_uniform_int(post_prog, "dither_levels", DITHER_LEVELS);
238 set_uniform_int(post_prog, "dither_size", DITHER_SZ);
240 glActiveTextureARB(GL_TEXTURE0);
241 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
242 glEnable(GL_TEXTURE_2D);
243 glActiveTextureARB(GL_TEXTURE1);
244 glBindTexture(GL_TEXTURE_2D, dither_tex);
245 glEnable(GL_TEXTURE_2D);
247 glBegin(GL_QUADS);
248 glColor3f(0, 1, 0);
249 glTexCoord2f(0, 0); glVertex2f(-1, -1);
250 glTexCoord2f(1, 0); glVertex2f(1, -1);
251 glTexCoord2f(1, 1); glVertex2f(1, 1);
252 glTexCoord2f(0, 1); glVertex2f(-1, 1);
253 glEnd();
255 glActiveTextureARB(GL_TEXTURE1);
256 glDisable(GL_TEXTURE_2D);
257 glActiveTextureARB(GL_TEXTURE0);
258 glDisable(GL_TEXTURE_2D);
260 bind_program(0);
262 glPopAttrib();
264 glMatrixMode(GL_PROJECTION);
265 glPopMatrix();
266 glMatrixMode(GL_MODELVIEW);
267 glPopMatrix();
268 }
270 glutSwapBuffers();
271 assert(glGetError() == GL_NO_ERROR);
272 }
274 void idle()
275 {
276 glutPostRedisplay();
277 }
279 void reshape(int x, int y)
280 {
281 glViewport(0, 0, x, y);
283 glMatrixMode(GL_PROJECTION);
284 glLoadIdentity();
285 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
287 if(x != xsz || y != ysz) {
288 destroy_rtarg(rtarg);
289 rtarg = 0;
290 xsz = x;
291 ysz = y;
292 }
293 }
295 void keyb(unsigned char key, int x, int y)
296 {
297 switch(key) {
298 case 27:
299 exit(0);
301 case 'f':
302 {
303 static bool fullscreen;
304 static int orig_x, orig_y;
306 fullscreen = !fullscreen;
307 if(fullscreen) {
308 orig_x = xsz;
309 orig_y = ysz;
310 glutFullScreen();
311 } else {
312 glutReshapeWindow(orig_x, orig_y);
313 }
314 }
315 break;
317 case 'r':
318 opt_regular_render = !opt_regular_render;
319 break;
321 case 'h':
322 opt_highres = !opt_highres;
323 if(rtarg) {
324 destroy_rtarg(rtarg);
325 rtarg = 0;
326 }
327 break;
328 }
329 }
331 bool bnstate[16];
332 int prev_x, prev_y;
334 void mouse(int bn, int state, int x, int y)
335 {
336 int idx = bn - GLUT_LEFT_BUTTON;
338 if(idx < (int)(sizeof bnstate / sizeof *bnstate)) {
339 bnstate[idx] = state == GLUT_DOWN;
340 }
341 prev_x = x;
342 prev_y = y;
343 }
345 void motion(int x, int y)
346 {
347 int dx = x - prev_x;
348 int dy = y - prev_y;
349 prev_x = x;
350 prev_y = y;
352 if(bnstate[0]) {
353 cam_theta = fmod(cam_theta + dx * 0.5, 360.0);
354 cam_phi += dy * 0.5;
355 if(cam_phi < -90) {
356 cam_phi = -90;
357 }
358 if(cam_phi > 90) {
359 cam_phi = 90;
360 }
361 }
362 if(bnstate[2]) {
363 cam_dist += dy * 0.1;
364 if(cam_dist < 0) {
365 cam_dist = 0;
366 }
367 }
368 }
370 struct render_target *create_rtarg(int xsz, int ysz)
371 {
372 struct render_target *rt = new render_target;
374 glGenFramebuffersEXT(1, &rt->fbo);
375 glBindFramebufferEXT(GL_FRAMEBUFFER, rt->fbo);
377 // create the render target texture
378 glGenTextures(1, &rt->color_tex);
379 glBindTexture(GL_TEXTURE_2D, rt->color_tex);
380 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
381 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
382 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
383 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
384 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
386 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color_tex, 0);
388 // create depth buffer
389 glGenRenderbuffersEXT(1, &rt->depth_buf);
390 glBindRenderbufferEXT(GL_RENDERBUFFER, rt->depth_buf);
391 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xsz, ysz);
393 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth_buf);
395 if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
396 fprintf(stderr, "incomplete fbo\n");
397 return 0;
398 }
400 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
401 return rt;
402 }
404 void destroy_rtarg(struct render_target *rt)
405 {
406 if(!rt) {
407 return;
408 }
409 glDeleteFramebuffersEXT(1, &rt->fbo);
410 glDeleteTextures(1, &rt->color_tex);
411 glDeleteRenderbuffersEXT(1, &rt->depth_buf);
412 delete rt;
413 }