View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# RenderTargets and Transparency

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1vinnyhalts  Members

Posted 07 March 2013 - 11:48 AM

Hi, I am trying to draw a weapon on screen, using a rendertarget. It works, and it moves smoothly, but there is one problem. My model is see through. I have searched this, and tried many different things, but nothing seems to work. If anyone knows what I am doing wrong, and if they could direct me in the right direction, that would be great.

Here is some of my code:

//This is inside LoadContent()
var pp = XNAGame.Instance.GraphicsDevice.PresentationParameters;
RT = new RenderTarget2D(XNAGame.Instance.GraphicsDevice,
pp.BackBufferWidth, //Same width as backbuffer
pp.BackBufferHeight, //Same height
false, //No mip-mapping
pp.BackBufferFormat, //Same colour format
pp.DepthStencilFormat);

....
transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart part in mesh.MeshParts)
{
part.Effect = e;
}
}

//My drawing code:
XNAGame.Instance.GraphicsDevice.SetRenderTarget(RT);
XNAGame.Instance.GraphicsDevice.Clear(Color.Transparent);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart part in mesh.MeshParts)
{
//part.Effect = e;
e.Parameters["World"].SetValue(transforms[mesh.ParentBone.Index] * Matrix.Identity);
e.Parameters["View"].SetValue(Matrix.Identity);
e.Parameters["LightDirection"].SetValue(Vector3.Normalize(Skydome.Instance.sunDir));
e.Parameters["CameraPosition"].SetValue(XNAGame.Instance.Camera.Position);
e.Parameters["Projection"].SetValue(Matrix.Identity);
e.Parameters["ColorMap"].SetValue(diff);
e.Parameters["NormalMap"].SetValue(norm);
e.Parameters["SpecularMap"].SetValue(spec);
//e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
//e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
//e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
//e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
}
mesh.Draw();
}
RtTex = RT;
XNAGame.Instance.GraphicsDevice.SetRenderTarget(null);


Here is the shader I am using, I got it off of an opensource project:

// XNA 4.0 Normal Mapping

// Matrix
float4x4 World;
float4x4 View;
float4x4 Projection;

// Light related
float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = .4;

float3 LightDirection;
float4 DiffuseColor = float4(1, 1, 1, 1);
float DiffuseIntensity = .4;

float4 SpecularColor = float4(1, 1, 1, 1);
float3 CameraPosition;

texture2D ColorMap;
sampler2D ColorMapSampler = sampler_state
{
Texture = <ColorMap>;
MinFilter = linear;
MagFilter = linear;
MipFilter = linear;
};

texture2D NormalMap;
sampler2D NormalMapSampler = sampler_state
{
Texture = <NormalMap>;
MinFilter = linear;
MagFilter = linear;
MipFilter = linear;
};

texture SpecularMap;
sampler SpecMapSampler = sampler_state
{
Texture = <SpecularMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};

// The input for the VertexShader
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
float3 Normal : NORMAL0;
float3 Binormal : BINORMAL0;
float3 Tangent : TANGENT0;
};

// The output from the vertex shader, used for later processing
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
float3 View : TEXCOORD1;
float3x3 WorldToTangentSpace : TEXCOORD2;
};

{

float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.TexCoord = input.TexCoord;

output.WorldToTangentSpace[0] = mul(normalize(input.Tangent), World);
output.WorldToTangentSpace[1] = mul(normalize(input.Binormal), World);
output.WorldToTangentSpace[2] = mul(normalize(input.Normal), World);

output.View = normalize(float4(CameraPosition,1.0) - worldPosition);

return output;
}

{
float4 color = tex2D(ColorMapSampler, input.TexCoord);

float4 SpecMapColor = tex2D(SpecMapSampler, input.TexCoord);

float3 normalMap = 2.0 *(tex2D(NormalMapSampler, input.TexCoord)) - 1.0;
normalMap = normalize(mul(normalMap, input.WorldToTangentSpace));
float4 normal = float4(normalMap,1.0);

float4 diffuse = saturate(dot(LightDirection,normal));
float4 reflect = normalize(2*diffuse*normal-float4(LightDirection,1.0));
float4 specular = pow(saturate(dot(reflect,input.View)),32);

return  color * AmbientColor * AmbientIntensity +
color * DiffuseIntensity * DiffuseColor * diffuse +
color * SpecularColor * specular;
}

// Our Techinique
technique DiffuseSpecularNormal
{
pass Pass0
{
AlphaBlendEnable = FALSE;
}
}


I thought that maybe it was my SpriteBatch, but I made a screenshot function and commented out all SpriteBatch code, just to check it.

public void scrn(string name)
{
Stream str = File.OpenWrite("Dump - " + name + ".png");
RtTex.SaveAsPng(str, RtTex.Width, RtTex.Height);
str.Close();
str.Dispose();
}


So it clearly, as far as I can tell, is not my SpriteBatch. I have attached a photo of how it looks (it is transparent).

Thanks for taking the time to read this/help me.

### #2phil_t  Members

Posted 07 March 2013 - 12:33 PM

It's not obvious it's transparent (given the smooth background), but it looks possibly like the z-order of the meshes is all off.

Are you making sure to set GraphicsDevice.DepthStencilState to DepthStencilState.Default prior to rendering your model?

### #3vinnyhalts  Members

Posted 07 March 2013 - 01:43 PM

It's not obvious it's transparent (given the smooth background), but it looks possibly like the z-order of the meshes is all off.

