I'm trying to simulate a "heightmap" buffer in XNA4.0 but the results don't look correct. Here's what I'm hoping to achieve: http://www.youtube.com/watch?feature=player_detailpage&v=-Q6ISVaM5Ww#t=517s (8:38).
From what I understand, here are the steps to reach there:
Pass height buffer + current entity's heightmap
Generate a stencil and update the height buffer
Render sprite+stencil
For now, I'm just trying to get the height buffer thing to work. So here's the problem. Inside the draw loop, I do the following:
Create a new render target & set it
Draw the heightmap with a sprite batch(no shaders)
graphicsDevice.SetRenderTarget(null)
Draw the rendertarget with SpriteBatch
I expected to see all entities' heightmaps. But only the last entity's heightmap is visible. Any hints on what I'm doing wrong? Here's the code inside the draw loop:
RenderTarget2D tempDepthStencil = new RenderTarget2D(graphicsDevice, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height, false, graphicsDevice.DisplayMode.Format, DepthFormat.None);
graphicsDevice.SetRenderTarget(tempDepthStencil);
// Gather depth information
SpriteBatch depthStencilSpriteBatch = new SpriteBatch(graphicsDevice);
depthStencilSpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise);
depthStencilSpriteBatch.Draw(texHeightmap, pos, null, Color.White, 0, Vector2.Zero, 1, spriteEffects, 1);
depthStencilSpriteBatch.End();
graphicsDevice.SetRenderTarget(null);
SpriteBatch b1 = new SpriteBatch(graphicsDevice);
b1.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null);
b1.Draw((Texture2D)tempDepthStencil, Vector2.Zero, null, Color.White, 0, Vector2.Zero, 1, spriteEffects, 1);
b1.End();