Skip to content

Commit

Permalink
[097] Texturen und Blocktypen dynamisch ermitteln und rendern
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwendel_cp committed Mar 10, 2015
1 parent 931c80e commit a02001a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 61 deletions.
2 changes: 1 addition & 1 deletion OctoAwesome/OctoAwesome.Model/DebugMapGenerator.cs
Expand Up @@ -35,7 +35,7 @@ public IChunk[] GenerateChunk(IPlanet planet, Index2 index)

if (z < (int)(16 + height))
{
result[layer].SetBlock(x, y, z, new StoneBlock());
result[layer].SetBlock(x, y, z, new SandBlock());
}
}
}
Expand Down
Binary file modified OctoAwesome/OctoAwesome.Model/Resources/grass_bottom.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified OctoAwesome/OctoAwesome.Model/Resources/grass_top.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions OctoAwesome/OctoAwesomeDX/BlockDefinitionManager.cs
Expand Up @@ -16,6 +16,10 @@ public static IEnumerable<IBlockDefinition> GetBlockDefinitions()
{
definitions = new List<IBlockDefinition>();
definitions.Add(new GrassBlockDefinition());
definitions.Add(new GroundBlockDefinition());
definitions.Add(new SandBlockDefinition());
definitions.Add(new StoneBlockDefinition());
definitions.Add(new WaterBlockDefinition());
}

return definitions;
Expand Down
128 changes: 68 additions & 60 deletions OctoAwesome/OctoAwesomeDX/Components/ChunkRenderer.cs
Expand Up @@ -62,8 +62,8 @@ public void Draw(Matrix view)
return;

effect.World = Matrix.CreateTranslation(
RelativeIndex.X * Chunk.CHUNKSIZE_X,
RelativeIndex.Y * Chunk.CHUNKSIZE_Y,
RelativeIndex.X * Chunk.CHUNKSIZE_X,
RelativeIndex.Y * Chunk.CHUNKSIZE_Y,
RelativeIndex.Z * Chunk.CHUNKSIZE_Z);
effect.View = view;
effect.Texture = textures;
Expand Down Expand Up @@ -96,50 +96,50 @@ public void RegenerateVertexBuffer()

List<VertexPositionNormalTexture> vertices = new List<VertexPositionNormalTexture>();
List<int> index = new List<int>();
int textureColumns = textures.Width / Render3DComponent.TEXTURESIZE;
float textureWidth = 1f / textureColumns;

// BlockTypes sammlen
var definitions = BlockDefinitionManager.GetBlockDefinitions();
Dictionary<Type, int> typeMapping = new Dictionary<Type, int>();
int definitionIndex = 0;
foreach (var definition in definitions)
typeMapping.Add(definition.GetBlockType(), definitionIndex++);

for (int z = 0; z < Chunk.CHUNKSIZE_Z; z++)
{
for (int y = 0; y < Chunk.CHUNKSIZE_Y; y++)
{
for (int x = 0; x < Chunk.CHUNKSIZE_X; x++)
{
Index3 pos = new Index3(x, y, z);
IBlock block = chunk.GetBlock(x, y, z);
if (block == null)
continue;

if (chunk.GetBlock(pos) == null)
if (!typeMapping.ContainsKey(block.GetType()))
continue;

int textureIndex;
if (!typeMapping.TryGetValue(block.GetType(), out textureIndex))
continue;
textureIndex *= 3;

// Textur-Koordinate "berechnen"
Vector2 textureOffset = new Vector2();
Vector2 textureSize = new Vector2(0.245f, 0.245f);
if (chunk.GetBlock(pos) is GrassBlock)
{
textureOffset = new Vector2(0.002f, 0.002f);
}
else if (chunk.GetBlock(pos) is SandBlock)
{
textureOffset = new Vector2(0.252f, 0.002f);
}
else if (chunk.GetBlock(pos) is GroundBlock)
{
textureOffset = new Vector2(0.502f, 0.002f);
}
else if (chunk.GetBlock(pos) is StoneBlock)
{
textureOffset = new Vector2(0.752f, 0.002f);
}
else if (chunk.GetBlock(pos) is WaterBlock)
{
textureOffset = new Vector2(0.002f, 0.252f);
}
Vector2 textureSize = new Vector2(textureWidth - 0.005f, textureWidth - 0.005f);

// Oben
if (y == Chunk.CHUNKSIZE_Y - 1 || chunk.GetBlock(new Index3(x, y + 1, z)) == null)
if (z == Chunk.CHUNKSIZE_Z - 1 || chunk.GetBlock(new Index3(x, y, z + 1)) == null)
{
textureOffset = new Vector2(
(((textureIndex + 0) % textureColumns) * textureWidth) + 0.002f,
((int)((textureIndex + 0) / textureColumns) * textureWidth) + 0.002f);

int localOffset = vertices.Count;
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 1, z + 0), Vector3.Up, textureOffset));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 1, z + 0), Vector3.Up, new Vector2(textureOffset.X + textureSize.X, textureOffset.Y)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 1, z + 1), Vector3.Up, new Vector2(textureOffset.X, textureOffset.Y + textureSize.Y)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 1, z + 1), Vector3.Up, textureOffset + textureSize));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 1, z + 1), new Vector3(0, 0, 1), textureOffset));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 1, z + 1), new Vector3(0, 0, 1), new Vector2(textureOffset.X + textureSize.X, textureOffset.Y)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 0, z + 1), new Vector3(0, 0, 1), new Vector2(textureOffset.X, textureOffset.Y + textureSize.X)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 0, z + 1), new Vector3(0, 0, 1), textureOffset + textureSize));
index.Add(localOffset + 0);
index.Add(localOffset + 1);
index.Add(localOffset + 3);
Expand All @@ -148,14 +148,18 @@ public void RegenerateVertexBuffer()
index.Add(localOffset + 2);
}

