Jump to content

  • Log In with Google      Sign In   
  • Create Account


Planar projection shader


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
2 replies to this topic

#1 Black Knight   GDNet+   -  Reputation: 754

Like
0Likes
Like

Posted 25 February 2013 - 07:03 PM

I am  rendering a scene to a reflection texture and then drawing a water plane with that texture using projection. Most of the samples I have seen on the web pass in a view-projection matrix to the vertex shader and transform the vertex, then in the pixel shader they do :

 

 

 

projection.x = input.projectionCoords.x / input.projectionCoords.w / 2.0 + 0.5;
projection.y = input.projectionCoords.y / input.projectionCoords.w / 2.0 + 0.5;
 

 

I have a shader that does the following and it works too but I don't know where I found this particular code or how it gives the same results with the above code. Here is the code I have

 

Vertex shader : (world is an identityMatrix, viewProj is my cameras combined viewProjection matrix:

 

output.position3D =  mul(float4(vsin.position,1.0), world).xyz;
output.position = mul(float4(output.position3D,1.0), viewProj);

output.reflectionTexCoord.x = 0.5 * (output.position.w + output.position.x);
output.reflectionTexCoord.y = 0.5 * (output.position.w - output.position.y);
output.reflectionTexCoord.z = output.position.w;

 

 

Pixel shader : 

 

 

float2 projectedTexCoord = (input.reflectionTexCoord.xy / input.reflectionTexCoord.z);
 

 

What confuses me is the usage of "0.5 * (output.position.w + output.position.x)" and "0.5 * (output.position.w - output.position.y)". How does this have the same effect and what does the w component mean here?

 



Sponsor:

#2 Black Knight   GDNet+   -  Reputation: 754

Like
0Likes
Like

Posted 26 February 2013 - 08:06 AM

After a while I realized that it ends up being the same thing :

 

0.5 * (output.position.w + output.position.x);

0.5 * output.position.w + 0.5 * output.position.x
 

 

Then in the pixel shader :

 

(0.5 * output.position.w + 0.5 * output.position.x) / output.position.w

(0.5 * output.position.w)/output.position.w + (0.5 * output.position.x) / output.position.w
 

 

The first part becomes 0.5 :

 

0.5 + 0.5 * ( output.position.x / output.position.w)
 

 

This is equal to :

 

(output.position.x / output.position.w) / 2 + 0.5
 

 

I believe moving this calculation to the vertex shader is more efficient so I will just leave it there.



#3 Styves   Members   -  Reputation: 979

Like
1Likes
Like

Posted 26 February 2013 - 01:29 PM

I've always found doing it this way a bit redundant because you can save yourself the trouble of messing around to get it right (and also avoiding the need to compute it per pixel or per vertex) by multiplying your matrix by a scale/bias matrix.

 

 

Something like this (pseudo code, might not work out of the box - some cases you'll need to flip the Y scale (-0.5) for the right projection, but that's easy to figure out):

Matrix44 ScaleBias = Matrix44(0.5, 0.0, 0.0, 0.0,
                              0.0, 0.5, 0.0, 0.0,
                              0.0, 0.0, 1.0, 0.0,
                              0.5, 0.5, 0.0, 1.0);

ViewProj = View * Proj * ScaleBias;





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