Since opengl ES 2.0 somehow doesnt support float textures i was forced to make them unsigned bytes so basically i have a float number and i need to convert it to 4 unsigned chars.
i cant use bit shifting and strings. (and i need to process data to texture at realtime.)
i am looking for a fast way to do this.
i dont need good precision my current function only accepts 5 digit numbers that are between 0..127*127 which is retarded anyway my function is retarded...
however it works for floats between 0..1 and for some other
struct TRGBA_FLOAT
{
Byte r;
Byte g;
Byte b;
Byte a;
TRGBA_FLOAT()
{
r=0;
g=0;
b=0;
a=0;
}
TRGBA_FLOAT(Byte n)
{
r = n;
g = n;
b = n;
a = n;
}
TRGBA_FLOAT(Byte ar, Byte ag, Byte ab, Byte aa)
{
r = ar;
g = ag;
b = ab;
a = aa;
}
};
//function returns 5 digit float number as a function of 4 bytes, accepts only positive numbers
TRGBA_FLOAT FloatToUnsignedChar(float num)
{
if (num > 16129.0) return TRGBA_FLOAT(0);
if (num < -16129.0) return TRGBA_FLOAT(0);
int MAX_RGBA_FLOAT_ITERATIONS = 5;
TRGBA_FLOAT res;
//determine how big number is
//left number has higher priority and right side will loose precision
float p;
int num_len = 0;
if (num > 1.0)
{
p = num;
num_len = 0;
while ((p / 10.0) > 1.0)
{
p = p / 10.0;
num_len = num_len + 1;
}
p = num / 10.0;
num_len = num_len + 1;
}
int max_iterations = MAX_RGBA_FLOAT_ITERATIONS - num_len;
if (max_iterations == 0)
{
if ( num >= 127.0 * 2.0 )
{
float x = num / 127.0;
res.r = 127;
res.g = Byte(int(x));//
res.b = Byte(num - float(int(x) * 127.0));//Byte(int(mynumber - float(res.g)));
res.a = 0;
} else
if (num <= 127.0)
{
res.r = Byte(num);
res.g = 1;
res.b = 0;
}
else
{
res.r = 127;
res.g = 1;
res.b = Byte(num) - 127;
}
res.a = 0;
return res;
}
//lets try to calculate number of decimals
int i = int(num);
p = num - float(i);
int decnum = 0;
//0.045
//0.45
//4.5
//0.5
//5 - 5
int return_number=0;
bool stop = false;
int iterations = 0;
while (!stop)
{
if (decnum == max_iterations) break;
iterations = iterations + 1;
if (iterations > max_iterations) break;
if (p < 1.0)
{
p = p * 10.0;
decnum = decnum + 1;
if (p == 0.0) break;
continue;
}
if (p > 1.0)
{
p = p - float(int(p));
decnum = decnum + 1;
if (p == 0.0) break;
}
}
float sth = 0.0;
if (decnum == 0) sth = int(pow(10, num_len));
decnum = decnum - 1;
//77.891
p = num - float(int(num));
for (i=0; i < decnum; i++) p = p * 10;
//891
float mynumber = float(int(num) * int(pow(10, num_len)) + int(p)) - sth ;
//77891
if ( mynumber >= 127.0 * 2.0 )
{
float x = mynumber / 127.0;
res.r = 127;
res.g = Byte(int(x));//
res.b = Byte(mynumber - float(int(x) * 127.0));//Byte(int(mynumber - float(res.g)));
} else
if (mynumber <= 127.0)
{
res.r = Byte(mynumber);
res.g = 1;
res.b = 0;
}
else
{
res.r = 127;
res.g = 1;
res.b = Byte(mynumber) - 127;
}
res.a = Byte(decnum);
return res;
}
float RGBA_FLOAT_TO_FLOAT(TRGBA_FLOAT num)
{
float kp = float(int(num.r*num.g+num.b));
for (int i=0; i < num.a; i++)
kp = kp / 10.0;
return kp;
}