// Links
if (x == 0 || chunk.GetBlock(new Index3(x - 1, y, z)) == null)
// Unten
if (z == 0 || chunk.GetBlock(new Index3(x, y, z - 1)) == null)
{
textureOffset = new Vector2(
(((textureIndex + 1) % textureColumns) * textureWidth) + 0.002f,
((int)((textureIndex + 1) / textureColumns) * textureWidth) + 0.002f);

int localOffset = vertices.Count;
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 1, z + 0), Vector3.Left, textureOffset));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 1, z + 1), Vector3.Left, new Vector2(textureOffset.X + textureSize.X, textureOffset.Y)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 0, z + 0), Vector3.Left, new Vector2(textureOffset.X, textureOffset.Y + +textureSize.X)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 0, z + 1), Vector3.Left, textureOffset + textureSize));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 1, z + 0), new Vector3(0, 0, -1), textureOffset));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 1, z + 0), new Vector3(0, 0, -1), new Vector2(textureOffset.X + textureSize.X, textureOffset.Y)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 0, z + 0), new Vector3(0, 0, -1), new Vector2(textureOffset.X, textureOffset.Y + +textureSize.X)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 0, z + 0), new Vector3(0, 0, -1), textureOffset + textureSize));
index.Add(localOffset + 0);
index.Add(localOffset + 1);
index.Add(localOffset + 3);
Expand All @@ -164,14 +168,18 @@ public void RegenerateVertexBuffer()
index.Add(localOffset + 2);
}

// Vorne
if (z == Chunk.CHUNKSIZE_Z - 1 || chunk.GetBlock(new Index3(x, y, z + 1)) == null)
textureOffset = new Vector2(
(((textureIndex + 2) % textureColumns) * textureWidth) + 0.002f,
((int)((textureIndex + 2) / textureColumns) * textureWidth) + 0.002f);

// Hinten
if (y == Chunk.CHUNKSIZE_Y - 1 || chunk.GetBlock(new Index3(x, y + 1, z)) == null)
{
int localOffset = vertices.Count;
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 1, z + 1), Vector3.Forward, textureOffset));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 1, z + 1), Vector3.Forward, new Vector2(textureOffset.X + textureSize.X, textureOffset.Y)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 0, z + 1), Vector3.Forward, new Vector2(textureOffset.X, textureOffset.Y + +textureSize.X)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 0, z + 1), Vector3.Forward, textureOffset + textureSize));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 1, z + 0), new Vector3(0, 1, 0), textureOffset + textureSize));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 1, z + 0), new Vector3(0, 1, 0), new Vector2(textureOffset.X, textureOffset.Y + textureSize.Y)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 1, z + 1), new Vector3(0, 1, 0), new Vector2(textureOffset.X + textureSize.X, textureOffset.Y)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 1, z + 1), new Vector3(0, 1, 0), textureOffset));
index.Add(localOffset + 0);
index.Add(localOffset + 1);
index.Add(localOffset + 3);
Expand All @@ -180,14 +188,14 @@ public void RegenerateVertexBuffer()
index.Add(localOffset + 2);
}

