1
\$\begingroup\$

I'm new to XNA (ver 4) and I'm obviously doing something wrong but I don't know what.

So far I have managed to model my scene (quite simple, just a bunch of squares which are calculated--there are no models involved) correctly except for a big problem with the light. I'm using a BasicEffect to render things, as I lower the transparency level the scene also gets darker until it goes totally black when the transparency is zero. There's nothing black in the scene other than the background fill color (which doesn't show anywhere, anyway.)

Setting various depths of the zbuffer when clearing the screen has no effect on the result.

Each vertex has a color value that is being calculated and appears to be being displayed properly, I haven't tried to do anything with textures yet. All the polygons should be completely opaque.

Edit again: Fusspawn solved half my problem--my understanding of alpha was backwards. That still doesn't explain why I can see through things even when alpha is at 1.0, though.

Edit: I didn't add the code before because I think this is some misunderstanding of how things work rather than a simple bug. This code generates all the triangles where they are supposed to be, the only problem is lighting.

(Note: I've replaced the recursive hunt with a breadth-first one to get the cutoff distance right. No change to the vision problem.)

        private void PushBox(int x, int y, int z, int Distance)
    {
        if (World.MapData[x, y, z].Contents.Impassible()) return;
        if (World.MapData[x, y, z].Scanned) return;
        if (Distance >= MaxDistance) return;
        World.MapData[x, y, z].Scanned = true;
        // Left
        if (World.MapData[x + 1, y, z].Contents.Impassible())
            Walls.Square(Vertex(x + 1, y, z, WallColor), Vertex(x + 1, y + 1, z, WallColor), Vertex(x + 1, y + 1, z + 1, WallColor), Vertex(x + 1, y, z + 1, WallColor));
        else
            PushBox(x + 1, y, z, Distance + 1);
        // Right
        if (World.MapData[x - 1, y, z].Contents.Impassible())
            Walls.Square(Vertex(x, y, z, WallColor), Vertex(x, y, z + 1, WallColor), Vertex(x, y + 1, z + 1, WallColor), Vertex(x, y + 1, z, WallColor));
        else
            PushBox(x - 1, y, z, Distance + 1);
        // Behind
        if (World.MapData[x, y - 1, z].Contents.Impassible())
            //Distance = Distance;
This square is generated wrong (no such square was visible so I didn't catch it), it's now fixed.  It isn't the problem.
            Walls.Square(Vertex(x, y, z, WallColor), Vertex(x, y, z + 1, WallColor), Vertex(x + 1, y, z + 1, WallColor), Vertex(x + 1, y, z, WallColor));
        else
            PushBox(x, y - 1, z, Distance + 1);
        // Front
        if (World.MapData[x, y + 1, z].Contents.Impassible())
            Walls.Square(Vertex(x, y + 1, z, WallColor), Vertex(x, y + 1, z + 1, WallColor), Vertex(x + 1, y + 1, z + 1, WallColor), Vertex(x + 1, y + 1, z, WallColor));
        else
            PushBox(x, y + 1, z, Distance + 1);
        if (World.MapData[x, y, z - 1].Contents.Impassible())
            Ceiling.Square(Vertex(x, y, z, CeilingColor), Vertex(x, y + 1, z, CeilingColor), Vertex(x + 1, y + 1, z, CeilingColor), Vertex(x + 1, y, z, CeilingColor));
        else
            PushBox(x, y, z - 1, Distance + 1);
        if (World.MapData[x, y, z + 1].Contents.Impassible())
            Floor.Square(Vertex(x, y, z + 1, FloorColor), Vertex(x + 1, y, z + 1, FloorColor), Vertex(x + 1, y + 1, z + 1, FloorColor), Vertex(x, y + 1, z + 1, FloorColor));
        else
            PushBox(x, y, z + 1, Distance + 1);
    }

    private VertexPositionColor Vertex(int x, int y, int z, Color Base)
    {
        float Distance = Math.Abs(State.xLocation - x) + Math.Abs(State.yLocation - y) + Math.Abs(State.zLocation - z);
        float Scale = Math.Max((MaxDistance - Distance) / MaxDistance, 0);
        Color ThisColor = Color.Multiply(Base, Scale);
        //ThisColor = Color.White;
        return new VertexPositionColor(new Vector3(x, y, z), ThisColor);
    }

and the main draw routine:

        protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, MaxDepth, 0);

        // TODO: Add your drawing code here

        spriteBatch.Begin();
        DrawStatusMessages();
        DrawDungeon();
        spriteBatch.End();
        base.Draw(gameTime);
    }
\$\endgroup\$
2
  • 4
    \$\begingroup\$ You might have to provide some code. \$\endgroup\$
    – Nayrb
    Dec 17, 2010 at 10:39
  • 1
    \$\begingroup\$ +1 Without some code, there could potentially be a whole host of things going wrong \$\endgroup\$
    – Ray Dey
    Dec 17, 2010 at 12:19

2 Answers 2

4
\$\begingroup\$

I might be missing something simple here. But shouldn't this be exactly what should happen?

For instance.

if i have a black piece of paper, and apply a completely transparent piece of paper on top of it. All I'm going to do is see through the transparency to the black?

Have you tried just rendering any old texture in the background first, and seeing if the screen turns to whatever colour the background texture is when turned transparent?

\$\endgroup\$
3
  • \$\begingroup\$ You are right that I had the Alpha backwards but even with it set all the way up I'm still seeing through things nearly perfectly when it should be fully opaque. \$\endgroup\$ Dec 17, 2010 at 22:04
  • \$\begingroup\$ +1, funny ..how many people complicate simple things...u just need to think before touching the keyboard ! \$\endgroup\$
    – Vishnu
    Dec 21, 2010 at 11:07
  • \$\begingroup\$ @Vish: The alpha was a red herring--the real problem was I was setting it to opaque in the wrong place and it was being ignored. \$\endgroup\$ Dec 21, 2010 at 23:15
0
\$\begingroup\$

Sounds like it could be depth testing. Try this:

DepthStencilState depthStencilState = new DepthStencilState();
depthStencilState.DepthBufferEnable = true;

device.DepthStencilState = depthStencilState;

I'm no expert but when the depth buffer isn't enabled then faces at the back can show through to the front. Try this code out and see if it works, if it does then just google 'xna 4 depth testing' or similar and read up on it.

\$\endgroup\$
1
  • \$\begingroup\$ The real culprit was I was setting it to opaque at initialization rather than in the draw loop. \$\endgroup\$ Dec 21, 2010 at 23:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .