summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/de/gamezock/metacraft/data/Map.java116
-rw-r--r--src/de/gamezock/metacraft/data/TestMap.java15
-rw-r--r--src/de/gamezock/metacraft/data/TestTile.java134
-rw-r--r--src/de/gamezock/metacraft/data/Tile.java13
-rw-r--r--src/de/gamezock/metacraft/data/TileData.java14
-rw-r--r--src/de/gamezock/metacraft/data/TileType.java5
-rw-r--r--src/de/gamezock/metacraft/ui/Main.java42
-rw-r--r--src/de/gamezock/metacraft/ui/Renderer.java37
8 files changed, 134 insertions, 242 deletions
diff --git a/src/de/gamezock/metacraft/data/Map.java b/src/de/gamezock/metacraft/data/Map.java
index c6d306b..79c591a 100644
--- a/src/de/gamezock/metacraft/data/Map.java
+++ b/src/de/gamezock/metacraft/data/Map.java
@@ -1,33 +1,105 @@
package de.gamezock.metacraft.data;
+import java.nio.IntBuffer;
+import java.util.List;
+import java.util.Vector;
-public class Map {
- Tile[][] tiles;
+import javax.vecmath.Point3f;
+import javax.vecmath.Vector3f;
+
+
+public abstract class Map {
private float[][] heightmap;
+ private VertexBuffer vertexBuffer;
+ private IntBuffer indexBuffer;
+
+
+ private void setVertex(int x, int y, Point3f vertex) {
+ vertexBuffer.setVertex(getVertexIndex(x, y), vertex);
+ }
+
+ private Point3f getVertex(int x, int y) {
+ return vertexBuffer.getVertex(getVertexIndex(x, y));
+ }
+
+ private int getVertexIndex(int x, int y) {
+ if(y >= getHeight()) {
+ return getWidth()*getHeight() + (y-getHeight())*(getWidth()-1) + x;
+ } else {
+ return y*getWidth() + x;
+ }
+ }
+
+ public VertexBuffer getVertexBuffer() {
+ return vertexBuffer;
+ }
- public Map() {
- int width = 5;
- int height = 5;
+ public IntBuffer getIndexBuffer() {
+ return indexBuffer;
+ }
+
+ protected void createMapData(float[][] heightmap) {
+ this.heightmap = heightmap;
+
+ List<IndexedTriangle> triangleData = new Vector<IndexedTriangle>();
- heightmap = new float[width][height];
- tiles = new Tile[width][height];
+ vertexBuffer = new VertexBuffer(getWidth()*getHeight() + (getWidth()-1)*(getHeight()-1));
- for(int i = 0; i < width; ++i) {
- for(int j = 0; j < height; ++j) {
- //heightmap[i][j] = ((i-2)*(i-2)+(j-2)*(j-2))*0.5f;
- heightmap[i][j] = 0;
- tiles[i][j] = new Tile(new TestTile());
+ for(int x = 0; x < getWidth(); ++x) {
+ for(int y = 0; y < getHeight(); ++y) {
+ setVertex(x, y, new Point3f(x, y, heightmap[x][y]));
+
+ if(x > 0 && y > 0) {
+ Point3f center = new Point3f();
+
+ center.add(getVertex(x-1, y-1));
+ center.add(getVertex(x, y-1));
+ center.add(getVertex(x-1, y));
+ center.add(getVertex(x, y));
+
+ center.scale(0.25f);
+
+ setVertex(x-1, getHeight()+y-1, center);
+ }
}
}
- }
-
- public Tile getTile(int x, int y) {
- return tiles[x][y];
- }
-
- public float getTileHeight(int x, int y) {
- return heightmap[x][y];
+
+ for(int x = 0; x < getWidth()-1; ++x) {
+ for(int y = 0; y < getHeight()-1; ++y) {
+ for(int i = 0; i < 4; ++i) {
+ int i_1 = (i+1)%4;
+ int i_2 = (i+2)%4;
+
+ int v1 = getVertexIndex(x + i_1/2, y + i/2);
+ int v2 = getVertexIndex(x, getHeight()+y);
+ int v3 = getVertexIndex(x + i_2/2, y + i_1/2);
+
+ triangleData.add(new IndexedTriangle(vertexBuffer, v1, v2, v3));
+ }
+ }
+ }
+
+ indexBuffer = IntBuffer.allocate(3 * triangleData.size());
+ for(IndexedTriangle t : triangleData) {
+ Vector3f normal = t.getNormal();
+
+ for(int i = 0; i < 3; ++i) {
+ int index = t.getIndex(i);
+
+ Vector3f vNormal = vertexBuffer.getNormal(index);
+ vNormal.add(normal);
+ vertexBuffer.setNormal(index, vNormal);
+
+ indexBuffer.put(index);
+ }
+ }
+
+ for(int i = 0; i < vertexBuffer.getSize(); ++i) {
+ Vector3f normal = vertexBuffer.getNormal(i);
+ normal.normalize();
+ vertexBuffer.setNormal(i, normal);
+ }
}
public int getWidth() {
@@ -37,8 +109,4 @@ public class Map {
public int getHeight() {
return heightmap[0].length;
}
-
- public int getTileSize() {
- return tiles[0][0].getData().getSize();
- }
}
diff --git a/src/de/gamezock/metacraft/data/TestMap.java b/src/de/gamezock/metacraft/data/TestMap.java
new file mode 100644
index 0000000..58a6bba
--- /dev/null
+++ b/src/de/gamezock/metacraft/data/TestMap.java
@@ -0,0 +1,15 @@
+package de.gamezock.metacraft.data;
+
+public class TestMap extends Map {
+ public TestMap() {
+ float[][] heightmap = new float[41][41];
+
+ for(int x = 0; x < heightmap.length; ++x) {
+ for(int y = 0; y < heightmap[0].length; ++y) {
+ heightmap[x][y] = (float)Math.pow((Math.cos(x*Math.PI/4)+Math.cos(y*Math.PI/4)), 3)*0.2f;
+ }
+ }
+
+ createMapData(heightmap);
+ }
+}
diff --git a/src/de/gamezock/metacraft/data/TestTile.java b/src/de/gamezock/metacraft/data/TestTile.java
deleted file mode 100644
index 0516cec..0000000
--- a/src/de/gamezock/metacraft/data/TestTile.java
+++ /dev/null
@@ -1,134 +0,0 @@
-package de.gamezock.metacraft.data;
-
-import java.nio.IntBuffer;
-import java.util.List;
-import java.util.Vector;
-
-import javax.vecmath.Point3f;
-import javax.vecmath.Vector3f;
-
-public class TestTile implements TileData {
- private List<IndexedTriangle> triangleData;
- private float[][] heightmap;
- private VertexBuffer vertexBuffer;
- private IntBuffer indexBuffer;
-
-
- public TestTile() {
- heightmap = new float[getSize()+1][getSize()+1];
-
- for(int i = 0; i <= getSize(); ++i) {
- for(int j = 0; j <= getSize(); ++j) {
- heightmap[i][j] = ((i-4)*(i-4)+(j-4)*(j-4))*0.05f;
- }
- }
-
- createTileData();
- }
-
- private void setVertex(int x, int y, Point3f vertex) {
- vertexBuffer.setVertex(getVertexIndex(x, y), vertex);
- }
-
- private Point3f getVertex(int x, int y) {
- return vertexBuffer.getVertex(getVertexIndex(x, y));
- }
-
- private int getVertexIndex(int x, int y) {
- if(y > getSize()) {
- return (getSize()+1)*(getSize()+1) + (y-getSize()-1)*getSize() + x;
- } else {
- return y*(getSize()+1) + x;
- }
- }
-
- private void createTileData() {
- triangleData = new Vector<IndexedTriangle>();
-
- vertexBuffer = new VertexBuffer((getSize()+1)*(getSize()+1) + getSize()*getSize());
-
- for(int x = 0; x < heightmap.length; ++x) {
- for(int y = 0; y < heightmap[x].length; ++y) {
- setVertex(x, y, new Point3f(x, y, heightmap[x][y]));
-
- if(x != 0 && y != 0) {
- Point3f center = new Point3f();
-
- center.add(getVertex(x-1, y-1));
- center.add(getVertex(x, y-1));
- center.add(getVertex(x-1, y));
- center.add(getVertex(x, y));
-
- center.scale(0.25f);
-
- setVertex(x-1, y+getSize(), center);
- }
- }
- }
-
- for(int x = 0; x < heightmap.length-1; ++x) {
- for(int y = 0; y < heightmap[x].length-1; ++y) {
- for(int i = 0; i < 4; ++i) {
- int i_1 = (i+1)%4;
- int i_2 = (i+2)%4;
-
- int v1 = getVertexIndex(x + i_1/2, y + i/2);
- int v2 = getVertexIndex(x, y+getSize()+1);
- int v3 = getVertexIndex(x + i_2/2, y + i_1/2);
-
- triangleData.add(new IndexedTriangle(vertexBuffer, v1, v2, v3));
- }
- }
- }
-
- for(IndexedTriangle t : triangleData) {
- Vector3f normal = t.getNormal();
-
- for(int i = 0; i < 3; ++i) {
- int index = t.getIndex(i);
-
- Vector3f vNormal = vertexBuffer.getNormal(index);
- vNormal.add(normal);
- vertexBuffer.setNormal(index, vNormal);
- }
- }
-
- for(int i = 0; i < vertexBuffer.getSize(); ++i) {
- Vector3f normal = vertexBuffer.getNormal(i);
- normal.normalize();
- vertexBuffer.setNormal(i, normal);
- }
-
- indexBuffer = IntBuffer.allocate(3 * triangleData.size());
- for(IndexedTriangle t : triangleData) {
- for(int i = 0; i < 3; ++i) {
- indexBuffer.put(t.getIndex(i));
- }
- }
- }
-
- @Override
- public int getSize() {
- return 8;
- }
-
- @Override
- public float[][] getHeightmap() {
- return heightmap;
- }
-
- @Override
- public TileType getType() {
- return new TileType();
- }
-
- @Override
- public List<IndexedTriangle> getTriangles() {
- return triangleData;
- }
-
- @Override
- public IntBuffer getIndexBuffer() {
- return indexBuffer;
- }
-}
diff --git a/src/de/gamezock/metacraft/data/Tile.java b/src/de/gamezock/metacraft/data/Tile.java
deleted file mode 100644
index 75c4628..0000000
--- a/src/de/gamezock/metacraft/data/Tile.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package de.gamezock.metacraft.data;
-
-public class Tile {
- private TileData tileData;
-
- public Tile(TileData tileData) {
- this.tileData = tileData;
- }
-
- public TileData getData() {
- return tileData;
- }
-}
diff --git a/src/de/gamezock/metacraft/data/TileData.java b/src/de/gamezock/metacraft/data/TileData.java
deleted file mode 100644
index 5b34553..0000000
--- a/src/de/gamezock/metacraft/data/TileData.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package de.gamezock.metacraft.data;
-
-import java.nio.IntBuffer;
-import java.util.List;
-
-public interface TileData {
- public int getSize();
-
- public float[][] getHeightmap();
- public List<IndexedTriangle> getTriangles();
- public IntBuffer getIndexBuffer();
-
- public TileType getType();
-}
diff --git a/src/de/gamezock/metacraft/data/TileType.java b/src/de/gamezock/metacraft/data/TileType.java
deleted file mode 100644
index 35db302..0000000
--- a/src/de/gamezock/metacraft/data/TileType.java
+++ /dev/null
@@ -1,5 +0,0 @@
-package de.gamezock.metacraft.data;
-
-public class TileType {
-
-}
diff --git a/src/de/gamezock/metacraft/ui/Main.java b/src/de/gamezock/metacraft/ui/Main.java
index f5eb3f9..287af22 100644
--- a/src/de/gamezock/metacraft/ui/Main.java
+++ b/src/de/gamezock/metacraft/ui/Main.java
@@ -16,16 +16,17 @@ import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import de.gamezock.metacraft.data.Map;
+import de.gamezock.metacraft.data.TestMap;
public class Main implements GLEventListener {
private Renderer renderer = new Renderer(this);
private Frame frame = new Frame("metacraft");
private GLCanvas canvas;
-
private GLU glu = new GLU();
- private Map currentMap = new Map();
+ private Map currentMap = new TestMap();
+
public Main() {
GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2));
@@ -56,27 +57,6 @@ public class Main implements GLEventListener {
frame.setVisible(true);
canvas.requestFocusInWindow();
-
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- }
-
- new Thread(new Runnable() {
-
- @Override
- public void run() {
- while(true) {
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- }
-
- canvas.display();
- }
- }
-
- }).start();
}
Map getCurrentMap() {
@@ -117,6 +97,22 @@ public class Main implements GLEventListener {
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, new float[] {10, 10, 10, 1}, 0);
gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, new float[] {0, 0, 0, 0}, 0);
+
+ new Thread(new Runnable() {
+
+ @Override
+ public void run() {
+ while(true) {
+ canvas.display();
+
+ try {
+ Thread.sleep(10);
+ } catch (InterruptedException e) {
+ }
+ }
+ }
+
+ }).start();
}
@Override
diff --git a/src/de/gamezock/metacraft/ui/Renderer.java b/src/de/gamezock/metacraft/ui/Renderer.java
index 6d8d04a..8d4b8d2 100644
--- a/src/de/gamezock/metacraft/ui/Renderer.java
+++ b/src/de/gamezock/metacraft/ui/Renderer.java
@@ -1,14 +1,11 @@
package de.gamezock.metacraft.ui;
import java.nio.IntBuffer;
-import java.util.List;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
-import de.gamezock.metacraft.data.IndexedTriangle;
import de.gamezock.metacraft.data.Map;
-import de.gamezock.metacraft.data.Tile;
import de.gamezock.metacraft.data.VertexBuffer;
public class Renderer {
@@ -18,22 +15,6 @@ public class Renderer {
this.main = main;
}
- private void renderTile(GL2 gl, Tile tile) {
- List<IndexedTriangle> triangles = tile.getData().getTriangles();
-
- if(triangles.isEmpty())
- return;
-
- VertexBuffer buffer = triangles.get(0).getBuffer();
-
- gl.glInterleavedArrays(GL2.GL_N3F_V3F, 0, buffer.getBuffer().clear());
-
- IntBuffer indexBuffer = tile.getData().getIndexBuffer();
- indexBuffer.clear();
-
- gl.glDrawElements(GL.GL_TRIANGLES, indexBuffer.capacity(), GL2.GL_UNSIGNED_INT, indexBuffer);
- }
-
public void render(GL2 gl) {
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
@@ -48,16 +29,14 @@ public class Renderer {
gl.glColor3f(1, 1, 1);
- for(int x = 0; x < currentMap.getWidth(); ++x) {
- for(int y = 0; y < currentMap.getHeight(); ++y) {
- gl.glPushMatrix();
-
- gl.glTranslatef(currentMap.getTileSize()*x, currentMap.getTileSize()*y, currentMap.getTileHeight(x, y));
- renderTile(gl, currentMap.getTile(x, y));
-
- gl.glPopMatrix();
- }
- }
+ VertexBuffer buffer = currentMap.getVertexBuffer();
+
+ gl.glInterleavedArrays(GL2.GL_N3F_V3F, 0, buffer.getBuffer().clear());
+
+ IntBuffer indexBuffer = currentMap.getIndexBuffer();
+ indexBuffer.clear();
+
+ gl.glDrawElements(GL.GL_TRIANGLES, indexBuffer.capacity(), GL2.GL_UNSIGNED_INT, indexBuffer);
gl.glPopMatrix();
}