Sign in to follow this  
ekba89

Deferred Shading Problem

Recommended Posts

I downloaded deferred shading tutorial in XNA from riemers site (here is the [url="http://www.riemers.net/eng/recipes30.php"]link[/url]). Now i'm trying to implement it in c++. My problem is light changes shapes, disappears when i change my camera position. I looked normal map, color map and depth map. They are correct. Also I'm using exact same shaders from tutorial so i don't think there should be a problem. Here is the code i use and i added some pictures how it looks. I really appreciate any help i'm frustrated with this problem right now.

I'm creating depth, color and normal maps here.
[code]
void DrawShader1()
{
D3DXMATRIX mat;
D3DXMatrixIdentity(&mat);

LPDIRECT3DSURFACE9 deviceSurface;
device->GetRenderTarget(0, &deviceSurface);
device->SetRenderTarget(0, colorSurface);
device->SetRenderTarget(1, normalSurface);
device->SetRenderTarget(2, depthSurface);
device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

shader1->SetTechnique("MultipleTargets");
shader1->SetMatrix("xWorld", &mat);
shader1->SetMatrix("xView", &viewMatrix);
shader1->SetMatrix("xProjection", &projectionMatrix);
shader1->SetTexture("xTexture", texture);

shader1->Begin(NULL, 0);
shader1->BeginPass(0);
device->SetVertexDeclaration(vertexDeclaration);
device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, vertices, 8*sizeof(float));
shader1->EndPass();
shader1->End();

device->SetRenderTarget(0, deviceSurface);
device->SetRenderTarget(1, NULL);
device->SetRenderTarget(2, NULL);
deviceSurface->Release();
}
[/code]

Shading map code here.
[code]
void DrawShader2()
{
LPDIRECT3DSURFACE9 deviceSurface;
device->GetRenderTarget(0, &deviceSurface);
device->SetRenderTarget(0, shadingSurface);
device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
device->SetRenderState(D3DRS_SRCBLENDALPHA, D3DBLEND_ONE);
device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

shader2->SetTechnique("DeferredSpotLight");
shader2->SetTexture("xNormalMap", normalTexture);
shader2->SetTexture("xDepthMap", depthTexture);
shader2->SetFloat("xConeAngle", light.ConeAngle);
shader2->SetFloat("xConeDecay", light.ConeDecay);
shader2->SetFloatArray("xLightPosition", (float *)&light.Position, 3);
shader2->SetFloatArray("xConeDirection", (float *)&light.Direction, 3);
shader2->SetFloat("xLightStrength", light.Strength);

D3DXMATRIX mat;
D3DXMatrixInverse(&mat, NULL, &(viewMatrix * projectionMatrix));
shader2->SetMatrix("xViewProjectionInv", &mat);

shader2->Begin(NULL, 0);
shader2->BeginPass(0);
device->SetVertexDeclaration(finalVertexDeclaration);
device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, finalVertices, 5*sizeof(float));
shader2->EndPass();
shader2->End();

device->SetRenderTarget(0, deviceSurface);
deviceSurface->Release();

device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
}
[/code]

And combine them here
[code]
void DrawShader3()
{
shader3->SetTechnique("CombineColorAndShading");
shader3->SetTexture("xColorMap", colorTexture);
shader3->SetTexture("xShadingMap", shadingTexture);
shader3->SetFloat("xAmbient", 0.4f);

device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

shader3->Begin(NULL, 0);
shader3->BeginPass(0);
device->SetVertexDeclaration(finalVertexDeclaration);
device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, finalVertices, 5*sizeof(float));
shader3->EndPass();
shader3->End();
}
[/code]

Thanks for replies.

Share this post


Link to post
Share on other sites
There doesnt appear to be anything wrong with your code, so the issue likely lies somewhere else. Try to narrow the problem area down. Try other things like moving the light around to see where it stops drawing, then it will give you an idea of what is happening. Perhaps the light moves behind the wall, or the light moves outside the cameras view frustum... it is difficult to determine based on the information.

Share this post


