dbf-udg

view src/udg.cc @ 5:e09cbb2e9d4f

metaballs
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 18 Feb 2013 03:46:52 +0200
parents 5fb21401b7c8
children 57ea4988a9f2
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 float *img = new float[DITHER_SZ * DITHER_SZ * DITHER_LEVELS];
74 float *ptr = img;
76 for(int i=0; i<DITHER_LEVELS; i++) {
77 float val = (float)i / (float)(DITHER_LEVELS - 1);
78 for(int y=0; y<DITHER_SZ; y++) {
79 for(int x=0; x<DITHER_SZ; x++) {
80 /* (1 + M) / (1 + MxN) */
81 float thres = (1.0 + dither_matrix[x][y]) / (1.0 + DITHER_SZ * DITHER_SZ);
82 *ptr++ = val >= thres ? 1.0 : 0.0;
83 }
84 }
85 }
87 if(!(prog = create_program_load("sdr/dither.v.glsl", "sdr/dither.p.glsl"))) {
88 return false;
89 }
91 glGenTextures(1, &dither_tex);
92 glBindTexture(GL_TEXTURE_2D, dither_tex);
93 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
94 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
95 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
96 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
97 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, DITHER_SZ, DITHER_SZ * DITHER_LEVELS, 0, GL_LUMINANCE, GL_FLOAT, img);
99 if(!init_scroller()) {
100 return false;
101 }
103 if(!mball_init()) {
104 return false;
105 }
107 glEnable(GL_CULL_FACE);
108 glEnable(GL_DEPTH_TEST);
109 glEnable(GL_LIGHTING);
110 glEnable(GL_LIGHT0);
111 glEnable(GL_NORMALIZE);
113 return true;
114 }
116 void draw_backdrop()
117 {
118 glPushAttrib(GL_ENABLE_BIT);
119 glDisable(GL_DEPTH_TEST);
120 glDisable(GL_LIGHTING);
122 glMatrixMode(GL_MODELVIEW);
123 glPushMatrix();
124 glLoadIdentity();
125 glMatrixMode(GL_PROJECTION);
126 glPushMatrix();
127 glLoadIdentity();
129 glBegin(GL_QUADS);
130 glColor3f(0, 0, 0);
131 glVertex2f(-1, -1);
132 glVertex2f(1, -1);
133 glColor3f(1, 1, 1);
134 glVertex2f(1, 1);
135 glVertex2f(-1, 1);
136 glEnd();
138 glPopMatrix();
139 glMatrixMode(GL_MODELVIEW);
140 glPopMatrix();
142 glPopAttrib();
143 /*draw_scroller(glutGet(GLUT_ELAPSED_TIME) / 1000.0); */
144 }
146 void disp()
147 {
148 float ldir[] = {-1, 1, 2, 0};
149 int xres, yres;
151 if(opt_highres) {
152 xres = xsz;
153 yres = ysz;
154 } else {
155 xres = xsz / DITHER_SZ;
156 yres = ysz / DITHER_SZ;
157 }
159 if(!rtarg) {
160 printf("(re)creating render target (%dx%d)\n", xres, yres);
161 if(!(rtarg = create_rtarg(xres, yres))) {
162 exit(0);
163 }
164 }
166 if(!opt_regular_render) {
167 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
168 }
169 glViewport(0, 0, xres, yres);
171 glClearColor(1, 1, 1, 1);
172 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
174 draw_backdrop();
176 glMatrixMode(GL_MODELVIEW);
177 glLoadIdentity();
179 glTranslatef(0, 0, -cam_dist);
180 glRotatef(cam_phi, 1, 0, 0);
181 glRotatef(cam_theta, 0, 1, 0);
183 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
185 const float blue[] = {0.4, 0.45, 1.0, 1};
186 const float white[] = {1, 1, 1, 1};
187 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
188 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
189 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
191 mball_render();
194 if(!opt_regular_render) {
195 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
196 glViewport(0, 0, xsz, ysz);
198 glClear(GL_COLOR_BUFFER_BIT);
200 glMatrixMode(GL_PROJECTION);
201 glPushMatrix();
202 glLoadIdentity();
203 glMatrixMode(GL_MODELVIEW);
204 glLoadIdentity();
205 glPushMatrix();
207 glPushAttrib(GL_ENABLE_BIT);
208 glDisable(GL_DEPTH_TEST);
210 bind_program(prog);
211 set_uniform_int(prog, "framebuf", 0);
212 set_uniform_int(prog, "dither_tex", 1);
213 set_uniform_int(prog, "dither_levels", DITHER_LEVELS);
214 set_uniform_int(prog, "dither_size", DITHER_SZ);
216 glActiveTextureARB(GL_TEXTURE0);
217 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
218 glEnable(GL_TEXTURE_2D);
219 glActiveTextureARB(GL_TEXTURE1);
220 glBindTexture(GL_TEXTURE_2D, dither_tex);
221 glEnable(GL_TEXTURE_2D);
223 glBegin(GL_QUADS);
224 glColor3f(0, 1, 0);
225 glTexCoord2f(0, 0); glVertex2f(-1, -1);
226 glTexCoord2f(1, 0); glVertex2f(1, -1);
227 glTexCoord2f(1, 1); glVertex2f(1, 1);
228 glTexCoord2f(0, 1); glVertex2f(-1, 1);
229 glEnd();
231 glActiveTextureARB(GL_TEXTURE1);
232 glDisable(GL_TEXTURE_2D);
233 glActiveTextureARB(GL_TEXTURE0);
234 glDisable(GL_TEXTURE_2D);
236 bind_program(0);
238 glPopAttrib();
240 glMatrixMode(GL_PROJECTION);
241 glPopMatrix();
242 glMatrixMode(GL_MODELVIEW);
243 glPopMatrix();
244 }
246 glutSwapBuffers();
247 assert(glGetError() == GL_NO_ERROR);
248 }
250 void idle()
251 {
252 glutPostRedisplay();
253 }
255 void reshape(int x, int y)
256 {
257 glViewport(0, 0, x, y);
259 glMatrixMode(GL_PROJECTION);
260 glLoadIdentity();
261 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
263 if(x != xsz || y != ysz) {
264 destroy_rtarg(rtarg);
265 rtarg = 0;
266 xsz = x;
267 ysz = y;
268 }
269 }
271 void keyb(unsigned char key, int x, int y)
272 {
273 switch(key) {
274 case 27:
275 exit(0);
277 case 'f':
278 {
279 static bool fullscreen;
280 static int orig_x, orig_y;
282 fullscreen = !fullscreen;
283 if(fullscreen) {
284 orig_x = xsz;
285 orig_y = ysz;
286 glutFullScreen();
287 } else {
288 glutReshapeWindow(orig_x, orig_y);
289 }
290 }
291 break;
293 case 'r':
294 opt_regular_render = !opt_regular_render;
295 break;
297 case 'h':
298 opt_highres = !opt_highres;
299 if(rtarg) {
300 destroy_rtarg(rtarg);
301 rtarg = 0;
302 }
303 break;
304 }
305 }
307 bool bnstate[16];
308 int prev_x, prev_y;
310 void mouse(int bn, int state, int x, int y)
311 {
312 int idx = bn - GLUT_LEFT_BUTTON;
314 if(idx < (int)(sizeof bnstate / sizeof *bnstate)) {
315 bnstate[idx] = state == GLUT_DOWN;
316 }
317 prev_x = x;
318 prev_y = y;
319 }
321 void motion(int x, int y)
322 {
323 int dx = x - prev_x;
324 int dy = y - prev_y;
325 prev_x = x;
326 prev_y = y;
328 if(bnstate[0]) {
329 cam_theta = fmod(cam_theta + dx * 0.5, 360.0);
330 cam_phi += dy * 0.5;
331 if(cam_phi < -90) {
332 cam_phi = -90;
333 }
334 if(cam_phi > 90) {
335 cam_phi = 90;
336 }
337 }
338 if(bnstate[2]) {
339 cam_dist += dy * 0.1;
340 if(cam_dist < 0) {
341 cam_dist = 0;
342 }
343 }
344 }
346 struct render_target *create_rtarg(int xsz, int ysz)
347 {
348 struct render_target *rt = new render_target;
350 glGenFramebuffersEXT(1, &rt->fbo);
351 glBindFramebufferEXT(GL_FRAMEBUFFER, rt->fbo);
353 // create the render target texture
354 glGenTextures(1, &rt->color_tex);
355 glBindTexture(GL_TEXTURE_2D, rt->color_tex);
356 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
357 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
358 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
359 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
360 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
362 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color_tex, 0);
364 // create depth buffer
365 glGenRenderbuffersEXT(1, &rt->depth_buf);
366 glBindRenderbufferEXT(GL_RENDERBUFFER, rt->depth_buf);
367 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xsz, ysz);
369 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth_buf);
371 if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
372 fprintf(stderr, "incomplete fbo\n");
373 return 0;
374 }
376 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
377 return rt;
378 }
380 void destroy_rtarg(struct render_target *rt)
381 {
382 if(!rt) {
383 return;
384 }
385 glDeleteFramebuffersEXT(1, &rt->fbo);
386 glDeleteTextures(1, &rt->color_tex);
387 glDeleteRenderbuffersEXT(1, &rt->depth_buf);
388 delete rt;
389 }