diff options
author | Stephane Marchesin <stephane.marchesin@gmail.com> | 2010-04-25 01:39:59 +0200 |
---|---|---|
committer | Stephane Marchesin <stephane.marchesin@gmail.com> | 2010-04-25 01:39:59 +0200 |
commit | b6b06dbf40ed50541ac2ee188ec52acbdd3a7ad7 (patch) | |
tree | ae96830c0cd298ee11cb4a7ef934eab4f812a1e2 | |
parent | a3f015419eb737c6095d88f686312124a86ea74e (diff) |
-rw-r--r-- | postproc.cpp | 591 |
1 files changed, 591 insertions, 0 deletions
diff --git a/postproc.cpp b/postproc.cpp new file mode 100644 index 0000000..2c64361 --- /dev/null +++ b/postproc.cpp @@ -0,0 +1,591 @@ + +#include <stdio.h> +#include <stdlib.h> +#define GL_GLEXT_PROTOTYPES +#include <GL/gl.h> +#include <GL/glu.h> +#include <GL/glext.h> +#include "SDL.h" + + +static GLfloat Xrot = 0, Yrot = 0, Zrot = 0; +//static GLfloat ClearColor[4] = {0.2, 0.2, 0.9, 0.0}; +static GLfloat ClearColor[4] = {0.0, 0.0, 0.0, 0.0}; +//static GLfloat ClearColor[4] = {1.0, 1.0, 1.0, 1.0}; +static GLfloat NearClip = 5.0, FarClip = 25.0, ViewDist = 7.0; +static GLuint color_tex, depth_tex, render_fb; +static GLhandleARB prog_glsl; + +struct box { + float tx, ty, tz; + float rx, ry, rz, ra; + float sx, sy, sz; + float color[4]; +}; + +#define NUM_BOXES 100 + +struct box Boxes[NUM_BOXES]; + +#define test_opengl_error() test_opengl_error_real(__FILE__,__func__, __LINE__) +void test_opengl_error_real (const char * file, const char* func, int line) +{ + GLenum errCode; + const GLubyte *errString; + + // did we get an error ? + if ((errCode = glGetError ()) != GL_NO_ERROR) + { + printf("============================================================\n"); + printf("OpenGL error at %s:%d\nin function %s\n",file,line,func); + printf("Error code 0x%x :",errCode); + errString = gluErrorString (errCode); + // work around older versions of glu not supporting error 0x0506 + if ((errCode==0x0506)&&(!errString)) + printf("GL_INVALID_FRAMEBUFFER_OPERATION_EXT\n"); + else if (!errString) + printf("Unknown OpenGL error\n"); + else + printf("%s\n", (const char *) errString); + printf("============================================================\n"); + } +} + +static PFNGLGETOBJECTPARAMETERIVARBPROC eglGetObjectParameterivARB =NULL; +static PFNGLGETINFOLOGARBPROC eglGetInfoLogARB =NULL; +static PFNGLCREATESHADEROBJECTARBPROC eglCreateShaderObjectARB =NULL; +static PFNGLSHADERSOURCEARBPROC eglShaderSourceARB =NULL; +static PFNGLCOMPILESHADERARBPROC eglCompileShaderARB =NULL; +static PFNGLCREATEPROGRAMOBJECTARBPROC eglCreateProgramObjectARB =NULL; +static PFNGLATTACHOBJECTARBPROC eglAttachObjectARB =NULL; +static PFNGLLINKPROGRAMARBPROC eglLinkProgramARB =NULL; +static PFNGLUNIFORM1IARBPROC eglUniform1iARB =NULL; +static PFNGLGETUNIFORMLOCATIONARBPROC eglGetUniformLocationARB =NULL; +static PFNGLUSEPROGRAMOBJECTARBPROC eglUseProgramObjectARB =NULL; + +static void vr_glext_glsl_init_exts() +{ + static bool init=false; + if (init) + return; + eglGetObjectParameterivARB=(PFNGLGETOBJECTPARAMETERIVARBPROC)SDL_GL_GetProcAddress("glGetObjectParameterivARB"); + eglGetInfoLogARB=(PFNGLGETINFOLOGARBPROC)SDL_GL_GetProcAddress("glGetInfoLogARB"); + eglCreateShaderObjectARB=(PFNGLCREATESHADEROBJECTARBPROC)SDL_GL_GetProcAddress("glCreateShaderObjectARB"); + eglShaderSourceARB=(PFNGLSHADERSOURCEARBPROC)SDL_GL_GetProcAddress("glShaderSourceARB"); + eglCompileShaderARB=(PFNGLCOMPILESHADERARBPROC)SDL_GL_GetProcAddress("glCompileShaderARB"); + eglCreateProgramObjectARB=(PFNGLCREATEPROGRAMOBJECTARBPROC)SDL_GL_GetProcAddress("glCreateProgramObjectARB"); + eglAttachObjectARB=(PFNGLATTACHOBJECTARBPROC)SDL_GL_GetProcAddress("glAttachObjectARB"); + eglLinkProgramARB=(PFNGLLINKPROGRAMARBPROC)SDL_GL_GetProcAddress("glLinkProgramARB"); + eglUniform1iARB=(PFNGLUNIFORM1IARBPROC)SDL_GL_GetProcAddress("glUniform1iARB"); + eglGetUniformLocationARB=(PFNGLGETUNIFORMLOCATIONARBPROC)SDL_GL_GetProcAddress("glGetUniformLocationARB"); + eglUseProgramObjectARB=(PFNGLUSEPROGRAMOBJECTARBPROC)SDL_GL_GetProcAddress("glUseProgramObjectARB"); +} + + +static void vr_glext_glsl_dump_log(GLhandleARB p,const GLcharARB* shader) +{ + vr_glext_glsl_init_exts(); + GLint log_size; + eglGetObjectParameterivARB(p, GL_OBJECT_INFO_LOG_LENGTH_ARB, &log_size); + if (log_size>1) + { + GLcharARB* s=(GLcharARB*)malloc((log_size+1)*sizeof(GLcharARB)); + eglGetInfoLogARB(p,log_size,NULL,s); + printf("============================================================\n"); + printf("Faulty ARB program. Log (%d bytes) : \n%s\n",log_size,s); + free(s); + printf("Faulty ARB program : \n"); + int lc=0; + printf("\n%4d: ",++lc); + for(int i=0;i<(int)strlen(shader);i++) + { + if (shader[i]=='\n') + printf("\n%4d: ",++lc); + else + printf("%c",shader[i]); + } + printf("\n"); + printf("============================================================\n"); + } +} + +bool vr_glext_glsl_create_program(const GLcharARB* vshader,const GLcharARB* fshader,GLhandleARB* res) +{ + vr_glext_glsl_init_exts(); + GLint ok; + + GLhandleARB vs=eglCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); + GLhandleARB fs=eglCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); + eglShaderSourceARB(vs, 1, &vshader,NULL); + eglShaderSourceARB(fs, 1, &fshader,NULL); + eglCompileShaderARB(vs); + vr_glext_glsl_dump_log(vs,vshader); + eglGetObjectParameterivARB(vs,GL_OBJECT_COMPILE_STATUS_ARB,&ok); + if (!ok) + { + printf("compiling vertex shader\n"); + return false; + } + eglCompileShaderARB(fs); + vr_glext_glsl_dump_log(fs,fshader); + eglGetObjectParameterivARB(fs,GL_OBJECT_COMPILE_STATUS_ARB,&ok); + if (!ok) + { + printf("compiling fragment shader\n"); + return false; + } + GLhandleARB p = eglCreateProgramObjectARB(); + eglAttachObjectARB(p,vs); + eglAttachObjectARB(p,fs); + eglLinkProgramARB(p); + vr_glext_glsl_dump_log(p,"(Linking Stage)"); + eglGetObjectParameterivARB(p,GL_OBJECT_LINK_STATUS_ARB,&ok); + if (!ok) + { + printf("linking program\n"); + return false; + } + *res=p; + return true; +} + +char vshader[] = +"void main()" +"{" +" gl_TexCoord[0] = gl_MultiTexCoord0;" +" gl_Position = ftransform();" +"}"; + + +char fshader[] = +"uniform sampler2D depth_texture; \n" +"uniform sampler2D color_texture; \n" +"void main () \n" +"{ \n" +" const float delta = 1.0/1024.0; \n" +" const float half = delta / 2.0; \n" + +#if 0 +" float d1 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 10.0 * delta, 0.0))); \n" +" float d2 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( -10.0 * delta, 0.0))); \n" +" float d3 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 8.0 * delta, 8.0 * delta))); \n" +" float d4 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( -8.0 * delta, 8.0 * delta))); \n" +" float d5 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 8.0 * delta, -8.0 * delta))); \n" +" float d6 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( -8.0 * delta, -8.0 * delta))); \n" +" float d7 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 0.0, 10.0 * delta))); \n" +" float d8 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 0.0, -10.0 * delta))); \n" +" float d9 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 8.0 * delta, 8.0 * delta))); \n" +" float da = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 8.0 * delta, -8.0 * delta))); \n" +" float db = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( -8.0 * delta, 8.0 * delta))); \n" +" float dc = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( -8.0 * delta, -8.0 * delta))); \n" + +" float ds1 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 7.0 * delta, 0.0))); \n" +" float ds2 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( -7.0 * delta, 0.0))); \n" +" float ds3 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 5.0 * delta, 5.0 * delta))); \n" +" float ds4 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( -5.0 * delta, 5.0 * delta))); \n" +" float ds5 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 5.0 * delta, -5.0 * delta))); \n" +" float ds6 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( -5.0 * delta, -5.0 * delta))); \n" +" float ds7 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 0.0, 7.0 * delta))); \n" +" float ds8 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 0.0, -7.0 * delta))); \n" +" float ds9 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 5.0 * delta, 5.0 * delta))); \n" +" float dsa = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 5.0 * delta, -5.0 * delta))); \n" +" float dsb = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( -5.0 * delta, 5.0 * delta))); \n" +" float dsc = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( -5.0 * delta, -5.0 * delta))); \n" + +" float dc1 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 3.0 * delta, 0.0))); \n" +" float dc2 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( -3.0 * delta, 0.0))); \n" +" float dc3 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 2.0 * delta, 2.0 * delta))); \n" +" float dc4 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( -2.0 * delta, 2.0 * delta))); \n" +" float dc5 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 2.0 * delta, -2.0 * delta))); \n" +" float dc6 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( -2.0 * delta, -2.0 * delta))); \n" +" float dc7 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 0.0, 3.0 * delta))); \n" +" float dc8 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 0.0, -3.0 * delta))); \n" +" float dc9 = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 2.0 * delta, 2.0 * delta))); \n" +" float dca = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( 2.0 * delta, -2.0 * delta))); \n" +" float dcb = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( -2.0 * delta, 2.0 * delta))); \n" +" float dcc = float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2( -2.0 * delta, -2.0 * delta))); \n" + +" float d = float(texture2D(depth_texture, gl_TexCoord[0].xy )); \n" +" float fd = (1.0/3.0 * (d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9 + da + db + dc) + 2.0/3.0 * (ds1 + ds2 + ds3 + ds4 + ds5 + ds6 + ds7 + ds8 + ds9 + dsa + dsb + dsc) + dc1 + dc2 + dc3 + dc4 + dc5 + dc6 + dc7 + dc8 + dc9 + dca + dcb + dcc +d)/25.0 - d; \n" +//" float fd = ((d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9 + da + db + dc) + d)/13.0 - d; \n" +#else +" float d = 0.0; \n" + +" d += 0.505480 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(0.695047 * delta,7.355039 * delta)));\n" +" d += 0.220702 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(5.121977 * delta,-9.728344 * delta)));\n" +" d += 0.543350 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(-0.606182 * delta,6.959354 * delta)));\n" +" d += 0.743143 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(-4.867543 * delta,-0.237501 * delta)));\n" +" d += 0.558876 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(-1.481525 * delta,-6.659680 * delta)));\n" +" d += 0.932282 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(-2.215878 * delta,0.836371 * delta)));\n" +" d += 0.210786 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(4.699031 * delta,10.122847 * delta)));\n" +" d += 0.242755 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(5.846053 * delta,8.892679 * delta)));\n" +" d += 0.987660 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(-0.761120 * delta,0.643427 * delta)));\n" +" d += 0.985361 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(-0.218133 * delta,-1.064058 * delta)));\n" +" d += 0.753374 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(2.938722 * delta,3.744242 * delta)));\n" +" d += 0.403444 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(4.737152 * delta,7.083564 * delta)));\n" +" d += 0.206273 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(-7.325160 * delta,8.522102 * delta)));\n" +" d += 0.831194 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(3.334447 * delta,-1.916467 * delta)));\n" +" d += 0.475257 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(-1.215176 * delta,-7.618093 * delta)));\n" +" d += 0.920091 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(-1.202422 * delta,-2.284027 * delta)));\n" +" d += 0.300928 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(-2.672836 * delta,-9.430100 * delta)));\n" +" d += 0.425978 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(1.773709 * delta,8.069904 * delta)));\n" +" d += 0.579018 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(-0.293382 * delta,6.605125 * delta)));\n" +" d += 0.883902 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(2.890216 * delta,-1.232641 * delta)));\n" +" d += 0.735226 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(4.188049 * delta,2.658273 * delta)));\n" +" d += 0.817384 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(1.743708 * delta,3.618174 * delta)));\n" +" d += 0.462156 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(-7.668356 * delta,-1.715950 * delta)));\n" +" d += 0.357720 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(2.648421 * delta,-8.673310 * delta)));\n" +" d += 0.443542 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(-0.123771 * delta,8.063602 * delta)));\n" +" d += 0.439365 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(6.855833 * delta,-4.334925 * delta)));\n" +" d += 0.785900 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(3.040863 * delta,-3.166581 * delta)));\n" +" d += 0.392074 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(3.127253 * delta,-8.069988 * delta)));\n" +" d += 0.533866 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(7.060996 * delta,-0.592556 * delta)));\n" +" d += 0.599526 * float(texture2D(depth_texture, gl_TexCoord[0].xy + vec2(half) + vec2(2.694366 * delta,5.802559 * delta)));\n" +" d /= 17.276613;\n" + +" float fd = d - float(texture2D(depth_texture, gl_TexCoord[0].xy )); \n" +#endif +" fd = fd * 25.0;\n" +" if (fd>0.0) fd = 0.0; \n" +" vec4 cs = texture2D(color_texture, gl_TexCoord[0].xy); \n" + +/* +" float luminance = 0.3 * cs.r + 0.59 * cs.g + 0.11 * cs.b; \n" +" cs = 0.8 * cs + (0.2 *(1.0 - d)) * cs + 0.2 * (d) * vec4(0.5); \n" +*/ + "vec4 c = vec4(0.0);\n" + "vec4 cl = vec4(0.0);\n" + "vec4 sample;\n" + + + +" sample = vec4(0.161729) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(-12.071574 * delta, 0.153622 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.421053) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(-0.944107 * delta, -8.264892 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.051966) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(-4.213698 * delta, -14.792520 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.341270) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(6.621652 * delta, 6.493092 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.391833) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(6.028410 * delta, -6.213845 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.583215) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(-5.705786 * delta, -3.252683 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.060565) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(14.832157 * delta, 2.080770 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.140813) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(-12.243018 * delta, -2.633351 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.436842) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(-0.602685 * delta, -8.117351 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.218144) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(-7.784732 * delta, 7.823411 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.999171) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(-0.043027 * delta, -0.253957 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.935022) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(-2.315059 * delta, -0.123630 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.999983) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(0.005478 * delta, -0.036014 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.691448) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(-2.251168 * delta, -4.944656 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.093066) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(11.980635 * delta, -6.813206 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.421495) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(7.886395 * delta, 2.630699 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.405684) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(-8.258098 * delta, 1.994588 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.990040) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(-0.682021 * delta, 0.579320 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.431295) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(0.163248 * delta, -8.200633 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" sample = vec4(0.662642) * texture2D(color_texture, gl_TexCoord[0].xy + vec2(1.552690 * delta, -5.523653 * delta) );\n" +" c += sample;\n" +" cl = max(cl, sample);\n" +" c /= vec4(9.437279);\n" +// border extraction filter +" vec4 fc = (2.0 * cs - c ) ; \n" + + +// bloom +" float luminance = 0.3 * cl.r + 0.59 * cl.g + 0.11 * cl.b; \n" +" luminance *= luminance; \n" +" luminance *= luminance; \n" +" luminance *= luminance; \n" +" gl_FragColor = (fc + vec4(fd)) * vec4(1.0 + 1.0 * luminance) ; \n" +//" gl_FragColor = fc; \n" +//" gl_FragColor = texture2D(color_texture, gl_TexCoord[0].xy); \n" +//" gl_FragColor = 1.0 + vec4(fd); \n" +//" gl_FragColor = vec4(d); \n" +"} \n" +; + + + + +/* Return random float in [0,1] */ +static float Random(void) +{ + int i = rand(); + return (float) (i % 1000) / 1000.0; +} + + +static void MakeBoxes(void) +{ + int i; + for (i = 0; i < NUM_BOXES; i++) { + Boxes[i].tx = -1.0 + 2.0 * Random(); + Boxes[i].ty = -1.0 + 2.0 * Random(); + Boxes[i].tz = -1.0 + 2.0 * Random(); + Boxes[i].sx = 0.1 + Random() * 0.4; + Boxes[i].sy = 0.1 + Random() * 0.4; + Boxes[i].sz = 0.1 + Random() * 0.4; + Boxes[i].rx = Random(); + Boxes[i].ry = Random(); + Boxes[i].rz = Random(); + Boxes[i].ra = Random() * 360.0; + Boxes[i].color[0] = 0.2 + 0.8*Random(); + Boxes[i].color[1] = 0.2 + 0.8*Random(); + Boxes[i].color[2] = 0.2 + 0.8*Random(); + Boxes[i].color[3] = 1.0; + } +} + +static void +drawBox(GLfloat size, GLenum type) +{ + static GLfloat n[6][3] = + { + {-1.0, 0.0, 0.0}, + {0.0, 1.0, 0.0}, + {1.0, 0.0, 0.0}, + {0.0, -1.0, 0.0}, + {0.0, 0.0, 1.0}, + {0.0, 0.0, -1.0} + }; + static GLint faces[6][4] = + { + {0, 1, 2, 3}, + {3, 2, 6, 7}, + {7, 6, 5, 4}, + {4, 5, 1, 0}, + {5, 6, 2, 1}, + {7, 4, 0, 3} + }; + GLfloat v[8][3]; + GLint i; + + v[0][0] = v[1][0] = v[2][0] = v[3][0] = -size / 2; + v[4][0] = v[5][0] = v[6][0] = v[7][0] = size / 2; + v[0][1] = v[1][1] = v[4][1] = v[5][1] = -size / 2; + v[2][1] = v[3][1] = v[6][1] = v[7][1] = size / 2; + v[0][2] = v[3][2] = v[4][2] = v[7][2] = -size / 2; + v[1][2] = v[2][2] = v[5][2] = v[6][2] = size / 2; + + for (i = 5; i >= 0; i--) { + glBegin(type); + glNormal3fv(&n[i][0]); + glVertex3fv(&v[faces[i][0]][0]); + glVertex3fv(&v[faces[i][1]][0]); + glVertex3fv(&v[faces[i][2]][0]); + glVertex3fv(&v[faces[i][3]][0]); + glEnd(); + } +} + + + + +static void DrawBoxes(void) +{ + int i; + for (i = 0; i < NUM_BOXES; i++) { + glPushMatrix(); + glTranslatef(Boxes[i].tx, Boxes[i].ty, Boxes[i].tz); + glRotatef(Boxes[i].ra, Boxes[i].rx, Boxes[i].ry, Boxes[i].rz); + glScalef(Boxes[i].sx, Boxes[i].sy, Boxes[i].sz); + glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, Boxes[i].color); + drawBox(1.0, GL_QUADS); + glPopMatrix(); + } +} + + + +static void Draw(void) +{ + // render data to offscreen color+depth buffer + glBindFramebuffer(GL_FRAMEBUFFER_EXT, render_fb); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + eglUseProgramObjectARB(0); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(-1.0, 1.0, -1.0, 1.0, NearClip, FarClip); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -ViewDist); + glRotatef(Xrot, 1, 0, 0); + glRotatef(Yrot, 0, 1, 0); + glRotatef(Zrot, 0, 0, 1); + + glActiveTextureARB( GL_TEXTURE1_ARB ); + glDisable(GL_TEXTURE_3D); + glDisable(GL_TEXTURE_2D); + glActiveTextureARB( GL_TEXTURE0_ARB ); + glDisable(GL_TEXTURE_3D); + glDisable(GL_TEXTURE_2D); + glEnable(GL_LIGHTING); + glDepthFunc(GL_LESS); + DrawBoxes(); + + + // now postprocess to front buffer + glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0); + eglUseProgramObjectARB(prog_glsl); + + glActiveTextureARB( GL_TEXTURE1_ARB ); + glBindTexture(GL_TEXTURE_2D, color_tex); + glDisable(GL_TEXTURE_3D); + glEnable(GL_TEXTURE_2D); + eglUniform1iARB(eglGetUniformLocationARB(prog_glsl, "color_texture"),1); + + + glActiveTextureARB( GL_TEXTURE0_ARB ); + glBindTexture(GL_TEXTURE_2D, depth_tex); + glDisable(GL_TEXTURE_3D); + glEnable(GL_TEXTURE_2D); + eglUniform1iARB(eglGetUniformLocationARB(prog_glsl, "depth_texture"),0); + + glColor4f(1.0,1.0,1.0,1.0); + + + + glDisable(GL_LIGHTING); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glBegin(GL_QUADS); + glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -1.0); + glTexCoord2f(1.0, 0.0); glVertex2f(1.0, -1.0); + glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0); + glTexCoord2f(0.0, 1.0); glVertex2f(-1.0, 1.0); + glEnd(); + + test_opengl_error(); + + SDL_GL_SwapBuffers(); +} + + +static void Reshape(int width, int height) +{ + glViewport(0, 0, width, height); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(-1.0, 1.0, -1.0, 1.0, NearClip, FarClip); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -ViewDist); +} + + +static void Init(int w,int h) +{ + SDL_Init(SDL_INIT_EVERYTHING); + SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); + SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 ); + SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 ); + SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 0 ); + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); + SDL_SetVideoMode(w, h, 0, SDL_OPENGL); + Reshape(w, h); + + glClearColor(ClearColor[0], ClearColor[1], ClearColor[2], ClearColor[3]); + glEnable(GL_DEPTH_TEST); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glEnable(GL_CULL_FACE); + glEnable(GL_NORMALIZE); + MakeBoxes(); + + + glGenFramebuffersEXT(1, &render_fb); + glBindFramebuffer(GL_FRAMEBUFFER_EXT, render_fb); + + + // color buffer stuff + glGenTextures(1,&color_tex); + glBindTexture(GL_TEXTURE_2D, color_tex); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + + // depth buffer stuff + glGenTextures(1,&depth_tex); + glBindTexture(GL_TEXTURE_2D, depth_tex); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, w, h, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); + glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ); + glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP ); + + // Attach texture to FBO + glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, color_tex, 0); + glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depth_tex, 0); + test_opengl_error(); + + if ( !vr_glext_glsl_create_program(vshader,fshader,&prog_glsl) ) + return; +} + + + +int main(int argc, char *argv[]) +{ + Init(1024,1024); + bool quit=false; + do + { + Draw(); + + double dt = 0.012; + Xrot += 16.0 * dt; + Yrot += 12.0 * dt; + Zrot += 8.0 * dt; + + SDL_Event event; + while(SDL_PollEvent(&event)>0) + { + switch(event.type) + { + case SDL_KEYDOWN: + case SDL_QUIT: + quit = true; + } + } + } + while(!quit); + + return 0; +} |