Link to post
Share on other sites
Thanks for reply. Actually i was having problem to narrow so i added all my code. First i tried to integrate it with my game and i had the same problem so i wrote the from the beginning but that doesn't change the problem. By the way as you said i tried changing light positions as you said and here is what i found. I was creating only one light as below.
[code]
light.Direction = D3DXVECTOR3(0, -1, 0);
light.Position = D3DXVECTOR3(150, 0.2f, 150);
light.ConeDecay = 1.0f;
light.ConeAngle = 0.7f;
light.Strength = 0.4f;
[/code]

By the way my ground is between (0, 0) (200, 200). So i tried changing position in the code above. Until i make x and z 400 nothing changed. When i make them 400 i see a little bit of light. But if i move my camera a little bit higher (45 in the y axis) i start to see the light in its default position. And light's shape is strange it is not perfect circle. It changes shape when i increase camera y position. First it becomes like upside down 'V' then it becomes rectangle and it disappears :S. I really appreciate any help i'm trying to solve what is wrong for 2 days. So far i couldn't find anything. By the way i added all the code on c++ side and the shader code that im creating shading map below.

[code]
#include <Windows.h>
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

#include <d3d9.h>
#include <d3dx9.h>

struct VertexFormat
{
D3DXVECTOR3 Position;
D3DXVECTOR3 Normal;
D3DXVECTOR2 TextureCoords;
};

struct FinalVertexFormat
{
D3DXVECTOR3 Position;
D3DXVECTOR2 TextureCoords;
};

struct Light
{
D3DXVECTOR3 Position;
float Strength;
D3DXVECTOR3 Direction;
float ConeAngle;
float ConeDecay;
};

int appRunning;
HWND hWnd;
LPDIRECT3DDEVICE9 device;

LPDIRECT3DTEXTURE9 texture;

LPDIRECT3DTEXTURE9 colorTexture;
LPDIRECT3DTEXTURE9 normalTexture;
LPDIRECT3DTEXTURE9 depthTexture;
LPDIRECT3DTEXTURE9 shadingTexture;

LPDIRECT3DSURFACE9 colorSurface;
LPDIRECT3DSURFACE9 normalSurface;
LPDIRECT3DSURFACE9 depthSurface;
LPDIRECT3DSURFACE9 shadingSurface;

LPDIRECT3DVERTEXDECLARATION9 vertexDeclaration;
LPDIRECT3DVERTEXDECLARATION9 finalVertexDeclaration;

D3DXVECTOR3 cameraPosition;
D3DXMATRIX viewMatrix;
D3DXMATRIX projectionMatrix;

VertexFormat *vertices;
FinalVertexFormat *finalVertices;

LPD3DXEFFECT shader1;
LPD3DXEFFECT shader2;
LPD3DXEFFECT shader3;

Light light;

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if(message == WM_DESTROY)
appRunning = 0;

if(message == WM_KEYDOWN)
{
switch(wParam)
{
case VK_UP:
cameraPosition.z += 1;
D3DXMatrixLookAtLH(&viewMatrix, &cameraPosition, &D3DXVECTOR3(100, 0, 100), &D3DXVECTOR3(0, 1, 0));
break;
case VK_DOWN:
cameraPosition.z -= 1;
D3DXMatrixLookAtLH(&viewMatrix, &cameraPosition, &D3DXVECTOR3(100, 0, 100), &D3DXVECTOR3(0, 1, 0));
break;
case VK_LEFT:
cameraPosition.x -= 1;
D3DXMatrixLookAtLH(&viewMatrix, &cameraPosition, &D3DXVECTOR3(100, 0, 100), &D3DXVECTOR3(0, 1, 0));
break;
case VK_RIGHT:
cameraPosition.x += 1;
D3DXMatrixLookAtLH(&viewMatrix, &cameraPosition, &D3DXVECTOR3(100, 0, 100), &D3DXVECTOR3(0, 1, 0));
break;
case VK_F1:
cameraPosition.y += 1;
D3DXMatrixLookAtLH(&viewMatrix, &cameraPosition, &D3DXVECTOR3(100, 0, 100), &D3DXVECTOR3(0, 1, 0));
break;
case VK_F2:
cameraPosition.y -= 1;
D3DXMatrixLookAtLH(&viewMatrix, &cameraPosition, &D3DXVECTOR3(100, 0, 100), &D3DXVECTOR3(0, 1, 0));
break;
case VK_F3:
light.Position.x += 1;
break;
case VK_F4:
light.Position.x -= 1;
break;
case VK_F5:
light.Position.z += 1;
break;
case VK_F6:
light.Position.z -= 1;
break;
}
}