Are you making sure to set GraphicsDevice.DepthStencilState to DepthStencilState.Default prior to rendering your model?

I just tried it, it does not seem to change anything.

### #4phil_t  Members

Posted 07 March 2013 - 02:35 PM

Where did you put that line?

Can you post a better screenshot? It's very hard to tell anything from that. We don't know what the object is supposed to look like, so it's hard to tell what's wrong.

How does it look from different angles?
What does the original look like in your modelling program?
Is it really transparent? Can you put something other than a blank background behind it, so it is easy to tell?

### #5Jason Z  Members

Posted 07 March 2013 - 05:00 PM

Is your camera positions such that it is inside the camera?  This would make you only see faces that are pointing towards the camera even though it is inside, meaning that there would be lots of faces culled out by back face culling.

Jason Zink :: DirectX MVP

Direct3D 11 engine on CodePlex: Hieroglyph 3

Games: Lunar Rift

### #6vinnyhalts  Members

Posted 07 March 2013 - 08:57 PM

Where did you put that line?

Can you post a better screenshot? It's very hard to tell anything from that. We don't know what the object is supposed to look like, so it's hard to tell what's wrong.

How does it look from different angles?
What does the original look like in your modelling program?
Is it really transparent? Can you put something other than a blank background behind it, so it is easy to tell?

I put the line right after I set my new render target.

        public override void Draw(GameTime gameTime)
{
XNAGame.Instance.GraphicsDevice.SetRenderTarget(RT);
XNAGame.Instance.GraphicsDevice.Clear(Color.Transparent);
XNAGame.Instance.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart part in mesh.MeshParts)
{
//part.Effect = e;
e.Parameters["World"].SetValue(transforms[mesh.ParentBone.Index] * Matrix.Identity);
e.Parameters["View"].SetValue(Matrix.Identity);
e.Parameters["LightDirection"].SetValue(Vector3.Normalize(Skydome.Instance.sunDir));
e.Parameters["CameraPosition"].SetValue(XNAGame.Instance.Camera.Position);
e.Parameters["Projection"].SetValue(Matrix.Identity);
e.Parameters["ColorMap"].SetValue(diff);
e.Parameters["NormalMap"].SetValue(norm);
e.Parameters["SpecularMap"].SetValue(spec);
//e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
//e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
//e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
//e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
}
mesh.Draw();
}
RtTex = RT;
XNAGame.Instance.GraphicsDevice.SetRenderTarget(null);

//Stream str = File.OpenWrite("Dump.png") ;
//RtTex.SaveAsPng(str, RtTex.Width, RtTex.Height);
//str.Close();
}
}


Here is a photo of the gun in game, without using a different render target.

Is your camera positions such that it is inside the camera?  This would make you only see faces that are pointing towards the camera even though it is inside, meaning that there would be lots of faces culled out by back face culling.

I've tested it with other models and tried moving the model further away. Nothing seems to work.

Edit: I added more photos to show the problem.

Edited by vinnyhalts, 09 March 2013 - 02:49 PM.

### #7phil_t  Members

Posted 08 March 2013 - 02:19 AM

Hmm... well, alpha-blending should be disabled given that you have AlphaBlendEnable = FALSE in your shader.
But just for kicks, can you add this next to the line where you set the DepthStencilState:

XNAGame.Instance.GraphicsDevice.BlendState = BlendState.Opaque;

### #8vinnyhalts  Members

Posted 08 March 2013 - 04:06 AM

Hmm... well, alpha-blending should be disabled given that you have AlphaBlendEnable = FALSE in your shader.
But just for kicks, can you add this next to the line where you set the DepthStencilState:

XNAGame.Instance.GraphicsDevice.BlendState = BlendState.Opaque;

Nothing changed.

Edit, it has to be something in the Render Target. As I disabled it, and just drew the image as I would any other model, and it worked. But this does not solve my problem, as it still jitters when I move in game. I still need to use a Render Target.

Any ideas why?

Edited by vinnyhalts, 08 March 2013 - 01:59 PM.

### #9phil_t  Members

Posted 08 March 2013 - 11:14 AM

The only thing I can thing of is that the alpha in your gun source textures must be partially transparent. That would explain the difference you're seeing between render target and no render target.

return color * AmbientColor * AmbientIntensity +
color * DiffuseIntensity * DiffuseColor * diffuse +
color * SpecularColor * specular;

with

float4 temp = color * AmbientColor * AmbientIntensity +
color * DiffuseIntensity * DiffuseColor * diffuse +
color * SpecularColor * specular;
temp.a = 1; // Force alpha to be 1
return temp;

### #10vinnyhalts  Members

Posted 08 March 2013 - 01:58 PM

The only thing I can thing of is that the alpha in your gun source textures must be partially transparent. That would explain the difference you're seeing between render target and no render target.

return color * AmbientColor * AmbientIntensity +
color * DiffuseIntensity * DiffuseColor * diffuse +
color * SpecularColor * specular;

with

float4 temp = color * AmbientColor * AmbientIntensity +
color * DiffuseIntensity * DiffuseColor * diffuse +
color * SpecularColor * specular;
temp.a = 1; // Force alpha to be 1
return temp;

Yeah, that did it. It seems to be working now. It's a bit jagged, but that's another problem for another time. Thank you very much. I would not have known it was the textures, as I am using public domain models and textures.

Edit: I fixed the Anti aliasing, I just turned up my multisampling. Just in the event anyone should read this and need help:

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.rendertarget2d_members.aspx

Edited by vinnyhalts, 08 March 2013 - 02:06 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.