dbf-udg

view src/udg.cc @ 4:5fb21401b7c8

lala
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 07 Feb 2013 18:52:28 +0200
parents 403ec1be3a1a
children e09cbb2e9d4f
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"
10 #define DITHER_SZ 8
11 #define DITHER_LEVELS 16
13 #if DITHER_SZ == 4
14 #define dither_matrix dither_matrix4
15 #elif DITHER_SZ == 8
16 #define dither_matrix halftone_matrix8
17 #else
18 #error "invalid dither size"
19 #endif
21 struct render_target {
22 unsigned int fbo;
23 unsigned int color_tex, depth_buf;
24 };
26 bool init();
27 void cleanup();
28 void disp();
29 void idle();
30 void reshape(int x, int y);
31 void keyb(unsigned char key, int x, int y);
32 void mouse(int bn, int state, int x, int y);
33 void motion(int x, int y);
34 struct render_target *create_rtarg(int xsz, int ysz);
35 void destroy_rtarg(struct render_target *rt);
37 int xsz, ysz;
38 float cam_theta, cam_phi = 25, cam_dist = 8;
39 unsigned int dither_tex;
40 struct render_target *rtarg;
41 unsigned int prog;
43 int main(int argc, char **argv)
44 {
45 glutInit(&argc, argv);
46 glutInitWindowSize(1024, 768);
47 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
48 glutCreateWindow("DBF UDG compo entry by Nuclear");
50 glutDisplayFunc(disp);
51 glutIdleFunc(idle);
52 glutReshapeFunc(reshape);
53 glutKeyboardFunc(keyb);
54 glutMouseFunc(mouse);
55 glutMotionFunc(motion);
57 glewInit();
59 if(!init()) {
60 return 1;
61 }
63 glutMainLoop();
64 return 0;
65 }
67 bool init()
68 {
69 float *img = new float[DITHER_SZ * DITHER_SZ * DITHER_LEVELS];
70 float *ptr = img;
72 for(int i=0; i<DITHER_LEVELS; i++) {
73 float val = (float)i / (float)(DITHER_LEVELS - 1);
74 for(int y=0; y<DITHER_SZ; y++) {
75 for(int x=0; x<DITHER_SZ; x++) {
76 /* (1 + M) / (1 + MxN) */
77 float thres = (1.0 + dither_matrix[x][y]) / (1.0 + DITHER_SZ * DITHER_SZ);
78 *ptr++ = val >= thres ? 1.0 : 0.0;
79 }
80 }
81 }
83 if(!(prog = create_program_load("sdr/dither.v.glsl", "sdr/dither.p.glsl"))) {
84 return false;
85 }
87 glGenTextures(1, &dither_tex);
88 glBindTexture(GL_TEXTURE_2D, dither_tex);
89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
93 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, DITHER_SZ, DITHER_SZ * DITHER_LEVELS, 0, GL_LUMINANCE, GL_FLOAT, img);
95 if(!init_scroller()) {
96 return false;
97 }
99 glEnable(GL_CULL_FACE);
100 glEnable(GL_DEPTH_TEST);
101 glEnable(GL_LIGHTING);
102 glEnable(GL_LIGHT0);
104 return true;
105 }
107 void draw_backdrop()
108 {
109 glPushAttrib(GL_ENABLE_BIT);
110 glDisable(GL_DEPTH_TEST);
111 glDisable(GL_LIGHTING);
113 glMatrixMode(GL_MODELVIEW);
114 glPushMatrix();
115 glLoadIdentity();
116 glMatrixMode(GL_PROJECTION);
117 glPushMatrix();
118 glLoadIdentity();
120 glBegin(GL_QUADS);
121 glColor3f(0, 0, 0);
122 glVertex2f(-1, -1);
123 glVertex2f(1, -1);
124 glColor3f(1, 1, 1);
125 glVertex2f(1, 1);
126 glVertex2f(-1, 1);
127 glEnd();
129 glPopMatrix();
130 glMatrixMode(GL_MODELVIEW);
131 glPopMatrix();
133 glPopAttrib();
134 /*draw_scroller(glutGet(GLUT_ELAPSED_TIME) / 1000.0); */
135 }
137 void disp()
138 {
139 float ldir[] = {-1, 1, 2, 0};
141 int xres = xsz / DITHER_SZ;
142 int yres = ysz / DITHER_SZ;
143 /*int xres = xsz;
144 int yres = ysz;*/
146 if(!rtarg) {
147 printf("(re)creating render target (%dx%d)\n", xres, yres);
148 if(!(rtarg = create_rtarg(xres, yres))) {
149 exit(0);
150 }
151 }
153 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
154 glViewport(0, 0, xres, yres);
156 glClearColor(1, 1, 1, 1);
157 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
159 draw_backdrop();
161 glMatrixMode(GL_MODELVIEW);
162 glLoadIdentity();
164 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
166 glTranslatef(0, 0, -cam_dist);
167 glRotatef(cam_phi, 1, 0, 0);
168 glRotatef(cam_theta, 0, 1, 0);
170 const float blue[] = {0.4, 0.45, 1.0, 1};
171 const float white[] = {1, 1, 1, 1};
172 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
173 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
174 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
176 glFrontFace(GL_CW);
177 glutSolidTeapot(1.0);
178 glFrontFace(GL_CCW);
181 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
182 glViewport(0, 0, xsz, ysz);
184 glClear(GL_COLOR_BUFFER_BIT);
186 glMatrixMode(GL_PROJECTION);
187 glPushMatrix();
188 glLoadIdentity();
189 glMatrixMode(GL_MODELVIEW);
190 glLoadIdentity();
191 glPushMatrix();
193 glPushAttrib(GL_ENABLE_BIT);
194 glDisable(GL_DEPTH_TEST);
196 bind_program(prog);
197 set_uniform_int(prog, "framebuf", 0);
198 set_uniform_int(prog, "dither_tex", 1);
199 set_uniform_int(prog, "dither_levels", DITHER_LEVELS);
200 set_uniform_int(prog, "dither_size", DITHER_SZ);
202 glActiveTextureARB(GL_TEXTURE0);
203 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
204 glEnable(GL_TEXTURE_2D);
205 glActiveTextureARB(GL_TEXTURE1);
206 glBindTexture(GL_TEXTURE_2D, dither_tex);
207 glEnable(GL_TEXTURE_2D);
209 glBegin(GL_QUADS);
210 glColor3f(0, 1, 0);
211 glTexCoord2f(0, 0); glVertex2f(-1, -1);
212 glTexCoord2f(1, 0); glVertex2f(1, -1);
213 glTexCoord2f(1, 1); glVertex2f(1, 1);
214 glTexCoord2f(0, 1); glVertex2f(-1, 1);
215 glEnd();
217 glActiveTextureARB(GL_TEXTURE1);
218 glDisable(GL_TEXTURE_2D);
219 glActiveTextureARB(GL_TEXTURE0);
220 glDisable(GL_TEXTURE_2D);
222 bind_program(0);
224 glPopAttrib();
226 glMatrixMode(GL_PROJECTION);
227 glPopMatrix();
228 glMatrixMode(GL_MODELVIEW);
229 glPopMatrix();
231 glutSwapBuffers();
232 assert(glGetError() == GL_NO_ERROR);
233 }
235 void idle()
236 {
237 glutPostRedisplay();
238 }
240 void reshape(int x, int y)
241 {
242 glViewport(0, 0, x, y);
244 glMatrixMode(GL_PROJECTION);
245 glLoadIdentity();
246 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
248 if(x != xsz || y != ysz) {
249 destroy_rtarg(rtarg);
250 rtarg = 0;
251 xsz = x;
252 ysz = y;
253 }
254 }
256 void keyb(unsigned char key, int x, int y)
257 {
258 switch(key) {
259 case 27:
260 exit(0);
262 case 'f':
263 {
264 static bool fullscreen;
265 static int orig_x, orig_y;
267 fullscreen = !fullscreen;
268 if(fullscreen) {
269 orig_x = xsz;
270 orig_y = ysz;
271 glutFullScreen();
272 } else {
273 glutReshapeWindow(orig_x, orig_y);
274 }
275 }
276 break;
277 }
278 }
280 bool bnstate[16];
281 int prev_x, prev_y;
283 void mouse(int bn, int state, int x, int y)
284 {
285 int idx = bn - GLUT_LEFT_BUTTON;
287 if(idx < (int)(sizeof bnstate / sizeof *bnstate)) {
288 bnstate[idx] = state == GLUT_DOWN;
289 }
290 prev_x = x;
291 prev_y = y;
292 }
294 void motion(int x, int y)
295 {
296 int dx = x - prev_x;
297 int dy = y - prev_y;
298 prev_x = x;
299 prev_y = y;
301 if(bnstate[0]) {
302 cam_theta = fmod(cam_theta + dx * 0.5, 360.0);
303 cam_phi += dy * 0.5;
304 if(cam_phi < -90) {
305 cam_phi = -90;
306 }
307 if(cam_phi > 90) {
308 cam_phi = 90;
309 }
310 }
311 if(bnstate[2]) {
312 cam_dist += dy * 0.1;
313 if(cam_dist < 0) {
314 cam_dist = 0;
315 }
316 }
317 }
319 struct render_target *create_rtarg(int xsz, int ysz)
320 {
321 struct render_target *rt = new render_target;
323 glGenFramebuffersEXT(1, &rt->fbo);
324 glBindFramebufferEXT(GL_FRAMEBUFFER, rt->fbo);
326 // create the render target texture
327 glGenTextures(1, &rt->color_tex);
328 glBindTexture(GL_TEXTURE_2D, rt->color_tex);
329 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
330 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
331 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
332 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
333 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
335 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color_tex, 0);
337 // create depth buffer
338 glGenRenderbuffersEXT(1, &rt->depth_buf);
339 glBindRenderbufferEXT(GL_RENDERBUFFER, rt->depth_buf);
340 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xsz, ysz);
342 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth_buf);
344 if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
345 fprintf(stderr, "incomplete fbo\n");
346 return 0;
347 }
349 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
350 return rt;
351 }
353 void destroy_rtarg(struct render_target *rt)
354 {
355 if(!rt) {
356 return;
357 }
358 glDeleteFramebuffersEXT(1, &rt->fbo);
359 glDeleteTextures(1, &rt->color_tex);
360 glDeleteRenderbuffersEXT(1, &rt->depth_buf);
361 delete rt;
362 }