nick5454

I have a game where your in a maze

And I want to display the map in a second viewport that display on top of the game screen.

That works fine. But, I need to make this map overlay like 35% transparent.

Does anyone know how to make an entire viewport transparent

Thanks,

Nick



Re: XNA Framework Making an entire viewport transparent

Shawn Hargreaves - MSFT

The best way is probably to draw the map to a separate RenderTarget2D, then use a single sprite draw to display the contents of that rendertarget over your main scene. Makes it very easy to do all kinds of effects like scaling, moving around, translucency, and tinting, during that final sprite draw.





Re: XNA Framework Making an entire viewport transparent

nick5454

I cant find any examples on drawing to that Render2D object. do you know of any links

 

nvm, ill just ask another





Re: XNA Framework Making an entire viewport transparent

dczraptor

nick5454 wrote:

I cant find any examples on drawing to that Render2D object. do you know of any links

nvm, ill just ask another

// set up a render target

RenderTarget2D rt = new RenderTarget2D(graphics.GraphicsDevice, width, height, 1, graphics.GraphicsDevice.DisplayMode.Format);

RenderTarget2D backBuffer = (RenderTarget2D)graphics.GraphicsDevice.GetRenderTarget(0);

graphics.GraphicsDevice.SetRenderTarget(0, rt);

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// draw

model.Draw(gameTime);

// reset the render target

graphics.GraphicsDevice.ResolveRenderTarget(0);

Texture2D renderedTexture = rt.GetTexture();

rt.Dispose();

graphics.GraphicsDevice.SetRenderTarget(0, backBuffer);






Re: XNA Framework Making an entire viewport transparent

nick5454

Ok so I have this. Its not working. But, am I doing this correctly. I created a buffer in the load content page and set it to target index 0. I then get the target back and finally call resolve. Are these the correct steps Or am I missing something

load content event:

RenderTarget2D rt = new RenderTarget2D(graphics.GraphicsDevice, 200, 200, 1, graphics.GraphicsDevice.DisplayMode.Format);

graphics.GraphicsDevice.SetRenderTarget(0, rt);

the draw event

graphics.GraphicsDevice.Clear(Color.Black);

// Draw the game screen

BackgroundBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.FrontToBack, SaveStateMode.SaveState);

Vector2 pos = new Vector2(0, 0);

Rectangle rect = new Rectangle(0,0,32,32);

Rectangle clientRect = Window.ClientBounds;

int height = (clientRect.Height / (32 * scale)) ;

int width = (clientRect.Width / ( 32 * scale)) ;

for (int y = 0; y < height ; y++)

for (int x = 0; x < width ; x++) {

Texture2D text2D = null;

int newWidth = (player.X - (width / 2)) + x;

int newHeight = (player.Y - (height / 2)) + y;

switch (map.GetTerrainAtLocation(newWidth, newHeight)) {

case TerrainType.Space:

text2D = SpriteTextureStone;

break;

case TerrainType.Wall:

text2D = SpriteTextureGrass;

break;

case TerrainType.Finish:

text2D = SpriteTextureFinish;

break;

}

if( text2D != null )

BackgroundBatch.Draw(text2D, new Rectangle(x * (text2D.Width * scale), y * (text2D.Height * scale), text2D.Width * scale, text2D.Height * scale), null, Color.White, 0.0f, pos, SpriteEffects.None, 1.0f);

}

BackgroundBatch.End();

// Draw the sprite in the middle (player)

ForegroundBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.FrontToBack, SaveStateMode.SaveState);

ForegroundBatch.Draw(SpriteTextureChick, new Rectangle(((width/2-1) * (32 * scale)) - ( 7 * scale), ((height/2-1 ) * (32 * scale)) + ( 5 * scale), SpriteTextureChick.Width, SpriteTextureChick.Height), Color.White);

ForegroundBatch.End();

// now draw a semi transparent map

RenderTarget2D rt = new RenderTarget2D(graphics.GraphicsDevice, 200, 200, 1, graphics.GraphicsDevice.DisplayMode.Format);

object o = graphics.GraphicsDevice.GetRenderTarget(0);

RenderTarget2D backBuffer = (RenderTarget2D)graphics.GraphicsDevice.GetRenderTarget(0);

graphics.GraphicsDevice.SetRenderTarget(0, rt);

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

graphics.GraphicsDevice.ResolveRenderTarget(0);

Texture2D renderedTexture = rt.GetTexture();

rt.Dispose();

graphics.GraphicsDevice.SetRenderTarget(0, backBuffer);

base.Draw(gameTime);





Re: XNA Framework Making an entire viewport transparent

nick5454

dczraptor wrote:
nick5454 wrote:

I cant find any examples on drawing to that Render2D object. do you know of any links

nvm, ill just ask another

// set up a render target

RenderTarget2D rt = new RenderTarget2D(graphics.GraphicsDevice, width, height, 1, graphics.GraphicsDevice.DisplayMode.Format);

RenderTarget2D backBuffer = (RenderTarget2D)graphics.GraphicsDevice.GetRenderTarget(0);

graphics.GraphicsDevice.SetRenderTarget(0, rt);

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// draw

model.Draw(gameTime);

// reset the render target

graphics.GraphicsDevice.ResolveRenderTarget(0);

Texture2D renderedTexture = rt.GetTexture();

rt.Dispose();

graphics.GraphicsDevice.SetRenderTarget(0, backBuffer);

I don't understand. The backbnuffer is null, because target 0 doesnt exist yet.

And what am I supposed to with the texture

I'm just confused.

Nick





Re: XNA Framework Making an entire viewport transparent

dczraptor

The backBuffer variable stores the current back buffer, so that you have a reference to it later. After you finish rendering, you save the texture. You can then render the texture, or manipulate it in some way (such as transparency), then render it onto the main screen.




Re: XNA Framework Making an entire viewport transparent

nick5454

well when I call GetTarget to store the backBuffer variable its null. How do I get a valid reference to the BackBuffer



Re: XNA Framework Making an entire viewport transparent

Leaf.

You don't have to get a reference to the back buffer. To set the render target to be the back buffer just call SetRenderTarget(0, null). So for that code snippet you can ignore that the backbuffer is null.

Cheers,
Leaf.






Re: XNA Framework Making an entire viewport transparent

nick5454

thanks