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;
AddressU = Clamp;
AddressV = Clamp;
};
// The input for the VertexShader
struct VertexShaderInput
{
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
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
float3 View : TEXCOORD1;
float3x3 WorldToTangentSpace : TEXCOORD2;
};
// The VertexShader.
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
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;
}
// The Pixel Shader
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
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;
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
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.