# How to draw to a surface, then draw that surface to the screen

i have a 2D MonoGame Game (using DirectX), that was originally an XNA game. It involves prerendered shadows (they are Texture2Ds drawn with SpriteBatch.draw). Right now the shadows are being drawn with an alpha value, so overlapping shadows appear darker. This is NOT what i want. Overlapping shadows should be the same shade of black.

So what i want to do is draw all these shadows to a surface, such that the shadows are completely black (alpha == 255), and the surface background is completely transparent (alpha == 0), and then take that surface and draw it to the screen using Color.White*0.25f to give alpha to the shadows at the end.

I'm not sure how to switch rendering targets, or whatever the terminology is, and go about this. Can someone help?

what ive tried so far:

GraphicsDevice.SetRenderTarget(RENTAR); // RENTAR is the name of my RenderTarget2D
GraphicsDevice.DepthStencilState = new DepthStencilState() { DepthBufferEnable = true }; // not really sure what this does but it was in examples
GraphicsDevice.Clear(Color.White*0.0f); // i clear it with a totally transparent white background, in other words, white with alpha == 0

// now draw shadows normally, but instead of Color.White*0.25f, i use Color.White so that the black shadows are drawn completely black, with no alpha

GraphicsDevice.SetRenderTarget(null); // switch render target back to the screen
SpriteBatch.Draw(RENTAR, new Rectangle(0, 0, 800, 600), Color.White*0.25f); // see i added the "*0.25f" to try to draw this texture with alpha, but it comes out totally blank


So all of this works except that the shadows end up being completely black. I want them completely black in the RenderTarget, so that they dont overlap with any darker shades, and then i want to draw the final RenderTarget on the screen with an alpha of 0.25f, hence "Color.White*0.25f" in the final spritebatch draw. But its as if that 0.25f doesn't exit.

In general, please refer to Reimer's for everything XNA.

Stencil texture:
To avoid duplicating, here's a link to the specific tutorial regarding render targets.

Stencil buffer:
You intend on using an off-screen rendertarget as a stencil; why not use the stencil, instead? With the stencil active and always incrementing, draw the shadows, but don't output a color. After the shadows are rendered, the stencil will be filled with the number of times each pixel was shadowed. Reset the stencil to affect only pixels whose stencil value is >0 (shadowed 1 or more times). Activate the shadow-shader and draw a fullscreen quad using SpriteBatch.

• Im gonna need code with your answer because ive never done anything with stencils before. Also, i looked at Reiners but his example does not show how to affect alpha values of the final rendertarget being drawn to screen. I thought it would just be Color.White*0.25f like normal but this did not work im my attempt (i added the code in my OP). Apr 18, 2015 at 21:10

Are you sure that Color.White*0.25f is the right way to go? I would have used

SpriteBatch.Draw(RENTAR, new Rectangle(0, 0, 800, 600), new color(255,255,255, 64));


I think it's the only way to ensure that you're getting the transparency level in to the draw color.