Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


#Actualrozz666

Posted 14 March 2013 - 02:20 PM

One way to simplify this kind of process is to use a union to represent a color value:
 
union uColor {
unint32_t u32;
struct {
unsigned char alpha;
unsigned char green;
unsigned char blue;
unsigned char red;
};
};
You can create a color:
 
uColor col;
 
then reference the uint value:
 
col.u32
 
or a specific channel:
 
col.red
 
You cast an array of texels as uColor* then use the union to easily access the channels without trying to work with two index scales between uint and uchar.

Actually, that's undefined behaviour. You are not allowed to read from a member that wasn't written to directly. Also, casting a pointer to unsigned char to a structure is also undefined behaviour, since there is no guarantee by C++ that such cast is valid (e.g. due to alignment).


#1rozz666

Posted 14 March 2013 - 02:17 PM

One way to simplify this kind of process is to use a union to represent a color value:
 
union uColor {
unint32_t u32;
struct {
unsigned char alpha;
unsigned char green;
unsigned char blue;
unsigned char red;
};
};
You can create a color:
 
uColor col;
 
then reference the uint value:
 
col.u32
 
or a specific channel:
 
col.red
 
You cast an array of texels as uColor* then use the union to easily access the channels without trying to work with two index scales between uint and uchar.

Actually, that's undefined behaviour. You are not allowed to read from a member that wasn't written to directly. Also, casting a pointer to unsigned char to a structure is also undefined behaviour, since there is no guarantee by C++, this such cast is valid (e.g. due to alignment).


PARTNERS