Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

xna 4.0 - ResolveTexture2D - The nightmare in XNA 4

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...