// Rechts
if (x == Chunk.CHUNKSIZE_X - 1 || chunk.GetBlock(new Index3(x + 1, y, z)) == null)
// Vorne
if (y == 0 || chunk.GetBlock(new Index3(x, y - 1, z)) == null)
{
int localOffset = vertices.Count;
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 1, z + 1), Vector3.Right, textureOffset));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 1, z + 0), Vector3.Right, new Vector2(textureOffset.X + textureSize.X, textureOffset.Y)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 0, z + 1), Vector3.Right, new Vector2(textureOffset.X, textureOffset.Y + +textureSize.X)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 0, z + 0), Vector3.Right, textureOffset + textureSize));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 0, z + 1), new Vector3(0, -1, 0), textureOffset));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 0, z + 1), new Vector3(0, -1, 0), new Vector2(textureOffset.X + textureSize.X, textureOffset.Y)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 0, z + 0), new Vector3(0, -1, 0), new Vector2(textureOffset.X, textureOffset.Y + +textureSize.X)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 0, z + 0), new Vector3(0, -1, 0), textureOffset + textureSize));
index.Add(localOffset + 0);
index.Add(localOffset + 1);
index.Add(localOffset + 3);
Expand All @@ -196,14 +204,14 @@ public void RegenerateVertexBuffer()
index.Add(localOffset + 2);
}

// Hinten
if (z == 0 || chunk.GetBlock(new Index3(x, y, z - 1)) == null)
// Links
if (x == 0 || chunk.GetBlock(new Index3(x - 1, y, z)) == null)
{
int localOffset = vertices.Count;
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 1, z + 0), Vector3.Backward, textureOffset));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 1, z + 0), Vector3.Backward, new Vector2(textureOffset.X + textureSize.X, textureOffset.Y)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 0, z + 0), Vector3.Backward, new Vector2(textureOffset.X, textureOffset.Y + +textureSize.X)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 0, z + 0), Vector3.Backward, textureOffset + textureSize));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 1, z + 0), new Vector3(-1, 0, 0), new Vector2(textureOffset.X, textureOffset.Y + +textureSize.X)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 1, z + 1), new Vector3(-1, 0, 0), textureOffset));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 0, z + 0), new Vector3(-1, 0, 0), textureOffset + textureSize));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 0, z + 1), new Vector3(-1, 0, 0), new Vector2(textureOffset.X + textureSize.X, textureOffset.Y)));
index.Add(localOffset + 0);
index.Add(localOffset + 1);
index.Add(localOffset + 3);
Expand All @@ -212,14 +220,14 @@ public void RegenerateVertexBuffer()
index.Add(localOffset + 2);
}

// Unten
if (y == 0 || chunk.GetBlock(new Index3(x, y - 1, z)) == null)
// Rechts
if (x == Chunk.CHUNKSIZE_X - 1 || chunk.GetBlock(new Index3(x + 1, y, z)) == null)
{
int localOffset = vertices.Count;
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 0, z + 1), Vector3.Down, textureOffset));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 0, z + 1), Vector3.Down, new Vector2(textureOffset.X + textureSize.X, textureOffset.Y)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 0, y + 0, z + 0), Vector3.Down, new Vector2(textureOffset.X, textureOffset.Y + +textureSize.X)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 0, z + 0), Vector3.Down, textureOffset + textureSize));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 1, z + 1), new Vector3(1, 0, 0), new Vector2(textureOffset.X + textureSize.X, textureOffset.Y)));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 1, z + 0), new Vector3(1, 0, 0), textureOffset + textureSize));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 0, z + 1), new Vector3(1, 0, 0), textureOffset));
vertices.Add(new VertexPositionNormalTexture(new Vector3(x + 1, y + 0, z + 0), new Vector3(1, 0, 0), new Vector2(textureOffset.X, textureOffset.Y + +textureSize.X)));
index.Add(localOffset + 0);
index.Add(localOffset + 1);
index.Add(localOffset + 3);
Expand Down

0 comments on commit a02001a

Please sign in to comment.