dbf-udg

annotate src/udg.cc @ 10:1120c069eb17

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