From f8fcd426a10e0f13f01b899e6eb6b3770b08a192 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Mon, 3 Jul 2023 22:22:52 -0700 Subject: [PATCH] Encoding and decoding of float into normalied float vec4 --- armature.glsl | 14 +++++++++++--- main.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/armature.glsl b/armature.glsl index 884436e..804c146 100644 --- a/armature.glsl +++ b/armature.glsl @@ -17,6 +17,13 @@ uniform vs_params { }; uniform sampler2D bones_tex; +float decode_normalized_float32(vec4 v) +{ + float sign = 2.0 * v.x - 1.0; + + return sign * (v.z*255.0 + v.y); +} + // in textures, color elements are delivered as unsigend normalize floats // in [0, 1]. This makes them into [-1, 1] as the bone matrices require // such values to be correct @@ -31,13 +38,14 @@ vec4 make_signed_again(vec4 v) { void main() { vec4 total_position = vec4(0.0f); - for(int i = 0; i < 4; i++) + for(int bone_influence_index = 0; bone_influence_index < 4; bone_influence_index++) { - float index_float = indices_in[i]; + float index_float = indices_in[bone_influence_index]; int index = int(index_float * 65535.0); - float weight = weights_in[i]; + float weight = weights_in[bone_influence_index]; float y_coord = (0.5 + index)/bones_tex_size.y; + vec4 col0 = texture(bones_tex, vec2((0.5 + 0)/bones_tex_size.x, y_coord)); vec4 col1 = texture(bones_tex, vec2((0.5 + 1)/bones_tex_size.x, y_coord)); vec4 col2 = texture(bones_tex, vec2((0.5 + 2)/bones_tex_size.x, y_coord)); diff --git a/main.c b/main.c index fd8b1ce..1e8eb55 100644 --- a/main.c +++ b/main.c @@ -2705,6 +2705,41 @@ Mat4 get_animated_bone_transform(Bone *bone, float time) return M4D(1.0f); } +typedef struct +{ + MD_u8 rgba[4]; +} PixelData; + +PixelData encode_normalized_float32(float to_encode) +{ + Vec4 to_return_vector = {0}; + + // x is just -1.0f or 1.0f, encoded as a [0,1] normalized float. + if(to_encode < 0.0f) to_return_vector.x = -1.0f; + else to_return_vector.x = 1.0f; + to_return_vector.x = to_return_vector.x / 2.0f + 0.5f; + + float without_sign = fabsf(to_encode); + to_return_vector.y = without_sign - floorf(without_sign); + + to_return_vector.z = fabsf(to_encode) - to_return_vector.y; + assert(to_return_vector.z < 255.0f); + to_return_vector.z /= 255.0f; + + // w is unused for now + + PixelData to_return = {0}; + + for(int i = 0; i < 4; i++) + { + assert(0.0f <= to_return_vector.Elements[i] && to_return_vector.Elements[i] <= 1.0f); + to_return.rgba[i] = (MD_u8)(to_return_vector.Elements[i] * 255.0f); + } + + return to_return; +} + + void draw_armature(Mat4 view, Mat4 projection, Transform t, Armature *armature, float elapsed_time) { MD_ArenaTemp scratch = MD_GetScratch(0, 0);