5
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Does gl_FragCoord not work for you? \$\endgroup\$ – edA-qa mort-ora-y Aug 19 '12 at 17:51
5
\$\begingroup\$

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;
}
\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Unfortunately I need a ps_2.0 solution. \$\endgroup\$ – jmasterx Aug 19 '12 at 17:54
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Adam Aug 19 '12 at 17:58
2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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