dbf-udg

annotate src/udg.cc @ 8:f0a47f46ee45

lalala
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 18 Feb 2013 06:53:44 +0200
parents 603656331514
children 7056437a361b
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@5 9 #include "mballs.h"
nuclear@0 10
nuclear@2 11 #define DITHER_SZ 8
nuclear@0 12 #define DITHER_LEVELS 16
nuclear@0 13
nuclear@1 14 #if DITHER_SZ == 4
nuclear@1 15 #define dither_matrix dither_matrix4
nuclear@1 16 #elif DITHER_SZ == 8
nuclear@3 17 #define dither_matrix halftone_matrix8
nuclear@1 18 #else
nuclear@1 19 #error "invalid dither size"
nuclear@1 20 #endif
nuclear@1 21
nuclear@0 22 struct render_target {
nuclear@0 23 unsigned int fbo;
nuclear@0 24 unsigned int color_tex, depth_buf;
nuclear@0 25 };
nuclear@0 26
nuclear@0 27 bool init();
nuclear@0 28 void cleanup();
nuclear@0 29 void disp();
nuclear@0 30 void idle();
nuclear@0 31 void reshape(int x, int y);
nuclear@0 32 void keyb(unsigned char key, int x, int y);
nuclear@0 33 void mouse(int bn, int state, int x, int y);
nuclear@0 34 void motion(int x, int y);
nuclear@0 35 struct render_target *create_rtarg(int xsz, int ysz);
nuclear@0 36 void destroy_rtarg(struct render_target *rt);
nuclear@0 37
nuclear@0 38 int xsz, ysz;
nuclear@8 39 float cam_theta, cam_phi = 25, cam_dist = 11;
nuclear@0 40 unsigned int dither_tex;
nuclear@0 41 struct render_target *rtarg;
nuclear@7 42 unsigned int post_prog, phong_prog;
nuclear@0 43
nuclear@5 44 int opt_highres, opt_regular_render;
nuclear@8 45 bool opt_autorot = true;
nuclear@5 46
nuclear@5 47
nuclear@0 48 int main(int argc, char **argv)
nuclear@0 49 {
nuclear@0 50 glutInit(&argc, argv);
nuclear@1 51 glutInitWindowSize(1024, 768);
nuclear@0 52 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
nuclear@0 53 glutCreateWindow("DBF UDG compo entry by Nuclear");
nuclear@0 54
nuclear@0 55 glutDisplayFunc(disp);
nuclear@0 56 glutIdleFunc(idle);
nuclear@0 57 glutReshapeFunc(reshape);
nuclear@0 58 glutKeyboardFunc(keyb);
nuclear@0 59 glutMouseFunc(mouse);
nuclear@0 60 glutMotionFunc(motion);
nuclear@0 61
nuclear@0 62 glewInit();
nuclear@0 63
nuclear@0 64 if(!init()) {
nuclear@0 65 return 1;
nuclear@0 66 }
nuclear@0 67
nuclear@0 68 glutMainLoop();
nuclear@0 69 return 0;
nuclear@0 70 }
nuclear@0 71
nuclear@0 72 bool init()
nuclear@0 73 {
nuclear@6 74 FILE *fp = fopen("udg.ppm", "wb");
nuclear@6 75 if(fp) {
nuclear@6 76 fprintf(fp, "P6\n%d %d\n255\n", DITHER_SZ, DITHER_SZ * DITHER_LEVELS);
nuclear@6 77 }
nuclear@6 78
nuclear@6 79 unsigned char *img = new unsigned char[DITHER_SZ * DITHER_SZ * DITHER_LEVELS];
nuclear@6 80 unsigned char *ptr = img;
nuclear@0 81
nuclear@0 82 for(int i=0; i<DITHER_LEVELS; i++) {
nuclear@0 83 float val = (float)i / (float)(DITHER_LEVELS - 1);
nuclear@0 84 for(int y=0; y<DITHER_SZ; y++) {
nuclear@0 85 for(int x=0; x<DITHER_SZ; x++) {
nuclear@0 86 /* (1 + M) / (1 + MxN) */
nuclear@1 87 float thres = (1.0 + dither_matrix[x][y]) / (1.0 + DITHER_SZ * DITHER_SZ);
nuclear@6 88 *ptr++ = val >= thres ? 255 : 0;
nuclear@6 89
nuclear@6 90 if(fp) {
nuclear@6 91 int r = ptr[-1] ? 246 : 10;
nuclear@6 92 int g = ptr[-1] ? 198 : 72;
nuclear@6 93 int b = ptr[-1] ? 141 : 85;
nuclear@6 94 fputc(r, fp);
nuclear@6 95 fputc(g, fp);
nuclear@6 96 fputc(b, fp);
nuclear@6 97 }
nuclear@0 98 }
nuclear@0 99 }
nuclear@0 100 }
nuclear@0 101
nuclear@6 102 if(fp) {
nuclear@6 103 fclose(fp);
nuclear@6 104 }
nuclear@6 105
nuclear@7 106 if(!(phong_prog = create_program_load("sdr/phong.v.glsl", "sdr/phong.p.glsl"))) {
nuclear@7 107 return false;
nuclear@7 108 }
nuclear@7 109
nuclear@7 110 if(!(post_prog = create_program_load("sdr/dither.v.glsl", "sdr/dither.p.glsl"))) {
nuclear@0 111 return false;
nuclear@0 112 }
nuclear@0 113
nuclear@0 114 glGenTextures(1, &dither_tex);
nuclear@0 115 glBindTexture(GL_TEXTURE_2D, dither_tex);
nuclear@0 116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nuclear@0 117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nuclear@0 118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
nuclear@0 119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
nuclear@6 120 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, DITHER_SZ, DITHER_SZ * DITHER_LEVELS, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, img);
nuclear@0 121
nuclear@3 122 if(!init_scroller()) {
nuclear@3 123 return false;
nuclear@3 124 }
nuclear@3 125
nuclear@5 126 if(!mball_init()) {
nuclear@5 127 return false;
nuclear@5 128 }
nuclear@5 129
nuclear@0 130 glEnable(GL_CULL_FACE);
nuclear@0 131 glEnable(GL_DEPTH_TEST);
nuclear@0 132 glEnable(GL_LIGHTING);
nuclear@0 133 glEnable(GL_LIGHT0);
nuclear@8 134 glEnable(GL_LIGHT1);
nuclear@5 135 glEnable(GL_NORMALIZE);
nuclear@0 136
nuclear@0 137 return true;
nuclear@0 138 }
nuclear@0 139
nuclear@0 140 void draw_backdrop()
nuclear@0 141 {
nuclear@0 142 glPushAttrib(GL_ENABLE_BIT);
nuclear@0 143 glDisable(GL_DEPTH_TEST);
nuclear@0 144 glDisable(GL_LIGHTING);
nuclear@0 145
nuclear@0 146 glMatrixMode(GL_MODELVIEW);
nuclear@0 147 glPushMatrix();
nuclear@0 148 glLoadIdentity();
nuclear@0 149 glMatrixMode(GL_PROJECTION);
nuclear@0 150 glPushMatrix();
nuclear@0 151 glLoadIdentity();
nuclear@0 152
nuclear@0 153 glBegin(GL_QUADS);
nuclear@0 154 glColor3f(0, 0, 0);
nuclear@0 155 glVertex2f(-1, -1);
nuclear@0 156 glVertex2f(1, -1);
nuclear@0 157 glColor3f(1, 1, 1);
nuclear@0 158 glVertex2f(1, 1);
nuclear@0 159 glVertex2f(-1, 1);
nuclear@0 160 glEnd();
nuclear@0 161
nuclear@0 162 glPopMatrix();
nuclear@0 163 glMatrixMode(GL_MODELVIEW);
nuclear@0 164 glPopMatrix();
nuclear@0 165
nuclear@0 166 glPopAttrib();
nuclear@4 167 /*draw_scroller(glutGet(GLUT_ELAPSED_TIME) / 1000.0); */
nuclear@0 168 }
nuclear@0 169
nuclear@0 170 void disp()
nuclear@0 171 {
nuclear@0 172 float ldir[] = {-1, 1, 2, 0};
nuclear@8 173 float ldir2[] = {0.0, 0.35, -0.9, 0};
nuclear@8 174
nuclear@8 175 float lcol[] = {1, 1, 1, 1};
nuclear@8 176 float lcol2[] = {0.35, 0.3, 0.15, 1};
nuclear@8 177
nuclear@5 178 int xres, yres;
nuclear@8 179 float auto_angle = glutGet(GLUT_ELAPSED_TIME) / 100.0;
nuclear@0 180
nuclear@5 181 if(opt_highres) {
nuclear@5 182 xres = xsz;
nuclear@5 183 yres = ysz;
nuclear@5 184 } else {
nuclear@5 185 xres = xsz / DITHER_SZ;
nuclear@5 186 yres = ysz / DITHER_SZ;
nuclear@5 187 }
nuclear@4 188
nuclear@0 189 if(!rtarg) {
nuclear@4 190 printf("(re)creating render target (%dx%d)\n", xres, yres);
nuclear@4 191 if(!(rtarg = create_rtarg(xres, yres))) {
nuclear@0 192 exit(0);
nuclear@0 193 }
nuclear@0 194 }
nuclear@0 195
nuclear@5 196 if(!opt_regular_render) {
nuclear@5 197 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
nuclear@5 198 }
nuclear@4 199 glViewport(0, 0, xres, yres);
nuclear@0 200
nuclear@0 201 glClearColor(1, 1, 1, 1);
nuclear@0 202 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 203
nuclear@0 204 draw_backdrop();
nuclear@0 205
nuclear@0 206 glMatrixMode(GL_MODELVIEW);
nuclear@0 207 glLoadIdentity();
nuclear@0 208
nuclear@8 209 glTranslatef(0, 0.8, -cam_dist);
nuclear@0 210 glRotatef(cam_phi, 1, 0, 0);
nuclear@8 211 glRotatef(opt_autorot ? auto_angle : cam_theta, 0, 1, 0);
nuclear@0 212
nuclear@5 213 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
nuclear@8 214 glLightfv(GL_LIGHT0, GL_DIFFUSE, lcol);
nuclear@8 215
nuclear@8 216 glLightfv(GL_LIGHT1, GL_POSITION, ldir2);
nuclear@8 217 glLightfv(GL_LIGHT1, GL_DIFFUSE, lcol2);
nuclear@5 218
nuclear@2 219 const float blue[] = {0.4, 0.45, 1.0, 1};
nuclear@2 220 const float white[] = {1, 1, 1, 1};
nuclear@2 221 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
nuclear@2 222 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
nuclear@2 223 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
nuclear@2 224
nuclear@7 225 bind_program(phong_prog);
nuclear@5 226 mball_render();
nuclear@7 227 bind_program(0);
nuclear@0 228
nuclear@0 229
nuclear@5 230 if(!opt_regular_render) {
nuclear@5 231 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
nuclear@5 232 glViewport(0, 0, xsz, ysz);
nuclear@1 233
nuclear@5 234 glClear(GL_COLOR_BUFFER_BIT);
nuclear@0 235
nuclear@5 236 glMatrixMode(GL_PROJECTION);
nuclear@5 237 glPushMatrix();
nuclear@5 238 glLoadIdentity();
nuclear@5 239 glMatrixMode(GL_MODELVIEW);
nuclear@5 240 glLoadIdentity();
nuclear@5 241 glPushMatrix();
nuclear@0 242
nuclear@5 243 glPushAttrib(GL_ENABLE_BIT);
nuclear@5 244 glDisable(GL_DEPTH_TEST);
nuclear@0 245
nuclear@7 246 bind_program(post_prog);
nuclear@7 247 set_uniform_int(post_prog, "framebuf", 0);
nuclear@7 248 set_uniform_int(post_prog, "dither_tex", 1);
nuclear@7 249 set_uniform_int(post_prog, "dither_levels", DITHER_LEVELS);
nuclear@7 250 set_uniform_int(post_prog, "dither_size", DITHER_SZ);
nuclear@0 251
nuclear@5 252 glActiveTextureARB(GL_TEXTURE0);
nuclear@5 253 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
nuclear@5 254 glEnable(GL_TEXTURE_2D);
nuclear@5 255 glActiveTextureARB(GL_TEXTURE1);
nuclear@5 256 glBindTexture(GL_TEXTURE_2D, dither_tex);
nuclear@5 257 glEnable(GL_TEXTURE_2D);
nuclear@0 258
nuclear@5 259 glBegin(GL_QUADS);
nuclear@5 260 glColor3f(0, 1, 0);
nuclear@5 261 glTexCoord2f(0, 0); glVertex2f(-1, -1);
nuclear@5 262 glTexCoord2f(1, 0); glVertex2f(1, -1);
nuclear@5 263 glTexCoord2f(1, 1); glVertex2f(1, 1);
nuclear@5 264 glTexCoord2f(0, 1); glVertex2f(-1, 1);
nuclear@5 265 glEnd();
nuclear@0 266
nuclear@5 267 glActiveTextureARB(GL_TEXTURE1);
nuclear@5 268 glDisable(GL_TEXTURE_2D);
nuclear@5 269 glActiveTextureARB(GL_TEXTURE0);
nuclear@5 270 glDisable(GL_TEXTURE_2D);
nuclear@0 271
nuclear@5 272 bind_program(0);
nuclear@0 273
nuclear@5 274 glPopAttrib();
nuclear@0 275
nuclear@5 276 glMatrixMode(GL_PROJECTION);
nuclear@5 277 glPopMatrix();
nuclear@5 278 glMatrixMode(GL_MODELVIEW);
nuclear@5 279 glPopMatrix();
nuclear@5 280 }
nuclear@0 281
nuclear@0 282 glutSwapBuffers();
nuclear@0 283 assert(glGetError() == GL_NO_ERROR);
nuclear@0 284 }
nuclear@0 285
nuclear@0 286 void idle()
nuclear@0 287 {
nuclear@0 288 glutPostRedisplay();
nuclear@0 289 }
nuclear@0 290
nuclear@0 291 void reshape(int x, int y)
nuclear@0 292 {
nuclear@0 293 glViewport(0, 0, x, y);
nuclear@0 294
nuclear@0 295 glMatrixMode(GL_PROJECTION);
nuclear@0 296 glLoadIdentity();
nuclear@0 297 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
nuclear@0 298
nuclear@0 299 if(x != xsz || y != ysz) {
nuclear@0 300 destroy_rtarg(rtarg);
nuclear@0 301 rtarg = 0;
nuclear@0 302 xsz = x;
nuclear@0 303 ysz = y;
nuclear@0 304 }
nuclear@0 305 }
nuclear@0 306
nuclear@0 307 void keyb(unsigned char key, int x, int y)
nuclear@0 308 {
nuclear@0 309 switch(key) {
nuclear@0 310 case 27:
nuclear@0 311 exit(0);
nuclear@4 312
nuclear@8 313 case 'a':
nuclear@8 314 opt_autorot = !opt_autorot;
nuclear@8 315 break;
nuclear@8 316
nuclear@4 317 case 'f':
nuclear@4 318 {
nuclear@4 319 static bool fullscreen;
nuclear@4 320 static int orig_x, orig_y;
nuclear@4 321
nuclear@4 322 fullscreen = !fullscreen;
nuclear@4 323 if(fullscreen) {
nuclear@4 324 orig_x = xsz;
nuclear@4 325 orig_y = ysz;
nuclear@4 326 glutFullScreen();
nuclear@4 327 } else {
nuclear@4 328 glutReshapeWindow(orig_x, orig_y);
nuclear@4 329 }
nuclear@4 330 }
nuclear@4 331 break;
nuclear@5 332
nuclear@5 333 case 'r':
nuclear@5 334 opt_regular_render = !opt_regular_render;
nuclear@5 335 break;
nuclear@5 336
nuclear@5 337 case 'h':
nuclear@5 338 opt_highres = !opt_highres;
nuclear@5 339 if(rtarg) {
nuclear@5 340 destroy_rtarg(rtarg);
nuclear@5 341 rtarg = 0;
nuclear@5 342 }
nuclear@5 343 break;
nuclear@0 344 }
nuclear@0 345 }
nuclear@0 346
nuclear@0 347 bool bnstate[16];
nuclear@0 348 int prev_x, prev_y;
nuclear@0 349
nuclear@0 350 void mouse(int bn, int state, int x, int y)
nuclear@0 351 {
nuclear@0 352 int idx = bn - GLUT_LEFT_BUTTON;
nuclear@0 353
nuclear@0 354 if(idx < (int)(sizeof bnstate / sizeof *bnstate)) {
nuclear@0 355 bnstate[idx] = state == GLUT_DOWN;
nuclear@0 356 }
nuclear@0 357 prev_x = x;
nuclear@0 358 prev_y = y;
nuclear@0 359 }
nuclear@0 360
nuclear@0 361 void motion(int x, int y)
nuclear@0 362 {
nuclear@0 363 int dx = x - prev_x;
nuclear@0 364 int dy = y - prev_y;
nuclear@0 365 prev_x = x;
nuclear@0 366 prev_y = y;
nuclear@0 367
nuclear@0 368 if(bnstate[0]) {
nuclear@0 369 cam_theta = fmod(cam_theta + dx * 0.5, 360.0);
nuclear@0 370 cam_phi += dy * 0.5;
nuclear@0 371 if(cam_phi < -90) {
nuclear@0 372 cam_phi = -90;
nuclear@0 373 }
nuclear@0 374 if(cam_phi > 90) {
nuclear@0 375 cam_phi = 90;
nuclear@0 376 }
nuclear@0 377 }
nuclear@0 378 if(bnstate[2]) {
nuclear@0 379 cam_dist += dy * 0.1;
nuclear@0 380 if(cam_dist < 0) {
nuclear@0 381 cam_dist = 0;
nuclear@0 382 }
nuclear@0 383 }
nuclear@0 384 }
nuclear@0 385
nuclear@0 386 struct render_target *create_rtarg(int xsz, int ysz)
nuclear@0 387 {
nuclear@0 388 struct render_target *rt = new render_target;
nuclear@0 389
nuclear@0 390 glGenFramebuffersEXT(1, &rt->fbo);
nuclear@0 391 glBindFramebufferEXT(GL_FRAMEBUFFER, rt->fbo);
nuclear@0 392
nuclear@0 393 // create the render target texture
nuclear@0 394 glGenTextures(1, &rt->color_tex);
nuclear@0 395 glBindTexture(GL_TEXTURE_2D, rt->color_tex);
nuclear@0 396 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
nuclear@0 397 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
nuclear@1 398 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nuclear@1 399 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nuclear@0 400 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
nuclear@0 401
nuclear@0 402 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color_tex, 0);
nuclear@0 403
nuclear@0 404 // create depth buffer
nuclear@0 405 glGenRenderbuffersEXT(1, &rt->depth_buf);
nuclear@0 406 glBindRenderbufferEXT(GL_RENDERBUFFER, rt->depth_buf);
nuclear@0 407 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xsz, ysz);
nuclear@0 408
nuclear@0 409 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth_buf);
nuclear@0 410
nuclear@0 411 if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
nuclear@0 412 fprintf(stderr, "incomplete fbo\n");
nuclear@0 413 return 0;
nuclear@0 414 }
nuclear@0 415
nuclear@0 416 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
nuclear@0 417 return rt;
nuclear@0 418 }
nuclear@0 419
nuclear@0 420 void destroy_rtarg(struct render_target *rt)
nuclear@0 421 {
nuclear@0 422 if(!rt) {
nuclear@0 423 return;
nuclear@0 424 }
nuclear@0 425 glDeleteFramebuffersEXT(1, &rt->fbo);
nuclear@0 426 glDeleteTextures(1, &rt->color_tex);
nuclear@0 427 glDeleteRenderbuffersEXT(1, &rt->depth_buf);
nuclear@0 428 delete rt;
nuclear@0 429 }