Sign in to follow this  
ChristianFrantz

DepthBuffer problems

Recommended Posts

I have an issue with drawing a Texture2d in my 3d game. I followed RB Whitakers advice on changing the depth buffer, but it didnt seem to work.

 

XitfvBe.png

 

The ground is supposed to be checkered. The ground is just a flat model with a texture attached to it. 

 public void Draw(Matrix view, Matrix projection, Viewport viewport, GraphicsDevice device)
        {
            Matrix[] transforms = new Matrix[Model.Bones.Count];
            Model.CopyAbsoluteBoneTransformsTo(transforms);
            Matrix worldMatrix = Matrix.Identity;
            Matrix rotationYMatrix = Matrix.CreateRotationY(ForwardDirection);
            Matrix translateMatrix = Matrix.CreateTranslation(Position);

            worldMatrix = rotationYMatrix * translateMatrix;

            smoke.Draw(view, projection, worldMatrix, viewport);
            device.DepthStencilState = new DepthStencilState()
            {
                DepthBufferEnable = true
            };

            foreach (ModelMesh mesh in Model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World =
                        worldMatrix * transforms[mesh.ParentBone.Index]; ;
                    effect.View = view;
                    effect.Projection = projection;

                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;
                }
                mesh.Draw();
            }
        }

The item smoke is what is giving me the trouble. It just draws a texture at the position of the ship. Is there something else I have to do besides changing the DepthBUffer?

Share this post


Link to post
Share on other sites

Draw your smoke after setting the depth buffer. anything that uses the depth buffer needs to be drawn after its set, otherwise it can be overwritten

Edited by iedoc

Share this post


Link to post
Share on other sites

ok, so the way it looks like your code works right now, is you draw the smoke onto the screen with the depth buffer disabled, then enable the depth buffer and draw 3d stuff. this  means anything else drawn on the screen after the smoke will be drawn over the smoke, no matter what the depth of the smoke is.

 

also, setting the depth buffer is setting a state, so once you set it, it will stay that way. i see you only set the state of the depth buffer once, which means next time around, if you haven't explicitly disabled the depth buffer, it will be on before the smoke is even drawn. if you don't want the depth buffer to be used while your drawing the smoke, you should disable the depth buffer before you draw the smoke, then enable it again after you draw the smoke (but again, it will be drawn over since it was the first thing drawn).

 

you can try drawing the smoke after you draw the 3d stuff, first disabling the depth buffer then enabling it again, but the smoke will always be on top of everything then. right now the smoke will always be behind everything.

Share this post


Link to post
Share on other sites

absolutely, if you want smoke in your game, you'll probably want to do it that way. texture a square (rectangle) and have the normal always face the viewer. later you can implement 3d smoke if you need.

Edited by iedoc

Share this post


Link to post
Share on other sites
I'm sorry, I actually misunderstood what your doing. I thought you just had a single quad you were drawing for the smoke. You could use instancing to draw all the particles, that way you don't have to send 3000 quads to the gpu. You don't have to make sure every particle is facing the viewer. Make them all face random directions. You shouldn't have a problem with performance if you use instancing. But even if you don't use instancing, 3000 quads being drawn without instancing shouldn't create a performance issue either

Share this post


Link to post
Share on other sites

I used to go after whitaker's tutorials too.

Yeah 2D Drawing combined with 3D Drawing seems to be really annoying as every thing that is drawn after the 2DTexture will be "nearer" than the Texture itself and so will be in front of it.

Moving the whole 2D-Draw along with the Depth-Buffer-Enable after a specific object will help with this issue making the Texture in front of the Object drawn before (from every view-point)

 

You can draw the Smoke within a rectangle where you give the desired position. (Idk if you can have a neutral Color, which make the spaces between the 2D smoke transparent).

That way you wouldnt need the quads.

 

With the Quads you can always capsule the drawing stage in its own function called by draw() to deactivate backfaceculling, (has impact on performance as well)

But What I would do is just make the Quads and don't care about the direction it's facing.

Just rotate them and rand speed and around rand axis (with sin / cos fuction and a rand parameter perhaps?) without a texture just coloring the vertices.

You also could just uste Triangles instad of Quads

 

Edit: Ah forget what I just mentioned about the pure Texture2D - Technique. As long as you dont want to update the precise position of the rectangle acording to the view,

it will always be in the same place, regardless of your movement, like a sticker.

 

Better stick to the Quads, but I still recommend Triangles as in XNA Quads are (afaik) not a primitive and thus must be made of triangles.

If you only use triangles, you'll halve the number of Vertices to use (3000 Quads = 6000 Triangles = 18000 Vertices comppared to 3000 Triangles = 9000 Vertices).

 

And just make a VertexColorPositionNormal struct so you can get rid of the texture and just use the built in colors. Should simplify things alot

Edited by JustJim

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