return DefWindowProc(hWnd, message, wParam, lParam);
}

void SetupVariables()
{
D3DXCreateTexture(device, 640, 480, 0, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &colorTexture);
D3DXCreateTexture(device, 640, 480, 0, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &depthTexture);
D3DXCreateTexture(device, 640, 480, 0, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &normalTexture);
D3DXCreateTexture(device, 640, 480, 0, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &shadingTexture);

colorTexture->GetSurfaceLevel(0, &colorSurface);
depthTexture->GetSurfaceLevel(0, &depthSurface);
normalTexture->GetSurfaceLevel(0, &normalSurface);
shadingTexture->GetSurfaceLevel(0, &shadingSurface);

D3DVERTEXELEMENT9 vertexElements[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 3*sizeof(float), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
{0, 6*sizeof(float), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
D3DDECL_END(),
};
device->CreateVertexDeclaration(vertexElements, &vertexDeclaration);

D3DVERTEXELEMENT9 vertexElements2[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 3*sizeof(float), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
D3DDECL_END(),
};
device->CreateVertexDeclaration(vertexElements2, &finalVertexDeclaration);

vertices = new VertexFormat[6];
vertices[0].Position = D3DXVECTOR3(0, 0, 0);
vertices[0].Normal = D3DXVECTOR3(0, 1, 0);
vertices[0].TextureCoords = D3DXVECTOR2(0, 1);
vertices[1].Position = D3DXVECTOR3(0, 0, 200);
vertices[1].Normal = D3DXVECTOR3(0, 1, 0);
vertices[1].TextureCoords = D3DXVECTOR2(0, 0);
vertices[2].Position = D3DXVECTOR3(200, 0, 0);
vertices[2].Normal = D3DXVECTOR3(0, 1, 0);
vertices[2].TextureCoords = D3DXVECTOR2(1, 1);

vertices[3].Position = D3DXVECTOR3(200, 0, 0);
vertices[3].Normal = D3DXVECTOR3(0, 1, 0);
vertices[3].TextureCoords = D3DXVECTOR2(1, 1);
vertices[4].Position = D3DXVECTOR3(0, 0, 200);
vertices[4].Normal = D3DXVECTOR3(0, 1, 0);
vertices[4].TextureCoords = D3DXVECTOR2(0, 0);
vertices[5].Position = D3DXVECTOR3(200, 0, 200);
vertices[5].Normal = D3DXVECTOR3(0, 1, 0);
vertices[5].TextureCoords = D3DXVECTOR2(1, 0);

finalVertices = new FinalVertexFormat[4];
finalVertices[0].Position = D3DXVECTOR3(-1, 1, 0);
finalVertices[0].TextureCoords = D3DXVECTOR2(0, 0);
finalVertices[1].Position = D3DXVECTOR3(1, 1, 0);
finalVertices[1].TextureCoords = D3DXVECTOR2(1, 0);
finalVertices[2].Position = D3DXVECTOR3(-1, -1, 0);
finalVertices[2].TextureCoords = D3DXVECTOR2(0, 1);
finalVertices[3].Position = D3DXVECTOR3(1, -1, 0);
finalVertices[3].TextureCoords = D3DXVECTOR2(1, 1);

D3DXCreateEffectFromFile(device, "Shader1.fx", NULL, NULL, 0, NULL, &shader1, NULL);
D3DXCreateEffectFromFile(device, "Shader2.fx", NULL, NULL, 0, NULL, &shader2, NULL);
D3DXCreateEffectFromFile(device, "Shader3.fx", NULL, NULL, 0, NULL, &shader3, NULL);

D3DXCreateTextureFromFile(device, "wall.dds", &texture);

light.Direction = D3DXVECTOR3(0, -1, 0);
light.Position = D3DXVECTOR3(150, 0.2f, 150);
light.ConeDecay = 1.0f;
light.ConeAngle = 0.7f;
light.Strength = 0.4f;
}

void CreateDevice()
{
LPDIRECT3D9 dx = Direct3DCreate9(D3D9b_SDK_VERSION);

D3DPRESENT_PARAMETERS parameters;
ZeroMemory(&parameters, sizeof(D3DPRESENT_PARAMETERS));
parameters.AutoDepthStencilFormat = D3DFMT_D16;
parameters.EnableAutoDepthStencil = TRUE;
parameters.BackBufferWidth = 640;
parameters.BackBufferHeight = 480;
parameters.BackBufferFormat = D3DFMT_X8R8G8B8;
parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
parameters.Windowed = true;
parameters.hDeviceWindow = hWnd;

dx->CreateDevice(0, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &parameters, &device);

device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
device->SetRenderState(D3DRS_LIGHTING, FALSE);

cameraPosition = D3DXVECTOR3(0, 20, -10);
D3DXMatrixLookAtLH(&viewMatrix, &cameraPosition, &D3DXVECTOR3(100, 0, 100), &D3DXVECTOR3(0, 1, 0));
D3DXMatrixPerspectiveFovLH(&projectionMatrix, D3DX_PI/4, 640.0f/480.0f, 0.1f, 1000.0f);
}

void DrawShader1()
{
D3DXMATRIX mat;
D3DXMatrixIdentity(&mat);

LPDIRECT3DSURFACE9 deviceSurface;
device->GetRenderTarget(0, &deviceSurface);
device->SetRenderTarget(0, colorSurface);
device->SetRenderTarget(1, normalSurface);
device->SetRenderTarget(2, depthSurface);
device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

shader1->SetTechnique("MultipleTargets");
shader1->SetMatrix("xWorld", &mat);
shader1->SetMatrix("xView", &viewMatrix);
shader1->SetMatrix("xProjection", &projectionMatrix);
shader1->SetTexture("xTexture", texture);

shader1->Begin(NULL, 0);
shader1->BeginPass(0);
device->SetVertexDeclaration(vertexDeclaration);
device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, vertices, 8*sizeof(float));
shader1->EndPass();
shader1->End();

device->SetRenderTarget(0, deviceSurface);
device->SetRenderTarget(1, NULL);
device->SetRenderTarget(2, NULL);
deviceSurface->Release();
}

