packing depth float in 4 bytes

edited August 2014 in GLSL / Shaders

Hi

I'm trying to pack and unpack depth float and I don't know what is wrong, somebody can help me? thanks!!

here is the code

PShader depthShader;
PShader output;

PGraphics src, dest;
PGraphics depth;

boolean renderDepth = false;

public void setup() {
  size(800, 400, P2D);
  depthShader = loadShader("colorfrag.glsl", 
  "colorvert.glsl");

  output = loadShader("unpack.glsl");

  src = createGraphics(width, height, OPENGL);
  dest = createGraphics(width, height, OPENGL);
  depth = createGraphics(width, height, OPENGL);
  depth.smooth(8);
  depth.shader(depthShader);
}

public void draw() {
  background(0);

  drawGeometry(src, true);
  drawGeometry(depth, false);
  depthShader.set("maxDepth", 2400.0); 

  dest.beginDraw();
  dest.image(depth, 0, 0);
  dest.shader(output);


  dest.endDraw();

  if (!renderDepth)
    image(dest, 0, 0);
  else
    image(depth, 0, 0);
}

private void drawGeometry(PGraphics pg, boolean lights) {
  pg.beginDraw();

  //
  pg.background(0);
  pg.translate(680, 150, -1000);
  println(""+mouseX+" "+mouseY);
  pg.box(100000, 10, 100000);
  pg.fill(150);
  pg.noStroke();
  if (lights)
    pg.lights();
  pg.pushMatrix();
  for (int i = 0; i < 20; i++) {
    pg.translate(10, 10, 200);
    pg.sphere(150);
  }



  pg.popMatrix();

  pg.endDraw();
}

void keyPressed() {
  renderDepth = !renderDepth;
}

pack shader colorvert.glsl

#define PROCESSING_COLOR_SHADER

uniform mat4 transform;

attribute vec4 vertex;
attribute vec4 color;

varying vec4 vertColor;

void main() {
  gl_Position = transform * vertex;    
  vertColor = color;
}

pack shader colorfrag.glsl

#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

varying vec4 vertColor;
uniform float maxDepth;

 vec4 pack (float depth)
        {
            const vec4 bitSh = vec4(256 * 256 * 256,
                                    256 * 256,
                                    256,
                                    1.0);
            const vec4 bitMsk = vec4(0,
                                     1.0 / 256.0,
                                     1.0 / 256.0,
                                     1.0 / 256.0);
            vec4 comp = fract(depth * bitSh);
            comp -= comp.xxyz * bitMsk;
            return comp;
        }


float unpack (vec4 colour)
        {
            const vec4 bitShifts = vec4(1.0 / (256.0 * 256.0 * 256.0),
                                        1.0 / (256.0 * 256.0),
                                        1.0 / 256.0,
                                        1);
            return dot(colour , bitShifts);
        }
void main() {  
  float depth = gl_FragCoord.z / gl_FragCoord.w;

  //TEST PACK AND UNPACK
  //float depth2 = unpack(pack(1.0 - depth/maxDepth));
  //gl_FragColor =  vec4(vec3(depth2), 1.0); 

  //PACK
  gl_FragColor = pack(1.0 - depth/maxDepth);

  //WITHOUT PACK
 //gl_FragColor = vec4(vec3(1.0 - depth/maxDepth), 1.0); 
}

and unpack.glsl

#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

#define PROCESSING_TEXTURE_SHADER

uniform sampler2D texture;

varying vec4 vertexture;
varying vec4 vertTexCoord;

float unpack (vec4 colour)
        {
            const vec4 bitShifts = vec4(1.0 / (256.0 * 256.0 * 256.0),
                                        1.0 / (256.0 * 256.0),
                                        1.0 / 256.0,
                                        1);
            return dot(colour , bitShifts);
        }

void main()
{

    vec2 vUv = vertTexCoord.st;

    float depth = unpack(texture2D(texture, vUv));

    gl_FragColor = vec4(vec3(depth), 1.0);
   // gl_FragColor = texture2D(texture, vUv);
}
Sign In or Register to comment.