From cceae491ced7d1c5aacb4a466bdc79e8de367bc7 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 11 Jan 2010 16:37:05 +0100 Subject: Started Renderer --- src/de/gamezock/metacraft/data/Map.java | 41 ++++++++ src/de/gamezock/metacraft/data/TestTile.java | 25 +++++ src/de/gamezock/metacraft/data/Tile.java | 13 +++ src/de/gamezock/metacraft/data/TileData.java | 7 ++ src/de/gamezock/metacraft/ui/Main.java | 137 +++++++++++++++++++++++++++ src/de/gamezock/metacraft/ui/Renderer.java | 88 +++++++++++++++++ 6 files changed, 311 insertions(+) create mode 100644 src/de/gamezock/metacraft/data/Map.java create mode 100644 src/de/gamezock/metacraft/data/TestTile.java create mode 100644 src/de/gamezock/metacraft/data/Tile.java create mode 100644 src/de/gamezock/metacraft/data/TileData.java create mode 100644 src/de/gamezock/metacraft/ui/Main.java create mode 100644 src/de/gamezock/metacraft/ui/Renderer.java (limited to 'src/de') diff --git a/src/de/gamezock/metacraft/data/Map.java b/src/de/gamezock/metacraft/data/Map.java new file mode 100644 index 0000000..e387628 --- /dev/null +++ b/src/de/gamezock/metacraft/data/Map.java @@ -0,0 +1,41 @@ +package de.gamezock.metacraft.data; + +public class Map { + private Tile[][] tiles; + private float[][] heightmap; + + private int width, height; + private int tileSize; + + public Map() { + width = height = 1; + + heightmap = new float[width][height]; + heightmap[0][0] = 0; + + tiles = new Tile[width][height]; + tiles[0][0] = new Tile(new TestTile()); + + tileSize = tiles[0][0].getData().getSize(); + } + + 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; + } + + public int getHeight() { + return height; + } + + public int getTileSize() { + return tileSize; + } +} diff --git a/src/de/gamezock/metacraft/data/TestTile.java b/src/de/gamezock/metacraft/data/TestTile.java new file mode 100644 index 0000000..94a2af1 --- /dev/null +++ b/src/de/gamezock/metacraft/data/TestTile.java @@ -0,0 +1,25 @@ +package de.gamezock.metacraft.data; + +public class TestTile extends TileData { + float[][] heightmap; + + public TestTile() { + heightmap = new float[getSize()+1][getSize()+1]; + + for(int i = 0; i < getSize()+1; ++i) { + for(int j = 0; j < getSize()+1; ++j) { + heightmap[i][j] = 1 * ((i+j)%2); + } + } + } + + @Override + public int getSize() { + return 8; + } + + @Override + public float[][] getHeightmap() { + return heightmap; + } +} diff --git a/src/de/gamezock/metacraft/data/Tile.java b/src/de/gamezock/metacraft/data/Tile.java new file mode 100644 index 0000000..718c514 --- /dev/null +++ b/src/de/gamezock/metacraft/data/Tile.java @@ -0,0 +1,13 @@ +package de.gamezock.metacraft.data; + +public class Tile { + 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 new file mode 100644 index 0000000..5a3103a --- /dev/null +++ b/src/de/gamezock/metacraft/data/TileData.java @@ -0,0 +1,7 @@ +package de.gamezock.metacraft.data; + +public abstract class TileData { + public abstract int getSize(); + + public abstract float[][] getHeightmap(); +} diff --git a/src/de/gamezock/metacraft/ui/Main.java b/src/de/gamezock/metacraft/ui/Main.java new file mode 100644 index 0000000..a721feb --- /dev/null +++ b/src/de/gamezock/metacraft/ui/Main.java @@ -0,0 +1,137 @@ +package de.gamezock.metacraft.ui; + +import java.awt.Frame; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +import javax.media.opengl.GL; +import javax.media.opengl.GL2; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.GLProfile; +import javax.media.opengl.awt.GLCanvas; +import javax.media.opengl.glu.GLU; + +import de.gamezock.metacraft.data.Map; + +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(); + + public Main() { + GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); + caps.setHardwareAccelerated(true); + caps.setDoubleBuffered(true); + + canvas = new GLCanvas(caps); + canvas.addGLEventListener(this); + + frame.add(canvas); + frame.setSize(800, 600); + frame.setResizable(false); + frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + quit(); + } + }); + + frame.setVisible(true); + + } + + Map getCurrentMap() { + return currentMap; + } + + @Override + public void display(GLAutoDrawable drawable) { + renderer.render(drawable.getGL().getGL2()); + } + + @Override + public void dispose(GLAutoDrawable drawable) { + } + + @Override + public void init(GLAutoDrawable drawable) { + GL2 gl = drawable.getGL().getGL2(); + + gl.glShadeModel(GL2.GL_SMOOTH); + gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + gl.glClearDepth(1.0f); + gl.glEnable(GL.GL_DEPTH_TEST); + gl.glDepthFunc(GL.GL_LEQUAL); + gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); + + gl.glEnable(GL2.GL_LIGHTING); + + gl.glColorMaterial(GL.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE); + + gl.glMatrixMode(GL2.GL_MODELVIEW); + glu.gluLookAt(0, -10, -25, 0, 0, 0, 0, 1, 0); + + gl.glEnable(GL2.GL_LIGHT0); + gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, new float[] {0, 0, 0, 1}, 0); + gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, new float[] {0.5f, 0.5f, 0.5f, 1}, 0); + gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, new float[] {0, 0, 0, 0}, 0); + 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); + } + + @Override + public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { + GL2 gl = drawable.getGL().getGL2(); + + if(height <= 0) { + height = 1; + } + + float aspect = (float)width / (float)height; + gl.glMatrixMode(GL2.GL_PROJECTION); + gl.glLoadIdentity(); + glu.gluPerspective(50.0f, aspect, 0.1, 1000.0); + + gl.glMatrixMode(GL2.GL_MODELVIEW); + } + + public void quit() { + frame.dispose(); + System.exit(0); + } + + /** + * @param args + */ + public static void main(String[] args) { + final Main main = new Main(); + + try { + Thread.sleep(500); + } catch (InterruptedException e) { + } + + new Thread(new Runnable() { + + @Override + public void run() { + while(true) { + try { + Thread.sleep(10); + } catch (InterruptedException e) { + } + + main.canvas.display(); + } + } + + }).start(); + } +} diff --git a/src/de/gamezock/metacraft/ui/Renderer.java b/src/de/gamezock/metacraft/ui/Renderer.java new file mode 100644 index 0000000..cea0480 --- /dev/null +++ b/src/de/gamezock/metacraft/ui/Renderer.java @@ -0,0 +1,88 @@ +package de.gamezock.metacraft.ui; + +import javax.media.opengl.GL; +import javax.media.opengl.GL2; +import javax.vecmath.Point3f; +import javax.vecmath.Vector3f; + +import de.gamezock.metacraft.data.Map; +import de.gamezock.metacraft.data.Tile; + +public class Renderer { + private Main main; + + public Renderer(Main main) { + this.main = main; + } + + private void renderTileQuad(GL2 gl, Tile tile, int x, int y) { + float[][] heightmap = tile.getData().getHeightmap(); + + 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]); + + Vector3f edge1 = new Vector3f(), edge2 = new Vector3f(); + + edge1.sub(v1, v2); + edge2.sub(v3, v2); + + Vector3f normal = new Vector3f(); + normal.cross(edge1, edge2); + normal.normalize(); + + gl.glColor3f(1, 1, 1); + gl.glNormal3f(normal.x, normal.y, normal.z); + gl.glVertex3f(v1.x, v1.y, v1.z); + gl.glVertex3f(v2.x, v2.y, v2.z); + gl.glVertex3f(v3.x, v3.y, v3.z); + } + } + + private void renderTile(GL2 gl, Tile tile) { + gl.glBegin(GL.GL_TRIANGLES); + + for(int i = 0; i < tile.getData().getSize(); ++i) { + for(int j = 0; j < tile.getData().getSize(); ++j) { + renderTileQuad(gl, tile, i, j); + } + } + + gl.glEnd(); + } + + public void render(GL2 gl) { + gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + + Map currentMap = main.getCurrentMap(); + + gl.glPushMatrix(); + gl.glRotatef((System.currentTimeMillis()%3600)/10.0f, 0, 0, 1); + + for(int i = 0; i < currentMap.getWidth(); ++i) { + for(int j = 0; j < currentMap.getHeight(); ++j) { + gl.glPushMatrix(); + gl.glTranslatef(i*currentMap.getTileSize(), j*currentMap.getTileSize(), currentMap.getTileHeight(i, j)); + + renderTile(gl, currentMap.getTile(i, j)); + + gl.glPopMatrix(); + } + } + + gl.glPopMatrix(); + } + +} -- cgit v1.2.3