summaryrefslogtreecommitdiffstats
path: root/src/de/gamezock
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/gamezock')
-rw-r--r--src/de/gamezock/metacraft/data/TestMap.java2
-rw-r--r--src/de/gamezock/metacraft/resources/shaders/default.frag30
-rw-r--r--src/de/gamezock/metacraft/resources/shaders/default.vert16
-rw-r--r--src/de/gamezock/metacraft/ui/Main.java17
-rw-r--r--src/de/gamezock/metacraft/ui/ShaderLoader.java72
5 files changed, 134 insertions, 3 deletions
diff --git a/src/de/gamezock/metacraft/data/TestMap.java b/src/de/gamezock/metacraft/data/TestMap.java
index 58a6bba..14e2c7b 100644
--- a/src/de/gamezock/metacraft/data/TestMap.java
+++ b/src/de/gamezock/metacraft/data/TestMap.java
@@ -6,7 +6,7 @@ public class TestMap extends Map {
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;
+ heightmap[x][y] = (float)(Math.cos(Math.sqrt((x-20)*(x-20)+(y-20)*(y-20))*Math.PI/4))*0.5f;
}
}
diff --git a/src/de/gamezock/metacraft/resources/shaders/default.frag b/src/de/gamezock/metacraft/resources/shaders/default.frag
new file mode 100644
index 0000000..c87f14f
--- /dev/null
+++ b/src/de/gamezock/metacraft/resources/shaders/default.frag
@@ -0,0 +1,30 @@
+uniform sampler2D tex;
+
+varying vec4 diffuse, ambient, globalAmbient;
+varying vec3 normal, pos;
+
+
+void main() {
+ vec3 n, l;
+ float NdotL;
+ vec4 specularColor;
+ float dist, distSq, att, specularFactor;
+
+ n = normalize(normal);
+
+ l = gl_LightSource[0].position.xyz - pos;
+
+ distSq = dot(l, l);
+
+ NdotL = dot(n, l);
+
+ if (NdotL > 0.0) {
+ dist = sqrt(distSq);
+
+ att = 1.0 / (gl_LightSource[0].quadraticAttenuation * distSq);
+ gl_FragColor = att * (diffuse * NdotL / dist + ambient + globalAmbient) /* * texture2D(tex, gl_TexCoord[0].st)*/;
+ }
+ else {
+ gl_FragColor = vec4(0, 0, 0, 1);
+ }
+}
diff --git a/src/de/gamezock/metacraft/resources/shaders/default.vert b/src/de/gamezock/metacraft/resources/shaders/default.vert
new file mode 100644
index 0000000..002b730
--- /dev/null
+++ b/src/de/gamezock/metacraft/resources/shaders/default.vert
@@ -0,0 +1,16 @@
+varying vec4 diffuse, ambient, globalAmbient;
+varying vec3 normal, pos;
+
+
+void main() {
+ normal = gl_NormalMatrix * gl_Normal;
+
+ pos = vec3(gl_ModelViewMatrix * gl_Vertex);
+
+ diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
+ ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
+ globalAmbient = gl_FrontMaterial.ambient, gl_LightModel.ambient;
+
+ gl_TexCoord[0] = gl_MultiTexCoord0;
+ gl_Position = ftransform();
+}
diff --git a/src/de/gamezock/metacraft/ui/Main.java b/src/de/gamezock/metacraft/ui/Main.java
index 287af22..bfdde1c 100644
--- a/src/de/gamezock/metacraft/ui/Main.java
+++ b/src/de/gamezock/metacraft/ui/Main.java
@@ -95,20 +95,33 @@ public class Main implements GLEventListener {
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.glLightf(GL2.GL_LIGHT0, GL2.GL_QUADRATIC_ATTENUATION, 0.01f);
gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, new float[] {0, 0, 0, 0}, 0);
+ ShaderLoader.load(gl, "default.vert", "default.frag");
+
new Thread(new Runnable() {
@Override
public void run() {
+ long time = System.currentTimeMillis();
+ int frames = 0;
+
while(true) {
canvas.display();
+ ++frames;
+
+ if(System.currentTimeMillis() - time >= 1000) {
+ System.err.println(frames + " fps");
+ frames = 0;
+ time += 1000;
+ }
try {
- Thread.sleep(10);
+ Thread.sleep(5);
} catch (InterruptedException e) {
- }
+ }
}
}
diff --git a/src/de/gamezock/metacraft/ui/ShaderLoader.java b/src/de/gamezock/metacraft/ui/ShaderLoader.java
new file mode 100644
index 0000000..25bc4eb
--- /dev/null
+++ b/src/de/gamezock/metacraft/ui/ShaderLoader.java
@@ -0,0 +1,72 @@
+package de.gamezock.metacraft.ui;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.AbstractMap;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.media.opengl.GL2;
+
+public class ShaderLoader {
+ private static Map<Entry<String, String>, Integer> programs = new HashMap<Entry<String, String>, Integer>();
+
+ static private String readStream(InputStream stream) throws IOException {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
+ String ret = "", line;
+
+ while ((line = reader.readLine()) != null) {
+ ret += line + "\n";
+ }
+
+ return ret;
+ }
+
+ static boolean load(GL2 gl, String vsName, String fsName) {
+ Integer savedProg = programs.get(new AbstractMap.SimpleEntry<String, String>(vsName, fsName));
+ if(savedProg != null) {
+ gl.glUseProgram(savedProg);
+ return true;
+ }
+
+ int vs = gl.glCreateShader(GL2.GL_VERTEX_SHADER);
+ int fs = gl.glCreateShader(GL2.GL_FRAGMENT_SHADER);
+
+ InputStream vsStream = ShaderLoader.class.getResourceAsStream("/de/gamezock/metacraft/resources/shaders/" + vsName);
+ String vsSource;
+ try {
+ vsSource = readStream(vsStream);
+ } catch (IOException e) {
+ return false;
+ }
+
+ gl.glShaderSource(vs, 1, new String[] {vsSource}, null);
+ gl.glCompileShader(vs);
+
+ InputStream fsStream = ShaderLoader.class.getResourceAsStream("/de/gamezock/metacraft/resources/shaders/" + fsName);
+ String fsSource;
+ try {
+ fsSource = readStream(fsStream);
+ } catch (IOException e) {
+ return false;
+ }
+
+ gl.glShaderSource(fs, 1, new String[] {fsSource}, null);
+ gl.glCompileShader(fs);
+
+ int program = gl.glCreateProgram();
+ gl.glAttachShader(program, vs);
+ gl.glAttachShader(program, fs);
+ gl.glLinkProgram(program);
+ gl.glValidateProgram(program);
+
+ gl.glUseProgram(program);
+
+ programs.put(new AbstractMap.SimpleEntry<String, String>(vsName, fsName), program);
+
+ return true;
+ }
+}