Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Good morning, I'm new to shader coding and trying to implement some visual effects code in shaders using billboards. (Yes, I couldn't have picked anything harder to start with, but I'm lucky that way)

Setup: I have rendered the full screen z depth to an array of floats in a previous pass. In the fragment shader I need the scene depth where the rendered fragment is displayed (to see if it's occluded). I can use tex2d() to get the depth value if I have the screen coordinates of the point being rendered in the fragment shader.

Question: In the fragment shader how do you calculate the screen coordinates of the pixel (in the range 0-1.0)?

Is the position passed to the fragment shader a pixel offset? If so, I guess it would be: float2( position.x / screen-width, position.y / screen-height )

Thanks for any help/

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

CG (as your tags indicate) supports the WPOS semantic in some profiles, for example this one. An input parameter bound to the WPOS semantic will get the window position (with the origin in the lower-left) of the fragment.

Other languages, like GLSL, have similar predefined uniforms or inputs (gl_FragCoord for example), although the specifics of their value ranges may vary.

share|improve this answer
    
Gah. Of course the profile I'm using is all "to-be-written" –  Jay Dec 3 '12 at 18:29
    
Woot! The documentation on wpos is pretty thin. I passed in the inverse of the screen size to the shader to calculate the offset into the texture array like this: float Zs = tex2D( DepthTexture, float2( scr.x * iViewWidth, 1 - ( scr.y * iViewHeight ) ) ); –  Jay Dec 4 '12 at 14:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.