diff --git a/core/src/se/cookery/gfx/character/Player.java b/core/src/se/cookery/gfx/character/Player.java index 9b18364..8e15aa2 100644 --- a/core/src/se/cookery/gfx/character/Player.java +++ b/core/src/se/cookery/gfx/character/Player.java @@ -14,7 +14,7 @@ public class Player { public Player(AssetManager assetManager) { Geometry box = GeometryUtil.createBox(assetManager, 0.5f,0.7f,0.5f, ColorRGBA.Red); - box.setLocalTranslation(0.25f, 0.7f, 0.25f); + box.setLocalTranslation(0.5f, 0.7f, 0.5f); playerNode.attachChild(box); } diff --git a/core/src/se/cookery/gfx/terrain/TerrainMesh.java b/core/src/se/cookery/gfx/terrain/TerrainMesh.java index abbbdef..e638196 100644 --- a/core/src/se/cookery/gfx/terrain/TerrainMesh.java +++ b/core/src/se/cookery/gfx/terrain/TerrainMesh.java @@ -32,47 +32,69 @@ public class TerrainMesh extends Mesh { * @param heightCount the number of squares that make up the whole terrain */ public TerrainMesh(float width, float height, int widthCount, int heightCount){ - this.width = 1; - this.height = 1; + this.width = width; + this.height = height; updateGeometry(); } - protected void updateGeometry() { - // Setup vertexes - setBuffer(VertexBuffer.Type.Position, 3, new float[]{ - // x, y, z - 0, 0, 0, - 0, 0, height, - width, 0, height, - width, 0, 0, - }); - - // Setup planes - setBuffer(VertexBuffer.Type.Index, 3, new short[]{ - 0, 1, 3, - 3, 1, 2 - }); - - // Setup texture UV coordinates - setBuffer(VertexBuffer.Type.TexCoord, 2, new float[]{ - // u, v - 0, 0, - 1, 0, - 1, 1, - 0, 1 - }); - - // Setup light normals - setBuffer(VertexBuffer.Type.Normal, 3, new float[]{ - 0, 0, 1, - 0, 0, 1, - 0, 0, 1, - 0, 0, 1 - }); + setBuffer(VertexBuffer.Type.Position, 3, generateVertexBuffer()); + setBuffer(VertexBuffer.Type.Index, 3, generateIndexBuffer()); + setBuffer(VertexBuffer.Type.TexCoord, 2, generateTextureBuffer()); + setBuffer(VertexBuffer.Type.Normal, 3, generateNormalBuffer()); updateBound(); setStatic(); } + + /** + * Generates a vertex buffer containing vertex xyz coordinates. + */ + protected float[] generateVertexBuffer() { + float[] vertexBuf = new float[3 * widthCount * heightCount]; + + return new float[]{ + // x, y, z + 0, 0, 0, + 0, 0, height, + width, 0, height, + width, 0, 0, + }; + } + + /** + * Generates a index buffer containing planes for the mesh. + */ + protected short[] generateIndexBuffer() { + return new short[]{ + 0, 1, 3, + 3, 1, 2 + }; + } + + /** + * Generates a texture buffer containing texture UV coordinates. + */ + protected float[] generateTextureBuffer() { + return new float[]{ + // u, v + 0, 0, + 1, 0, + 1, 1, + 0, 1 + }; + } + + /** + * Generates a normal buffer containing light normals. + */ + protected float[] generateNormalBuffer() { + return new float[]{ + 0, 0, 1, + 0, 0, 1, + 0, 0, 1, + 0, 0, 1 + }; + } } \ No newline at end of file diff --git a/core/src/se/cookery/gfx/util/GeometryUtil.java b/core/src/se/cookery/gfx/util/GeometryUtil.java index 0eed071..aed648c 100644 --- a/core/src/se/cookery/gfx/util/GeometryUtil.java +++ b/core/src/se/cookery/gfx/util/GeometryUtil.java @@ -7,8 +7,8 @@ import com.jme3.scene.shape.Box; public class GeometryUtil { - public static Geometry createBox(AssetManager assetManager, float x, float y, float z, ColorRGBA color) { - Geometry box = new Geometry("Box", new Box(x,y,z)); + public static Geometry createBox(AssetManager assetManager, float width, float height, float depth, ColorRGBA color) { + Geometry box = new Geometry("Box", new Box(width/2, height/2, depth/2)); box.setMaterial(MaterialUtil.createMaterial(assetManager, color)); return box; diff --git a/core/test/se/cookery/core/world/BlockTest.java b/core/test/se/cookery/core/world/BlockTest.java index f82b522..92db559 100644 --- a/core/test/se/cookery/core/world/BlockTest.java +++ b/core/test/se/cookery/core/world/BlockTest.java @@ -12,8 +12,8 @@ class BlockTest { Block block = new Block(0, 0); assertEquals(0, block.getBlockStartX()); assertEquals(0, block.getBlockStartY()); - assertEquals(19, block.getBlockEndX()); - assertEquals(19, block.getBlockEndY()); + assertEquals(Block.BLOCK_SIZE-1, block.getBlockEndX()); + assertEquals(Block.BLOCK_SIZE-1, block.getBlockEndY()); } @Test diff --git a/core/test/se/cookery/gfx/terrain/TerrainMeshTest.java b/core/test/se/cookery/gfx/terrain/TerrainMeshTest.java new file mode 100644 index 0000000..ff2aaf9 --- /dev/null +++ b/core/test/se/cookery/gfx/terrain/TerrainMeshTest.java @@ -0,0 +1,36 @@ +package se.cookery.gfx.terrain; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class TerrainMeshTest { + + @Test + public void singleSquareTerrain(){ + TerrainMesh terrain = new TerrainMesh(1, 1, 1 ,1); + + float[] index = terrain.generateVertexBuffer(); + assertEquals(3*4, index.length); + assertArrayEquals(new float[]{ + 0, 0, 0, + 0, 0, 1, + 1, 0, 1, + 1, 0, 0, + }, index); + } + + @Test + public void fourSquareTerrain(){ + TerrainMesh terrain = new TerrainMesh(1, 1, 2 ,2); + + float[] index = terrain.generateVertexBuffer(); + assertEquals(3*9, index.length); + assertArrayEquals(new float[]{ + 0, 0, 0, + 0, 0, 1, + 1, 0, 1, + 1, 0, 0, + }, index); + } +} \ No newline at end of file