PDA

View Full Version : glColor4b with integers.



Registrar
02-11-2011, 05:23 PM
Is there a way to use something like a union to convert from an int to the 4 bytes that glColor4b accepts? I have been experimenting with things like this to acheive this but have had no success as I don't know how to make the 4 bytes of an int into the right values.


union {
int c;
byte b[4];
} cdat;

cdat.c = ???;
glColor4bv(cdat.b);

Dark Photon
02-11-2011, 06:23 PM
union {
int c;
byte b[4];
} cdat;

cdat.c = ???;
glColor4bv(cdat.b);
I'm pretty sure you don't want signed bytes (-128..127), which is what 4b will give you. You want unsigned bytes (0..255), which is glColor4ub*. Also change "byte b[4]" to "ubyte b[4]" or "unsigned char b[4]" or "uint8_t b[4]" (if you #include stdint.h).

Also, why the union? Just poke components into the ubyte array directly. The int member packing isn't portable anyway, as the byte order of the int depends on the machine (little or big endian).

Registrar
02-11-2011, 06:31 PM
I use the union because I don't know how to put the components in directly. Thanks for your answer.

V-man
02-12-2011, 01:23 PM
You should declare that as
uint c;

and for setting up
c = 0xAABBGGRR;

Or perhaps I didn't understand your problem.
A union doesn't convert a value. A union is the exact same memory location.