dbf-udg

view src/udg.cc @ 1:1d5dc834d403

blockify
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 08 Jan 2013 14:19:06 +0200
parents 2d27bfd21fc5
children c45c7a1f7d9d
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <assert.h>
5 #include <GL/glew.h>
7 #ifndef __APPLE__
8 #include <GL/glut.h>
9 #else
10 #include <GLUT/glut.h>
11 #endif
13 #include "sdr.h"
14 #include "dither_matrix.h"
16 #define DITHER_SZ 4
17 #define DITHER_LEVELS 16
19 #if DITHER_SZ == 4
20 #define dither_matrix dither_matrix4
21 #elif DITHER_SZ == 8
22 #define dither_matrix dither_matrix8
23 #else
24 #error "invalid dither size"
25 #endif
27 struct render_target {
28 unsigned int fbo;
29 unsigned int color_tex, depth_buf;
30 };
32 bool init();
33 void cleanup();
34 void disp();
35 void idle();
36 void reshape(int x, int y);
37 void keyb(unsigned char key, int x, int y);
38 void mouse(int bn, int state, int x, int y);
39 void motion(int x, int y);
40 struct render_target *create_rtarg(int xsz, int ysz);
41 void destroy_rtarg(struct render_target *rt);
43 int xsz, ysz;
44 float cam_theta, cam_phi = 25, cam_dist = 8;
45 unsigned int dither_tex;
46 struct render_target *rtarg;
47 unsigned int prog;
49 int main(int argc, char **argv)
50 {
51 glutInit(&argc, argv);
52 glutInitWindowSize(1024, 768);
53 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
54 glutCreateWindow("DBF UDG compo entry by Nuclear");
56 glutDisplayFunc(disp);
57 glutIdleFunc(idle);
58 glutReshapeFunc(reshape);
59 glutKeyboardFunc(keyb);
60 glutMouseFunc(mouse);
61 glutMotionFunc(motion);
63 glewInit();
65 if(!init()) {
66 return 1;
67 }
69 glutMainLoop();
70 return 0;
71 }
73 bool init()
74 {
75 float *img = new float[DITHER_SZ * DITHER_SZ * DITHER_LEVELS];
76 float *ptr = img;
78 for(int i=0; i<DITHER_LEVELS; i++) {
79 float val = (float)i / (float)(DITHER_LEVELS - 1);
80 for(int y=0; y<DITHER_SZ; y++) {
81 for(int x=0; x<DITHER_SZ; x++) {
82 /* (1 + M) / (1 + MxN) */
83 float thres = (1.0 + dither_matrix[x][y]) / (1.0 + DITHER_SZ * DITHER_SZ);
84 *ptr++ = val >= thres ? 1.0 : 0.0;
85 }
86 }
87 }
89 if(!(prog = create_program_load("sdr/dither.v.glsl", "sdr/dither.p.glsl"))) {
90 return false;
91 }
93 glGenTextures(1, &dither_tex);
94 glBindTexture(GL_TEXTURE_2D, dither_tex);
95 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
96 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
97 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
98 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
99 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, DITHER_SZ, DITHER_SZ * DITHER_LEVELS, 0, GL_LUMINANCE, GL_FLOAT, img);
101 glEnable(GL_CULL_FACE);
102 glEnable(GL_DEPTH_TEST);
103 glEnable(GL_LIGHTING);
104 glEnable(GL_LIGHT0);
106 return true;
107 }
109 void draw_backdrop()
110 {
111 glPushAttrib(GL_ENABLE_BIT);
112 glDisable(GL_DEPTH_TEST);
113 glDisable(GL_LIGHTING);
115 glMatrixMode(GL_MODELVIEW);
116 glPushMatrix();
117 glLoadIdentity();
118 glMatrixMode(GL_PROJECTION);
119 glPushMatrix();
120 glLoadIdentity();
122 glBegin(GL_QUADS);
123 glColor3f(0, 0, 0);
124 glVertex2f(-1, -1);
125 glVertex2f(1, -1);
126 glColor3f(1, 1, 1);
127 glVertex2f(1, 1);
128 glVertex2f(-1, 1);
129 glEnd();
131 glPopMatrix();
132 glMatrixMode(GL_MODELVIEW);
133 glPopMatrix();
135 glPopAttrib();
136 }
138 void disp()
139 {
140 float ldir[] = {-1, 1, 2, 0};
142 if(!rtarg) {
143 printf("(re)creating render target (%dx%d)\n", xsz / DITHER_SZ, ysz / DITHER_SZ);
144 if(!(rtarg = create_rtarg(xsz / DITHER_SZ, ysz / DITHER_SZ))) {
145 exit(0);
146 }
147 }
149 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
150 glViewport(0, 0, xsz / DITHER_SZ, ysz / DITHER_SZ);
152 glClearColor(1, 1, 1, 1);
153 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
155 draw_backdrop();
157 glEnable(GL_DEPTH_TEST);
159 glMatrixMode(GL_MODELVIEW);
160 glLoadIdentity();
162 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
164 glTranslatef(0, 0, -cam_dist);
165 glRotatef(cam_phi, 1, 0, 0);
166 glRotatef(cam_theta, 0, 1, 0);
168 glFrontFace(GL_CW);
169 glutSolidTeapot(1.0);
170 glFrontFace(GL_CCW);
174 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
175 glViewport(0, 0, xsz, ysz);
177 glClear(GL_COLOR_BUFFER_BIT);
179 glMatrixMode(GL_PROJECTION);
180 glPushMatrix();
181 glLoadIdentity();
182 glMatrixMode(GL_MODELVIEW);
183 glLoadIdentity();
184 glPushMatrix();
186 glPushAttrib(GL_ENABLE_BIT);
187 glDisable(GL_DEPTH_TEST);
189 bind_program(prog);
190 set_uniform_int(prog, "framebuf", 0);
191 set_uniform_int(prog, "dither_tex", 1);
192 set_uniform_int(prog, "dither_levels", DITHER_LEVELS);
193 set_uniform_int(prog, "dither_size", DITHER_SZ);
195 glActiveTextureARB(GL_TEXTURE0);
196 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
197 glActiveTextureARB(GL_TEXTURE1);
198 glBindTexture(GL_TEXTURE_2D, dither_tex);
200 glBegin(GL_QUADS);
201 glColor3f(0, 1, 0);
202 glTexCoord2f(0, 0); glVertex2f(-1, -1);
203 glTexCoord2f(1, 0); glVertex2f(1, -1);
204 glTexCoord2f(1, 1); glVertex2f(1, 1);
205 glTexCoord2f(0, 1); glVertex2f(-1, 1);
206 glEnd();
208 glActiveTextureARB(GL_TEXTURE1);
209 glDisable(GL_TEXTURE_2D);
210 glActiveTextureARB(GL_TEXTURE0);
211 glDisable(GL_TEXTURE_2D);
213 bind_program(0);
215 glPopAttrib();
217 glMatrixMode(GL_PROJECTION);
218 glPopMatrix();
219 glMatrixMode(GL_MODELVIEW);
220 glPopMatrix();
222 glutSwapBuffers();
223 assert(glGetError() == GL_NO_ERROR);
224 }
226 void idle()
227 {
228 glutPostRedisplay();
229 }
231 void reshape(int x, int y)
232 {
233 glViewport(0, 0, x, y);
235 glMatrixMode(GL_PROJECTION);
236 glLoadIdentity();
237 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
239 if(x != xsz || y != ysz) {
240 destroy_rtarg(rtarg);
241 rtarg = 0;
242 xsz = x;
243 ysz = y;
244 }
245 }
247 void keyb(unsigned char key, int x, int y)
248 {
249 switch(key) {
250 case 27:
251 exit(0);
252 }
253 }
255 bool bnstate[16];
256 int prev_x, prev_y;
258 void mouse(int bn, int state, int x, int y)
259 {
260 int idx = bn - GLUT_LEFT_BUTTON;
262 if(idx < (int)(sizeof bnstate / sizeof *bnstate)) {
263 bnstate[idx] = state == GLUT_DOWN;
264 }
265 prev_x = x;
266 prev_y = y;
267 }
269 void motion(int x, int y)
270 {
271 int dx = x - prev_x;
272 int dy = y - prev_y;
273 prev_x = x;
274 prev_y = y;
276 if(bnstate[0]) {
277 cam_theta = fmod(cam_theta + dx * 0.5, 360.0);
278 cam_phi += dy * 0.5;
279 if(cam_phi < -90) {
280 cam_phi = -90;
281 }
282 if(cam_phi > 90) {
283 cam_phi = 90;
284 }
285 }
286 if(bnstate[2]) {
287 cam_dist += dy * 0.1;
288 if(cam_dist < 0) {
289 cam_dist = 0;
290 }
291 }
292 }
294 struct render_target *create_rtarg(int xsz, int ysz)
295 {
296 struct render_target *rt = new render_target;
298 glGenFramebuffersEXT(1, &rt->fbo);
299 glBindFramebufferEXT(GL_FRAMEBUFFER, rt->fbo);
301 // create the render target texture
302 glGenTextures(1, &rt->color_tex);
303 glBindTexture(GL_TEXTURE_2D, rt->color_tex);
304 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
305 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
306 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
307 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
308 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
310 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color_tex, 0);
312 // create depth buffer
313 glGenRenderbuffersEXT(1, &rt->depth_buf);
314 glBindRenderbufferEXT(GL_RENDERBUFFER, rt->depth_buf);
315 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xsz, ysz);
317 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth_buf);
319 if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
320 fprintf(stderr, "incomplete fbo\n");
321 return 0;
322 }
324 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
325 return rt;
326 }
328 void destroy_rtarg(struct render_target *rt)
329 {
330 if(!rt) {
331 return;
332 }
333 glDeleteFramebuffersEXT(1, &rt->fbo);
334 glDeleteTextures(1, &rt->color_tex);
335 glDeleteRenderbuffersEXT(1, &rt->depth_buf);
336 delete rt;
337 }