From b9cc8e8871de73bf70406913f18b446a1fc723e8 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 12 Jan 2010 13:01:59 +0100 Subject: Generate triangle data from tile heightmaps --- src/de/gamezock/metacraft/data/Map.java | 89 ++++++++++++++++++++++++++------- 1 file changed, 72 insertions(+), 17 deletions(-) (limited to 'src/de/gamezock/metacraft/data/Map.java') diff --git a/src/de/gamezock/metacraft/data/Map.java b/src/de/gamezock/metacraft/data/Map.java index e387628..770ffe7 100644 --- a/src/de/gamezock/metacraft/data/Map.java +++ b/src/de/gamezock/metacraft/data/Map.java @@ -1,41 +1,96 @@ package de.gamezock.metacraft.data; +import java.util.List; +import java.util.Vector; + +import javax.vecmath.Point3f; + public class Map { - private Tile[][] tiles; - private float[][] heightmap; - - private int width, height; - private int tileSize; - + private List triangleData; + private float[][] heightmap; + + public Map() { - width = height = 1; + int width = 5; + int height = 5; - heightmap = new float[width][height]; - heightmap[0][0] = 0; + float[][] tileHeightmap = new float[width][height]; + Tile[][] tiles = new Tile[width][height]; - tiles = new Tile[width][height]; - tiles[0][0] = new Tile(new TestTile()); + for(int i = 0; i < width; ++i) { + for(int j = 0; j < height; ++j) { + tileHeightmap[i][j] = ((i-2)*(i-2)+(j-2)*(j-2))*0.5f; + tiles[i][j] = new Tile(new TestTile()); + } + } - tileSize = tiles[0][0].getData().getSize(); + createMapData(tiles, tileHeightmap); } - public Tile getTile(int x, int y) { + private void createMapData(Tile[][] tiles, float[][] tileHeightmap) { + int tileSize = tiles[0][0].getData().getSize(); + + heightmap = new float[tiles.length * tileSize][tiles[0].length * tileSize]; + + for(int i = 0; i < tiles.length; ++i) { + for(int j = 0; j < tiles[i].length; ++j) { + float[][] localHeightmap = tiles[i][j].getData().getHeightmap(); + + for(int x = 0; x < tileSize; ++x) { + for(int y = 0; y < tileSize; ++y) { + heightmap[i*tileSize + x][j*tileSize + y] = tileHeightmap[i][j] + localHeightmap[x][y]; + } + } + } + } + + triangleData = new Vector(); + + for(int x = 0; x < heightmap.length-1; ++x) { + for(int y = 0; y < heightmap[x].length-1; ++y) { + float[] h = new float[4]; + h[0] = heightmap[x][y]; + h[1] = heightmap[x+1][y]; + h[2] = heightmap[x+1][y+1]; + h[3] = heightmap[x][y+1]; + + float center = (h[0] + h[1] + h[2] + h[3]) / 4; + + for(int i = 0; i < 4; ++i) { + int i_1 = (i+1)%4; + int i_2 = (i+2)%4; + + Point3f v1 = new Point3f(x + i_1/2, y + i/2, h[i]); + Point3f v2 = new Point3f(x+0.5f, y+0.5f, center); + Point3f v3 = new Point3f(x + i_2/2, y + i_1/2, h[i_1]); + + triangleData.add(new Triangle(v1, v2, v3)); + } + } + } + } + + /*public Tile getTile(int x, int y) { return tiles[x][y]; } public float getTileHeight(int x, int y) { return heightmap[x][y]; - } + }*/ public int getWidth() { - return width; + return heightmap.length; } public int getHeight() { - return height; + return heightmap[0].length; } - public int getTileSize() { + /*public int getTileSize() { return tileSize; + }*/ + + public List getTriangleData() { + return triangleData; } } -- cgit v1.2.3