dbf-udg

annotate src/udg.cc @ 4:5fb21401b7c8

lala
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 07 Feb 2013 18:52:28 +0200
parents 403ec1be3a1a
children e09cbb2e9d4f
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@0 109 glPushAttrib(GL_ENABLE_BIT);
nuclear@0 110 glDisable(GL_DEPTH_TEST);
nuclear@0 111 glDisable(GL_LIGHTING);
nuclear@0 112
nuclear@0 113 glMatrixMode(GL_MODELVIEW);
nuclear@0 114 glPushMatrix();
nuclear@0 115 glLoadIdentity();
nuclear@0 116 glMatrixMode(GL_PROJECTION);
nuclear@0 117 glPushMatrix();
nuclear@0 118 glLoadIdentity();
nuclear@0 119
nuclear@0 120 glBegin(GL_QUADS);
nuclear@0 121 glColor3f(0, 0, 0);
nuclear@0 122 glVertex2f(-1, -1);
nuclear@0 123 glVertex2f(1, -1);
nuclear@0 124 glColor3f(1, 1, 1);
nuclear@0 125 glVertex2f(1, 1);
nuclear@0 126 glVertex2f(-1, 1);
nuclear@0 127 glEnd();
nuclear@0 128
nuclear@0 129 glPopMatrix();
nuclear@0 130 glMatrixMode(GL_MODELVIEW);
nuclear@0 131 glPopMatrix();
nuclear@0 132
nuclear@0 133 glPopAttrib();
nuclear@4 134 /*draw_scroller(glutGet(GLUT_ELAPSED_TIME) / 1000.0); */
nuclear@0 135 }
nuclear@0 136
nuclear@0 137 void disp()
nuclear@0 138 {
nuclear@0 139 float ldir[] = {-1, 1, 2, 0};
nuclear@0 140
nuclear@4 141 int xres = xsz / DITHER_SZ;
nuclear@4 142 int yres = ysz / DITHER_SZ;
nuclear@4 143 /*int xres = xsz;
nuclear@4 144 int yres = ysz;*/
nuclear@4 145
nuclear@0 146 if(!rtarg) {
nuclear@4 147 printf("(re)creating render target (%dx%d)\n", xres, yres);
nuclear@4 148 if(!(rtarg = create_rtarg(xres, yres))) {
nuclear@0 149 exit(0);
nuclear@0 150 }
nuclear@0 151 }
nuclear@0 152
nuclear@0 153 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
nuclear@4 154 glViewport(0, 0, xres, yres);
nuclear@0 155
nuclear@0 156 glClearColor(1, 1, 1, 1);
nuclear@0 157 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 158
nuclear@0 159 draw_backdrop();
nuclear@0 160
nuclear@0 161 glMatrixMode(GL_MODELVIEW);
nuclear@0 162 glLoadIdentity();
nuclear@0 163
nuclear@0 164 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
nuclear@0 165
nuclear@0 166 glTranslatef(0, 0, -cam_dist);
nuclear@0 167 glRotatef(cam_phi, 1, 0, 0);
nuclear@0 168 glRotatef(cam_theta, 0, 1, 0);
nuclear@0 169
nuclear@2 170 const float blue[] = {0.4, 0.45, 1.0, 1};
nuclear@2 171 const float white[] = {1, 1, 1, 1};
nuclear@2 172 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
nuclear@2 173 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
nuclear@2 174 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
nuclear@2 175
nuclear@0 176 glFrontFace(GL_CW);
nuclear@0 177 glutSolidTeapot(1.0);
nuclear@0 178 glFrontFace(GL_CCW);
nuclear@0 179
nuclear@0 180
nuclear@0 181 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
nuclear@1 182 glViewport(0, 0, xsz, ysz);
nuclear@1 183
nuclear@0 184 glClear(GL_COLOR_BUFFER_BIT);
nuclear@0 185
nuclear@0 186 glMatrixMode(GL_PROJECTION);
nuclear@0 187 glPushMatrix();
nuclear@0 188 glLoadIdentity();
nuclear@0 189 glMatrixMode(GL_MODELVIEW);
nuclear@0 190 glLoadIdentity();
nuclear@0 191 glPushMatrix();
nuclear@0 192
nuclear@0 193 glPushAttrib(GL_ENABLE_BIT);
nuclear@0 194 glDisable(GL_DEPTH_TEST);
nuclear@0 195
nuclear@0 196 bind_program(prog);
nuclear@0 197 set_uniform_int(prog, "framebuf", 0);
nuclear@0 198 set_uniform_int(prog, "dither_tex", 1);
nuclear@0 199 set_uniform_int(prog, "dither_levels", DITHER_LEVELS);
nuclear@0 200 set_uniform_int(prog, "dither_size", DITHER_SZ);
nuclear@0 201
nuclear@0 202 glActiveTextureARB(GL_TEXTURE0);
nuclear@0 203 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
nuclear@3 204 glEnable(GL_TEXTURE_2D);
nuclear@0 205 glActiveTextureARB(GL_TEXTURE1);
nuclear@0 206 glBindTexture(GL_TEXTURE_2D, dither_tex);
nuclear@3 207 glEnable(GL_TEXTURE_2D);
nuclear@0 208
nuclear@0 209 glBegin(GL_QUADS);
nuclear@0 210 glColor3f(0, 1, 0);
nuclear@0 211 glTexCoord2f(0, 0); glVertex2f(-1, -1);
nuclear@0 212 glTexCoord2f(1, 0); glVertex2f(1, -1);
nuclear@0 213 glTexCoord2f(1, 1); glVertex2f(1, 1);
nuclear@0 214 glTexCoord2f(0, 1); glVertex2f(-1, 1);
nuclear@0 215 glEnd();
nuclear@0 216
nuclear@0 217 glActiveTextureARB(GL_TEXTURE1);
nuclear@0 218 glDisable(GL_TEXTURE_2D);
nuclear@0 219 glActiveTextureARB(GL_TEXTURE0);
nuclear@0 220 glDisable(GL_TEXTURE_2D);
nuclear@0 221
nuclear@0 222 bind_program(0);
nuclear@0 223
nuclear@0 224 glPopAttrib();
nuclear@0 225
nuclear@0 226 glMatrixMode(GL_PROJECTION);
nuclear@0 227 glPopMatrix();
nuclear@0 228 glMatrixMode(GL_MODELVIEW);
nuclear@0 229 glPopMatrix();
nuclear@0 230
nuclear@0 231 glutSwapBuffers();
nuclear@0 232 assert(glGetError() == GL_NO_ERROR);
nuclear@0 233 }
nuclear@0 234
nuclear@0 235 void idle()
nuclear@0 236 {
nuclear@0 237 glutPostRedisplay();
nuclear@0 238 }
nuclear@0 239
nuclear@0 240 void reshape(int x, int y)
nuclear@0 241 {
nuclear@0 242 glViewport(0, 0, x, y);
nuclear@0 243
nuclear@0 244 glMatrixMode(GL_PROJECTION);
nuclear@0 245 glLoadIdentity();
nuclear@0 246 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
nuclear@0 247
nuclear@0 248 if(x != xsz || y != ysz) {
nuclear@0 249 destroy_rtarg(rtarg);
nuclear@0 250 rtarg = 0;
nuclear@0 251 xsz = x;
nuclear@0 252 ysz = y;
nuclear@0 253 }
nuclear@0 254 }
nuclear@0 255
nuclear@0 256 void keyb(unsigned char key, int x, int y)
nuclear@0 257 {
nuclear@0 258 switch(key) {
nuclear@0 259 case 27:
nuclear@0 260 exit(0);
nuclear@4 261
nuclear@4 262 case 'f':
nuclear@4 263 {
nuclear@4 264 static bool fullscreen;
nuclear@4 265 static int orig_x, orig_y;
nuclear@4 266
nuclear@4 267 fullscreen = !fullscreen;
nuclear@4 268 if(fullscreen) {
nuclear@4 269 orig_x = xsz;
nuclear@4 270 orig_y = ysz;
nuclear@4 271 glutFullScreen();
nuclear@4 272 } else {
nuclear@4 273 glutReshapeWindow(orig_x, orig_y);
nuclear@4 274 }
nuclear@4 275 }
nuclear@4 276 break;
nuclear@0 277 }
nuclear@0 278 }
nuclear@0 279
nuclear@0 280 bool bnstate[16];
nuclear@0 281 int prev_x, prev_y;
nuclear@0 282
nuclear@0 283 void mouse(int bn, int state, int x, int y)
nuclear@0 284 {
nuclear@0 285 int idx = bn - GLUT_LEFT_BUTTON;
nuclear@0 286
nuclear@0 287 if(idx < (int)(sizeof bnstate / sizeof *bnstate)) {
nuclear@0 288 bnstate[idx] = state == GLUT_DOWN;
nuclear@0 289 }
nuclear@0 290 prev_x = x;
nuclear@0 291 prev_y = y;
nuclear@0 292 }
nuclear@0 293
nuclear@0 294 void motion(int x, int y)
nuclear@0 295 {
nuclear@0 296 int dx = x - prev_x;
nuclear@0 297 int dy = y - prev_y;
nuclear@0 298 prev_x = x;
nuclear@0 299 prev_y = y;
nuclear@0 300
nuclear@0 301 if(bnstate[0]) {
nuclear@0 302 cam_theta = fmod(cam_theta + dx * 0.5, 360.0);
nuclear@0 303 cam_phi += dy * 0.5;
nuclear@0 304 if(cam_phi < -90) {
nuclear@0 305 cam_phi = -90;
nuclear@0 306 }
nuclear@0 307 if(cam_phi > 90) {
nuclear@0 308 cam_phi = 90;
nuclear@0 309 }
nuclear@0 310 }
nuclear@0 311 if(bnstate[2]) {
nuclear@0 312 cam_dist += dy * 0.1;
nuclear@0 313 if(cam_dist < 0) {
nuclear@0 314 cam_dist = 0;
nuclear@0 315 }
nuclear@0 316 }
nuclear@0 317 }
nuclear@0 318
nuclear@0 319 struct render_target *create_rtarg(int xsz, int ysz)
nuclear@0 320 {
nuclear@0 321 struct render_target *rt = new render_target;
nuclear@0 322
nuclear@0 323 glGenFramebuffersEXT(1, &rt->fbo);
nuclear@0 324 glBindFramebufferEXT(GL_FRAMEBUFFER, rt->fbo);
nuclear@0 325
nuclear@0 326 // create the render target texture
nuclear@0 327 glGenTextures(1, &rt->color_tex);
nuclear@0 328 glBindTexture(GL_TEXTURE_2D, rt->color_tex);
nuclear@0 329 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
nuclear@0 330 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
nuclear@1 331 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nuclear@1 332 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nuclear@0 333 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
nuclear@0 334
nuclear@0 335 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color_tex, 0);
nuclear@0 336
nuclear@0 337 // create depth buffer
nuclear@0 338 glGenRenderbuffersEXT(1, &rt->depth_buf);
nuclear@0 339 glBindRenderbufferEXT(GL_RENDERBUFFER, rt->depth_buf);
nuclear@0 340 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xsz, ysz);
nuclear@0 341
nuclear@0 342 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth_buf);
nuclear@0 343
nuclear@0 344 if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
nuclear@0 345 fprintf(stderr, "incomplete fbo\n");
nuclear@0 346 return 0;
nuclear@0 347 }
nuclear@0 348
nuclear@0 349 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
nuclear@0 350 return rt;
nuclear@0 351 }
nuclear@0 352
nuclear@0 353 void destroy_rtarg(struct render_target *rt)
nuclear@0 354 {
nuclear@0 355 if(!rt) {
nuclear@0 356 return;
nuclear@0 357 }
nuclear@0 358 glDeleteFramebuffersEXT(1, &rt->fbo);
nuclear@0 359 glDeleteTextures(1, &rt->color_tex);
nuclear@0 360 glDeleteRenderbuffersEXT(1, &rt->depth_buf);
nuclear@0 361 delete rt;
nuclear@0 362 }