Jump to content

  • Log In with Google      Sign In   
  • Create Account

Screen space , World space issue ?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 mrmohadnan   Members   -  Reputation: 279

Like
0Likes
Like

Posted 11 July 2012 - 02:59 AM

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, 11 July 2012 - 03:01 AM.


Sponsor:

#2 Hornsj3   Members   -  Reputation: 191

Like
1Likes
Like

Posted 11 July 2012 - 04:23 AM

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.

#3 mrmohadnan   Members   -  Reputation: 279

Like
0Likes
Like

Posted 11 July 2012 - 04:30 AM

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, 11 July 2012 - 04:48 AM.


#4 japro   Members   -  Reputation: 887

Like
0Likes
Like

Posted 11 July 2012 - 04:33 AM

It's not 1 anymore after multiplying with the matrix (in the general case at least)...

#5 mrmohadnan   Members   -  Reputation: 279

Like
0Likes
Like

Posted 11 July 2012 - 04:41 AM

Ok thanks , but before multiplying by matrix it's still 1 so I am still asking why perespetive divide ??? why he always use it ?

#6 japro   Members   -  Reputation: 887

Like
0Likes
Like

Posted 11 July 2012 - 04:44 AM

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?

#7 mrmohadnan   Members   -  Reputation: 279

Like
0Likes
Like

Posted 11 July 2012 - 04:51 AM

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, 11 July 2012 - 04:52 AM.


#8 japro   Members   -  Reputation: 887

Like
1Likes
Like

Posted 11 July 2012 - 05:20 AM

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].

#9 Hornsj3   Members   -  Reputation: 191

Like
1Likes
Like

Posted 11 July 2012 - 04:12 PM

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.

#10 mrmohadnan   Members   -  Reputation: 279

Like
0Likes
Like

Posted 12 July 2012 - 04:53 AM

thanks both of you I will try it and see by the way :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS