Encoding and decoding of float into normalied float vec4

main
parent 1df63e6f74
commit f8fcd426a1

@ -17,6 +17,13 @@ uniform vs_params {
}; };
uniform sampler2D bones_tex; 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 textures, color elements are delivered as unsigend normalize floats
// in [0, 1]. This makes them into [-1, 1] as the bone matrices require // in [0, 1]. This makes them into [-1, 1] as the bone matrices require
// such values to be correct // such values to be correct
@ -31,13 +38,14 @@ vec4 make_signed_again(vec4 v) {
void main() { void main() {
vec4 total_position = vec4(0.0f); 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); 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; 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 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 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)); vec4 col2 = texture(bones_tex, vec2((0.5 + 2)/bones_tex_size.x, y_coord));

@ -2705,6 +2705,41 @@ Mat4 get_animated_bone_transform(Bone *bone, float time)
return M4D(1.0f); 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) void draw_armature(Mat4 view, Mat4 projection, Transform t, Armature *armature, float elapsed_time)
{ {
MD_ArenaTemp scratch = MD_GetScratch(0, 0); MD_ArenaTemp scratch = MD_GetScratch(0, 0);

Loading…
Cancel
Save