Skip to content

Instantly share code, notes, and snippets.

@chuckleplant
Last active May 30, 2016 15:55
Show Gist options
  • Save chuckleplant/a770500994f62a12ba7e307487887969 to your computer and use it in GitHub Desktop.
Save chuckleplant/a770500994f62a12ba7e307487887969 to your computer and use it in GitHub Desktop.
// GLSL
const float pixelDepth = 256.0f;
const vec4 bitSh = vec4(
pixelDepth * pixelDepth * pixelDepth,
pixelDepth * pixelDepth,
pixelDepth,
1.);
const vec4 bitShifts = vec4(1.) / bitSh;
vec2 decodeFloats16()
{
vec2 coord;
vec4 color = texture2D(lookUpX, texCoordVar);
// Denormalize
float r = color.r * 255.0f;
float g = color.g * 255.0f;
float b = color.b * 255.0f;
float a = color.a * 255.0f;
coord.x = (r * 255.0f + g) / float(inputWidth) + uPixelWidth/2.0f;
coord.y = (b * 255.0f + a) / float(inputHeight) + uPixelHeight/2.0f;
return coord;
}
float decodeFloatRGBA(vec4 rgba)
{
return dot(rgba, bitShifts);
}
vec2 getTrueTexCoord()
{
vec4 colX = texture2D(lookUpX, texCoordVar);
vec4 colY = texture2D(lookUpY, texCoordVar);
return vec2(
decodeFloatRGBA(colX),
decodeFloatRGBA(colY));
}
//////////////////////////////////////////////////////////////////////////////////// C++
// NOTE: Need to add half pixel width to the encoded tex coord either from cpu or in shader
// TODO:
ofFloatColor encodeFloat32(float value)
{
static const float pd = 255.; // pixel depth
static const ofVec4f bitSh( pd*pd*pd, pd*pd, pd, 1.);
static const ofVec4f bitMsk(0.0f, 1.0f/pd, 1.0f/pd, 1.0f/pd);
float aux;
ofVec4f enc(
modf(value * bitSh.x, &aux),
modf(value * bitSh.y, &aux),
modf(value * bitSh.z, &aux),
modf(value * bitSh.w, &aux));
ofVec4f encXXYZ(enc.x, enc.x, enc.y, enc.z);
enc -= encXXYZ * bitMsk;
return ofFloatColor(enc.x, enc.y, enc.z, enc.w);
}
ofColor encodeFloat16(int x, int y)
{
ofColor color;
color.r = x / 255;
color.g = x % 255;
color.b = y / 255;
color.a = y % 255;
return color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment