Sign in to follow this  

[Solved] Deferred Rendered Spotlight Problem

This topic is 1976 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I've been trying to implement a simple deferred shader with a spotlight.

The original forward rendered image looks like this:

[url="http://img35.imageshack.us/i/gbdlight0.jpg/"][img]http://img35.imageshack.us/img35/1221/gbdlight0.th.jpg[/img][/url]

But in my deferred shader, when I transfer the lighting code into the deferred shader, it looks like this:

[url="http://img818.imageshack.us/i/gbdlight1.jpg/"][img]http://img818.imageshack.us/img818/6056/gbdlight1.th.jpg[/img][/url]

Here is the normal and position buffers I use - both normalized:

[url="http://img833.imageshack.us/i/gbdlight2.jpg/"][img]http://img833.imageshack.us/img833/4922/gbdlight2.th.jpg[/img][/url]
[url="http://img411.imageshack.us/i/gbdlight3.jpg/"][img]http://img411.imageshack.us/img411/4745/gbdlight3.th.jpg[/img][/url]

The code for GBuffer.hlsl:

[CODE]
#pragma pack_matrix(row_major)
Texture2D Map : register(t0);

cbuffer Matrix : register(b0)
{
matrix world;
matrix view;
matrix proj;
};

SamplerState TextureSampler
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};

struct VS_IN
{
float4 pos : POSITION;
float3 normal : NORMAL;
float2 tex0 : TEXCOORD;
};

struct PS_IN
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD0;
float3 normal : TEXCOORD1;
float4 worldPos : TEXCOORD2;
};

PS_IN VS_Effect(VS_IN vertex)
{
PS_IN vsOut = ( PS_IN )0;
float4 posWorld = mul(vertex.pos, world);
float4 viewPos = mul(posWorld, view );
vsOut.pos = mul(viewPos, proj );
vsOut.tex0 = vertex.tex0;
vsOut.normal = normalize(mul(vertex.normal, (float3x3)world));
vsOut.worldPos = posWorld;
return vsOut;
}

float4 PS_Color(PS_IN input) : SV_TARGET
{
float4 color = float4(0,0,0,0);
float4 texMap = Map.Sample(TextureSampler, input.tex0);
color = texMap;
return color;
}

float4 PS_Normal(PS_IN input) : SV_TARGET
{
float4 normal = float4(input.normal, 1);
return normal;
}

float4 PS_WorldPos(PS_IN input) : SV_TARGET
{
float4 color = normalize(input.worldPos);
return color;
}
[/CODE]

The code for Combined.hlsl:

[CODE]
#pragma pack_matrix(row_major)

Texture2D ColorMap : register(t0);
Texture2D NormalMap : register(t1);
Texture2D WorldPosMap : register(t2);

cbuffer Light : register(b0)
{
float4 lightPos;
float4 lightDir;
float4 lightColor;
float lightRad;
float lightInt;
};

cbuffer Param : register(b1)
{
float4 camPos;
float4 diffColor;
};

SamplerState TextureSampler
{
Filter = MIN_MAG_MIP_POINT;
AddressU = Wrap;
AddressV = Wrap;
};

struct VS_IN
{
float4 pos : POSITION;
float2 tex0 : TEXCOORD;
};

struct PS_IN
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD;
};

PS_IN VS_Effect(uint id: SV_VertexID)
{
// form a full-screen triangle
float2 pos = float2(id == 1 ? 2 : 0, id == 2 ? 2 : 0);
PS_IN output;
output.pos = float4(pos.x * 2 - 1, 1 - pos.y * 2, 0, 1);
output.tex0 = pos.xy;
return output;
}

float4 PS_Effect(PS_IN input) : SV_TARGET
{
float4 color = {0,0,0,0};
float4 colorMap = ColorMap.Sample(TextureSampler, input.tex0);
float4 normal = normalize(NormalMap.Sample(TextureSampler, input.tex0));
float4 worldPos = WorldPosMap.Sample(TextureSampler, input.tex0);

float4 L = normalize(lightPos - worldPos);
float4 D = normalize(lightDir);
if(dot(-L, D) > 0.9f)
{
float4 N = normal;
float lambertTerm = max(dot(N,L), 0);

if(lambertTerm > 0)
{
color += colorMap*lightColor*diffColor*lambertTerm;
}
}
return color;
}
[/CODE]

What am I doing wrong? Edited by gboxentertainment

Share this post


Link to post
Share on other sites
Okay, I've figured out what the problem was.

I used my quad texture format for the textures that I rendered for normals and world position:

DXGI_FORMAT_R8G8B8A8_UNORM

This format clamped my values to [0,1].

I have changed the format to:

DXGI_FORMAT_R16G16B16A16_FLOAT

Which doesn't clamp my values. It uses twice the amount of memory (about 14mb additional for 1280x720 image), but I guess its a sacrifice I'll have to make. Unless someone can suggest a more efficient format? Or more efficient way of getting my normal and position buffers?

Share this post


Link to post
Share on other sites
Just map your normal components so they fit in (0,1) ... add 1, multiply by 0.5.

Then reverse the mapping when your reading the data.

Share this post


Link to post
Share on other sites
[quote name='phil_t' timestamp='1345406791' post='4971197']
Just map your normal components so they fit in (0,1) ... add 1, multiply by 0.5.

Then reverse the mapping when your reading the data.
[/quote]

Thank you - now I understand why my normals look different to a lot of other developer's normals.

Is it a similar thing for mapping my world coordinates? Except does it involve taking the maximum scene boundaries?

Share this post


Link to post
Share on other sites
Storing normals as XYZ in an 8-bit texture will give you pretty poor results, unless you do [url="http://sebh-blog.blogspot.com/2010/08/cryteks-best-fit-normals.html"]some magic[/url] to remap the normals. Even 10-bit formats generally won't give you enough precision for specular lighting. However there are various methods for [url="http://aras-p.info/texts/CompactNormalStorage.html"]packing normals[/url] that can give better results.

As for position...don't bother storing position in a texture. Just [url="http://mynameismjp.wordpress.com/2010/09/05/position-from-depth-3/"]reconstruct it from your depth buffer[/url]. It will give you better precision, and you won't have to waste another G-Buffer texture on it.

Share this post


Link to post
Share on other sites
Sign in to follow this