dbf-udg

view src/udg.cc @ 3:403ec1be3a1a

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 11 Jan 2013 22:52:56 +0200
parents c45c7a1f7d9d
children 5fb21401b7c8
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 /*
110 glPushAttrib(GL_ENABLE_BIT);
111 glDisable(GL_DEPTH_TEST);
112 glDisable(GL_LIGHTING);
114 glMatrixMode(GL_MODELVIEW);
115 glPushMatrix();
116 glLoadIdentity();
117 glMatrixMode(GL_PROJECTION);
118 glPushMatrix();
119 glLoadIdentity();
121 glBegin(GL_QUADS);
122 glColor3f(0, 0, 0);
123 glVertex2f(-1, -1);
124 glVertex2f(1, -1);
125 glColor3f(1, 1, 1);
126 glVertex2f(1, 1);
127 glVertex2f(-1, 1);
128 glEnd();
130 glPopMatrix();
131 glMatrixMode(GL_MODELVIEW);
132 glPopMatrix();
134 glPopAttrib();
135 */
136 draw_scroller(glutGet(GLUT_ELAPSED_TIME) / 1000.0);
137 }
139 void disp()
140 {
141 float ldir[] = {-1, 1, 2, 0};
143 if(!rtarg) {
144 printf("(re)creating render target (%dx%d)\n", xsz / DITHER_SZ, ysz / DITHER_SZ);
145 if(!(rtarg = create_rtarg(xsz / DITHER_SZ, ysz / DITHER_SZ))) {
146 exit(0);
147 }
148 }
150 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
151 glViewport(0, 0, xsz / DITHER_SZ, ysz / DITHER_SZ);
153 glClearColor(1, 1, 1, 1);
154 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
156 draw_backdrop();
158 glMatrixMode(GL_MODELVIEW);
159 glLoadIdentity();
161 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
163 glTranslatef(0, 0, -cam_dist);
164 glRotatef(cam_phi, 1, 0, 0);
165 glRotatef(cam_theta, 0, 1, 0);
167 const float blue[] = {0.4, 0.45, 1.0, 1};
168 const float white[] = {1, 1, 1, 1};
169 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
170 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
171 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
173 glFrontFace(GL_CW);
174 glutSolidTeapot(1.0);
175 glFrontFace(GL_CCW);
178 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
179 glViewport(0, 0, xsz, ysz);
181 glClear(GL_COLOR_BUFFER_BIT);
183 glMatrixMode(GL_PROJECTION);
184 glPushMatrix();
185 glLoadIdentity();
186 glMatrixMode(GL_MODELVIEW);
187 glLoadIdentity();
188 glPushMatrix();
190 glPushAttrib(GL_ENABLE_BIT);
191 glDisable(GL_DEPTH_TEST);
193 bind_program(prog);
194 set_uniform_int(prog, "framebuf", 0);
195 set_uniform_int(prog, "dither_tex", 1);
196 set_uniform_int(prog, "dither_levels", DITHER_LEVELS);
197 set_uniform_int(prog, "dither_size", DITHER_SZ);
199 glActiveTextureARB(GL_TEXTURE0);
200 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
201 glEnable(GL_TEXTURE_2D);
202 glActiveTextureARB(GL_TEXTURE1);
203 glBindTexture(GL_TEXTURE_2D, dither_tex);
204 glEnable(GL_TEXTURE_2D);
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 }