Click here to Skip to main content
13,046,679 members (61,253 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I'm trying to render a 3D scene to an off-screen image, so that I can then do some post-processing to apply a Gaussian blur to the final image before rendering it to the screen.

I've posted the body of the main Draw method below.

The DoDraw method contains all the detailed scene rendering.

1. If I take out the wrapper and just use the code in DoDraw, everything works fine.

2. But with the Draw wrapper code in place, the FIRST frame renders OK (including blurring), but all subsequent frames just give a black image - the first substantive line in DoDraw is:


The rest of the drawing code uses
to do the rendering.

3. First time through, the ColorData I get from the RawImage texture is fine - after that, R=G=B=0 / A=255 for all pixels. N.B. Those lines are only there to try to find out what's going on - I wouldn't normally be using GetData!

4. If I change the RawImage to
Texture2D RawImage = (Texture2D)SomeTextureLoadedFromContent;

then everything works fine - blurring works as intended - all frames display fine.

5. The RawRenderTarget is initialised in the LoadContent method using
ThePresentationParameters = TheGraphicsDevice.PresentationParameters;
RawRenderTarget = new RenderTarget2D(TheGraphicsDevice, ThePresentationParameters.BackBufferWidth, ThePresentationParameters.BackBufferHeight, false, ThePresentationParameters.BackBufferFormat, ThePresentationParameters.DepthStencilFormat);

The implication is that somehow DoDraw is failing to render properly to the RawRenderTarget after the first frame. I've tried various options for how the RawRenderTarget is initialised (including initialising a new object for every frame!!), playing with depth buffer formats, trying different clear options etc., but nothing I do seems to make any difference.

Can anyone suggest anything else I can try or anything I might be missing?

This is XNA 4.0 using VS 2010 in debug mode on a Win7 x64 machine.

If any further info on anything would help, please let me know.




            Texture2D RawImage = (Texture2D)RawRenderTarget;
            Color[] ColorData = new Color[1024 * 768];
            Rectangle SrcRect = new Rectangle(0, 0, RawImage.Width, RawImage.Height);
            GaussianEffect.CurrentTechnique = GaussianEffect.Techniques["GaussianBlur"];
            Rectangle DestRect1 = new Rectangle(0, 0, BlurredRenderTarget1.Width, BlurredRenderTarget1.Height);
            TheSpriteBatch.Begin(0, BlendState.Opaque, null, null, null, GaussianEffect);
            TheSpriteBatch.Draw(RawImage, DestRect1, Color.White);
            Texture2D BlurredTexture1 = (Texture2D)BlurredRenderTarget1;
            Rectangle DestRect2 = new Rectangle(0, 0, BlurredRenderTarget2.Width, BlurredRenderTarget2.Height);
            TheSpriteBatch.Begin(0, BlendState.Opaque, null, null, null, GaussianEffect);
            TheSpriteBatch.Draw(BlurredTexture1, DestRect2, Color.White);
            Texture2D BlurredTexture2 = (Texture2D)BlurredRenderTarget2;
            Rectangle DisplayRect = new Rectangle(0, 0, ThePresentationParameters.BackBufferWidth, ThePresentationParameters.BackBufferWidth);
            TheSpriteBatch.Draw(BlurredTexture2, DisplayRect, Color.White);
Posted 10-Jan-12 23:23pm
NuttingCDEF 11-Jan-12 16:05pm
OK - I solved my own problem!

The issue was that setting the render target seemingly also resets the culling mode - so after the first frame, the culling mode was wrong (having been set in the Initialize method, which seemed to make sense at the time) so triangles weren't being rendered in my DoDraw method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 11 Jan 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100