summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2010-01-16 17:50:43 +0100
committerMatthias Schiffer <matthias@gamezock.de>2010-01-16 17:50:43 +0100
commit32117d0bf27e0a72165707fe4e56d231136e734b (patch)
treedf5de7dedbfa7084227122505b96a4bc1d0845a0 /src
parentb9cc8e8871de73bf70406913f18b446a1fc723e8 (diff)
downloadmetacraft-32117d0bf27e0a72165707fe4e56d231136e734b.tar
metacraft-32117d0bf27e0a72165707fe4e56d231136e734b.zip
Use index buffers to render tiles
Diffstat (limited to 'src')
-rw-r--r--src/de/gamezock/metacraft/data/IndexedTriangle.java51
-rw-r--r--src/de/gamezock/metacraft/data/Map.java70
-rw-r--r--src/de/gamezock/metacraft/data/TestTile.java114
-rw-r--r--src/de/gamezock/metacraft/data/TileData.java5
-rw-r--r--src/de/gamezock/metacraft/data/Triangle.java30
-rw-r--r--src/de/gamezock/metacraft/data/VertexBuffer.java60
-rw-r--r--src/de/gamezock/metacraft/ui/Main.java2
-rw-r--r--src/de/gamezock/metacraft/ui/Renderer.java49
8 files changed, 292 insertions, 89 deletions
diff --git a/src/de/gamezock/metacraft/data/IndexedTriangle.java b/src/de/gamezock/metacraft/data/IndexedTriangle.java
new file mode 100644
index 0000000..9a999d6
--- /dev/null
+++ b/src/de/gamezock/metacraft/data/IndexedTriangle.java
@@ -0,0 +1,51 @@
+package de.gamezock.metacraft.data;
+
+import javax.vecmath.Point3f;
+import javax.vecmath.Vector3f;
+
+public class IndexedTriangle {
+ private VertexBuffer buffer;
+
+ private int[] indices;
+ private Vector3f normal;
+
+ public IndexedTriangle(VertexBuffer buffer, int v1, int v2, int v3) {
+ this.buffer = buffer;
+
+ indices = new int[3];
+ indices[0] = v1;
+ indices[1] = v2;
+ indices[2] = v3;
+
+ Vector3f edge1 = new Vector3f(), edge2 = new Vector3f();
+
+ Point3f p1 = buffer.getVertex(v1), p2 = buffer.getVertex(v2), p3 = buffer.getVertex(v3);
+
+ edge1.sub(p1, p2);
+ edge2.sub(p3, p2);
+
+ normal = new Vector3f();
+ normal.cross(edge1, edge2);
+ normal.normalize();
+ }
+
+ public VertexBuffer getBuffer() {
+ return buffer;
+ }
+
+ public int getIndex(int i) {
+ return indices[i];
+ }
+
+ public Point3f getVertex(int i) {
+ return buffer.getVertex(indices[i]);
+ }
+
+ public Vector3f getVertexNormal(int i) {
+ return buffer.getNormal(indices[i]);
+ }
+
+ public Vector3f getNormal() {
+ return normal;
+ }
+}
diff --git a/src/de/gamezock/metacraft/data/Map.java b/src/de/gamezock/metacraft/data/Map.java
index 770ffe7..c6d306b 100644
--- a/src/de/gamezock/metacraft/data/Map.java
+++ b/src/de/gamezock/metacraft/data/Map.java
@@ -1,12 +1,8 @@
package de.gamezock.metacraft.data;
-import java.util.List;
-import java.util.Vector;
-
-import javax.vecmath.Point3f;
public class Map {
- private List<Triangle> triangleData;
+ Tile[][] tiles;
private float[][] heightmap;
@@ -14,69 +10,25 @@ public class Map {
int width = 5;
int height = 5;
- float[][] tileHeightmap = new float[width][height];
- Tile[][] tiles = new Tile[width][height];
+ heightmap = new float[width][height];
+ tiles = new Tile[width][height];
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;
+ //heightmap[i][j] = ((i-2)*(i-2)+(j-2)*(j-2))*0.5f;
+ heightmap[i][j] = 0;
tiles[i][j] = new Tile(new TestTile());
}
}
-
- createMapData(tiles, tileHeightmap);
}
- 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<Triangle>();
-
- 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) {
+ 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 heightmap.length;
@@ -86,11 +38,7 @@ public class Map {
return heightmap[0].length;
}
- /*public int getTileSize() {
- return tileSize;
- }*/
-
- public List<Triangle> getTriangleData() {
- return triangleData;
+ public int getTileSize() {
+ return tiles[0][0].getData().getSize();
}
}
diff --git a/src/de/gamezock/metacraft/data/TestTile.java b/src/de/gamezock/metacraft/data/TestTile.java
index 229f425..0516cec 100644
--- a/src/de/gamezock/metacraft/data/TestTile.java
+++ b/src/de/gamezock/metacraft/data/TestTile.java
@@ -1,18 +1,112 @@
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 {
- float[][] heightmap;
+ private List<IndexedTriangle> triangleData;
+ private float[][] heightmap;
+ private VertexBuffer vertexBuffer;
+ private IntBuffer indexBuffer;
+
public TestTile() {
- heightmap = new float[getSize()][getSize()];
+ heightmap = new float[getSize()+1][getSize()+1];
- for(int i = 0; i < getSize(); ++i) {
- for(int j = 0; j < getSize(); ++j) {
- heightmap[i][j] = 0.2f * ((i+j)%2);
+ 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;
@@ -27,4 +121,14 @@ public class TestTile implements TileData {
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/TileData.java b/src/de/gamezock/metacraft/data/TileData.java
index 2fb48cc..5b34553 100644
--- a/src/de/gamezock/metacraft/data/TileData.java
+++ b/src/de/gamezock/metacraft/data/TileData.java
@@ -1,9 +1,14 @@
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/Triangle.java b/src/de/gamezock/metacraft/data/Triangle.java
index bcfce9d..2049c6f 100644
--- a/src/de/gamezock/metacraft/data/Triangle.java
+++ b/src/de/gamezock/metacraft/data/Triangle.java
@@ -3,9 +3,10 @@ package de.gamezock.metacraft.data;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
-public class Triangle {
+public class Triangle implements Cloneable {
private Point3f[] vertices;
private Vector3f[] normals;
+ private Vector3f normal;
public Triangle(Point3f v1, Point3f v2, Point3f v3) {
@@ -15,16 +16,15 @@ public class Triangle {
vertices[1] = v2;
vertices[2] = v3;
- normals = new Vector3f[3];
-
Vector3f edge1 = new Vector3f(), edge2 = new Vector3f();
edge1.sub(v1, v2);
edge2.sub(v3, v2);
- Vector3f normal = new Vector3f();
+ normal = new Vector3f();
normal.cross(edge1, edge2);
normal.normalize();
+ normals = new Vector3f[3];
normals[0] = new Vector3f(normal);
normals[1] = new Vector3f(normal);
normals[2] = new Vector3f(normal);
@@ -34,7 +34,27 @@ public class Triangle {
return vertices[i];
}
- public Vector3f getNormal(int i) {
+ public Vector3f getVertexNormal(int i) {
return normals[i];
}
+
+ public Vector3f getNormal() {
+ return normal;
+ }
+
+ public void translate(Vector3f v) {
+ for(Point3f p : vertices) {
+ p.add(v);
+ }
+ }
+
+ public Object clone() {
+ Triangle t = new Triangle((Point3f)vertices[0].clone(), (Point3f)vertices[1].clone(), (Point3f)vertices[2].clone());
+
+ for(int i = 0; i < 3; ++i) {
+ t.normals[i] = (Vector3f)normals[i].clone();
+ }
+
+ return t;
+ }
}
diff --git a/src/de/gamezock/metacraft/data/VertexBuffer.java b/src/de/gamezock/metacraft/data/VertexBuffer.java
new file mode 100644
index 0000000..c2205a0
--- /dev/null
+++ b/src/de/gamezock/metacraft/data/VertexBuffer.java
@@ -0,0 +1,60 @@
+package de.gamezock.metacraft.data;
+
+import java.nio.FloatBuffer;
+
+import javax.vecmath.Point3f;
+import javax.vecmath.Tuple3f;
+import javax.vecmath.Vector3f;
+
+public class VertexBuffer {
+ private FloatBuffer buffer;
+
+ public VertexBuffer(int num) {
+ buffer = FloatBuffer.allocate(2*3*num);
+ }
+
+ public FloatBuffer getBuffer() {
+ return buffer;
+ }
+
+ private void setTuple(int i, Tuple3f tuple) {
+ buffer.position(3*i);
+
+ buffer.put(tuple.x);
+ buffer.put(tuple.y);
+ buffer.put(tuple.z);
+ }
+
+ private Point3f getTuple(int i) {
+ buffer.position(3*i);
+
+ return new Point3f(buffer.get(), buffer.get(), buffer.get());
+ }
+
+ public void setNormal(int i, Vector3f normal) {
+ setTuple(2*i, normal);
+ }
+
+ public void setVertex(int i, Point3f vertex) {
+ setTuple(2*i + 1, vertex);
+ }
+
+ public Vector3f getNormal(int i) {
+ return new Vector3f(getTuple(2*i));
+ }
+
+ public Point3f getVertex(int i) {
+ return getTuple(2*i + 1);
+ }
+
+ public int getSize() {
+ return buffer.capacity() / (2*3);
+ }
+
+ public void dump() {
+ buffer.clear();
+
+ while(buffer.hasRemaining())
+ System.out.println(buffer.get());
+ }
+}
diff --git a/src/de/gamezock/metacraft/ui/Main.java b/src/de/gamezock/metacraft/ui/Main.java
index c26e510..f5eb3f9 100644
--- a/src/de/gamezock/metacraft/ui/Main.java
+++ b/src/de/gamezock/metacraft/ui/Main.java
@@ -144,6 +144,6 @@ public class Main implements GLEventListener {
* @param args
*/
public static void main(String[] args) {
- new Main();
+ new Main();
}
}
diff --git a/src/de/gamezock/metacraft/ui/Renderer.java b/src/de/gamezock/metacraft/ui/Renderer.java
index 3213a53..6d8d04a 100644
--- a/src/de/gamezock/metacraft/ui/Renderer.java
+++ b/src/de/gamezock/metacraft/ui/Renderer.java
@@ -1,12 +1,15 @@
package de.gamezock.metacraft.ui;
+import java.nio.IntBuffer;
+import java.util.List;
+
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
-import javax.vecmath.Point3f;
-import javax.vecmath.Vector3f;
+import de.gamezock.metacraft.data.IndexedTriangle;
import de.gamezock.metacraft.data.Map;
-import de.gamezock.metacraft.data.Triangle;
+import de.gamezock.metacraft.data.Tile;
+import de.gamezock.metacraft.data.VertexBuffer;
public class Renderer {
private Main main;
@@ -15,14 +18,20 @@ public class Renderer {
this.main = main;
}
- private void renderTriangle(GL2 gl, Triangle t) {
- for(int i = 0; i < 3; ++i) {
- Point3f v = t.getVertex(i);
- Vector3f n = t.getNormal(i);
-
- gl.glNormal3f(n.x, n.y, n.z);
- gl.glVertex3f(v.x, v.y, v.z);
- }
+ 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) {
@@ -30,20 +39,26 @@ public class Renderer {
Map currentMap = main.getCurrentMap();
+ gl.glEnable(GL2.GL_VERTEX_ARRAY);
+ gl.glEnable(GL2.GL_NORMAL_ARRAY);
+
gl.glPushMatrix();
gl.glRotatef((System.currentTimeMillis()%9000)/25.0f, 0, 0, 1);
gl.glTranslatef(-20, -20, 0);
- gl.glBegin(GL.GL_TRIANGLES);
-
gl.glColor3f(1, 1, 1);
- for(Triangle t : currentMap.getTriangleData()) {
- renderTriangle(gl, t);
+ 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();
+ }
}
- gl.glEnd();
-
gl.glPopMatrix();
}
}