4
\$\begingroup\$

All my models are see through and I can't figure out why. For example:

Here's what it looks like when you are not looking through them: enter image description here

And when I look through one of them: enter image description here

Why can I see the black cube through the other Model? It's the same the other way around. I can see the gray object through the black cube. I draw the models like this:

public void Draw(GraphicsDevice graphicsDevice)
{
    foreach (ModelMesh mesh in model.Meshes)
    {
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.View = camera.View;
            effect.World = worldMatrix;
            effect.Projection = camera.Projection;

            mesh.Draw();

            // Debug: Draw bounding boxes
            modelBox.Draw(effect, camera.View, camera.Projection, effect.World, graphicsDevice);
        }
    }
}

What am I doing wrong?

More screenshots: http://imgur.com/qRA34Ul, http://imgur.com/K4PGWfL

\$\endgroup\$
  • \$\begingroup\$ But the cube is still in the same place (as seen in picture 1) as I move around the plane. How can it be drawn on top of it? \$\endgroup\$ – syy Aug 13 '16 at 7:37
  • \$\begingroup\$ Seems like a shader issue. \$\endgroup\$ – Ave Aug 13 '16 at 7:51
  • \$\begingroup\$ Should I not use BasicEffect? I don't know much about shaders. @Timelord64 I think you are right. Depending on the order I draw them some become "see through" and others are not. Of course that depends on the camera position. As you can see here: imgur.com/K4PGWfL and imgur.com/qRA34Ul. I don't fully understand your explanation though so I don't get how to fix it. \$\endgroup\$ – syy Aug 13 '16 at 21:01
  • \$\begingroup\$ @Timelord64 Haha, I get it. OpenGL has been frustrating me quite a bit. Do you know anything easier that I can use? Every tutorial I find uses OpenGL in some way. Thanks for all your help! \$\endgroup\$ – syy Aug 13 '16 at 22:36
  • 1
    \$\begingroup\$ I would recommend you just go through the legacy tutorials at NeHe.gamedev.net, and check out any other tutorials that might be relevant. It's based in C++, IIRC, but it's easier to do in C# once you have that understanding. \$\endgroup\$ – Gnemlock Aug 13 '16 at 23:54
2
\$\begingroup\$

I think I figured it out. If anyone knows another way please do share.

I had to enable DepthBuffer in DepthStencilState in the Draw() method like so:

public void Draw(GraphicsDevice graphicsDevice)
{
    // Depth Buffer
    DepthStencilState dss = new DepthStencilState();
    dss.DepthBufferEnable = true;
    graphicsDevice.DepthStencilState = dss;

    // Draw model
    foreach (ModelMesh mesh in model.Meshes)
    {
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.View = camera.View;
            effect.World = worldMatrix;
            effect.Projection = camera.Projection;

            mesh.Draw();

            // Debug: Draw bounding boxes
            modelBox.Draw(effect, camera.View, camera.Projection, effect.World, graphicsDevice);
        }
    }
}

It ends up coming out exactly how you would expect:

enter image description here enter image description here

\$\endgroup\$
  • 2
    \$\begingroup\$ In favor of removing the long comments on the question, I am going to comment a short explanation of what is happening, here. While its pretty obvious to us, OpenGL does not immediately know which object is at the front. If you tell it to draw object A first, before drawing object B, object B will always be drawn over the top of object A. The DepthBuffer is used to work out which object is in front of the other, and handles the basics of drawing objects in order of depth, or how far back they are. This is a standard call, and you should always enable it by default. \$\endgroup\$ – Gnemlock Aug 13 '16 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.