dbf-udg

annotate src/udg.cc @ 2:c45c7a1f7d9d

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 09 Jan 2013 02:37:05 +0200
parents 1d5dc834d403
children 403ec1be3a1a
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@0 5 #include <GL/glew.h>
nuclear@0 6
nuclear@0 7 #ifndef __APPLE__
nuclear@0 8 #include <GL/glut.h>
nuclear@0 9 #else
nuclear@0 10 #include <GLUT/glut.h>
nuclear@0 11 #endif
nuclear@0 12
nuclear@0 13 #include "sdr.h"
nuclear@0 14 #include "dither_matrix.h"
nuclear@0 15
nuclear@2 16 #define DITHER_SZ 8
nuclear@0 17 #define DITHER_LEVELS 16
nuclear@0 18
nuclear@1 19 #if DITHER_SZ == 4
nuclear@1 20 #define dither_matrix dither_matrix4
nuclear@1 21 #elif DITHER_SZ == 8
nuclear@1 22 #define dither_matrix dither_matrix8
nuclear@1 23 #else
nuclear@1 24 #error "invalid dither size"
nuclear@1 25 #endif
nuclear@1 26
nuclear@0 27 struct render_target {
nuclear@0 28 unsigned int fbo;
nuclear@0 29 unsigned int color_tex, depth_buf;
nuclear@0 30 };
nuclear@0 31
nuclear@0 32 bool init();
nuclear@0 33 void cleanup();
nuclear@0 34 void disp();
nuclear@0 35 void idle();
nuclear@0 36 void reshape(int x, int y);
nuclear@0 37 void keyb(unsigned char key, int x, int y);
nuclear@0 38 void mouse(int bn, int state, int x, int y);
nuclear@0 39 void motion(int x, int y);
nuclear@0 40 struct render_target *create_rtarg(int xsz, int ysz);
nuclear@0 41 void destroy_rtarg(struct render_target *rt);
nuclear@0 42
nuclear@0 43 int xsz, ysz;
nuclear@0 44 float cam_theta, cam_phi = 25, cam_dist = 8;
nuclear@0 45 unsigned int dither_tex;
nuclear@0 46 struct render_target *rtarg;
nuclear@0 47 unsigned int prog;
nuclear@0 48
nuclear@0 49 int main(int argc, char **argv)
nuclear@0 50 {
nuclear@0 51 glutInit(&argc, argv);
nuclear@1 52 glutInitWindowSize(1024, 768);
nuclear@0 53 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
nuclear@0 54 glutCreateWindow("DBF UDG compo entry by Nuclear");
nuclear@0 55
nuclear@0 56 glutDisplayFunc(disp);
nuclear@0 57 glutIdleFunc(idle);
nuclear@0 58 glutReshapeFunc(reshape);
nuclear@0 59 glutKeyboardFunc(keyb);
nuclear@0 60 glutMouseFunc(mouse);
nuclear@0 61 glutMotionFunc(motion);
nuclear@0 62
nuclear@0 63 glewInit();
nuclear@0 64
nuclear@0 65 if(!init()) {
nuclear@0 66 return 1;
nuclear@0 67 }
nuclear@0 68
nuclear@0 69 glutMainLoop();
nuclear@0 70 return 0;
nuclear@0 71 }
nuclear@0 72
nuclear@0 73 bool init()
nuclear@0 74 {
nuclear@0 75 float *img = new float[DITHER_SZ * DITHER_SZ * DITHER_LEVELS];
nuclear@0 76 float *ptr = img;
nuclear@0 77
nuclear@0 78 for(int i=0; i<DITHER_LEVELS; i++) {
nuclear@0 79 float val = (float)i / (float)(DITHER_LEVELS - 1);
nuclear@0 80 for(int y=0; y<DITHER_SZ; y++) {
nuclear@0 81 for(int x=0; x<DITHER_SZ; x++) {
nuclear@0 82 /* (1 + M) / (1 + MxN) */
nuclear@1 83 float thres = (1.0 + dither_matrix[x][y]) / (1.0 + DITHER_SZ * DITHER_SZ);
nuclear@0 84 *ptr++ = val >= thres ? 1.0 : 0.0;
nuclear@0 85 }
nuclear@0 86 }
nuclear@0 87 }
nuclear@0 88
nuclear@0 89 if(!(prog = create_program_load("sdr/dither.v.glsl", "sdr/dither.p.glsl"))) {
nuclear@0 90 return false;
nuclear@0 91 }
nuclear@0 92
nuclear@0 93 glGenTextures(1, &dither_tex);
nuclear@0 94 glBindTexture(GL_TEXTURE_2D, dither_tex);
nuclear@0 95 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nuclear@0 96 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nuclear@0 97 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
nuclear@0 98 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
nuclear@0 99 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, DITHER_SZ, DITHER_SZ * DITHER_LEVELS, 0, GL_LUMINANCE, GL_FLOAT, img);
nuclear@0 100
nuclear@0 101 glEnable(GL_CULL_FACE);
nuclear@0 102 glEnable(GL_DEPTH_TEST);
nuclear@0 103 glEnable(GL_LIGHTING);
nuclear@0 104 glEnable(GL_LIGHT0);
nuclear@0 105
nuclear@0 106 return true;
nuclear@0 107 }
nuclear@0 108
nuclear@0 109 void draw_backdrop()
nuclear@0 110 {
nuclear@0 111 glPushAttrib(GL_ENABLE_BIT);
nuclear@0 112 glDisable(GL_DEPTH_TEST);
nuclear@0 113 glDisable(GL_LIGHTING);
nuclear@0 114
nuclear@0 115 glMatrixMode(GL_MODELVIEW);
nuclear@0 116 glPushMatrix();
nuclear@0 117 glLoadIdentity();
nuclear@0 118 glMatrixMode(GL_PROJECTION);
nuclear@0 119 glPushMatrix();
nuclear@0 120 glLoadIdentity();
nuclear@0 121
nuclear@0 122 glBegin(GL_QUADS);
nuclear@0 123 glColor3f(0, 0, 0);
nuclear@0 124 glVertex2f(-1, -1);
nuclear@0 125 glVertex2f(1, -1);
nuclear@0 126 glColor3f(1, 1, 1);
nuclear@0 127 glVertex2f(1, 1);
nuclear@0 128 glVertex2f(-1, 1);
nuclear@0 129 glEnd();
nuclear@0 130
nuclear@0 131 glPopMatrix();
nuclear@0 132 glMatrixMode(GL_MODELVIEW);
nuclear@0 133 glPopMatrix();
nuclear@0 134
nuclear@0 135 glPopAttrib();
nuclear@0 136 }
nuclear@0 137
nuclear@0 138 void disp()
nuclear@0 139 {
nuclear@0 140 float ldir[] = {-1, 1, 2, 0};
nuclear@0 141
nuclear@0 142 if(!rtarg) {
nuclear@1 143 printf("(re)creating render target (%dx%d)\n", xsz / DITHER_SZ, ysz / DITHER_SZ);
nuclear@1 144 if(!(rtarg = create_rtarg(xsz / DITHER_SZ, ysz / DITHER_SZ))) {
nuclear@0 145 exit(0);
nuclear@0 146 }
nuclear@0 147 }
nuclear@0 148
nuclear@0 149 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
nuclear@1 150 glViewport(0, 0, xsz / DITHER_SZ, ysz / DITHER_SZ);
nuclear@0 151
nuclear@0 152 glClearColor(1, 1, 1, 1);
nuclear@0 153 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 154
nuclear@0 155 draw_backdrop();
nuclear@0 156
nuclear@0 157 glEnable(GL_DEPTH_TEST);
nuclear@0 158
nuclear@0 159 glMatrixMode(GL_MODELVIEW);
nuclear@0 160 glLoadIdentity();
nuclear@0 161
nuclear@0 162 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
nuclear@0 163
nuclear@0 164 glTranslatef(0, 0, -cam_dist);
nuclear@0 165 glRotatef(cam_phi, 1, 0, 0);
nuclear@0 166 glRotatef(cam_theta, 0, 1, 0);
nuclear@0 167
nuclear@2 168 const float blue[] = {0.4, 0.45, 1.0, 1};
nuclear@2 169 const float white[] = {1, 1, 1, 1};
nuclear@2 170 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
nuclear@2 171 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
nuclear@2 172 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
nuclear@2 173
nuclear@0 174 glFrontFace(GL_CW);
nuclear@0 175 glutSolidTeapot(1.0);
nuclear@0 176 glFrontFace(GL_CCW);
nuclear@0 177
nuclear@0 178
nuclear@0 179
nuclear@0 180 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
nuclear@1 181 glViewport(0, 0, xsz, ysz);
nuclear@1 182
nuclear@0 183 glClear(GL_COLOR_BUFFER_BIT);
nuclear@0 184
nuclear@0 185 glMatrixMode(GL_PROJECTION);
nuclear@0 186 glPushMatrix();
nuclear@0 187 glLoadIdentity();
nuclear@0 188 glMatrixMode(GL_MODELVIEW);
nuclear@0 189 glLoadIdentity();
nuclear@0 190 glPushMatrix();
nuclear@0 191
nuclear@0 192 glPushAttrib(GL_ENABLE_BIT);
nuclear@0 193 glDisable(GL_DEPTH_TEST);
nuclear@0 194
nuclear@0 195 bind_program(prog);
nuclear@0 196 set_uniform_int(prog, "framebuf", 0);
nuclear@0 197 set_uniform_int(prog, "dither_tex", 1);
nuclear@0 198 set_uniform_int(prog, "dither_levels", DITHER_LEVELS);
nuclear@0 199 set_uniform_int(prog, "dither_size", DITHER_SZ);
nuclear@0 200
nuclear@0 201 glActiveTextureARB(GL_TEXTURE0);
nuclear@0 202 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
nuclear@0 203 glActiveTextureARB(GL_TEXTURE1);
nuclear@0 204 glBindTexture(GL_TEXTURE_2D, dither_tex);
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 }