dbf-udg

view src/udg.cc @ 2:c45c7a1f7d9d

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 09 Jan 2013 02:37:05 +0200
parents 1d5dc834d403
children 403ec1be3a1a
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 8
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 const float blue[] = {0.4, 0.45, 1.0, 1};
169 const float white[] = {1, 1, 1, 1};
170 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
171 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
172 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
174 glFrontFace(GL_CW);
175 glutSolidTeapot(1.0);
176 glFrontFace(GL_CCW);
180 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
181 glViewport(0, 0, xsz, ysz);
183 glClear(GL_COLOR_BUFFER_BIT);
185 glMatrixMode(GL_PROJECTION);
186 glPushMatrix();
187 glLoadIdentity();
188 glMatrixMode(GL_MODELVIEW);
189 glLoadIdentity();
190 glPushMatrix();
192 glPushAttrib(GL_ENABLE_BIT);
193 glDisable(GL_DEPTH_TEST);
195 bind_program(prog);
196 set_uniform_int(prog, "framebuf", 0);
197 set_uniform_int(prog, "dither_tex", 1);
198 set_uniform_int(prog, "dither_levels", DITHER_LEVELS);
199 set_uniform_int(prog, "dither_size", DITHER_SZ);
201 glActiveTextureARB(GL_TEXTURE0);
202 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
203 glActiveTextureARB(GL_TEXTURE1);
204 glBindTexture(GL_TEXTURE_2D, dither_tex);
206 glBegin(GL_QUADS);
207 glColor3f(0, 1, 0);
208 glTexCoord2f(0, 0); glVertex2f(-1, -1);
209 glTexCoord2f(1, 0); glVertex2f(1, -1);
210 glTexCoord2f(1, 1); glVertex2f(1, 1);
211 glTexCoord2f(0, 1); glVertex2f(-1, 1);
212 glEnd();
214 glActiveTextureARB(GL_TEXTURE1);
215 glDisable(GL_TEXTURE_2D);
216 glActiveTextureARB(GL_TEXTURE0);
217 glDisable(GL_TEXTURE_2D);
219 bind_program(0);
221 glPopAttrib();
223 glMatrixMode(GL_PROJECTION);
224 glPopMatrix();
225 glMatrixMode(GL_MODELVIEW);
226 glPopMatrix();
228 glutSwapBuffers();
229 assert(glGetError() == GL_NO_ERROR);
230 }
232 void idle()
233 {
234 glutPostRedisplay();
235 }
237 void reshape(int x, int y)
238 {
239 glViewport(0, 0, x, y);
241 glMatrixMode(GL_PROJECTION);
242 glLoadIdentity();
243 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
245 if(x != xsz || y != ysz) {
246 destroy_rtarg(rtarg);
247 rtarg = 0;
248 xsz = x;
249 ysz = y;
250 }
251 }
253 void keyb(unsigned char key, int x, int y)
254 {
255 switch(key) {
256 case 27:
257 exit(0);
258 }
259 }
261 bool bnstate[16];
262 int prev_x, prev_y;
264 void mouse(int bn, int state, int x, int y)
265 {
266 int idx = bn - GLUT_LEFT_BUTTON;
268 if(idx < (int)(sizeof bnstate / sizeof *bnstate)) {
269 bnstate[idx] = state == GLUT_DOWN;
270 }
271 prev_x = x;
272 prev_y = y;
273 }
275 void motion(int x, int y)
276 {
277 int dx = x - prev_x;
278 int dy = y - prev_y;
279 prev_x = x;
280 prev_y = y;
282 if(bnstate[0]) {
283 cam_theta = fmod(cam_theta + dx * 0.5, 360.0);
284 cam_phi += dy * 0.5;
285 if(cam_phi < -90) {
286 cam_phi = -90;
287 }
288 if(cam_phi > 90) {
289 cam_phi = 90;
290 }
291 }
292 if(bnstate[2]) {
293 cam_dist += dy * 0.1;
294 if(cam_dist < 0) {
295 cam_dist = 0;
296 }
297 }
298 }
300 struct render_target *create_rtarg(int xsz, int ysz)
301 {
302 struct render_target *rt = new render_target;
304 glGenFramebuffersEXT(1, &rt->fbo);
305 glBindFramebufferEXT(GL_FRAMEBUFFER, rt->fbo);
307 // create the render target texture
308 glGenTextures(1, &rt->color_tex);
309 glBindTexture(GL_TEXTURE_2D, rt->color_tex);
310 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
311 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
312 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
313 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
314 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
316 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color_tex, 0);
318 // create depth buffer
319 glGenRenderbuffersEXT(1, &rt->depth_buf);
320 glBindRenderbufferEXT(GL_RENDERBUFFER, rt->depth_buf);
321 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xsz, ysz);
323 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth_buf);
325 if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
326 fprintf(stderr, "incomplete fbo\n");
327 return 0;
328 }
330 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
331 return rt;
332 }
334 void destroy_rtarg(struct render_target *rt)
335 {
336 if(!rt) {
337 return;
338 }
339 glDeleteFramebuffersEXT(1, &rt->fbo);
340 glDeleteTextures(1, &rt->color_tex);
341 glDeleteRenderbuffersEXT(1, &rt->depth_buf);
342 delete rt;
343 }