Sign in to follow this  
NewtonsBit

[XNA 4.0] Bug in draw

Recommended Posts

I currently have a strange bug in my draw loop that I can't figure out and its driving me batty. Here's a quick explanation of what is supposed to happen:

1) when I the game starts, it creates a ground plane that has a silver color.
2) When the user presses 'p' a red cube appears just above the ground plane at the origin.
3) When the users presses 'o' the cube explodes into 125 itty bitty cubes (not currently driven by particles, just 125 different objects) that bounce when they hit the ground plane.

What actually happens:

1) The ground plane is created correctly.
2) The user presses the 'p' button and the ground plane turns red. The cube is not visible.
3) The users presses the 'o' button and the explosion happens correctly, but the ground plane behaves as one of the itty bitty cubes and it goes bouncing off.

[img]http://public.gamedev.net/public/style_emoticons/default/blink.gif[/img][img]http://public.gamedev.net/public/style_emoticons/default/blink.gif[/img][img]http://public.gamedev.net/public/style_emoticons/default/blink.gif[/img]

I THINK what's happening is the effect parameters are going to the wrong object. In other words, the ground plane, which always the last object draw, takes the world matrix and color from the previous object.


I'm currently drawing the scene three times, once for a shadow depth map, once for a projected shadow map and the final perspective render. Here's the code to draw the scene (this gets called three times during draw).

[code]

private void DrawScene(GameTime gameTime)
{

// DRAW ALL IDRAW IN ITEMS
foreach (IDraw item in items)
{
// Draw each mesh in the model
for (int a = 0; a < item.Model.MeshCount; a++)
{
// Draw the mesh
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();

// SET PARAMETERS
effect.Parameters["Alpha"].SetValue(item.Model.Alpha);
effect.Parameters["World"].SetValue(item.Model.MeshMatrices[a]);
effect.Parameters["MeshColor"].SetValue(item.Model.MeshColors[a]);

// DRAW BUFFERS
game.GraphicsDevice.Indices = item.Model.Meshes[a].IB;
game.GraphicsDevice.SetVertexBuffer(item.Model.Meshes[a].VB);
game.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0,
item.Model.Meshes[a].VertexCount, 0, item.Model.Meshes[a].IndexCount / 3);

}
}
}

// DRAW THE GROUNDPLANE
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();

// SET PARAMETERS
effect.Parameters["Alpha"].SetValue(1.0f);
effect.Parameters["World"].SetValue(Matrix.Identity);
effect.Parameters["MeshColor"].SetValue(new Vector4(0, 0, 0, 0));

// DRAW BUFFERS
game.GraphicsDevice.Indices = groundPlane.IndexBuffer;
game.GraphicsDevice.SetVertexBuffer(groundPlane.VertexBuffer);
game.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0,
groundPlane.VertexCount, 0, groundPlane.IndexCount / 3);
}

[/code]


Am I placing pass.Apply() in the wrong place?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this