dbf-udg

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