dbf-udg

annotate src/udg.cc @ 0:2d27bfd21fc5

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 08 Jan 2013 03:16:48 +0200
parents
children 1d5dc834d403
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@0 16 #define DITHER_SZ 4
nuclear@0 17 #define DITHER_LEVELS 16
nuclear@0 18
nuclear@0 19 struct render_target {
nuclear@0 20 unsigned int fbo;
nuclear@0 21 unsigned int color_tex, depth_buf;
nuclear@0 22 };
nuclear@0 23
nuclear@0 24 bool init();
nuclear@0 25 void cleanup();
nuclear@0 26 void disp();
nuclear@0 27 void idle();
nuclear@0 28 void reshape(int x, int y);
nuclear@0 29 void keyb(unsigned char key, int x, int y);
nuclear@0 30 void mouse(int bn, int state, int x, int y);
nuclear@0 31 void motion(int x, int y);
nuclear@0 32 struct render_target *create_rtarg(int xsz, int ysz);
nuclear@0 33 void destroy_rtarg(struct render_target *rt);
nuclear@0 34
nuclear@0 35 int xsz, ysz;
nuclear@0 36 float cam_theta, cam_phi = 25, cam_dist = 8;
nuclear@0 37 unsigned int dither_tex;
nuclear@0 38 struct render_target *rtarg;
nuclear@0 39 unsigned int prog;
nuclear@0 40
nuclear@0 41 int main(int argc, char **argv)
nuclear@0 42 {
nuclear@0 43 glutInit(&argc, argv);
nuclear@0 44 glutInitWindowSize(800, 600);
nuclear@0 45 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
nuclear@0 46 glutCreateWindow("DBF UDG compo entry by Nuclear");
nuclear@0 47
nuclear@0 48 glutDisplayFunc(disp);
nuclear@0 49 glutIdleFunc(idle);
nuclear@0 50 glutReshapeFunc(reshape);
nuclear@0 51 glutKeyboardFunc(keyb);
nuclear@0 52 glutMouseFunc(mouse);
nuclear@0 53 glutMotionFunc(motion);
nuclear@0 54
nuclear@0 55 glewInit();
nuclear@0 56
nuclear@0 57 if(!init()) {
nuclear@0 58 return 1;
nuclear@0 59 }
nuclear@0 60
nuclear@0 61 glutMainLoop();
nuclear@0 62 return 0;
nuclear@0 63 }
nuclear@0 64
nuclear@0 65 bool init()
nuclear@0 66 {
nuclear@0 67 float *img = new float[DITHER_SZ * DITHER_SZ * DITHER_LEVELS];
nuclear@0 68 float *ptr = img;
nuclear@0 69
nuclear@0 70 for(int i=0; i<DITHER_LEVELS; i++) {
nuclear@0 71 float val = (float)i / (float)(DITHER_LEVELS - 1);
nuclear@0 72 for(int y=0; y<DITHER_SZ; y++) {
nuclear@0 73 for(int x=0; x<DITHER_SZ; x++) {
nuclear@0 74 /* (1 + M) / (1 + MxN) */
nuclear@0 75 float thres = (1.0 + dither_matrix4[x][y]) / (1.0 + DITHER_SZ * DITHER_SZ);
nuclear@0 76 *ptr++ = val >= thres ? 1.0 : 0.0;
nuclear@0 77 }
nuclear@0 78 }
nuclear@0 79 }
nuclear@0 80
nuclear@0 81 if(!(prog = create_program_load("sdr/dither.v.glsl", "sdr/dither.p.glsl"))) {
nuclear@0 82 return false;
nuclear@0 83 }
nuclear@0 84
nuclear@0 85 glGenTextures(1, &dither_tex);
nuclear@0 86 glBindTexture(GL_TEXTURE_2D, dither_tex);
nuclear@0 87 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nuclear@0 88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nuclear@0 89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
nuclear@0 90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
nuclear@0 91 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, DITHER_SZ, DITHER_SZ * DITHER_LEVELS, 0, GL_LUMINANCE, GL_FLOAT, img);
nuclear@0 92
nuclear@0 93 glEnable(GL_CULL_FACE);
nuclear@0 94 glEnable(GL_DEPTH_TEST);
nuclear@0 95 glEnable(GL_LIGHTING);
nuclear@0 96 glEnable(GL_LIGHT0);
nuclear@0 97
nuclear@0 98 return true;
nuclear@0 99 }
nuclear@0 100
nuclear@0 101 void draw_backdrop()
nuclear@0 102 {
nuclear@0 103 glPushAttrib(GL_ENABLE_BIT);
nuclear@0 104 glDisable(GL_DEPTH_TEST);
nuclear@0 105 glDisable(GL_LIGHTING);
nuclear@0 106
nuclear@0 107 glMatrixMode(GL_MODELVIEW);
nuclear@0 108 glPushMatrix();
nuclear@0 109 glLoadIdentity();
nuclear@0 110 glMatrixMode(GL_PROJECTION);
nuclear@0 111 glPushMatrix();
nuclear@0 112 glLoadIdentity();
nuclear@0 113
nuclear@0 114 glBegin(GL_QUADS);
nuclear@0 115 glColor3f(0, 0, 0);
nuclear@0 116 glVertex2f(-1, -1);
nuclear@0 117 glVertex2f(1, -1);
nuclear@0 118 glColor3f(1, 1, 1);
nuclear@0 119 glVertex2f(1, 1);
nuclear@0 120 glVertex2f(-1, 1);
nuclear@0 121 glEnd();
nuclear@0 122
nuclear@0 123 glPopMatrix();
nuclear@0 124 glMatrixMode(GL_MODELVIEW);
nuclear@0 125 glPopMatrix();
nuclear@0 126
nuclear@0 127 glPopAttrib();
nuclear@0 128 }
nuclear@0 129
nuclear@0 130 void disp()
nuclear@0 131 {
nuclear@0 132 float ldir[] = {-1, 1, 2, 0};
nuclear@0 133
nuclear@0 134 if(!rtarg) {
nuclear@0 135 printf("(re)creating render target (%dx%d)\n", xsz, ysz);
nuclear@0 136 if(!(rtarg = create_rtarg(xsz, ysz))) {
nuclear@0 137 exit(0);
nuclear@0 138 }
nuclear@0 139 }
nuclear@0 140
nuclear@0 141 glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
nuclear@0 142
nuclear@0 143 glClearColor(1, 1, 1, 1);
nuclear@0 144 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 145
nuclear@0 146 draw_backdrop();
nuclear@0 147
nuclear@0 148 glEnable(GL_DEPTH_TEST);
nuclear@0 149
nuclear@0 150 glMatrixMode(GL_MODELVIEW);
nuclear@0 151 glLoadIdentity();
nuclear@0 152
nuclear@0 153 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
nuclear@0 154
nuclear@0 155 glTranslatef(0, 0, -cam_dist);
nuclear@0 156 glRotatef(cam_phi, 1, 0, 0);
nuclear@0 157 glRotatef(cam_theta, 0, 1, 0);
nuclear@0 158
nuclear@0 159 glFrontFace(GL_CW);
nuclear@0 160 glutSolidTeapot(1.0);
nuclear@0 161 glFrontFace(GL_CCW);
nuclear@0 162
nuclear@0 163
nuclear@0 164
nuclear@0 165 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
nuclear@0 166 glClear(GL_COLOR_BUFFER_BIT);
nuclear@0 167
nuclear@0 168 glMatrixMode(GL_PROJECTION);
nuclear@0 169 glPushMatrix();
nuclear@0 170 glLoadIdentity();
nuclear@0 171 glMatrixMode(GL_MODELVIEW);
nuclear@0 172 glLoadIdentity();
nuclear@0 173 glPushMatrix();
nuclear@0 174
nuclear@0 175 glPushAttrib(GL_ENABLE_BIT);
nuclear@0 176 glDisable(GL_DEPTH_TEST);
nuclear@0 177
nuclear@0 178 bind_program(prog);
nuclear@0 179 set_uniform_int(prog, "framebuf", 0);
nuclear@0 180 set_uniform_int(prog, "dither_tex", 1);
nuclear@0 181 set_uniform_int(prog, "dither_levels", DITHER_LEVELS);
nuclear@0 182 set_uniform_int(prog, "dither_size", DITHER_SZ);
nuclear@0 183
nuclear@0 184 glActiveTextureARB(GL_TEXTURE0);
nuclear@0 185 glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
nuclear@0 186 glActiveTextureARB(GL_TEXTURE1);
nuclear@0 187 glBindTexture(GL_TEXTURE_2D, dither_tex);
nuclear@0 188
nuclear@0 189 glBegin(GL_QUADS);
nuclear@0 190 glColor3f(0, 1, 0);
nuclear@0 191 glTexCoord2f(0, 0); glVertex2f(-1, -1);
nuclear@0 192 glTexCoord2f(1, 0); glVertex2f(1, -1);
nuclear@0 193 glTexCoord2f(1, 1); glVertex2f(1, 1);
nuclear@0 194 glTexCoord2f(0, 1); glVertex2f(-1, 1);
nuclear@0 195 glEnd();
nuclear@0 196
nuclear@0 197 glActiveTextureARB(GL_TEXTURE1);
nuclear@0 198 glDisable(GL_TEXTURE_2D);
nuclear@0 199 glActiveTextureARB(GL_TEXTURE0);
nuclear@0 200 glDisable(GL_TEXTURE_2D);
nuclear@0 201
nuclear@0 202 bind_program(0);
nuclear@0 203
nuclear@0 204 glPopAttrib();
nuclear@0 205
nuclear@0 206 glMatrixMode(GL_PROJECTION);
nuclear@0 207 glPopMatrix();
nuclear@0 208 glMatrixMode(GL_MODELVIEW);
nuclear@0 209 glPopMatrix();
nuclear@0 210
nuclear@0 211 glutSwapBuffers();
nuclear@0 212 assert(glGetError() == GL_NO_ERROR);
nuclear@0 213 }
nuclear@0 214
nuclear@0 215 void idle()
nuclear@0 216 {
nuclear@0 217 glutPostRedisplay();
nuclear@0 218 }
nuclear@0 219
nuclear@0 220 void reshape(int x, int y)
nuclear@0 221 {
nuclear@0 222 glViewport(0, 0, x, y);
nuclear@0 223
nuclear@0 224 glMatrixMode(GL_PROJECTION);
nuclear@0 225 glLoadIdentity();
nuclear@0 226 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
nuclear@0 227
nuclear@0 228 if(x != xsz || y != ysz) {
nuclear@0 229 destroy_rtarg(rtarg);
nuclear@0 230 rtarg = 0;
nuclear@0 231 xsz = x;
nuclear@0 232 ysz = y;
nuclear@0 233 }
nuclear@0 234 }
nuclear@0 235
nuclear@0 236 void keyb(unsigned char key, int x, int y)
nuclear@0 237 {
nuclear@0 238 switch(key) {
nuclear@0 239 case 27:
nuclear@0 240 exit(0);
nuclear@0 241 }
nuclear@0 242 }
nuclear@0 243
nuclear@0 244 bool bnstate[16];
nuclear@0 245 int prev_x, prev_y;
nuclear@0 246
nuclear@0 247 void mouse(int bn, int state, int x, int y)
nuclear@0 248 {
nuclear@0 249 int idx = bn - GLUT_LEFT_BUTTON;
nuclear@0 250
nuclear@0 251 if(idx < (int)(sizeof bnstate / sizeof *bnstate)) {
nuclear@0 252 bnstate[idx] = state == GLUT_DOWN;
nuclear@0 253 }
nuclear@0 254 prev_x = x;
nuclear@0 255 prev_y = y;
nuclear@0 256 }
nuclear@0 257
nuclear@0 258 void motion(int x, int y)
nuclear@0 259 {
nuclear@0 260 int dx = x - prev_x;
nuclear@0 261 int dy = y - prev_y;
nuclear@0 262 prev_x = x;
nuclear@0 263 prev_y = y;
nuclear@0 264
nuclear@0 265 if(bnstate[0]) {
nuclear@0 266 cam_theta = fmod(cam_theta + dx * 0.5, 360.0);
nuclear@0 267 cam_phi += dy * 0.5;
nuclear@0 268 if(cam_phi < -90) {
nuclear@0 269 cam_phi = -90;
nuclear@0 270 }
nuclear@0 271 if(cam_phi > 90) {
nuclear@0 272 cam_phi = 90;
nuclear@0 273 }
nuclear@0 274 }
nuclear@0 275 if(bnstate[2]) {
nuclear@0 276 cam_dist += dy * 0.1;
nuclear@0 277 if(cam_dist < 0) {
nuclear@0 278 cam_dist = 0;
nuclear@0 279 }
nuclear@0 280 }
nuclear@0 281 }
nuclear@0 282
nuclear@0 283 struct render_target *create_rtarg(int xsz, int ysz)
nuclear@0 284 {
nuclear@0 285 struct render_target *rt = new render_target;
nuclear@0 286
nuclear@0 287 glGenFramebuffersEXT(1, &rt->fbo);
nuclear@0 288 glBindFramebufferEXT(GL_FRAMEBUFFER, rt->fbo);
nuclear@0 289
nuclear@0 290 // create the render target texture
nuclear@0 291 glGenTextures(1, &rt->color_tex);
nuclear@0 292 glBindTexture(GL_TEXTURE_2D, rt->color_tex);
nuclear@0 293 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
nuclear@0 294 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
nuclear@0 295 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@0 296 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@0 297 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
nuclear@0 298
nuclear@0 299 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color_tex, 0);
nuclear@0 300
nuclear@0 301 // create depth buffer
nuclear@0 302 glGenRenderbuffersEXT(1, &rt->depth_buf);
nuclear@0 303 glBindRenderbufferEXT(GL_RENDERBUFFER, rt->depth_buf);
nuclear@0 304 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xsz, ysz);
nuclear@0 305
nuclear@0 306 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth_buf);
nuclear@0 307
nuclear@0 308 if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
nuclear@0 309 fprintf(stderr, "incomplete fbo\n");
nuclear@0 310 return 0;
nuclear@0 311 }
nuclear@0 312
nuclear@0 313 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
nuclear@0 314 return rt;
nuclear@0 315 }
nuclear@0 316
nuclear@0 317 void destroy_rtarg(struct render_target *rt)
nuclear@0 318 {
nuclear@0 319 if(!rt) {
nuclear@0 320 return;
nuclear@0 321 }
nuclear@0 322 glDeleteFramebuffersEXT(1, &rt->fbo);
nuclear@0 323 glDeleteTextures(1, &rt->color_tex);
nuclear@0 324 glDeleteRenderbuffersEXT(1, &rt->depth_buf);
nuclear@0 325 delete rt;
nuclear@0 326 }