PDA

View Full Version : GLSL packing normal vec to single float



laggyluk
28-05-2013, 02:39 PM
I need to pass normal vector for a cube to shader using single float. no need to worry about precision as for a cube all normal vector components can be only -1,0,1 so one byte of a single will remain unused. I was trying to do it like that but the signs are messing up the float or sth

procedure packNormal(var a:single;x,y,z:smallint);
begin
a:=(x << 16) or ( y << 8) or z;
end;

//test
procedure unpackNormal(a:single;var x,y,z:smallint);
begin
x:=round(a) div 65536;
y:=round(a) div 256;
z:=round(a) mod 1;
end;


I guess I could use individual bits to encode the values but i feel like missing something obvious :P
maybe someone done something like that before?

laggyluk
29-05-2013, 06:54 AM
procedure packNormal(var a:single;x,y,z:smallint);
begin
a:= (x+2)*100+(y+2)*10+(z+2);
end;


procedure unpackNormal(a:single;var x,y,z:smallint);
var i:integer;
begin
i:=round(a);
x:= (i div 100)-2;
y:= ((i div 10) mod 10)-2;
z:= (i mod 10)-2;
end;