dbf-udg

annotate src/udg.cc @ 9:7056437a361b

added demosys
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 19 Feb 2013 18:17:17 +0200
parents f0a47f46ee45
children 1120c069eb17
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@0 144 return true;
nuclear@0 145 }
nuclear@0 146
nuclear@0 147 void draw_backdrop()
nuclear@0 148 {
nuclear@0 149 glPushAttrib(GL_ENABLE_BIT);
nuclear@0 150 glDisable(GL_DEPTH_TEST);
nuclear@0 151 glDisable(GL_LIGHTING);
nuclear@0 152
nuclear@0 153 glMatrixMode(GL_MODELVIEW);
nuclear@0 154 glPushMatrix();
nuclear@0 155 glLoadIdentity();
nuclear@0 156 glMatrixMode(GL_PROJECTION);
nuclear@0 157 glPushMatrix();
nuclear@0 158 glLoadIdentity();
nuclear@0 159
nuclear@0 160 glBegin(GL_QUADS);
nuclear@0 161 glColor3f(0, 0, 0);
nuclear@0 162 glVertex2f(-1, -1);
nuclear@0 163 glVertex2f(1, -1);
nuclear@0 164 glColor3f(1, 1, 1);
nuclear@0 165 glVertex2f(1, 1);
nuclear@0 166 glVertex2f(-1, 1);
nuclear@0 167 glEnd();
nuclear@0 168
nuclear@0 169 glPopMatrix();
nuclear@0 170 glMatrixMode(GL_MODELVIEW);
nuclear@0 171 glPopMatrix();
nuclear@0 172
nuclear@0 173 glPopAttrib();
nuclear@4 174 /*draw_scroller(glutGet(GLUT_ELAPSED_TIME) / 1000.0); */
nuclear@0 175 }
nuclear@0 176
nuclear@0 177 void disp()
nuclear@0 178 {
nuclear@0 179 float ldir[] = {-1, 1, 2, 0};
nuclear@8 180 float ldir2[] = {0.0, 0.35, -0.9, 0};
nuclear@8 181
nuclear@8 182 float lcol[] = {1, 1, 1, 1};
nuclear@8 183 float lcol2[] = {0.35, 0.3, 0.15, 1};
nuclear@8 184
nuclear@9 185 dsys_update(demo, dsys_msec_to_dtime(glutGet(GLUT_ELAPSED_TIME)));
nuclear@9 186
nuclear@9 187 float sec = dsys_dtime_to_sec(dsys_time(demo));
nuclear@9 188 float auto_angle = sec * 10.0;
nuclear@9 189
nuclear@5 190 int xres, yres;
nuclear@5 191 if(opt_highres) {
nuclear@5 192 xres = xsz;
nuclear@5 193 yres = ysz;
nuclear@5 194 } else {
nuclear@5 195 xres = xsz / DITHER_SZ;
nuclear@5 196 yres = ysz / DITHER_SZ;
nuclear@5 197 }
nuclear@4 198
nuclear@0 199 if(!rtarg) {
nuclear@4 200 printf("(re)creating render target (%dx%d)\n", xres, yres);
nuclear@4 201 if(!(rtarg = create_rtarg(xres, yres))) {
nuclear@0 202 exit(0);
nuclear@0 203 }
nuclear@0 204 }
nuclear@0 205
nuclear@5 206 if(!opt_regular_render) {
nuclear@5 207 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
nuclear@5 208 }
nuclear@4 209 glViewport(0, 0, xres, yres);
nuclear@0 210
nuclear@0 211 glClearColor(1, 1, 1, 1);
nuclear@0 212 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 213
nuclear@0 214 draw_backdrop();
nuclear@0 215
nuclear@0 216 glMatrixMode(GL_MODELVIEW);
nuclear@0 217 glLoadIdentity();
nuclear@0 218
nuclear@8 219 glTranslatef(0, 0.8, -cam_dist);
nuclear@0 220 glRotatef(cam_phi, 1, 0, 0);
nuclear@8 221 glRotatef(opt_autorot ? auto_angle : cam_theta, 0, 1, 0);
nuclear@0 222
nuclear@5 223 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
nuclear@8 224 glLightfv(GL_LIGHT0, GL_DIFFUSE, lcol);
nuclear@8 225
nuclear@8 226 glLightfv(GL_LIGHT1, GL_POSITION, ldir2);
nuclear@8 227 glLightfv(GL_LIGHT1, GL_DIFFUSE, lcol2);
nuclear@5 228
nuclear@2 229 const float blue[] = {0.4, 0.45, 1.0, 1};
nuclear@2 230 const float white[] = {1, 1, 1, 1};
nuclear@2 231 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
nuclear@2 232 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
nuclear@2 233 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
nuclear@2 234
nuclear@7 235 bind_program(phong_prog);
nuclear@9 236 mball_render(sec);
nuclear@7 237 bind_program(0);
nuclear@0 238
nuclear@0 239
nuclear@5 240 if(!opt_regular_render) {
nuclear@5 241 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
nuclear@5 242 glViewport(0, 0, xsz, ysz);
nuclear@1 243
nuclear@5 244 glClear(GL_COLOR_BUFFER_BIT);
nuclear@0 245
nuclear@5 246 glMatrixMode(GL_PROJECTION);
nuclear@5 247 glPushMatrix();
nuclear@5 248 glLoadIdentity();
nuclear@5 249 glMatrixMode(GL_MODELVIEW);
nuclear@5 250 glLoadIdentity();
nuclear@5 251 glPushMatrix();
nuclear@0 252
nuclear@5 253 glPushAttrib(GL_ENABLE_BIT);
nuclear@5 254 glDisable(GL_DEPTH_TEST);
nuclear@0 255
nuclear@7 256 bind_program(post_prog);
nuclear@7 257 set_uniform_int(post_prog, "framebuf", 0);
nuclear@7 258 set_uniform_int(post_prog, "dither_tex", 1);
nuclear@7 259 set_uniform_int(post_prog, "dither_levels", DITHER_LEVELS);
nuclear@7 260 set_uniform_int(post_prog, "dither_size", DITHER_SZ);
nuclear@0 261
nuclear@5 262 glActiveTextureARB(GL_TEXTURE0);
nuclear@5 263 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
nuclear@5 264 glEnable(GL_TEXTURE_2D);
nuclear@5 265 glActiveTextureARB(GL_TEXTURE1);
nuclear@5 266 glBindTexture(GL_TEXTURE_2D, dither_tex);
nuclear@5 267 glEnable(GL_TEXTURE_2D);
nuclear@0 268
nuclear@5 269 glBegin(GL_QUADS);
nuclear@5 270 glColor3f(0, 1, 0);
nuclear@5 271 glTexCoord2f(0, 0); glVertex2f(-1, -1);
nuclear@5 272 glTexCoord2f(1, 0); glVertex2f(1, -1);
nuclear@5 273 glTexCoord2f(1, 1); glVertex2f(1, 1);
nuclear@5 274 glTexCoord2f(0, 1); glVertex2f(-1, 1);
nuclear@5 275 glEnd();
nuclear@0 276
nuclear@5 277 glActiveTextureARB(GL_TEXTURE1);
nuclear@5 278 glDisable(GL_TEXTURE_2D);
nuclear@5 279 glActiveTextureARB(GL_TEXTURE0);
nuclear@5 280 glDisable(GL_TEXTURE_2D);
nuclear@0 281
nuclear@5 282 bind_program(0);
nuclear@0 283
nuclear@5 284 glPopAttrib();
nuclear@0 285
nuclear@5 286 glMatrixMode(GL_PROJECTION);
nuclear@5 287 glPopMatrix();
nuclear@5 288 glMatrixMode(GL_MODELVIEW);
nuclear@5 289 glPopMatrix();
nuclear@5 290 }
nuclear@0 291
nuclear@0 292 glutSwapBuffers();
nuclear@0 293 assert(glGetError() == GL_NO_ERROR);
nuclear@0 294 }
nuclear@0 295
nuclear@0 296 void idle()
nuclear@0 297 {
nuclear@0 298 glutPostRedisplay();
nuclear@0 299 }
nuclear@0 300
nuclear@0 301 void reshape(int x, int y)
nuclear@0 302 {
nuclear@0 303 glViewport(0, 0, x, y);
nuclear@0 304
nuclear@0 305 glMatrixMode(GL_PROJECTION);
nuclear@0 306 glLoadIdentity();
nuclear@0 307 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
nuclear@0 308
nuclear@0 309 if(x != xsz || y != ysz) {
nuclear@0 310 destroy_rtarg(rtarg);
nuclear@0 311 rtarg = 0;
nuclear@0 312 xsz = x;
nuclear@0 313 ysz = y;
nuclear@0 314 }
nuclear@0 315 }
nuclear@0 316
nuclear@0 317 void keyb(unsigned char key, int x, int y)
nuclear@0 318 {
nuclear@0 319 switch(key) {
nuclear@0 320 case 27:
nuclear@0 321 exit(0);
nuclear@4 322
nuclear@8 323 case 'a':
nuclear@8 324 opt_autorot = !opt_autorot;
nuclear@8 325 break;
nuclear@8 326
nuclear@4 327 case 'f':
nuclear@4 328 {
nuclear@4 329 static bool fullscreen;
nuclear@4 330 static int orig_x, orig_y;
nuclear@4 331
nuclear@4 332 fullscreen = !fullscreen;
nuclear@4 333 if(fullscreen) {
nuclear@4 334 orig_x = xsz;
nuclear@4 335 orig_y = ysz;
nuclear@4 336 glutFullScreen();
nuclear@4 337 } else {
nuclear@4 338 glutReshapeWindow(orig_x, orig_y);
nuclear@4 339 }
nuclear@4 340 }
nuclear@4 341 break;
nuclear@5 342
nuclear@5 343 case 'r':
nuclear@5 344 opt_regular_render = !opt_regular_render;
nuclear@5 345 break;
nuclear@5 346
nuclear@5 347 case 'h':
nuclear@5 348 opt_highres = !opt_highres;
nuclear@5 349 if(rtarg) {
nuclear@5 350 destroy_rtarg(rtarg);
nuclear@5 351 rtarg = 0;
nuclear@5 352 }
nuclear@5 353 break;
nuclear@0 354 }
nuclear@0 355 }
nuclear@0 356
nuclear@0 357 bool bnstate[16];
nuclear@0 358 int prev_x, prev_y;
nuclear@0 359
nuclear@0 360 void mouse(int bn, int state, int x, int y)
nuclear@0 361 {
nuclear@0 362 int idx = bn - GLUT_LEFT_BUTTON;
nuclear@0 363
nuclear@0 364 if(idx < (int)(sizeof bnstate / sizeof *bnstate)) {
nuclear@0 365 bnstate[idx] = state == GLUT_DOWN;
nuclear@0 366 }
nuclear@0 367 prev_x = x;
nuclear@0 368 prev_y = y;
nuclear@0 369 }
nuclear@0 370
nuclear@0 371 void motion(int x, int y)
nuclear@0 372 {
nuclear@0 373 int dx = x - prev_x;
nuclear@0 374 int dy = y - prev_y;
nuclear@0 375 prev_x = x;
nuclear@0 376 prev_y = y;
nuclear@0 377
nuclear@0 378 if(bnstate[0]) {
nuclear@0 379 cam_theta = fmod(cam_theta + dx * 0.5, 360.0);
nuclear@0 380 cam_phi += dy * 0.5;
nuclear@0 381 if(cam_phi < -90) {
nuclear@0 382 cam_phi = -90;
nuclear@0 383 }
nuclear@0 384 if(cam_phi > 90) {
nuclear@0 385 cam_phi = 90;
nuclear@0 386 }
nuclear@0 387 }
nuclear@0 388 if(bnstate[2]) {
nuclear@0 389 cam_dist += dy * 0.1;
nuclear@0 390 if(cam_dist < 0) {
nuclear@0 391 cam_dist = 0;
nuclear@0 392 }
nuclear@0 393 }
nuclear@0 394 }
nuclear@0 395
nuclear@0 396 struct render_target *create_rtarg(int xsz, int ysz)
nuclear@0 397 {
nuclear@0 398 struct render_target *rt = new render_target;
nuclear@0 399
nuclear@0 400 glGenFramebuffersEXT(1, &rt->fbo);
nuclear@0 401 glBindFramebufferEXT(GL_FRAMEBUFFER, rt->fbo);
nuclear@0 402
nuclear@0 403 // create the render target texture
nuclear@0 404 glGenTextures(1, &rt->color_tex);
nuclear@0 405 glBindTexture(GL_TEXTURE_2D, rt->color_tex);
nuclear@0 406 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
nuclear@0 407 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
nuclear@1 408 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nuclear@1 409 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nuclear@0 410 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
nuclear@0 411
nuclear@0 412 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color_tex, 0);
nuclear@0 413
nuclear@0 414 // create depth buffer
nuclear@0 415 glGenRenderbuffersEXT(1, &rt->depth_buf);
nuclear@0 416 glBindRenderbufferEXT(GL_RENDERBUFFER, rt->depth_buf);
nuclear@0 417 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xsz, ysz);
nuclear@0 418
nuclear@0 419 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth_buf);
nuclear@0 420
nuclear@0 421 if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
nuclear@0 422 fprintf(stderr, "incomplete fbo\n");
nuclear@0 423 return 0;
nuclear@0 424 }
nuclear@0 425
nuclear@0 426 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
nuclear@0 427 return rt;
nuclear@0 428 }
nuclear@0 429
nuclear@0 430 void destroy_rtarg(struct render_target *rt)
nuclear@0 431 {
nuclear@0 432 if(!rt) {
nuclear@0 433 return;
nuclear@0 434 }
nuclear@0 435 glDeleteFramebuffersEXT(1, &rt->fbo);
nuclear@0 436 glDeleteTextures(1, &rt->color_tex);
nuclear@0 437 glDeleteRenderbuffersEXT(1, &rt->depth_buf);
nuclear@0 438 delete rt;
nuclear@0 439 }