dbf-udg

view src/udg.cc @ 0:2d27bfd21fc5

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 08 Jan 2013 03:16:48 +0200
parents
children 1d5dc834d403
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 struct render_target {
20 unsigned int fbo;
21 unsigned int color_tex, depth_buf;
22 };
24 bool init();
25 void cleanup();
26 void disp();
27 void idle();
28 void reshape(int x, int y);
29 void keyb(unsigned char key, int x, int y);
30 void mouse(int bn, int state, int x, int y);
31 void motion(int x, int y);
32 struct render_target *create_rtarg(int xsz, int ysz);
33 void destroy_rtarg(struct render_target *rt);
35 int xsz, ysz;
36 float cam_theta, cam_phi = 25, cam_dist = 8;
37 unsigned int dither_tex;
38 struct render_target *rtarg;
39 unsigned int prog;
41 int main(int argc, char **argv)
42 {
43 glutInit(&argc, argv);
44 glutInitWindowSize(800, 600);
45 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
46 glutCreateWindow("DBF UDG compo entry by Nuclear");
48 glutDisplayFunc(disp);
49 glutIdleFunc(idle);
50 glutReshapeFunc(reshape);
51 glutKeyboardFunc(keyb);
52 glutMouseFunc(mouse);
53 glutMotionFunc(motion);
55 glewInit();
57 if(!init()) {
58 return 1;
59 }
61 glutMainLoop();
62 return 0;
63 }
65 bool init()
66 {
67 float *img = new float[DITHER_SZ * DITHER_SZ * DITHER_LEVELS];
68 float *ptr = img;
70 for(int i=0; i<DITHER_LEVELS; i++) {
71 float val = (float)i / (float)(DITHER_LEVELS - 1);
72 for(int y=0; y<DITHER_SZ; y++) {
73 for(int x=0; x<DITHER_SZ; x++) {
74 /* (1 + M) / (1 + MxN) */
75 float thres = (1.0 + dither_matrix4[x][y]) / (1.0 + DITHER_SZ * DITHER_SZ);
76 *ptr++ = val >= thres ? 1.0 : 0.0;
77 }
78 }
79 }
81 if(!(prog = create_program_load("sdr/dither.v.glsl", "sdr/dither.p.glsl"))) {
82 return false;
83 }
85 glGenTextures(1, &dither_tex);
86 glBindTexture(GL_TEXTURE_2D, dither_tex);
87 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
91 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, DITHER_SZ, DITHER_SZ * DITHER_LEVELS, 0, GL_LUMINANCE, GL_FLOAT, img);
93 glEnable(GL_CULL_FACE);
94 glEnable(GL_DEPTH_TEST);
95 glEnable(GL_LIGHTING);
96 glEnable(GL_LIGHT0);
98 return true;
99 }
101 void draw_backdrop()
102 {
103 glPushAttrib(GL_ENABLE_BIT);
104 glDisable(GL_DEPTH_TEST);
105 glDisable(GL_LIGHTING);
107 glMatrixMode(GL_MODELVIEW);
108 glPushMatrix();
109 glLoadIdentity();
110 glMatrixMode(GL_PROJECTION);
111 glPushMatrix();
112 glLoadIdentity();
114 glBegin(GL_QUADS);
115 glColor3f(0, 0, 0);
116 glVertex2f(-1, -1);
117 glVertex2f(1, -1);
118 glColor3f(1, 1, 1);
119 glVertex2f(1, 1);
120 glVertex2f(-1, 1);
121 glEnd();
123 glPopMatrix();
124 glMatrixMode(GL_MODELVIEW);
125 glPopMatrix();
127 glPopAttrib();
128 }
130 void disp()
131 {
132 float ldir[] = {-1, 1, 2, 0};
134 if(!rtarg) {
135 printf("(re)creating render target (%dx%d)\n", xsz, ysz);
136 if(!(rtarg = create_rtarg(xsz, ysz))) {
137 exit(0);
138 }
139 }
141 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
143 glClearColor(1, 1, 1, 1);
144 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
146 draw_backdrop();
148 glEnable(GL_DEPTH_TEST);
150 glMatrixMode(GL_MODELVIEW);
151 glLoadIdentity();
153 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
155 glTranslatef(0, 0, -cam_dist);
156 glRotatef(cam_phi, 1, 0, 0);
157 glRotatef(cam_theta, 0, 1, 0);
159 glFrontFace(GL_CW);
160 glutSolidTeapot(1.0);
161 glFrontFace(GL_CCW);
165 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
166 glClear(GL_COLOR_BUFFER_BIT);
168 glMatrixMode(GL_PROJECTION);
169 glPushMatrix();
170 glLoadIdentity();
171 glMatrixMode(GL_MODELVIEW);
172 glLoadIdentity();
173 glPushMatrix();
175 glPushAttrib(GL_ENABLE_BIT);
176 glDisable(GL_DEPTH_TEST);
178 bind_program(prog);
179 set_uniform_int(prog, "framebuf", 0);
180 set_uniform_int(prog, "dither_tex", 1);
181 set_uniform_int(prog, "dither_levels", DITHER_LEVELS);
182 set_uniform_int(prog, "dither_size", DITHER_SZ);
184 glActiveTextureARB(GL_TEXTURE0);
185 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
186 glActiveTextureARB(GL_TEXTURE1);
187 glBindTexture(GL_TEXTURE_2D, dither_tex);
189 glBegin(GL_QUADS);
190 glColor3f(0, 1, 0);
191 glTexCoord2f(0, 0); glVertex2f(-1, -1);
192 glTexCoord2f(1, 0); glVertex2f(1, -1);
193 glTexCoord2f(1, 1); glVertex2f(1, 1);
194 glTexCoord2f(0, 1); glVertex2f(-1, 1);
195 glEnd();
197 glActiveTextureARB(GL_TEXTURE1);
198 glDisable(GL_TEXTURE_2D);
199 glActiveTextureARB(GL_TEXTURE0);
200 glDisable(GL_TEXTURE_2D);
202 bind_program(0);
204 glPopAttrib();
206 glMatrixMode(GL_PROJECTION);
207 glPopMatrix();
208 glMatrixMode(GL_MODELVIEW);
209 glPopMatrix();
211 glutSwapBuffers();
212 assert(glGetError() == GL_NO_ERROR);
213 }
215 void idle()
216 {
217 glutPostRedisplay();
218 }
220 void reshape(int x, int y)
221 {
222 glViewport(0, 0, x, y);
224 glMatrixMode(GL_PROJECTION);
225 glLoadIdentity();
226 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
228 if(x != xsz || y != ysz) {
229 destroy_rtarg(rtarg);
230 rtarg = 0;
231 xsz = x;
232 ysz = y;
233 }
234 }
236 void keyb(unsigned char key, int x, int y)
237 {
238 switch(key) {
239 case 27:
240 exit(0);
241 }
242 }
244 bool bnstate[16];
245 int prev_x, prev_y;
247 void mouse(int bn, int state, int x, int y)
248 {
249 int idx = bn - GLUT_LEFT_BUTTON;
251 if(idx < (int)(sizeof bnstate / sizeof *bnstate)) {
252 bnstate[idx] = state == GLUT_DOWN;
253 }
254 prev_x = x;
255 prev_y = y;
256 }
258 void motion(int x, int y)
259 {
260 int dx = x - prev_x;
261 int dy = y - prev_y;
262 prev_x = x;
263 prev_y = y;
265 if(bnstate[0]) {
266 cam_theta = fmod(cam_theta + dx * 0.5, 360.0);
267 cam_phi += dy * 0.5;
268 if(cam_phi < -90) {
269 cam_phi = -90;
270 }
271 if(cam_phi > 90) {
272 cam_phi = 90;
273 }
274 }
275 if(bnstate[2]) {
276 cam_dist += dy * 0.1;
277 if(cam_dist < 0) {
278 cam_dist = 0;
279 }
280 }
281 }
283 struct render_target *create_rtarg(int xsz, int ysz)
284 {
285 struct render_target *rt = new render_target;
287 glGenFramebuffersEXT(1, &rt->fbo);
288 glBindFramebufferEXT(GL_FRAMEBUFFER, rt->fbo);
290 // create the render target texture
291 glGenTextures(1, &rt->color_tex);
292 glBindTexture(GL_TEXTURE_2D, rt->color_tex);
293 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
294 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
295 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
296 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
297 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
299 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color_tex, 0);
301 // create depth buffer
302 glGenRenderbuffersEXT(1, &rt->depth_buf);
303 glBindRenderbufferEXT(GL_RENDERBUFFER, rt->depth_buf);
304 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xsz, ysz);
306 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth_buf);
308 if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
309 fprintf(stderr, "incomplete fbo\n");
310 return 0;
311 }
313 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
314 return rt;
315 }
317 void destroy_rtarg(struct render_target *rt)
318 {
319 if(!rt) {
320 return;
321 }
322 glDeleteFramebuffersEXT(1, &rt->fbo);
323 glDeleteTextures(1, &rt->color_tex);
324 glDeleteRenderbuffersEXT(1, &rt->depth_buf);
325 delete rt;
326 }