Game Development Reference
In-Depth Information
// Return 1 if bit at zero-based index is 1 in the bit field;
// otherwise return 0.
float checkBit( float bitField, float bitIndex)
float bitValue = pow(2,bitIndex);
bitField = fmod(bitField,bitValue*2);
return (bitField - bitValue) >= 0.0f ? 1.0f : 0.0f;
Listing 3.1. Code for the checkBit() function.
Bit-field check using floating-point mathematics. The damage bit-field value is
stored on the CPU as an integer variable. However, current-generation shader
models do not support useful integer operations, so we have to do the work with
floating-point mathematics. First, store the bit-field value in a vertex shader
constant. We store the value “logically.” That is, if our bit-field is 010110, then
this translates to 22 in decimal, so we set the constant value to 22.0f for the
vertex shader. Then, in the vertex shade,r we can pass this value to the function
checkBit() in Listing 3.1.
Listing 3.2 shows an example of how to use the checkBit() function to detect
whether the vertex belongs to a damaged area or not.
It is important to note that using floating-point mathematics to do the bit
check cannot benefit from all bits available in the floating-point variable. If we
were doing integer mathematics, then a standard 32-bit integer can represent 32
extern float damageBitField;
// Check whether this vertex belongs to a "damaged" area.
// Damage ID is compressed with vertex normal.
float damageID = round(length(In.normal))-1;
if (damageID > 0)
float damaged = checkBit(damageBitField , damageID-1);
In.position *= 1.0f-damaged; // Collapse vertex if damaged
Listing 3.2. Sample code of a vertex shader that supports damage.
Search Nedrilad ::

Custom Search