Download

Pivot Painter Python to C++

Hi! I really need to convert one of the pivot painter functions from either python(blender pivot painter) or 3DS Max script. It has to do with shifting bits around, but all float 32 to float 16 conversions I’ve tried give me different results in C++ and they are incorrect. Basically what I am tring to do is to store that mesh indexes in texture alpha. I succesfully write data to the texture and all, but that bits shifting code produces weirds results in C++. I will be glad if someone here can help me. It seems that this bits shifting code isn’t just simple packing conversion, but there is more to it. Here is the pivot painter code from blender, that I need to convert to C++:



def packTextureBits(index):
    index = int(index)
    index = index +1024
    sigh=index&0x8000
    sigh=sigh<<16
    exptest=index&0x7fff
    if exptest==0:
        exp=0
    else:
        exp=index>>10
        exp=exp&0x1f
        exp=exp-15
        exp=exp+127
        exp=exp<<23
    mant=index&0x3ff
    mant=mant<<13
    index=sigh|exp|mant

    cp = pointer(c_int(index))
    fp = cast(cp, POINTER(c_float))
    return fp.contents.value


And here is my potential C++ conversion that doesn’t really work:



float PackIntToFloat(int value)
{
value += 1024;
int sign = (value & 0x8000) << 16;
int exp = value & 0x7fff;
if (exp != 0)
{
exp = value >> 10;
exp = exp & 0x1f;
exp = exp - 15 + 127;
exp = exp << 23;
}
int mant = (value & 0x3fff) << 13;
value = sign | exp | mant;

float fp;
memcpy(&fp, &value, sizeof(float));

return fp;
}