ResolveTexture2D - The nightmare in XNA 4
Question
I have the following declaration:
ResolveTexture2D rightTex;
And I use it in the Draw
method like so:
GraphicsDevice.ResolveBackBuffer(rightTex);
Now, I then draw it out using the SpriteBatch
:
spriteBatch.Draw(rightTex, new Rectangle(0, 0, 800, 600), Color.Cyan);
This works fantastic in XNA 3.1. But, now I'm converting to XNA 4, ResolveTexture2D
and the ResolveBackBuffer
method have been removed. How would I re-code this in order to work in XNA 4.0?
EDIT
So, here is some more code to maybe help. Here I initialise the RenderTargets:
PresentationParameters pp = GraphicsDevice.PresentationParameters;
leftTex = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, true, pp.BackBufferFormat, pp.DepthStencilFormat);
rightTex = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, true, pp.BackBufferFormat, pp.DepthStencilFormat);
Then, in my Draw
method I do:
GraphicsDevice.Clear(Color.Gray);
rightCam.render(model, Matrix.CreateScale(0.1f), modelAbsTrans);
GraphicsDevice.SetRenderTarget(rightTex);
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Gray);
leftCam.render(model, Matrix.CreateScale(0.1f), modelAbsTrans);
GraphicsDevice.SetRenderTarget(leftTex);
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);
//start the SpriteBatch with Additive Blend Mode
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive);
spriteBatch.Draw(rightTex, new Rectangle(0, 0, 800, 600), Color.Cyan);
spriteBatch.Draw(leftTex, new Rectangle(0, 0, 800, 600), Color.Red);
spriteBatch.End();
Solution
ahhh, here you go: Move the GraphicsDevice.SetRenderTarget() before the GraphicsDevice.Clear() call
OTHER TIPS
The removal of ResolveTexture2D
from XNA 4.0 is explained here.
Basically you should use render targets. The gist of the process goes like this:
Create a render target to use.
RenderTarget2D renderTarget = new RenderTarget2D(graphicsDevice, width, height);
Then set it onto the device:
graphicsDevice.SetRenderTarget(renderTarget);
Then render your scene.
Then un-set the render target:
graphicsDevice.SetRenderTarget(null);
Finally, you can use a RenderTarget2D as a Texture2D, like so:
spriteBatch.Draw(renderTarget, new Rectangle(0, 0, 800, 600), Color.Cyan);
You may also find this overview of RenderTarget changes in XNA 4.0 worth reading.