I'm still not a bit shifting pro so I gotta ask.
DWORD 0xFF000000
FF = Alpha Value = 255
How can I directly modify that byte?
Do I bitshift << 24 then set it with >> 24?
Printable View
I'm still not a bit shifting pro so I gotta ask.
DWORD 0xFF000000
FF = Alpha Value = 255
How can I directly modify that byte?
Do I bitshift << 24 then set it with >> 24?
Or better:Code:DWORD packed;
packed = alpha << 24;
alpha = packed >> 24;
Windows probably has some functions that do something like this.Code:#define LITTLE_ENDIAN // x86 = little endian
#ifdef LITTLE_ENDIAN
union Color {
DWORD dword;
unsigned char red,green,blue,alpha;
};
#else
union Color {
DWORD dword;
unsigned char alpha,blue,green,red;
};
#endif
HIWORD and LOWORD, and HIBYTE and LOBYTE, but they're not endian-aware.
I know this is a bump but I just cannot get it right...
DWORD dwAlpha = pVertices->Buffer()[i].diffuse;
dwAlpha = ((DWORD)(fAlpha * 200)) << iModifyDiffuseBit;
pVertices->Buffer()[i].diffuse = dwAlpha >> iModifyDiffuseBit;
The first line stores the DWORD diffuse into the dwAlpha.
The next calculates a new alpha, or depending on iModifyDiffuseBit the RGB value's.
The last line is suppose to replace the same bit on the diffuse now.
Given that the second line overrides the value assigned in the first, the first is useless.
Also, the shift in the second line and the one in the third line directly counteract each other, thus the effect is the same as if neither was there.
What is the result you expect, what do you actually get?
LoL, I cannot believe such basic mistakes.
I need to take the diffuse, modify a certain bit of it and replace the diffuse with the modifed one.
Example:
0xFFA0A0A0;
Modify and return
0xBBA0A0A0;
Ah.
OK, assuming you want the float alpha value fAlpha to replace the 0-255 alpha value in the highest byte of diffuse, you'd do:
The first line just scales the value up. (Note that this is not the best way, as it tends to undervalue just a bit.)Code:DWORD dwAlpha = static_cast<DWORD>(fAlpha * 255);
bla.diffuse = (0x00FFFFFF & bla.diffuse) | (dwAlpha << 24);
The first part of the second line blanks out the alpha value of the combined colour, the second part puts the new alpha at the right position and mixes it in.
If you want a different colour channel replaced, you must change mask and offset.
Changing offset is easy, how would one simply change the mask?
I do need to be able to pass in a diffuse and modify different values.
I know, it would be easy, write a large if then to determine the necessary mask.
Is there not a better way?
Edit:
Sometimes.... I'll just pass in the mask...