Hello everyone,
i know this topic might pop up a lot on the internet, but i cant quite find a solution that works for me... So i hope someone here can help me out or point a finger at where i am goind horribly wrong...
The basic idea is to calculate the world position of a fragment in the pixel shader from the depth value.
So this is my vertex shader:
Display All
Nothing fancy, mostly passing values trough to the pixel shader...
Here comes the pixel shader:
Display All
I checked that the inverse matrix i am using is correct by writing it down and calculating it by hand and then checking the inverse matrix the computer computed So i am quite sure the matrix is correct... The depth value i am using is stored as z/w.
But in the end, the values i get (i return them as color to check if they make any sense) dont seem to be right...
After more than a day on this problem i am getting a bit desperate but i cant find any more sources to go trough to find out where i am wrong, so my hope is on you guys in this forum. I am sure that its something realy stupid that i am missing but i cant figure it out... Please help me!
i know this topic might pop up a lot on the internet, but i cant quite find a solution that works for me... So i hope someone here can help me out or point a finger at where i am goind horribly wrong...
The basic idea is to calculate the world position of a fragment in the pixel shader from the depth value.
So this is my vertex shader:
Source Code
- cbuffer MatrixBuffer
- {
- matrix worldMatrix;
- matrix viewMatrix;
- matrix projectionMatrix;
- matrix inverseProjection; //This is the inverse of the matrix that is a concatenation of the view and projection matrix.
- };
- cbuffer LightPositionBuffer
- {
- float4 lightPosition;
- };
- struct VertexInputType
- {
- float3 position : POSITION;
- float4 color : COLOR;
- float3 normal : NORMAL;
- float2 tex : TEXCOORD0;
- };
- struct PixelInputType
- {
- float4 position : SV_POSITION;
- float4 iP1 : TEXCOORD1;
- float4 iP2 : TEXCOORD2;
- float4 iP3 : TEXCOORD3;
- float4 iP4 : TEXCOORD4;
- float4 lightPos : POSITION0;
- };
- PixelInputType PointLightVertexShader(VertexInputType input)
- {
- PixelInputType output;
- float4 localPosition = input.position.xyzx;
- localPosition.w = 1.0f;
- output.position = mul(localPosition, worldMatrix);
- output.position = mul(output.position, viewMatrix);
- output.position = mul(output.position, projectionMatrix);
- //i pass the matrix as 4 float4 to avoid problems
- output.iP1 = inverseProjection[0];
- output.iP2 = inverseProjection[1];
- output.iP3 = inverseProjection[2];
- output.iP4 = inverseProjection[3];
- float4 lPos = float4(lightPosition.xyz, 1.0f);
- output.lightPos = mul(lPos, worldMatrix);
- return output;
- }
Nothing fancy, mostly passing values trough to the pixel shader...
Here comes the pixel shader:
Source Code
- Texture2D normalTexture : register(t0);
- Texture2D depthTexture : register(t1);
- SamplerState SampleTypePoint : register(s0);
- cbuffer LightBuffer
- {
- float4 diffuseColorAndRange;
- };
- struct PixelInputType
- {
- float4 position : SV_POSITION;
- float4 iP1 : TEXCOORD1;
- float4 iP2 : TEXCOORD2;
- float4 iP3 : TEXCOORD3;
- float4 iP4 : TEXCOORD4;
- float4 lightPos : POSITION0;
- };
- float3 PointLightPixelShader(PixelInputType input) : SV_TARGET
- {
- float4x4 inPM = float4x4( input.iP1.x, input.iP1.y, input.iP1.z, input.iP1.w,
- input.iP2.x, input.iP2.y, input.iP2.z, input.iP2.w,
- input.iP3.x, input.iP3.y, input.iP3.z, input.iP3.w,
- input.iP4.x, input.iP4.y, input.iP4.z, input.iP4.w);
- //Those are only here because i wanted to be sure to have the right values.
- float screenWidth = 1024.0f;
- float screenHeight = 768.0f;
- //Calculating screen coordinates from 0.0 to 1.0 for both direction.s upper left corner is (0.0, 0.0)
- float2 tCoord = float2 ( input.position.x / screenWidth, input.position.y / screenHeight );
- //The depth value is fetched from the depth texture.
- float4 fragDepth = depthTexture.Sample(SampleTypePoint, tCoord);
- //And for later use the normal, too...
- float4 fragNormal = normalTexture.Sample(SampleTypePoint, tCoord);
- //im not quite sure if this makes sense, it brings the values from 0.0-1.0 into -1.0-1.0 range.. i am desperate so i tried it...
- float3 pos = float3 ( tCoord.x * 2.0 - 1.0, tCoord.y * 2.0 - 1.0, fragDepth.x );
- //The position is then multiplied with the inversed projection * view matrix. I also tried to switch the order in the mul...
- float4 clip = mul(inPM, float4(pos, 1.0));
- //After division by w, this should be the world coordinate. But it is not... :/
- pos = clip.xyz / clip.w;
- //The values dont seem to relate to anything...
- return pos.xyz;
- }
I checked that the inverse matrix i am using is correct by writing it down and calculating it by hand and then checking the inverse matrix the computer computed So i am quite sure the matrix is correct... The depth value i am using is stored as z/w.
But in the end, the values i get (i return them as color to check if they make any sense) dont seem to be right...
After more than a day on this problem i am getting a bit desperate but i cant find any more sources to go trough to find out where i am wrong, so my hope is on you guys in this forum. I am sure that its something realy stupid that i am missing but i cant figure it out... Please help me!
The post was edited 2 times, last by sebastianp ().