Skip to content

Instantly share code, notes, and snippets.

@obviousjim
Last active December 19, 2015 19:09
Show Gist options
  • Save obviousjim/6004436 to your computer and use it in GitHub Desktop.
Save obviousjim/6004436 to your computer and use it in GitHub Desktop.
GLSL for reconstructing depth from HSL image
//Convert an RGB vec3 to an HSL vec3. We use the HUE value to encode depth, while SATURATION and BRIGHTNESS are always maxed out
vec3 rgb2hsl( vec3 _input ){
float h = 0.0;
float s = 0.0;
float l = 0.0;
float r = _input.r;
float g = _input.g;
float b = _input.b;
float cMin = min( r, min( g, b ) );
float cMax = max( r, max( g, b ) );
l = ( cMax + cMin ) / 2.0;
if ( cMax > cMin ) {
float cDelta = cMax - cMin;
s = l < .05 ? cDelta / ( cMax + cMin ) : cDelta / ( 2.0 - ( cMax + cMin ) );
// hue
if ( r == cMax ) {
h = ( g - b ) / cDelta;
} else if ( g == cMax ) {
h = 2.0 + ( b - r ) / cDelta;
} else {
h = 4.0 + ( r - g ) / cDelta;
}
if ( h < 0.0) {
h += 6.0;
}
h = h / 6.0;
}
return vec3( h, s, l );
}
//sample the texture at depth position, get the Hue value (R component after conversion) and then map it between min & max
float depthValueFromSample( vec2 depthPos){
vec2 halfvec = vec2(.5,.5);
float depth = rgb2hsl( texture2DRect(texture, floor(depthPos) + halfvec ).xyz ).r;
return depth * ( maxDepth - minDepth ) + minDepth;
}
vec2 samplePos = gl_Vertex.xy;
vec2 depthPos = samplePos + depthRect.xy;
float depth = depthValueFromSample( depthPos );
// Reconstruct the 3D point position
vec4 pos = vec4((samplePos.x - depthPP.x) * depth / depthFOV.x,
(samplePos.y - depthPP.y) * depth / depthFOV.y,
depth, 1.0);
The XML that we export looks like this:
<depth>
<fovx>570.342224121</fovx>
<fovy>570.342224121</fovy>
<ppx>320.000000000</ppx>
<ppy>240.000000000</ppy>
<width>640.000000000</width>
<height>480.000000000</height>
<minDepth>0.000000000</minDepth>
<maxDepth>5999.999511719</maxDepth>
</depth>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment