Tuesday, June 26, 2018

xna 4.0 - Getting a black scene with XNA, what am I doing wrong?


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);
}

Answer



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?


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...