0
\$\begingroup\$

I'm trying to create a glow shader for a neon style game but I'm having a little trouble with rendertargets. Right now all I want is something VERY simple where I change the rendertarget, draw to it and then pass it into the shader. A lot of the examples I've looked at do the first part but don't include how to pass it all through a shader. Right now I'm not really concerned about the glow part, I just want to get it passing through a shader so I can work on the glow part.

edit - new code:

graphicsDevice.SetRenderTarget(renderTarget);

..
..
..

graphicsDevice.SetRenderTarget(null);

renderTargetTexture = (Texture2D)renderTarget;
loader.GlowShader.Parameters["glowTexture"].SetValue(renderTargetTexture);

for (int l = 0; l < loader.GlowShader.CurrentTechnique.Passes.Count; l++)
{
    loader.GlowShader.CurrentTechnique.Passes[l].Apply();
}

renderQuad.Draw();

And the glow shader (this is probaly really messy but I'm not really fuzzed about that right now, I just want the basic thing to work):

float4x4 World;
float4x4 View;
float4x4 Projection;

Texture glowTexture;

sampler glowSampler = sampler_state 
{ 
texture = <glowTexture>;
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
AddressU = clamp;
AddressV = clamp;
};

struct VertexShaderInput
{
float4 Position : POSITION0;
};

struct VertexShaderOutput
{
float4 Position : POSITION0;
};

VertexShaderOutput VSOutline(VertexShaderInput input)
{
VertexShaderOutput output;

float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);

output.Position = mul(viewPosition, Projection);

return output;
}

float4 PSOutline(VertexShaderOutput input, float2 pixel:VPOS) : COLOR0
{
float2 uvPixel = (pixel + 0.5) * float2(1.0 / 1600, 1.0 / 900);
    return tex2D(glowSampler, uvPixel);
}

technique Outline
{
pass Pass1
{
    AlphaBlendEnable = TRUE;
    DestBlend = INVSRCALPHA;
    SrcBlend = SRCALPHA;

    VertexShader = compile vs_3_0 VSOutline();
    PixelShader = compile ps_3_0 PSOutline();
}
}

On a side note, if I try running this and drawing it using sprite batches it works so I think up until "rendertargetTexture = (Texture2D)renderTarget" everything is correct. It's just when I try to use the quads that nothing appears still.

        graphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DarkSlateBlue, 1.0f, 0);
        using (SpriteBatch sprite = new SpriteBatch(graphicsDevice))
        {
            sprite.Begin();
            sprite.Draw(renderTargetTexture, new Vector2(0, 0), null, Color.White, 0, new Vector2(0, 0), 1, SpriteEffects.None, 1);
            sprite.End();
        }
\$\endgroup\$
1
\$\begingroup\$

Assuming that graphicsDevice, renderTarget and loader are properly set I think you are missing the apply() of the glow pass and a fullscreen quad draw. If you don't draw the fullscreen quad, the glow pixel shader cannot apply the effect since there is nothing being draw to the default render target. I think that's why nothing appears on the screen.

graphicsDevice.SetRenderTarget(renderTarget);
//
//...
//
graphicsDevice.SetRenderTarget(null);
renderTargetTexture = (Texture2D)renderTarget;
whateverEffectToGlow.whateverTechniqueToGlow.pass[0].apply;
RenderFullScreenQuad();

You can use this example to write RenderFullScreenQuad(): http://projectvanquish.wordpress.com/tag/fullscreen-quad/

\$\endgroup\$
  • \$\begingroup\$ Thanks, that cleared things up so much, but I still have a problem. I followed the steps you gave and nothing still appears, I've updated the code in the original post, what is it that I'm missing? If I draw the texture using sprite batch everything works correctly, but whenever I try to use the quad nothing appears. Something that i'm confused about is how does it know to draw the renderTargetTexture to the quad? \$\endgroup\$ – user1157885 Sep 11 '13 at 16:01
0
\$\begingroup\$

What you are missing out is actualy where you do your "posteffects". The first part all seems alright.

A breakdown of glow & post effects would be like this :

1 : Render all your models to offscreen rendertarget ( this you have done )

2 : Render your offscreen rendertarget to a fullscrean quad, that will cover the entire screen. this way you will get a texture that looks like your world over the screen, and can manipulate the pixels.

3 : once you are rendering the quad, you can manipulate the orginal texture to do diffrent stuff with the pixels, like bloom or glow.

4 : Once all this is done, you got a new rendertarget ( or backbuffer ) that have an applied glow onto it.

All in all, you only miss the part where you are drawing the fullscreen quad with the postpass texture. I think this article : Good article about lightprepass

\$\endgroup\$
  • \$\begingroup\$ Sorry, this is my fault, I wasn't clear enough. Right now the code I have doesn't actually work. If I draw it without the render targets it draws neon-style models. If I include the rendertargets nothing appears, the answer I'm looking for is an answer to why nothing appears, right now I just want it to appear the exact same way except using render targets so from there I can work on the glow code. \$\endgroup\$ – user1157885 Sep 10 '13 at 14:15

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.