Results 1 to 2 of 2

Thread: GLSL packing normal vec to single float

  1. #1

    GLSL packing normal vec to single float

    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
    Code:
    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
    maybe someone done something like that before?
    Last edited by laggyluk; 28-05-2013 at 02:41 PM.

  2. #2
    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;

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •