summaryrefslogtreecommitdiffstats
path: root/src/de/gamezock/metacraft/ui
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/gamezock/metacraft/ui')
-rw-r--r--src/de/gamezock/metacraft/ui/Main.java137
-rw-r--r--src/de/gamezock/metacraft/ui/Renderer.java88
2 files changed, 225 insertions, 0 deletions
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();
+ }
+
+}