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.

In my 2D game, I render tiles. I want to do lighting without needing an extra buffer the size of the screen.

Either in glsl or hlsl, what could I feed to the shader each time I render a bitmap so that it knows what pixel on the scren it will fall on. That way I can do per pixel lighting.

Currently I only get the UV coord relative to the texture, not the screen.

Thanks

Using ps 2.0

share|improve this question
    
Does gl_FragCoord not work for you? –  edA-qa mort-ora-y Aug 19 '12 at 17:51

3 Answers 3

up vote 5 down vote accepted

If it's ps3+, use vPos in assembly or VPOS/SV_POSITION in HLSL.

If you don't want to use ps3+, then you need to pass, in another texcoord, the position before it gets mangled into clipspace.

e.g.

struct output{
   float4 Position : SV_POSITION;
   float4 Fake_Position : TEXCOORD0;
}    
VShader(youstruct input)
{
    a = input.Position * World
    b = a * View;
    c = b * Projection;

    output.Position = c;
    output.fake_Position = b;
}
share|improve this answer

In HLSL you can use VPOS or SV_Position to get the screen space position in the pixel shader.

share|improve this answer
    
Unfortunately I need a ps_2.0 solution. –  Milo Aug 19 '12 at 17:54
2  
If you're rendering a quad it's possible - you just need to work out at draw call time the required transform from UV space to screen space and pass that to the shader in a constant. –  Adam Aug 19 '12 at 17:58

In the vertex shader, you could augment the output so that it contains a texture coordinate. That coordinate should be the vertex's world position, projected into screen space by world and project matrices. This would be identical to what the rasterizer does for its own purposes, except you can use it in your pixel shader.

share|improve this answer

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.