ResolveTexture2D - The nightmare in XNA 4

淺唱寂寞╮ 提交于 2019-12-12 01:59:21

问题


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();

回答1:


ahhh, here you go: Move the GraphicsDevice.SetRenderTarget() before the GraphicsDevice.Clear() call




回答2:


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.



来源:https://stackoverflow.com/questions/4133441/resolvetexture2d-the-nightmare-in-xna-4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!