Saturday, 8 June 2013

HLSL weird floating point behavior?

HLSL weird floating point behavior?

I have a small ray tracer, I'm storing values in a 3d texture, when the ray goes through the next cube it checks the value from the 3d texture, then on returning this it generates a colour for that pixel, e.g.:
float hitCube = CastRay(eye.x, eye.y, eye.z, direction);

if (hitCube == 1.0)
    return float4(1,0,0,1);
else if (hitCube == 2.0)
    return float4(0,1,0,1);
else if (hitCube == 3.0)
    return float4(0,0,0,1);
else if (hitCube == 4.0)
    return float4(1,1,1,1);

return float4(0.5,0.5,0.5,1);
Or in other words:
1 = red
2 = green
3 = black
4 = white
Code is just for testing at the moment, my problem lies in CastRay, doing the following:
float id = tex3Dlod(MapSampler, float4(x/width,y/height,z/depth,0)).w * 255;

if(id == 100.0)
    return 1.0;
else if (id != 0.0)
    return id;
Creates the correct render as there's no block set at 100.0, everything is set to a number between 0 and 4:
However doing:
float id = tex3Dlod(MapSampler, float4(x/width,y/height,z/depth,0)).w * 255;

if(id == 1.0)
    return 1.0;
else if (id != 0.0)
    return id;
Creates the following:

somehow any hit block is passing the check, however as seen in the first picture they're not all set to red (1). This is confusing me because when I return the id I do the exact same check to see if it's 1.0 but this time it works properly.
Checking for it to equal 3.0 this time I get:

This is weird again, the red and green (1 and 2) blocks are correctly working but 3 and 4 are both passing the == 3.0 check, which I cannot explain.

No comments:

Post a Comment