[fpc-pascal] Float packing

Ryan Joseph ryan at thealchemistguild.com
Mon Nov 6 04:43:29 CET 2017


I actually got the problem wrong and still haven’t figured it out so here’s another go.

I have a number which I need to encode as an RGB value each component being 4 bytes (12 bytes total) so I need to extract each digit as a byte (and then each byte back to a digit). It’s basic low level computer science stuff I never learned well. The old version of GLSL I’m using (1.2) doesn’t support bit shifting (which I think I could use) so I need to do this using arithmetic.

On Google I found a good method to have the compiler convert a word to an array of bytes using variant records such as:

type 
	TBytes = record
		case byte of
			0: (i : word);
			1: (u : array[0..3] of byte);
			2: (r, g, b, a: byte);
		end;


so for the number 1234:

var
 c: TBytes;
FillChar(c, sizeof(c), 0);
c.i := 300;
writeln(c.r, ' ', c.g, ' ', c.b, ' ', c.a);

I  get: 44 1 0 0

How do I convert this back to 300 using arithmetic? I see you can add 255 to first value plus the next value (1) to get 300 but what’s the correct method to use I could make into a function?


> On Nov 5, 2017, at 4:10 PM, Ryan Joseph <ryan at thealchemistguild.com> wrote:
> 
> I’m trying to pass a value between 1-1000 to an GLSL shader program which only supports floats with a precision of 1 decimal place so the plan is to pack the value into a vec4 (RGBA components between 0-1) which I could decode in the shader program.
> 
> For example if I want to encode the value 50 I do: 50/1000 = 0.050 and get a value with 3 decimal places which could fit into an RGB value,
> 
> but how can I extract the decimal places? I think it has something to do bit shifting but I wasn’t able to figure it out. The final result should be:
> 
> r = 0
> g = 5
> b = 0
> 
> 
> Regards,
> 	Ryan Joseph
> 
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Regards,
	Ryan Joseph




More information about the fpc-pascal mailing list