void DrawShader2()
{
LPDIRECT3DSURFACE9 deviceSurface;
device->GetRenderTarget(0, &deviceSurface);
device->SetRenderTarget(0, shadingSurface);
device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
device->SetRenderState(D3DRS_SRCBLENDALPHA, D3DBLEND_ONE);
device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

shader2->SetTechnique("DeferredSpotLight");
shader2->SetTexture("xNormalMap", normalTexture);
shader2->SetTexture("xDepthMap", depthTexture);
shader2->SetFloat("xConeAngle", light.ConeAngle);
shader2->SetFloat("xConeDecay", light.ConeDecay);
shader2->SetFloatArray("xLightPosition", (float *)&light.Position, 3);
shader2->SetFloatArray("xConeDirection", (float *)&light.Direction, 3);
shader2->SetFloat("xLightStrength", light.Strength);

D3DXMATRIX mat;
D3DXMatrixInverse(&mat, NULL, &(viewMatrix * projectionMatrix));
shader2->SetMatrix("xViewProjectionInv", &mat);

shader2->Begin(NULL, 0);
shader2->BeginPass(0);
device->SetVertexDeclaration(finalVertexDeclaration);
device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, finalVertices, 5*sizeof(float));
shader2->EndPass();
shader2->End();

device->SetRenderTarget(0, deviceSurface);
deviceSurface->Release();

device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
}

