• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
MohammadAhmed

Screen space , World space issue ?

9 posts in this topic

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
0

Share this post


Link to post
Share on other sites
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.
1

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 :

[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();
}
}
[/CODE]

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 :
[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();
}
}
[/CODE]
I Know what is done in both files but I don't understand the following in the second file code: Edited by mrmohadnan
0

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)...
0

Share this post


Link to post
Share on other sites
Ok thanks , but before multiplying by matrix it's still 1 so I am still asking why perespetive divide ??? why he always use it ?
0

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?
0

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:

[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();
}
}
[/CODE]

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
0

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

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  
Followers 0