Advertisement

PointLight troubles in directx11

Started by November 02, 2024 08:48 PM
1 comment, last by Sekt4nt 2 weeks, 5 days ago

When I implement point light, this occured, It is not the right way it should work, could someone help me pls ?


cbuffer CBuf : register(b0)
{
matrix transform;
};

struct VSOut
{
float4 color : COLOR;
float3 normal : NORMAL;
float3 worldPos : POSITION;
float4 pos : SV_Position;
};

VSOut main(float3 pos : POSITION, float4 color : COLOR, float3 normal : NORMAL)
{
VSOut vso;
vso.pos = mul(float4(pos, 1.0f), transform);
vso.worldPos = vso.pos;
vso.color = color;
vso.normal = normal;
return vso;
}

cbuffer AmbientLightBuffer : register(b0)
{
float4 ambientColor;
};

struct PointLight {
float3 Position;
float Range;
float3 Color;
float Intensity;
};

cbuffer LightBuffer : register(b1) {
PointLight pointLights[8];
int numPointLights;
float3 padding;
};

float4 main(float4 color : COLOR, float3 normal : NORMAL, float3 worldPos : POSITION) : SV_Target
{
float3 finalColor = color.rgb * ambientColor.rgb;

for (int i = 0; i < numPointLights; ++i)
{
float3 lightDir = pointLights[i].Position - worldPos;
float distance = length(lightDir);

if (distance < pointLights[i].Range)
{
lightDir = normalize(lightDir);

float diff = max(dot(normalize(normal), lightDir), 0.0f);

float attenuation = saturate(1.0f - (distance / pointLights[i].Range));
finalColor += diff * pointLights[i].Color * pointLights[i].Intensity * attenuation;
}
}

return float4(finalColor, color.a);
}

TVertexColor vertices[] = {
{{-1.0f,-1.0f,-1.0f}, {1.0f, 0.0f, 0.0f, 1.0f}, {-1.0f, 0.0f, 0.0f}},
{{1.0f,-1.0f,-1.0f}, {0.0f, 1.0f, 0.0f, 1.0f}, { 1.0f, 0.0f, 0.0f}},
{{-1.0f,1.0f,-1.0f}, {0.0f, 0.0f, 1.0f, 1.0f}, {-1.0f, 0.0f, 0.0f}},
{{1.0f,1.0f,-1.0f}, {1.0f, 1.0f, 0.0f, 1.0f}, { 1.0f, 0.0f, 0.0f}},
{{-1.0f,-1.0f,1.0f}, {1.0f, 0.0f, 1.0f, 1.0f}, {-1.0f, 0.0f, 0.0f}},
{{1.0f,-1.0f,1.0f}, {0.0f, 1.0f, 1.0f, 1.0f}, { 1.0f, 0.0f, 0.0f}},
{{-1.0f,1.0f,1.0f}, {0.0f, 1.0f, 1.0f, 1.0f}, {-1.0f, 0.0f, 0.0f}},
{{1.0f,1.0f,1.0f}, {0.0f, 1.0f, 1.0f, 1.0f}, { 1.0f, 0.0f, 0.0f}},
};

unsigned short indices[] = {
0,2,1, 2,3,1,
1,3,5, 3,7,5,
2,6,3, 3,6,7,
4,5,7, 4,7,6,
0,4,2, 2,4,6,
0,1,4, 1,5,4
};

TObjectManager* TObjectManager = renderDevice->GeTObjectManager();
TMesh* mesh = TObjectManager->AddMesh(vertices, sizeof(vertices) / sizeof(TVertexColor), indices, sizeof(indices) / sizeof(unsigned short), "romb");

//mesh->SetPosition(0.2f, 1.5f, 1.0f);

renderDevice->SetViewMatrix({ 5.0f, 5.0f, -5.0f, 1.0f }, { 0.0f, 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 0.0f });

TPointLight* light = TObjectManager->AddPointLight({ 2.0f, 2.0f, 2.0f }, { 1.0f, 0.8f, 0.6f }, 10.0f, 10.0f, "pointLight");

None

I solved the problem, my normals was incorrect

None

Advertisement