void DrawShader3()
{
shader3->SetTechnique("CombineColorAndShading");
shader3->SetTexture("xColorMap", colorTexture);
shader3->SetTexture("xShadingMap", shadingTexture);
shader3->SetFloat("xAmbient", 0.4f);

device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

shader3->Begin(NULL, 0);
shader3->BeginPass(0);
device->SetVertexDeclaration(finalVertexDeclaration);
device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, finalVertices, 5*sizeof(float));
shader3->EndPass();
shader3->End();
}

void Draw()
{
device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
device->BeginScene();

DrawShader1();
DrawShader2();
DrawShader3();

device->EndScene();
device->Present(NULL, NULL, NULL, NULL);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int nCmdShow)
{
WNDCLASSEX wndClass;
ZeroMemory(&wndClass, sizeof(WNDCLASSEX));
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.hInstance = hInstance;
wndClass.style = CS_VREDRAW | CS_HREDRAW;
wndClass.lpszClassName = "WindowClass";
wndClass.lpfnWndProc = WindowProc;

RegisterClassEx(&wndClass);

hWnd = CreateWindowEx(WS_EX_CONTROLPARENT, "WindowClass", "DeferredShading", WS_VISIBLE | WS_OVERLAPPEDWINDOW, 10, 10, 640, 480, NULL, NULL, hInstance, NULL);

CreateDevice();
SetupVariables();

appRunning = 1;
MSG message;
while(appRunning)
{
if(PeekMessage(&message, hWnd, 0, 0, PM_REMOVE))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
Draw();
}

DestroyWindow(hWnd);
return 0;
}
[/code]

[code]
float4x4 xViewProjectionInv;

float xLightStrength;
float3 xLightPosition;
float3 xConeDirection;
float xConeAngle;
float xConeDecay;

Texture xNormalMap;
sampler NormalMapSampler = sampler_state { texture = <xNormalMap> ; magfilter = POINT; minfilter = POINT; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};
Texture xDepthMap;
sampler DepthMapSampler = sampler_state { texture = <xDepthMap> ; magfilter = POINT; minfilter = POINT; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};

struct VertexToPixel
{
float4 Position : POSITION;
float2 TexCoord : TEXCOORD0;
};

struct PixelToFrame
{
float4 Color : COLOR0;
};

VertexToPixel MyVertexShader(float4 inPos: POSITION0, float2 texCoord: TEXCOORD0)
{
VertexToPixel Output = (VertexToPixel)0;

Output.Position = inPos;
Output.TexCoord = texCoord;

return Output;
}

PixelToFrame MyPixelShader(VertexToPixel PSIn) : COLOR0
{
PixelToFrame Output = (PixelToFrame)0;

float3 normal = tex2D(NormalMapSampler, PSIn.TexCoord).rgb;
normal = normal*2.0f-1.0f;
normal = normalize(normal);

float depth = tex2D(DepthMapSampler, PSIn.TexCoord).r;

float4 screenPos;
screenPos.x = PSIn.TexCoord.x*2.0f-1.0f;
screenPos.y = -(PSIn.TexCoord.y*2.0f-1.0f);
screenPos.z = depth;
screenPos.w = 1.0f;

float4 worldPos = mul(screenPos, xViewProjectionInv);
worldPos /= worldPos.w;

float3 lightDir = float3(0.0f, -1.0f, 0.0f);
float3 lightPos = float3(100.0f, 0.5f, 100.0f);

float3 lightDirection = normalize(worldPos - xLightPosition);
float coneDot = dot(lightDirection, normalize(xConeDirection));
bool coneCondition = coneDot >= xConeAngle;

float shading = 0;
if (coneCondition)
{
float coneAttenuation = pow(coneDot, xConeDecay);

shading = dot(normal, -lightDirection);
shading *= xLightStrength;
shading *= coneAttenuation;
}

Output.Color.rgb = shading;

return Output;
}

technique DeferredSpotLight
{
pass Pass0
{
VertexShader = compile vs_2_0 MyVertexShader();
PixelShader = compile ps_2_0 MyPixelShader();
}
}

[/code]

Share this post


