I am currently having issues with HLSL shaders. I am trying to implement point lights but I am getting unusual data when debugging the shader in Visual Studio.
I have read on several sites that say that the data stored in CBuffers should be aligned to 4 bytes, thus I added padding where it was needed (is this correct?).
When I set the data in c++, some of it comes through alright but other data is not. For example the color comes through alright but the range is being displayed as NaN in the VS graphics debugger.
Is there something wrong with the code below? (All structs are the same as those in c++ structs)
Please ask if there is anything else you would like to see code wise. Thanks!
PointLight Code:
float4 calcPointLight(PointLight pointLight, float3 normal, float3 worldPos0, float4 specularMap, float4 diffuseTex)
{
float3 lightDirection = worldPos0 - pointLight.pos;
float distanceToPoint = length(lightDirection);
// if (distanceToPoint > pointLight.range)
// return float4(0, 0, 0, 0);
lightDirection = normalize(lightDirection);
float4 color = calcLight(pointLight.base, lightDirection, normal, worldPos0, specularMap);
float attenuation = pointLight.atten[0] +
pointLight.atten[1] * distanceToPoint +
pointLight.atten[2] * distanceToPoint * distanceToPoint +
0.0001;
return color / attenuation;
}
float4 calcLight(BaseLight base, float3 direction, float3 normal, float3 worldPos0, float4 specularMap)
{
float diffuseFactor = (dot(normal, -direction));
float4 diffuseColor = float4(0, 0, 0, 0);
float4 specularColor = float4(0, 0, 0, 0);
if (diffuseFactor > 0.0f)
{
diffuseColor = base.color * base.intensity * diffuseFactor;
diffuseColor = saturate(diffuseColor);
float3 directionToEye = normalize(cameraPos - worldPos0);
float3 reflectDirection = normalize(reflect(direction, normal));
float specularFactor = dot(directionToEye, reflectDirection);
specularFactor = pow(abs(specularFactor), specularPower);
if (specularFactor > 0)
{
specularColor = base.color * 1.0f * specularFactor;
}
if (specularMap.a > 0.0f)
specularColor = specularColor * specularMap;
}
return saturate(ambient + diffuseColor + specularColor);
}
Structs
struct BaseLight
{
float4 color;
float intensity;
float3 padding3; //TEST
};
struct DirectionalLight
{
BaseLight base;
float3 direction;
};
struct PointLight
{
BaseLight base;
float3 atten;
float padding;
float3 pos;
float range;
};
CBuffers
cbuffer MatrixBuffer : register(b0)
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};
cbuffer LightingBuffer : register(b1)
{
float4 ambient;
float specularPower;
float3 padding3;
PointLight pointLight;
//DirectionalLight light;
};
cbuffer CameraBuffer : register(b2)
{
float4 cameraPos;
};