Jump to content
  • Advertisement
Sign in to follow this  
MohammadAhmed

Screen space , World space issue ?

This topic is 2348 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 every one .

1:why when the writer in his tutorial always do this in HLSL shader file;

position.xyz /=position.w;

or

depth.xy/=depth.y; where ( y == w == 1 )

why he do this while w is always 1 for points ????????????????????????

Sure I am taking position from model so position represented as [ X Y Z 1] (where 1 == w)

so why he divided it always on w??

----------------

2: why the writer do this when he want to take depth and store it in render target (texture) for further usage
he do this at the last step :

output.depth= input.Depth.x / Input.Depth.y ; where (input.Depth=vertexposition.zw) ?????? Edited by mrmohadnan

Share this post


Link to post
Share on other sites
Advertisement
Look up the terms perspective divide and homogeneous coordinates.

Also, post code for the shaders please. It is hard to imagine the full context.


Thank you.

Share this post


Link to post
Share on other sites
yeah Hornsj3 ok what is the point of using perspective divide as it's always 1 ??

here is the code :


float4x4 WorldViewProjection;
float4x4 InViewProjection;
texture2D DepthTexture;
texture2D NormalTexture;
sampler2D depthSampler = sampler_state
{
texture = <DepthTexture>;
minfilter=point;
magfilter=point;
mipfilter=point;
};
sampler2D normalSampler = sampler_state
{
texture = <DepthTexture>;
minfilter=point;
magfilter=point;
mipfilter=point;
};
float3 LightColor;
float3 LightPosition;
float3 LightAttenuation;
// Include shared funtions
#include "PPShared.vsi"
struct VertexShaderInput
{
float4 Position : POSITION0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float4 LightPosition :TEXCOORD0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
output.Position = mul(input.Position, WorldViewProjection);
output.LightPosition=output.Position;
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
// Find the Pixel Coordinates of the input position in Depth,Normal Textures
float2 texCoord = postProjectionToScreen(input.LightPosition) + halfPixel();

// extract the depth for this pixel from depth map that I created before
float4 depth = tex2D(depthSampler,texCoord);
// Recreate the position with the UV coordinates and depth value
float4 position;
position.x = texCoord.x * 2 -1;
position.y = (1- texCoord.y) * 2 - 1;
position.z = depth.r;
position.w = 1.0f;

// Transform position from screen space to world space
position = mul(position,InViewProjection);
position.xyz /= position.w;
// Extract the normal from normal map and move from
// 0 to 1 range to -1 to 1 range
float4 normal=(tex2D(normalSampler, texCoord)-0.5) * 2;
// Perform the lighting calculations for a point light
float3 lightDirection = normalize(LightPosition - position);
float lighting = clamp(dot(normal,lightDirection) , 0 , 1);
// Attenuate the light to simulate a point light
float d = distance(LightPosition,position);
float att = 1-pow(d / LightAttenuation , 6);
float3 output=LightColor * lighting * att ;
return (output,1 );
}
technique Technique1
{
pass Pass1
{
// TODO: set renderstates here.
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}


I had previously store the depth and normal values in special textures and use them in this effect and i use shared functions to map 3d to 2d for extracting values from DepthTexture and NormalTextures .


here is the effect file that used to store depth and normal :

// TODO: add effect parameters here.
struct VertexShaderInput
{
float4 Position : POSITION0;
float3 Nomral : NORMAL0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 Depth : TEXCOORD0;
float3 Normal : TEXCOORD1;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4x4 viewProjection=mul(View,Projection);
float4x4 worldViewProjection=mul(World,viewProjection);
output.Position=mul(input.Position,worldViewProjection);
output.Normal=mul(input.Nomral,World);
//Position's z and w components corresponds to the distance
//from camera and distance of the far plane respectivily
output.Depth.xy=output.Position.zw;
return output;
}
// I render to render targets simultaneously,so I can't
// return a float4 from PixelShaderFunction
struct PixelShaderOutput
{
float4 Normal:COLOR0;
float4 Depth :COLOR1;
};
PixelShaderOutput PixelShaderFunction(VertexShaderOutput input)
{
PixelShaderOutput output;
// Depth is stored as distance from camera / far plane distance
// to get value between 1 and 0
output.Depth=input.Depth.x/input.Depth.y;
// Normal map simply stores x, y and z components of normals
// shifted from -1 to 1 range to 0-1 range
output.Normal.xyz=(normalize(input.Normal).xyz/2)+ .5;
// other components must be intialized to compile
output.Depth.a=1;
output.Normal.a=1;
return output;
}
technique Technique1
{
pass Pass1
{
// TODO: set renderstates here.
VertexShader = compile vs_1_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}

I Know what is done in both files but I don't understand the following in the second file code: Edited by mrmohadnan

Share this post


Link to post
Share on other sites
It's not 1 anymore after multiplying with the matrix (in the general case at least)...

Share this post


Link to post
Share on other sites
I'm confused? I that code you posted there is no other perspective divide other than after the matrix multiply. And there it is definitely needed?

Share this post


Link to post
Share on other sites
japro I had posted the effect that I am not understand it very well even i had write it !!

in the second file code:


// TODO: add effect parameters here.
struct VertexShaderInput
{
float4 Position : POSITION0;
float3 Nomral : NORMAL0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 Depth : TEXCOORD0;
float3 Normal : TEXCOORD1;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4x4 viewProjection=mul(View,Projection);
float4x4 worldViewProjection=mul(World,viewProjection);
output.Position=mul(input.Position,worldViewProjection);
output.Normal=mul(input.Nomral,World);
//Position's z and w components corresponds to the distance
//from camera and distance of the far plane respectivily
output.Depth.xy=output.Position.zw;
return output;
}
// I render to render targets simultaneously,so I can't
// return a float4 from PixelShaderFunction
struct PixelShaderOutput
{
float4 Normal:COLOR0;
float4 Depth :COLOR1;
};
PixelShaderOutput PixelShaderFunction(VertexShaderOutput input)
{
PixelShaderOutput output;
// Depth is stored as distance from camera / far plane distance
// to get value between 1 and 0
output.Depth=input.Depth.x/input.Depth.y;
// Normal map simply stores x, y and z components of normals
// shifted from -1 to 1 range to 0-1 range
output.Normal.xyz=(normalize(input.Normal).xyz/2)+ .5;
// other components must be intialized to compile
output.Depth.a=1;
output.Normal.a=1;
return output;
}
technique Technique1
{
pass Pass1
{
// TODO: set renderstates here.
VertexShader = compile vs_1_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}


simply I am asking why this ?

1: output.Depth=input.Depth.x/input.Depth.y; ??

2: output.Normal.xyz=(normalize(input.Normal).xyz/2)+ .5; ??

3: output.Depth.a=1;
output.Normal.a=1;

4: what is the perspective divide and why using it ? Edited by mrmohadnan

Share this post


Link to post
Share on other sites
In the first case input.Depth contains the values that resulted from matrix multiplication and then interpolation which still have not been perspective divided. I don't see why Depth.y would equal 1 there?

The second line simply does what it says there? Convert [-1,1] to [0,1]. Most likely because the "Normal" render target clamps values to [0,1].

Share this post


Link to post
Share on other sites
This shader is going from screen space to world space.

It is taking a point (where w == 1 i.e. screen space) and multiplying by the inverse of the view*projection matrix. After this multiplication w should not be 1. You then divide the point by w to get the world space coordinates.

I'll have to do more digging in my graphics text book for a detailed explanation. What I've just given you is based off memory and a quick refresher.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!