Link to post
Share on other sites
This is getting stranger and stranger. I looked the variables i send to shader and they are correct. Then i create a release version. And when i start the release version from the .exe (not from visual studio) the result was different. It wasn't correct but it isn't the same result when i start my app from visual studio. I'm really getting frustrated so even any little advise would be appreciated and if someone is willing to try my code i can send other shader codes and also you can find shader codes in the link i mentioned in my first post.

Share this post


Link to post
Share on other sites
I notice your lights position has a y value of .2, the wall you are drawing has a y value of 0. So, your light is only .2 above the wall, which could cause some issues in actually seeing the light since it is so close. Other than that, your code looks like it should work.

I recommend changing this code here so you can see the entire area where your light is

float shading = 0;
if (coneCondition)
{
Output.Color.rgb = float3(1, 0. 0);// this should show the lights area as a solid red so you can see where it is at all times
} else {

Output.Color.rgb = shading;
}

Share this post


Link to post
Share on other sites
Thanks for the reply. I try to change lights y position between -10 to 10 it changed nothing. Even with negative values it behaves same. I'm really frustrated right now since i'm trying to solve what is wrong with the code for two days. Shaders works with the code in the XNA example so i think there should be something wrong with my code but i cannot find it. My normal color and depth maps seem correct. So i think there is something wrong in the code where i create shading map. By the way thanks for the reply again i really appreciate even any little advise.

Share this post


Link to post
Share on other sites
It looks like a run-of-the-mill wrong space issue to me. What you have here is actually already the view position, as long as you have your depth buffer as linear. (As is standard for us at least, I don't post much. :P)
So to get your world position you only need to multiply it with the to view inverse, not the view projection inverse.

Whenever your lights move when your camera moves, you're generally working in the wrong space.

I personally do everything in view space, but that's just a matter of personal taste, you should be able to do it in world space too.




[color="#000000"]float4 screenPos[/color][color="#666600"]; // Actually view position![/color][color="#000000"]
screenPos[/color][color="#666600"].[/color][color="#000000"]x [/color][color="#666600"]=[/color] [color="#660066"]PSIn[/color][color="#666600"].[/color][color="#660066"]TexCoord[/color][color="#666600"].[/color][color="#000000"]x[/color][color="#666600"]*[/color][color="#006666"]2.0f[/color][color="#666600"]-[/color][color="#006666"]1.0f[/color][color="#666600"];[/color][color="#000000"]
screenPos[/color][color="#666600"].[/color][color="#000000"]y [/color][color="#666600"]=[/color] [color="#666600"]-([/color][color="#660066"]PSIn[/color][color="#666600"].[/color][color="#660066"]TexCoord[/color][color="#666600"].[/color][color="#000000"]y[/color][color="#666600"]*[/color][color="#006666"]2.0f[/color][color="#666600"]-[/color][color="#006666"]1.0f[/color][color="#666600"]);[/color][color="#000000"]
screenPos[/color][color="#666600"].[/color][color="#000000"]z [/color][color="#666600"]=[/color][color="#000000"] depth;[/color]
[color="#000000"]float4 worldPos = mul(screenPos, toViewInverse);[/color]


Otherwise it looks allright, from what I see from a quick glance. Not sure about the spotlight attenuation calculations but I haven't done that in ages. x)

Share this post


Link to post
Share on other sites
Thanks for reply i tried your solution but it wasn't working either. And i think multiplying with view projection inverse is true because im getting screen position by multiplying world position with view * projection. Besides as i mentioned i take shaders from the link in my first post. It was from an XNA example and it works with XNA. So i think there is something wrong with my code but i can't find it. Is there a tool to debug shader and look at variables in shader?

Share this post


Link to post
Share on other sites
ok i solved my problem. I was creating depth texture like
[code]D3DXCreateTexture(device, 640, 480, 0, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &depthTexture);[/code]
i changed it to
[code]D3DXCreateTexture(device, 640, 480, 0, D3DUSAGE_RENDERTARGET, D3DFMT_R32F, D3DPOOL_DEFAULT, &depthTexture);[/code]

and now it works correct. It is a silly mistake but if anyone curious . And thanks again for all the replies and sharing pain with me :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this