aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/LICENSE21
-rw-r--r--src/ru/olamedia/camera/MatrixCamera.java262
-rw-r--r--src/ru/olamedia/game/GameManager.java49
-rw-r--r--src/ru/olamedia/game/GameTime.java37
-rw-r--r--src/ru/olamedia/geom/Mesh.java34
-rw-r--r--src/ru/olamedia/liveEntity/LiveEntity.java193
-rw-r--r--src/ru/olamedia/olacraft/render/jogl/ChunkRenderer.java5
-rw-r--r--src/ru/olamedia/olacraft/scene/GameScene.java47
-rw-r--r--src/ru/olamedia/olacraft/world/block/Block.java77
-rw-r--r--src/ru/olamedia/olacraft/world/chunk/BlockSlice.java69
-rw-r--r--src/ru/olamedia/olacraft/world/chunk/Chunk.java161
-rw-r--r--src/ru/olamedia/olacraft/world/chunk/ChunkUnavailableException.java7
-rw-r--r--src/ru/olamedia/olacraft/world/dataProvider/CachedChunkDataProvider.java3
-rw-r--r--src/ru/olamedia/olacraft/world/provider/WorldProvider.java20
14 files changed, 593 insertions, 392 deletions
diff --git a/src/LICENSE b/src/LICENSE
new file mode 100644
index 0000000..ce32279
--- /dev/null
+++ b/src/LICENSE
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2012 olamedia
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/src/ru/olamedia/camera/MatrixCamera.java b/src/ru/olamedia/camera/MatrixCamera.java
index 2515daf..e93e25c 100644
--- a/src/ru/olamedia/camera/MatrixCamera.java
+++ b/src/ru/olamedia/camera/MatrixCamera.java
@@ -1,22 +1,30 @@
package ru.olamedia.camera;
+import java.nio.FloatBuffer;
+
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
+import javax.media.opengl.GLContext;
+import javax.media.opengl.GLUniformData;
+import javax.media.opengl.fixedfunc.GLMatrixFunc;
import javax.media.opengl.glu.GLU;
import javax.vecmath.Vector3f;
-import javax.vecmath.Matrix4f;
import org.openmali.FastMath;
-import static org.openmali.FastMath.*;
-
import ru.olamedia.olacraft.game.Game;
import ru.olamedia.input.Keyboard;
import ru.olamedia.math.Frustum;
import com.jogamp.newt.event.KeyEvent;
+import com.jogamp.opengl.util.PMVMatrix;
public class MatrixCamera {
+
+ public PMVMatrix pmvMatrix = new PMVMatrix(true);
+ private boolean isDirty = true;
+ public GLUniformData pmvMatrixUniform;
+
protected float fov = 90f;
protected float aspect = 1f;
protected float zNear = 0.1f;
@@ -30,24 +38,13 @@ public class MatrixCamera {
private float yaw = 0; // around y
private float pitch = 0;
private float roll = 0;
- public Matrix4f projectionMatrix = new Matrix4f();
- private Matrix4f translationMatrix = new Matrix4f();
- private Matrix4f xRotationMatrix = new Matrix4f();
- private Matrix4f yRotationMatrix = new Matrix4f();
- private Matrix4f zRotationMatrix = new Matrix4f();
- private Matrix4f rotationMatrix = new Matrix4f();
- public Matrix4f viewMatrix = new Matrix4f();
- public Matrix4f worldMatrix = new Matrix4f();
private Vector3f look = new Vector3f();
private Vector3f right = new Vector3f();
private Vector3f up = new Vector3f();
public boolean isFrustumVisible = false;
- @SuppressWarnings("unused")
- private org.openmali.vecmath2.Matrix4f matrixToOpenMali(Matrix4f m) {
- return new org.openmali.vecmath2.Matrix4f(matrixToTransposeArray(m));
- }
+ int counter = 0;
public void pack() {
if (isAttachedToCameraman) {
@@ -55,37 +52,28 @@ public class MatrixCamera {
position.y = cameraman.getCameraY();
position.z = cameraman.getCameraZ();
}
+ pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
+ pmvMatrix.glLoadIdentity();
+ pmvMatrix.gluPerspective(fov, aspect, zNear, zFar);
+
+ pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
+ pmvMatrix.glLoadIdentity();
+ pmvMatrix.glRotatef(360 - pitch, 1, 0, 0);
+ pmvMatrix.glRotatef(360 - yaw, 0, 1, 0);
+ pmvMatrix.glRotatef(360 - roll, 0, 0, 1);
+ pmvMatrix.glTranslatef(-position.x, -position.y, -position.z);
+ // pmvMatrix.setDirty();
+ pmvMatrix.update();
+ FloatBuffer mv = FloatBuffer.allocate(16);
+ pmvMatrix.glGetFloatv(GLMatrixFunc.GL_MODELVIEW_MATRIX, mv);
+
+ right.set(mv.get(0), mv.get(4), mv.get(8));
+ look.set(mv.get(2), mv.get(6), mv.get(10));
+ up.cross(look, right);
+
+ pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf());
- worldMatrix.setIdentity();
- packProjectionMatrix();
- // projectionMatrix.transpose();
- // worldMatrix.mul(projectionMatrix);
- translationMatrix.setIdentity();
- translationMatrix.m03 = position.x;
- translationMatrix.m13 = position.y - 0.5f; // FIXME y is looking greater
- // than it should
- translationMatrix.m23 = position.z;
- packRotation();
- packView();
- // after view matrix created, retrieve vectors:
- viewMatrix.invert();
- viewMatrix.transpose();
- packLookVector();
- packRightVector();
- packUpVector();
packFrustum();
- // worldMatrix.mul(projectionMatrix, viewMatrix);
- // worldMatrix.transpose();
- // // oglViewMatrix.set(viewMatrix);
- // // oglViewMatrix.transpose();
- // frustum = FrustumUtil.extractFrustum(worldMatrix);
- // Matrix4f vm = new Matrix4f(viewMatrix);
- // vm.invert();
- // frustum.compute(matrixToOpenMali(projectionMatrix),
- // matrixToOpenMali(vm));
- // ......
- // finally
- // ......
}
private ru.olamedia.math.Vector3f nearc;
@@ -124,98 +112,90 @@ public class MatrixCamera {
frustum.farPlane.set3Points(flt, flb, frb);
}
- private float[] matrixToTransposeArray(Matrix4f m) {
- return new float[] {
- //
- m.m00, m.m01, m.m02, m.m03,//
- m.m10, m.m11, m.m12, m.m13,//
- m.m20, m.m21, m.m22, m.m23,//
- m.m30, m.m31, m.m32, m.m33,//
- };
- }
-
- private float[] matrixToArray(Matrix4f m) {
- return new float[] {
- //
- m.m00, m.m10, m.m20, m.m30,//
- m.m01, m.m11, m.m21, m.m31,//
- m.m02, m.m12, m.m22, m.m32,//
- m.m03, m.m13, m.m23, m.m33,//
- };
- }
-
private GLU glu;
public void setUp(GLAutoDrawable drawable) {
- GL2 gl = drawable.getGL().getGL2();
updateKeyboard();
updateMouse();
if (glu == null) {
glu = new GLU();
}
- // gl.glMatrixMode(GL2.GL_PROJECTION);
- // gl.glLoadIdentity();
- // glu.gluPerspective(100f, aspect, 0.2, 1000);
- loadProjectionMatrix(drawable);
- loadViewMatrix(drawable);
-
- // gl.glColor3f(1, 0, 0);
- // PlaneRenderer.render(frustum.leftPlane, drawable);
- // gl.glColor3f(1, 1, 0);
- // PlaneRenderer.render(frustum.rightPlane, drawable);
- // gl.glColor3f(1, 0, 1);
- // PlaneRenderer.render(frustum.topPlane, drawable);
- // gl.glColor3f(1, 1, 1);
- // PlaneRenderer.render(frustum.bottomPlane, drawable);
-
- // VectorRenderer.render(nearc, frustum.leftPlane.n, drawable);
+ if (isDirty) {
+ pack();
+ }
+ GL2 gl = GLContext.getCurrentGL().getGL2();
+ gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
+ gl.glLoadMatrixf(pmvMatrix.glGetPMatrixf());
+ gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
+ FloatBuffer pmv = FloatBuffer.allocate(16);
+ pmvMatrix.glGetFloatv(GLMatrixFunc.GL_MODELVIEW_MATRIX, pmv);
+ gl.glLoadMatrixf(pmv);
}
- private void loadViewMatrix(GLAutoDrawable drawable) {
- GL2 gl = drawable.getGL().getGL2();
- gl.glMatrixMode(GL2.GL_MODELVIEW);
- gl.glLoadTransposeMatrixf(matrixToArray(viewMatrix), 0);
+ public float intersectsRectangle(Vector3f vertex1, Vector3f vertex2, Vector3f vertex3, Vector3f vertex4) {
+ return Math.min(intersectsTriangle(vertex1, vertex2, vertex3), intersectsTriangle(vertex3, vertex4, vertex1));
}
- private void loadProjectionMatrix(GLAutoDrawable drawable) {
- GL2 gl = drawable.getGL().getGL2();
- gl.glMatrixMode(GL2.GL_PROJECTION);
- gl.glLoadTransposeMatrixf(matrixToArray(projectionMatrix), 0);
- }
+ public float intersectsTriangle(Vector3f vertex1, Vector3f vertex2, Vector3f vertex3) {
+ // Compute vectors along two edges of the triangle.
+ Vector3f edge1 = new Vector3f(), edge2 = new Vector3f();
- private void packProjectionMatrix() {
- projectionMatrix.setZero();
- final float tang = FastMath.tan(FastMath.toRad(fov) / 2.0f);
- final float size = zNear * tang;
- float left = -size, right = size, bottom = -size / aspect, top = size / aspect;
- // First Column
- projectionMatrix.m00 = 2 * zNear / (right - left);
- // Second Column
- projectionMatrix.m11 = 2 * zNear / (top - bottom);
- // Third Column
- projectionMatrix.m20 = (right + left) / (right - left);
- projectionMatrix.m21 = (top + bottom) / (top - bottom);
- projectionMatrix.m22 = -(zFar + zNear) / (zFar - zNear);
- projectionMatrix.m23 = -1;
- // Fourth Column
- projectionMatrix.m32 = -(2 * zFar * zNear) / (zFar - zNear);
- }
-
- private void packRotation() {
- xRotationMatrix.rotX(toRad(pitch));
- yRotationMatrix.rotY(toRad(yaw));
- zRotationMatrix.rotZ(toRad(roll));
-
- rotationMatrix.setIdentity();
- rotationMatrix.mul(zRotationMatrix);
- rotationMatrix.mul(yRotationMatrix);
- rotationMatrix.mul(xRotationMatrix);
+ edge1.sub(vertex2, vertex1);
+ edge2.sub(vertex3, vertex1);
+
+ // Compute the determinant.
+ Vector3f directionCrossEdge2 = new Vector3f();
+ directionCrossEdge2.cross(look, edge2);
+
+ float determinant = directionCrossEdge2.dot(edge1);
+ // If the ray and triangle are parallel, there is no collision.
+ if (determinant > -.0000001f && determinant < .0000001f) {
+ return Float.MAX_VALUE;
+ }
+
+ float inverseDeterminant = 1.0f / determinant;
+
+ // Calculate the U parameter of the intersection point.
+ Vector3f distanceVector = new Vector3f();
+ distanceVector.sub(position, vertex1);
+
+ float triangleU = directionCrossEdge2.dot(distanceVector);
+ triangleU *= inverseDeterminant;
+
+ // Make sure the U is inside the triangle.
+ if (triangleU < 0 || triangleU > 1) {
+ return Float.MAX_VALUE;
+ }
+
+ // Calculate the V parameter of the intersection point.
+ Vector3f distanceCrossEdge1 = new Vector3f();
+ distanceCrossEdge1.cross(distanceVector, edge1);
+
+ float triangleV = look.dot(distanceCrossEdge1);
+ triangleV *= inverseDeterminant;
+
+ // Make sure the V is inside the triangle.
+ if (triangleV < 0 || triangleU + triangleV > 1) {
+ return Float.MAX_VALUE;
+ }
+
+ // Get the distance to the face from our ray origin
+ float rayDistance = distanceCrossEdge1.dot(edge2);
+ rayDistance *= inverseDeterminant;
+
+ // Is the triangle behind us?
+ if (rayDistance < 0) {
+ rayDistance *= -1;
+ return Float.MAX_VALUE;
+ }
+ return rayDistance;
}
private void translatePoint(Vector3f point, Vector3f direction, float delta) {
point.x += direction.x * delta;
point.y += direction.y * delta;
point.z += direction.z * delta;
+ setDirty();
}
private void translate(Vector3f direction, float delta) {
@@ -228,31 +208,12 @@ public class MatrixCamera {
translate(look, -dz);
}
- private void packView() {
- viewMatrix.setIdentity();
- viewMatrix.mul(translationMatrix);
- viewMatrix.mul(rotationMatrix);
-
- }
-
- private void packUpVector() {
- up.set(viewMatrix.m01, viewMatrix.m11, viewMatrix.m21);
- }
-
- private void packRightVector() {
- right.set(viewMatrix.m00, viewMatrix.m10, viewMatrix.m20);
- }
-
- private void packLookVector() {
- look.set(viewMatrix.m02, viewMatrix.m12, viewMatrix.m22);
- }
-
public MatrixCamera() {
right = new Vector3f(1, 0, 0);
up = new Vector3f(0, 1, 0);
look = new Vector3f(0, 0, 1);
isPitchLocked = true;
- pack();
+ setDirty();
}
public void captureControls() {
@@ -269,6 +230,7 @@ public class MatrixCamera {
pitch += -dy;
yaw = yaw % 360;
pitch = pitch % 360;
+ setDirty();
}
public void updateMouse() {
@@ -279,7 +241,6 @@ public class MatrixCamera {
pitch = maxPitch;
}
}
- pack();
}
public void lockPitch(float min, float max) {
@@ -311,9 +272,7 @@ public class MatrixCamera {
(isAttachedToCameraman ? 0 : flyUp * distance - flyDown * distance),//
up * distance - down * distance//
);
- pack();
- // System.out.println("Moving... " + position.getX() + " "
- // + position.getY() + " " + position.getZ());
+ setDirty();
}
}
@@ -335,6 +294,7 @@ public class MatrixCamera {
*/
public void setFov(float fov) {
this.fov = fov;
+ setDirty();
}
/**
@@ -350,6 +310,7 @@ public class MatrixCamera {
*/
public void setAspect(float aspect) {
this.aspect = aspect;
+ setDirty();
}
/**
@@ -365,6 +326,7 @@ public class MatrixCamera {
*/
public void setzNear(float zNear) {
this.zNear = zNear;
+ setDirty();
}
/**
@@ -380,6 +342,7 @@ public class MatrixCamera {
*/
public void setzFar(float zFar) {
this.zFar = zFar;
+ setDirty();
}
/**
@@ -395,6 +358,7 @@ public class MatrixCamera {
*/
public void setYaw(float yaw) {
this.yaw = yaw;
+ setDirty();
}
/**
@@ -410,6 +374,7 @@ public class MatrixCamera {
*/
public void setPitch(float pitch) {
this.pitch = pitch;
+ setDirty();
}
/**
@@ -474,14 +439,21 @@ public class MatrixCamera {
public void setX(float x) {
position.x = x;
+ setDirty();
}
public void setY(float y) {
position.y = y;
+ setDirty();
}
public void setZ(float z) {
position.z = z;
+ setDirty();
+ }
+
+ private void setDirty() {
+ isDirty = true;
}
public float getX() {
@@ -496,14 +468,6 @@ public class MatrixCamera {
return position.z;
}
- public float[] getViewMatrixArray() {
- return matrixToArray(viewMatrix);
- }
-
- public float[] getProjectionMatrixArray() {
- return matrixToArray(viewMatrix);
- }
-
public float getRoll() {
return roll;
}
diff --git a/src/ru/olamedia/game/GameManager.java b/src/ru/olamedia/game/GameManager.java
index d9efdfb..7118c85 100644
--- a/src/ru/olamedia/game/GameManager.java
+++ b/src/ru/olamedia/game/GameManager.java
@@ -1,11 +1,16 @@
package ru.olamedia.game;
+import java.nio.FloatBuffer;
import java.util.Set;
import javax.media.opengl.GL;
+import javax.media.opengl.GL2;
import javax.media.opengl.GL2ES2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
+import javax.media.opengl.GLUniformData;
+import javax.media.opengl.fixedfunc.GLMatrixFunc;
+import javax.media.opengl.glu.GLU;
import ru.olamedia.olacraft.game.Game;
import ru.olamedia.olacraft.network.discovery.DiscoveryThread;
@@ -16,6 +21,7 @@ import ru.olamedia.olacraft.world.location.BlockLocation;
import ru.olamedia.tasks.TaskManager;
import com.jogamp.opengl.JoglVersion;
+import com.jogamp.opengl.util.PMVMatrix;
public class GameManager implements GLEventListener {
public static GameManager instance;
@@ -110,6 +116,48 @@ public class GameManager implements GLEventListener {
GameFrame.getFrame().getContentPane().add(menu);
menu.setVisible(true);
GameFrame.getFrame().validate();
+
+ }
+ private PMVMatrix m;
+ private void glTest(GLAutoDrawable drawable) {
+ // m = new PMVMatrix(true);
+ // m.glGetMviMatrixf();
+ // m.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
+ // m.glLoadIdentity();
+ // m.gluPerspective(30, 1, 1, 100);
+ // m.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
+ // m.glLoadIdentity();
+ // m.glTranslatef(5, 6, 7);
+ // m.glRotatef(50, 0, 1, 0);
+ // m.glRotatef(30, 1, 0, 0);
+ // m.setDirty();
+ // m.update();
+ //
+ // GLUniformData pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix",
+ // 4, 4, m.glGetPMvMatrixf());
+ //
+ // FloatBuffer pmv = m.glGetMviMatrixf();
+ // pmv = m.glGetPMatrixf();
+ // for (int i = 0; i < 16; i++) {
+ // System.out.println(pmv.get(i) + " == ");
+ // }
+ // GL2 gl = drawable.getGL().getGL2();
+ // gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
+ // gl.glLoadIdentity();
+ // GLU glu = new GLU();
+ // glu.gluPerspective(30, 1, 1, 100);
+ // gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
+ // gl.glLoadIdentity();
+ // gl.glTranslatef(5, 6, 7);
+ // gl.glRotatef(50, 0, 1, 0);
+ // gl.glRotatef(30, 1, 0, 0);
+ //
+ // FloatBuffer glmv = FloatBuffer.allocate(16);
+ // gl.glGetFloatv(GLMatrixFunc.GL_MODELVIEW_MATRIX, glmv);
+ // for (int i = 0; i < 16; i++) {
+ // System.out.println(pmv.get(i) + " == " + glmv.get(i));
+ // }
+ // System.exit(0);
}
public void start() {
@@ -167,6 +215,7 @@ public class GameManager implements GLEventListener {
System.err.println(Thread.currentThread() + " GL:" + gl);
System.err.println(Thread.currentThread() + " GL_VERSION=" + gl.glGetString(GL.GL_VERSION));
renderer.init(drawable);
+ glTest(drawable);
}
@Override
diff --git a/src/ru/olamedia/game/GameTime.java b/src/ru/olamedia/game/GameTime.java
index 9c2348a..7e4880f 100644
--- a/src/ru/olamedia/game/GameTime.java
+++ b/src/ru/olamedia/game/GameTime.java
@@ -8,6 +8,28 @@ import java.util.TimeZone;
import ru.olamedia.astronomy.SunCalendar;
public class GameTime {
+ public static class SunCalc {
+ private GameTime time;
+ private int r = 450;
+
+ public SunCalc(GameTime gameTime) {
+ this.time = gameTime;
+ }
+
+ public float getX() {
+ return (float) (Math.sin(Math.PI * time.getSunHA() / 180) * r);
+ }
+
+ public float getY() {
+ return (float) (Math.cos(Math.PI * time.getSunHA() / 180) * r);
+ }
+
+ public float getZ() {
+ return (float) (Math.sin(Math.PI * time.sunCalendar.getDeclination() / 180) * r);
+ }
+
+ }
+
public double longitude = 56.803698;
public double latitude = 60.635262;
public double gameSpeed = 60;// 1 real second is 60 seconds at game
@@ -35,6 +57,7 @@ public class GameTime {
public double sunHA; //
public double sunEA; //
public double sunTC; //
+ public SunCalc sun = new SunCalc(this);
public static double getSystemTime() {
return System.currentTimeMillis() / (double) (1000);
@@ -89,7 +112,7 @@ public class GameTime {
float[] spaceColors = new float[] { 0.03f, 0.03f, 0.05f }; // a little
// blue
-
+
clearColors = new float[] { 0.0f, 0.0f, 0.0f };
double crossAngle = sunCalendar.getHourAngle();
@@ -137,9 +160,19 @@ public class GameTime {
sunCalendar.update();
sunrise = Math.floor(sunCalendar.getSunrise() * 1000) / 1000;
sunset = Math.floor(sunCalendar.getSunset() * 1000) / 1000;
- sunHA = sunCalendar.getHourAngle();
+ sunHA = sunCalendar.getHourAngle(); // Hour Angle is 0° at solar
+ // noon
sunEA = Math.floor(sunCalendar.getElevationAngle() * 1000) / 1000;
sunTC = Math.floor(sunCalendar.getTimeCorrectionFactor() * 1000) / 1000;
}
}
+
+ public float getSunHA() {
+ return (float) sunCalendar.getHourAngle(); // Hour Angle is 0° at solar
+ // noon
+ }
+
+ public float getSunEA() {
+ return (float) sunCalendar.getElevationAngle();
+ }
}
diff --git a/src/ru/olamedia/geom/Mesh.java b/src/ru/olamedia/geom/Mesh.java
index 842291f..92f5666 100644
--- a/src/ru/olamedia/geom/Mesh.java
+++ b/src/ru/olamedia/geom/Mesh.java
@@ -7,7 +7,6 @@ import java.util.HashMap;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
-import javax.media.opengl.GL2ES1;
import javax.media.opengl.GL2ES2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLContext;
@@ -45,7 +44,6 @@ public class Mesh {
private static int vertexSize = 10;
- private GLArrayDataServer interleaved;
private HashMap<Integer, Integer> materials = new HashMap<Integer, Integer>();
private HashMap<Integer, GLArrayDataServer> arrays = new HashMap<Integer, GLArrayDataServer>();
@@ -60,7 +58,6 @@ public class Mesh {
this.GLTexture = tex;
}
- @SuppressWarnings("unused")
private static boolean useVbo = true;
private static boolean useQuad = true;
@@ -80,25 +77,6 @@ public class Mesh {
return ByteBuffer.allocateDirect(size * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
}
- /**
- * Reallocates an array with a new size, and copies the contents of the old
- * array to the new array.
- *
- * @param oldArray
- * the old array, to be reallocated.
- * @param newSize
- * the new array size.
- * @return A new array with the same contents.
- */
- private static float[] resizeArray(float[] oldArray, int newSize) {
- int oldSize = oldArray.length;
- float[] newArray = new float[newSize];
- int preserveLength = Math.min(oldSize, newSize);
- if (preserveLength > 0)
- System.arraycopy(oldArray, 0, newArray, 0, preserveLength);
- return newArray;
- }
-
public void compact() {
// data = resizeArray(data, vertexCount * vertexSize);
int size = vertexCount * vertexSize;
@@ -145,14 +123,6 @@ public class Mesh {
if (vertexCount > 0) {
// System.out.println(interleaved);
}
- /*
- * buffer.position(0);
- * buffer.limit(size * vertexSize);
- * buffer.position(0);
- * buffer.limit(vertexCount * vertexSize);
- * buffer.compact();
- * buffer.position(0);
- */
}
public void endMesh() {
@@ -248,8 +218,8 @@ public class Mesh {
}
GL glx = GLContext.getCurrentGL();
if (useVbo) {
- //GL2ES2 gl = glx.getGL2ES2();
- GL2 gl = glx.getGL2();
+ GL2ES2 gl = glx.getGL2ES2();
+ //GL2 gl = glx.getGL2();
for (Integer m : materials.keySet()) {
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBindTexture(GL.GL_TEXTURE_2D, (int) m);
diff --git a/src/ru/olamedia/liveEntity/LiveEntity.java b/src/ru/olamedia/liveEntity/LiveEntity.java
index ce31108..dd60310 100644
--- a/src/ru/olamedia/liveEntity/LiveEntity.java
+++ b/src/ru/olamedia/liveEntity/LiveEntity.java
@@ -7,9 +7,13 @@ import com.jogamp.newt.event.KeyEvent;
import ru.olamedia.camera.Cameraman;
import ru.olamedia.olacraft.game.Game;
import ru.olamedia.olacraft.inventory.Inventory;
+import ru.olamedia.olacraft.network.GameClient;
+import ru.olamedia.olacraft.scene.GameScene;
import ru.olamedia.olacraft.world.block.Block;
import ru.olamedia.olacraft.world.blockStack.BlockStack;
import ru.olamedia.olacraft.world.blockTypes.GravelBlockType;
+import ru.olamedia.olacraft.world.chunk.ChunkUnavailableException;
+import ru.olamedia.olacraft.world.location.BlockLocation;
import ru.olamedia.input.Keyboard;
public class LiveEntity implements Cameraman {
@@ -101,15 +105,16 @@ public class LiveEntity implements Cameraman {
return getHeight() * 0.9f;
}
- public boolean isEmptyUnderFoot() {
+ public boolean isEmptyUnderFoot() throws ChunkUnavailableException {
return isEmptyBlock(0, -1, 0);
}
/**
* @param delta
* the elapsed time since the last frame update in seconds
+ * @throws ChunkUnavailableException
*/
- public void updateKeyboard(float delta) {
+ public void updateKeyboard(float delta) throws ChunkUnavailableException {
acceleration.x = 0;
acceleration.y = 0;
acceleration.z = 0;
@@ -170,53 +175,16 @@ public class LiveEntity implements Cameraman {
acceleration.x *= normalSpeed;// running
acceleration.y *= normalSpeed;
acceleration.z *= normalSpeed;
-
- // if (inJump) {
- // float FPS = (float) (delta / 1);
- // float accelY = Game.world.gravity;
- // float dt = (float) (delta / 1); // in seconds
- // vVelocity -= accelY * dt;
- // y += vVelocity * dt;
- // if (vVelocity < 0) {
- // inFall = true;
- // }
- // if (vVelocity > 0) {
- // if (!keyJump) {
- // vVelocity = 0;
- // }
- // }
- // if (!applyPosition()) {
- // if (vVelocity > 0) {
- // // roof
- // vVelocity = 0;
- // } else {
- // y = underFoot().getY() + 1;
- // vVelocity = 0;
- // inJump = false;
- // inFall = false;
- // }
- // }
- //
- // } else {
- // if (!inFall) {
- // if (inAir()) {
- //
- // inJump = true;
- // inFall = true;
- // }
- // }
- // }
- if (!onGround && !inAir()) {
- // LANDING
+ if (!inAir()) {
+ // FIX Y TO GROUND LEVEL
onGround = true;
inJump = false;
velocity.y = 0;
acceleration.y = 0;
- y = getJumperBlock().getY() + 1;
+ y = getJumperBlock().getY() + 0.5f;
}
if (onGround && velocity.length() > 0 && acceleration.length() > 0) {
- // BEFORE NEW JUMP
- // Check direction changed
+ // SLOWLY CHANGE DIRECTIONS, BEFORE NEW JUMP, IF MOVING
float vy = velocity.y;
float ay = acceleration.y;
velocity.y = 0;
@@ -236,11 +204,6 @@ public class LiveEntity implements Cameraman {
if (velocity.length() * Math.cos(dAngle) > speedLimit) {
float deltaSpeed = (float) (velocity.length() * Math.cos(dAngle) - speedLimit);
velocity.scale((1 / velocity.length()) * (velocity.length() - deltaSpeed * 0.7f));
- // velocity.scale((float) (10 / velocity.length() *
- // Math.cos(dAngle)));
- // velocity.scale((float) (velocity.length()
- // - (velocity.length() - (10 / velocity.length() *
- // Math.cos(dAngle))) * 0.5f * delta));
}
} else {
if (velocity.length() > speedLimit) {
@@ -252,7 +215,7 @@ public class LiveEntity implements Cameraman {
acceleration.y = ay;
}
if (onGround && keyJump) {
- // JUMPING
+ // START NEW JUMP
inJump = true;
onGround = false;
velocity.y = getJumpVelocity(getMaxJumpHeight());
@@ -262,7 +225,8 @@ public class LiveEntity implements Cameraman {
System.out.println("Starting velocity " + velocity.y);
}
- if (onGround && !inJump) {
+ if (onGround) {
+ // FIX SPEED WHILE ON GROUND
if (velocity.length() > normalSpeed) {
velocity.scale(normalSpeed / velocity.length());
}
@@ -303,7 +267,8 @@ public class LiveEntity implements Cameraman {
// }
// velocity.scale(qd);
- if (!onGround || inAir()) { // 0_o
+ if (!onGround) {
+ // APPLY GRAVITY
acceleration.y = -Game.client.getWorldInfo().gravity;
}
@@ -417,29 +382,44 @@ public class LiveEntity implements Cameraman {
}
}
- }
- private Block underFoot() {
- return getBlock(0, -1, 0);
}
public boolean isEmptyBlock(int dx, int dy, int dz) {
- return Game.client.getWorldProvider().isEmptyBlock((int) x + dx, (int) Math.ceil(y) + dy, (int) z + dz);
+ BlockLocation loc = new BlockLocation((int) x + dx, (int) Math.ceil(y) + dy, (int) z + dz);
+ if (!Game.client.getWorldProvider().isChunkAvailable(loc.getChunkLocation())) {
+ Game.client.getWorldProvider().loadChunk(loc.getChunkLocation());
+ return false;
+ }
+
+ try {
+ return Game.client.getWorldProvider().isEmptyBlock((int) x + dx, (int) Math.ceil(y) + dy, (int) z + dz);
+ } catch (ChunkUnavailableException e) {
+ e.printStackTrace();
+ return false;
+ }
}
- public boolean haveBlockUnder(int dy) {
+ public boolean haveBlockUnder(int dy) throws ChunkUnavailableException {
return !isEmptyBlock(0, -dy, 0);
}
- public boolean inAir() {
- return !haveBlockUnder(1);
+ public boolean inAir() throws ChunkUnavailableException {
+ if (!isEmptyBlock(0, -1, 0)) {
+ if (y > getBlock(0, -1, 0).getY() + 0.5f) {
+ return true;
+ }
+ return false;
+ } else {
+ return true;
+ }
}
- public boolean underJumpHeight() {
+ public boolean underJumpHeight() throws ChunkUnavailableException {
return haveBlockUnder(1) || haveBlockUnder(2);
}
- public Block getJumperBlock() {
+ public Block getJumperBlock() throws ChunkUnavailableException {
if (haveBlockUnder(1)) {
return getBlock(0, -1, 0);
}
@@ -465,7 +445,42 @@ public class LiveEntity implements Cameraman {
z = savedZ;
}
- public boolean hasValidPosition() {
+ public boolean hasValidPosition(Block head, boolean checkTop, boolean checkBottom) throws ChunkUnavailableException {
+ // Check if we're too near to the wall
+ Block[] LRNeighbors = { head.getNeighbor(-1, 0, 0), head.getNeighbor(1, 0, 0) };
+ for (Block n : LRNeighbors) {
+ if (n.isEmpty()) {
+ continue;
+ }
+ if (Math.abs(n.getX() - x) < 0.5) {
+ return false;
+ }
+ }
+ Block[] FBNeighbors = { head.getNeighbor(0, 0, -1), head.getNeighbor(0, 0, 1) };
+ for (Block n : FBNeighbors) {
+ if (n.isEmpty()) {
+ continue;
+ }
+ if (Math.abs(n.getZ() - z) < 0.5) {
+ return false;
+ }
+ }
+ // if (checkBottom) {
+ // Block n = head.getNeighbor(0, -1, 0);
+ // if (Math.abs(n.getY() - y) < 0.5) {
+ // return false;
+ // }
+ // }
+ // if (checkTop) {
+ // Block n = head.getNeighbor(0, 1, 0);
+ // if (Math.abs(n.getY() - y) < 0.5) {
+ // return false;
+ // }
+ // }
+ return head.isEmpty();
+ }
+
+ public boolean hasValidPosition() throws ChunkUnavailableException {
Block foot = getBlock(0, 0, 0);
Block underFoot = getBlock(0, -1, 0);
Block head = getBlock(0, (int) getHeight(), 0);
@@ -478,51 +493,11 @@ public class LiveEntity implements Cameraman {
}
}
}
- // Check if we're too near to the wall
- float screenPlane = 0.2f;
- float screenPlaneVertical = 0.4f;
- Block[] headNeighbors = head.getNeighbors();
- for (Block b : headNeighbors) {
- if (b.isEmpty()) {
- continue;
- }
- if (b.getX() != head.getX()) {
- // LEFT or RIGHT
- float testX = b.getX();
- if (testX < head.getX()) {
- // LEFT, fixing
- testX = head.getX();
- }
- float minDistance = Math.abs(testX - getX());
- if (minDistance < screenPlane) {
- return false;
- }
- }
- if (b.getY() > head.getY()) { // Upper block
- float minDistance = Math.abs(b.getY() - getY() + this.getHeight());// player
- // height
- if (minDistance < screenPlaneVertical) {
- return false;
- }
- }
- if (b.getZ() != head.getZ()) {
- // FRONT OR BACK
- float testZ = b.getZ();
- if (testZ < head.getZ()) {
- // BACK, fixing
- testZ = head.getZ();
- }
- float minDistance = Math.abs(testZ - getZ());
- if (minDistance < screenPlane) {
- return false;
- }
- }
- }
- return foot.isEmpty() && head.isEmpty();
+ return hasValidPosition(foot, false, true) && hasValidPosition(head, true, false);
}
- public boolean applyPosition() {
+ public boolean applyPosition() throws ChunkUnavailableException {
isPositionChanged = false;
if (hasValidPosition()) {
backupPosition();
@@ -537,7 +512,7 @@ public class LiveEntity implements Cameraman {
// overriden at player class
}
- public void fixPosition() {
+ public void fixPosition() throws ChunkUnavailableException {
float dx = x - savedX;
float dy = y - savedY;
float dz = z - savedZ;
@@ -561,11 +536,13 @@ public class LiveEntity implements Cameraman {
isPositionChanged = false;
backupPosition();
onGround = false;
- Block below = getBlock(0, -1, 0);
- if (y == below.getY() + 1) {
- onGround = true;
+ try {
+ onGround = !inAir();
+ updateKeyboard(delta);
+ } catch (ChunkUnavailableException e) {
+ e.printStackTrace();
+ System.exit(0);
}
- updateKeyboard(delta);
// Check if position is valid
// fixPosition();
diff --git a/src/ru/olamedia/olacraft/render/jogl/ChunkRenderer.java b/src/ru/olamedia/olacraft/render/jogl/ChunkRenderer.java
index 22a88ab..f53bba6 100644
--- a/src/ru/olamedia/olacraft/render/jogl/ChunkRenderer.java
+++ b/src/ru/olamedia/olacraft/render/jogl/ChunkRenderer.java
@@ -1,8 +1,6 @@
package ru.olamedia.olacraft.render.jogl;
-import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
-import javax.media.opengl.GLContext;
import ru.olamedia.Options;
import ru.olamedia.geom.SimpleQuadMesh;
@@ -36,12 +34,13 @@ public class ChunkRenderer {
public boolean renderChunk(Chunk chunk, boolean skipnew) {
if (!chunk.isAvailable()) {
- // System.out.println("not available");
+ System.out.println("not available " + chunk);
chunk.request();
return skipnew;
}
if (!chunk.isNeighborsAvailable()) {
+ System.out.println("not available " + chunk);
chunk.requestNeighbors();
return skipnew;
}
diff --git a/src/ru/olamedia/olacraft/scene/GameScene.java b/src/ru/olamedia/olacraft/scene/GameScene.java
index 37a5098..86b9574 100644
--- a/src/ru/olamedia/olacraft/scene/GameScene.java
+++ b/src/ru/olamedia/olacraft/scene/GameScene.java
@@ -23,10 +23,12 @@ import ru.olamedia.olacraft.render.jogl.InventoryRenderer;
import ru.olamedia.olacraft.render.jogl.joglViewport;
import ru.olamedia.olacraft.weapon.Bullet;
import ru.olamedia.olacraft.weapon.BulletScene;
+import ru.olamedia.olacraft.world.block.Block;
import ru.olamedia.olacraft.world.blockTypes.AbstractBlockType;
import ru.olamedia.olacraft.world.blockTypes.GrassBlockType;
import ru.olamedia.olacraft.world.chunk.BlockSlice;
import ru.olamedia.olacraft.world.chunk.ChunkSlice;
+import ru.olamedia.olacraft.world.chunk.ChunkUnavailableException;
import ru.olamedia.olacraft.world.provider.WorldProvider;
import ru.olamedia.player.Player;
import ru.olamedia.vbo.VBO;
@@ -134,6 +136,8 @@ public class GameScene {
return liveEntities;
}
+ Block nearestBlock = null;
+
public void tick() {
time.tick();
Game.instance.tick();
@@ -141,6 +145,9 @@ public class GameScene {
Game.instance.camera.setAspect(aspect);
// bullets.update(Game.instance.getDelta());
physics.getWorld().step(Game.instance.getDelta());
+ BlockSlice pickSlice = new BlockSlice(provider, 10, 10, 10);
+ pickSlice.setCenter(player.getCameraX(), player.getCameraY(), player.getCameraZ());
+ nearestBlock = pickSlice.getNearest(Game.instance.camera);
}
public void render(GLAutoDrawable drawable) {
@@ -161,6 +168,32 @@ public class GameScene {
gl.glPushMatrix();
gl.glPushAttrib(GL2.GL_ALL_ATTRIB_BITS);
Game.instance.camera.setUp(drawable);
+ // RENDER SUN
+ gl.glPushAttrib(GL2.GL_ALL_ATTRIB_BITS);
+ gl.glEnable(GL2.GL_BLEND); // Enable Blending
+ gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE); // Set Blending Mode To
+ // Mix Based On SRC
+ // Alpha
+ GLUquadric sun = glu.gluNewQuadric();
+ glu.gluQuadricDrawStyle(sun, GLU.GLU_FILL);
+ glu.gluQuadricNormals(sun, GLU.GLU_SMOOTH);
+ gl.glPushMatrix();
+ gl.glTranslatef(time.sun.getX() + player.getCameraX(), time.sun.getY(), time.sun.getZ() + player.getCameraZ());
+ gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_FILL);
+ gl.glColor4f(1, 1, 1, 0.02f);
+ glu.gluSphere(sun, 100f, 10, 10);
+ glu.gluSphere(sun, 90f, 10, 10);
+ glu.gluSphere(sun, 80f, 10, 10);
+ glu.gluSphere(sun, 70f, 10, 10);
+ glu.gluSphere(sun, 60f, 10, 10);
+ glu.gluSphere(sun, 50f, 10, 10);
+ glu.gluSphere(sun, 40f, 10, 10);
+ glu.gluSphere(sun, 35f, 10, 10);
+ gl.glColor4f(1, 1, 1, 1f);
+ glu.gluSphere(sun, 30f, 10, 10);
+ gl.glPopMatrix();
+ gl.glPopAttrib();
+
viewSlice.setCenter((int) Game.instance.camera.getX(), (int) Game.instance.camera.getY(),
(int) Game.instance.camera.getZ());
// RENDER BLOCKS
@@ -190,7 +223,7 @@ public class GameScene {
gl.glPushMatrix();
gl.glTranslatef(entity.getX(), entity.getCameraY(), entity.getZ());
gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);
- glu.gluSphere(qobj0, 0.5f, 10, 10);
+ glu.gluSphere(qobj0, 0.1f, 10, 10);
gl.glPopMatrix();
}
gl.glPopAttrib();
@@ -246,12 +279,14 @@ public class GameScene {
viewport.drawText("mem: " + (heap.getUsed() / (1024 * 1024)) + "/" + (heap.getMax() / (1024 * 1024)), 10,
height - 50);
- viewport.drawText("y: " + Game.instance.player.getY(), width - msz - 10, height - msz - 25);
+ viewport.drawText("y: " + Game.instance.player.getY(), width - msz - 10, height - msz - 20);
+ viewport.drawText("y: " + Game.instance.player.getCameraY() + " (cam)", width - msz - 10, height - msz - 30);
viewport.drawText("x: " + Game.instance.player.getX(), width - msz - 10, height - msz - 40);
- viewport.drawText("z: " + Game.instance.player.getZ(), width - msz - 10, height - msz - 55);
+ viewport.drawText("z: " + Game.instance.player.getZ(), width - msz - 10, height - msz - 50);
viewport.drawText("players: " + liveEntities.size(), width - msz - 10, height - msz - 70);
viewport.drawText("bullets: " + getBulletsCount(), width - msz - 10, height - msz - 95);
- viewport.drawText("inAir: " + Game.instance.player.inAir(), width - msz - 10, height - msz - 110);
+ viewport.drawText("y velocity: " + player.velocity.y + " y accel: " + player.acceleration.y + " inJump: "
+ + player.inJump + " onGround: " + player.onGround, width - msz - 350 - 10, height - msz - 110);
viewport.drawText("rdistance: " + renderDistance, width - msz - 10, height - msz - 155);
ChunkSlice cs = viewSlice.getChunkSlice();
@@ -259,6 +294,10 @@ public class GameScene {
+ (cs.getY() + cs.getHeight() - 1) + " z: " + cs.getZ() + ".." + (cs.getZ() + cs.getDepth() - 1), width
- msz * 2 - 10, height - msz - 170);
viewport.drawText("time: " + time.getDateTimeString(), width - msz * 2 - 10, height - msz - 185);
+ if (nearestBlock != null) {
+ viewport.drawText("block: " + nearestBlock.getX() + ":" + nearestBlock.getY() + ":" + nearestBlock.getZ()
+ + " d " + nearestBlock.getDistance(Game.instance.camera), width - msz * 2 - 10, height - msz - 200);
+ }
gl.glPopAttrib();
gl.glPopMatrix();
diff --git a/src/ru/olamedia/olacraft/world/block/Block.java b/src/ru/olamedia/olacraft/world/block/Block.java
index a48c9df..44e9b1d 100644
--- a/src/ru/olamedia/olacraft/world/block/Block.java
+++ b/src/ru/olamedia/olacraft/world/block/Block.java
@@ -1,7 +1,11 @@
package ru.olamedia.olacraft.world.block;
+import javax.vecmath.Vector3f;
+
+import ru.olamedia.camera.MatrixCamera;
import ru.olamedia.olacraft.world.blockTypes.BlockType;
import ru.olamedia.olacraft.world.blockTypes.EmptyBlockType;
+import ru.olamedia.olacraft.world.chunk.ChunkUnavailableException;
import ru.olamedia.olacraft.world.provider.WorldProvider;
public class Block {
@@ -76,7 +80,7 @@ public class Block {
this.z = z;
}
- public boolean isEmpty() {
+ public boolean isEmpty() throws ChunkUnavailableException {
return provider.isEmptyBlock(x, y, z);
}
@@ -108,4 +112,75 @@ public class Block {
}
return type;
}
+
+ public float getDistance(MatrixCamera cam) {
+ String nearest = "";
+ float topDistance = cam.intersectsRectangle(getTopLeftBack(), getTopLeftFront(), getTopRightFront(),
+ getTopRightBack());
+ float bottomDistance = cam.intersectsRectangle(getBottomLeftBack(), getBottomLeftFront(),
+ getBottomRightFront(), getBottomRightBack());
+ float leftDistance = cam.intersectsRectangle(getTopLeftBack(), getTopLeftFront(), getBottomLeftFront(),
+ getBottomLeftBack());
+ float rightDistance = cam.intersectsRectangle(getTopRightBack(), getTopRightFront(), getBottomRightFront(),
+ getBottomRightBack());
+ float frontDistance = cam.intersectsRectangle(getTopLeftFront(), getTopRightFront(), getBottomRightFront(),
+ getBottomLeftFront());
+ float backDistance = cam.intersectsRectangle(getTopLeftBack(), getTopRightBack(), getBottomRightBack(),
+ getBottomLeftBack());
+ float topBottom = Math.min(topDistance, bottomDistance);
+ float leftRight = Math.min(leftDistance, rightDistance);
+ float frontBack = Math.min(frontDistance, backDistance);
+ float distance = Math.min(Math.min(topBottom, leftRight), frontBack);
+ if (distance == bottomDistance) {
+ nearest = "BOTTOM";
+ }
+ if (distance == topDistance) {
+ nearest = "TOP";
+ }
+ if (distance == leftDistance) {
+ nearest = "LEFT";
+ }
+ if (distance == rightDistance) {
+ nearest = "RIGHT";
+ }
+ if (distance == frontDistance) {
+ nearest = "FRONT";
+ }
+ if (distance == backDistance) {
+ nearest = "BACK";
+ }
+ return distance;
+ }
+
+ private Vector3f getBottomRightBack() {
+ return new Vector3f(x + 0.5f, y - 0.5f, z - 0.5f);
+ }
+
+ private Vector3f getBottomRightFront() {
+ return new Vector3f(x + 0.5f, y - 0.5f, z + 0.5f);
+ }
+
+ private Vector3f getBottomLeftFront() {
+ return new Vector3f(x - 0.5f, y - 0.5f, z + 0.5f);
+ }
+
+ private Vector3f getBottomLeftBack() {
+ return new Vector3f(x - 0.5f, y - 0.5f, z - 0.5f);
+ }
+
+ private Vector3f getTopRightBack() {
+ return new Vector3f(x + 0.5f, y + 0.5f, z - 0.5f);
+ }
+
+ private Vector3f getTopLeftFront() {
+ return new Vector3f(x - 0.5f, y + 0.5f, z + 0.5f);
+ }
+
+ private Vector3f getTopRightFront() {
+ return new Vector3f(x + 0.5f, y + 0.5f, z + 0.5f);
+ }
+
+ private Vector3f getTopLeftBack() {
+ return new Vector3f(x - 0.5f, y + 0.5f, z - 0.5f);
+ }
}
diff --git a/src/ru/olamedia/olacraft/world/chunk/BlockSlice.java b/src/ru/olamedia/olacraft/world/chunk/BlockSlice.java
index bc744c4..bad802f 100644
--- a/src/ru/olamedia/olacraft/world/chunk/BlockSlice.java
+++ b/src/ru/olamedia/olacraft/world/chunk/BlockSlice.java
@@ -1,16 +1,79 @@
package ru.olamedia.olacraft.world.chunk;
+import java.util.Iterator;
+
+import ru.olamedia.camera.MatrixCamera;
+import ru.olamedia.olacraft.world.block.Block;
import ru.olamedia.olacraft.world.location.BlockLocation;
import ru.olamedia.olacraft.world.location.ChunkLocation;
import ru.olamedia.olacraft.world.provider.WorldProvider;
-public class BlockSlice {
+public class BlockSlice implements Iterator<Block> {
protected WorldProvider provider;
protected BlockLocation offset;
protected int width;
protected int height;
protected int depth;
+ private int itX = 0;
+ private int itY = 0;
+ private int itZ = 0;
+
+ public Block getNearest(MatrixCamera cam) {
+ float notEmptyBlockDistance = Float.MAX_VALUE;
+ Block nearestBlock = null;
+ while (hasNext()) {
+ Block b = next();
+ try {
+ if (!b.isEmpty()) {
+ float d = b.getDistance(cam);
+ //System.out.print("d: " + d + " ");
+ if (d <= notEmptyBlockDistance) {
+ notEmptyBlockDistance = d;
+ nearestBlock = b;
+ }
+ }
+ } catch (ChunkUnavailableException e) {
+ e.printStackTrace();
+ }
+ }
+ return nearestBlock;
+ }
+
+ @Override
+ public boolean hasNext() {
+ itZ++;
+ if (itZ > depth) {
+ itY++;
+ itZ = 0;
+ if (itY > height) {
+ itX++;
+ itY = 0;
+ if (itX > width) {
+ itX = 0;
+ itY = 0;
+ itZ = 0;
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public Block next() {
+ return getBlock(offset.x + itX, offset.y + itY, offset.z + itZ);
+ }
+
+ private Block getBlock(int x, int y, int z) {
+ return new Block(provider, x, y, z);
+ }
+
+ @Override
+ public void remove() {
+ // do nothing >_> why we have to remove block???
+ }
+
protected ChunkSlice chunkSlice;
public void invalidateCache() {
@@ -134,4 +197,8 @@ public class BlockSlice {
public String toString() {
return this.getClass().getSimpleName() + "[" + offset + ";" + width + "x" + height + "x" + depth + "]";
}
+
+ public void setCenter(float x, float y, float z) {
+ setCenter((int) x, (int) y, (int) z);
+ }
}
diff --git a/src/ru/olamedia/olacraft/world/chunk/Chunk.java b/src/ru/olamedia/olacraft/world/chunk/Chunk.java
index 0eaad3c..9c0afb7 100644
--- a/src/ru/olamedia/olacraft/world/chunk/Chunk.java
+++ b/src/ru/olamedia/olacraft/world/chunk/Chunk.java
@@ -97,12 +97,12 @@ public class Chunk extends BlockSlice {
return mesh;
}
if (offset.y > provider.getInfo().maxHeight) {
- isMeshCostructed = true;
- return null;
+ // isMeshCostructed = true;
+ // return null;
}
if (offset.y < provider.getInfo().minHeight) {
- isMeshCostructed = true;
- return null;
+ // isMeshCostructed = true;
+ // return null;
}
if (null == mesh) {
mesh = new SimpleQuadMesh(14739); // unindexed
@@ -117,78 +117,81 @@ public class Chunk extends BlockSlice {
for (int y = offset.y; y < offset.y + getHeight(); y++) {
for (int z = offset.z; z < offset.z + getDepth(); z++) {
//
-
- if (!isEmptyBlock(x, y, z)) {
- mesh.setTranslation(x, y, z);
- // mesh.setColor4f(0, 1, 0, 1);
- float cbase = (float) (y / 200.0) * (float) (7.0 / 10.0);
- if (cbase > 9 / 10) {
- cbase = (float) (9.0 / 10.0);
- }
- // cbase = (float) (9.0 / 10.0);
- float cred, cgreen, cblue;
- // cbase;
- cred = cgreen = cblue = getLightLevel256(x, y, z);
- if (x == 1) {
- mesh.setColor4f(1, 0, 0, 1);
- // red to the right
- }
- if (x == 0 || z == 0) {
- if (y == 6) {
+ try {
+ if (!isEmptyBlock(x, y, z)) {
+ mesh.setTranslation(x, y, z);
+ // mesh.setColor4f(0, 1, 0, 1);
+ float cbase = (float) (y / 200.0) * (float) (7.0 / 10.0);
+ if (cbase > 9 / 10) {
+ cbase = (float) (9.0 / 10.0);
+ }
+ // cbase = (float) (9.0 / 10.0);
+ float cred, cgreen, cblue;
+ // cbase;
+ cred = cgreen = cblue = getLightLevel256(x, y, z);
+ if (x == 1) {
mesh.setColor4f(1, 0, 0, 1);
- } else if (y % 2 == 0) {
- mesh.setColor4f(1, 0, 1, 1);
- } else {
- mesh.setColor4f(1, 1, 0, 1);
+ // red to the right
}
- }
- if (z == 1) {
- mesh.setColor4f(0, 0, 1, 1);
- // blue to the bottom
- }
- if (renderBottom(x, y, z)) {
- setMeshColor(mesh, x, y - 1, z, false);
- mesh.setTexture(grass.getBottomTexture());
- mesh.addBottomQuad();
- visibleBottom++;
- }
- if (renderTop(x, y, z)) {
- if (x == 15 || z == 15) {
- // debug: show through..
- } else {
- setMeshColor(mesh, x, y + 1, z, false);
- mesh.setTexture(grass.getTopTexture());
- mesh.addTopQuad();
+ if (x == 0 || z == 0) {
+ if (y == 6) {
+ mesh.setColor4f(1, 0, 0, 1);
+ } else if (y % 2 == 0) {
+ mesh.setColor4f(1, 0, 1, 1);
+ } else {
+ mesh.setColor4f(1, 1, 0, 1);
+ }
}
- visibleTop++;
- }
- if (renderLeft(x, y, z)) {
- setMeshColor(mesh, x - 1, y, z, true);
- mesh.setTexture(grass.getLeftTexture());
- mesh.addLeftQuad();
- visibleLeft++;
- }
- if (renderRight(x, y, z)) {
- setMeshColor(mesh, x + 1, y, z, true);
- mesh.setTexture(grass.getRightTexture());
- mesh.addRightQuad();
- visibleRight++;
- }
- if (renderBack(x, y, z)) {
- setMeshColor(mesh, x, y, z - 1, true);
- mesh.setTexture(grass.getBackTexture());
- mesh.addBackQuad();
- visibleBack++;
- }
- if (renderFront(x, y, z)) {
- setMeshColor(mesh, x, y, z + 1, true);
- mesh.setTexture(grass.getFrontTexture());
- mesh.addFrontQuad();
- visibleFront++;
+ if (z == 1) {
+ mesh.setColor4f(0, 0, 1, 1);
+ // blue to the bottom
+ }
+ if (renderBottom(x, y, z)) {
+ setMeshColor(mesh, x, y - 1, z, false);
+ mesh.setTexture(grass.getBottomTexture());
+ mesh.addBottomQuad();
+ visibleBottom++;
+ }
+ if (renderTop(x, y, z)) {
+ if (x == 15 || z == 15) {
+ // debug: show through..
+ } else {
+ setMeshColor(mesh, x, y + 1, z, false);
+ mesh.setTexture(grass.getTopTexture());
+ mesh.addTopQuad();
+ }
+ visibleTop++;
+ }
+ if (renderLeft(x, y, z)) {
+ setMeshColor(mesh, x - 1, y, z, true);
+ mesh.setTexture(grass.getLeftTexture());
+ mesh.addLeftQuad();
+ visibleLeft++;
+ }
+ if (renderRight(x, y, z)) {
+ setMeshColor(mesh, x + 1, y, z, true);
+ mesh.setTexture(grass.getRightTexture());
+ mesh.addRightQuad();
+ visibleRight++;
+ }
+ if (renderBack(x, y, z)) {
+ setMeshColor(mesh, x, y, z - 1, true);
+ mesh.setTexture(grass.getBackTexture());
+ mesh.addBackQuad();
+ visibleBack++;
+ }
+ if (renderFront(x, y, z)) {
+ setMeshColor(mesh, x, y, z + 1, true);
+ mesh.setTexture(grass.getFrontTexture());
+ mesh.addFrontQuad();
+ visibleFront++;
+ }
+ // System.out.println("mesh not empty");
+ } else {
+ // System.out.println("mesh empty");
}
- // System.out.println("mesh not empty");
- } else {
- // System.out.println("mesh empty");
+ } catch (ChunkUnavailableException e) {
+ e.printStackTrace();
}
}
}
@@ -265,7 +268,7 @@ public class Chunk extends BlockSlice {
// return provider.getBlockType(int x, int y, int z);
// }
- public boolean isEmptyBlock(int x, int y, int z) {
+ public boolean isEmptyBlock(int x, int y, int z) throws ChunkUnavailableException {
return provider.isEmptyBlock(x, y, z);
}
@@ -277,27 +280,27 @@ public class Chunk extends BlockSlice {
return 15;
}
- public boolean renderBottom(int x, int y, int z) {
+ public boolean renderBottom(int x, int y, int z) throws ChunkUnavailableException {
return provider.renderBottom(x, y, z);
}
- public boolean renderTop(int x, int y, int z) {
+ public boolean renderTop(int x, int y, int z) throws ChunkUnavailableException {
return provider.renderTop(x, y, z);
}
- public boolean renderLeft(int x, int y, int z) {
+ public boolean renderLeft(int x, int y, int z) throws ChunkUnavailableException {
return provider.renderLeft(x, y, z);
}
- public boolean renderRight(int x, int y, int z) {
+ public boolean renderRight(int x, int y, int z) throws ChunkUnavailableException {
return provider.renderRight(x, y, z);
}
- public boolean renderFront(int x, int y, int z) {
+ public boolean renderFront(int x, int y, int z) throws ChunkUnavailableException {
return provider.renderFront(x, y, z);
}
- public boolean renderBack(int x, int y, int z) {
+ public boolean renderBack(int x, int y, int z) throws ChunkUnavailableException {
return provider.renderBack(x, y, z);
}
diff --git a/src/ru/olamedia/olacraft/world/chunk/ChunkUnavailableException.java b/src/ru/olamedia/olacraft/world/chunk/ChunkUnavailableException.java
new file mode 100644
index 0000000..9fef715
--- /dev/null
+++ b/src/ru/olamedia/olacraft/world/chunk/ChunkUnavailableException.java
@@ -0,0 +1,7 @@
+package ru.olamedia.olacraft.world.chunk;
+
+public class ChunkUnavailableException extends Exception {
+
+ private static final long serialVersionUID = -5382060550128159125L;
+
+}
diff --git a/src/ru/olamedia/olacraft/world/dataProvider/CachedChunkDataProvider.java b/src/ru/olamedia/olacraft/world/dataProvider/CachedChunkDataProvider.java
index 444cff6..9ba9496 100644
--- a/src/ru/olamedia/olacraft/world/dataProvider/CachedChunkDataProvider.java
+++ b/src/ru/olamedia/olacraft/world/dataProvider/CachedChunkDataProvider.java
@@ -1,8 +1,6 @@
package ru.olamedia.olacraft.world.dataProvider;
-import java.util.ArrayList;
import java.util.HashMap;
-import java.util.List;
import ru.olamedia.olacraft.world.data.RegionData;
import ru.olamedia.olacraft.world.location.RegionLocation;
@@ -10,7 +8,6 @@ import ru.olamedia.olacraft.world.location.RegionLocation;
public class CachedChunkDataProvider extends AbstractChunkDataProvider {
private AbstractChunkDataProvider provider;
private HashMap<String, RegionData> regionMap = new HashMap<String, RegionData>();
- private List<String> loading = new ArrayList<String>();
public CachedChunkDataProvider(AbstractChunkDataProvider provider) {
this.provider = provider;
diff --git a/src/ru/olamedia/olacraft/world/provider/WorldProvider.java b/src/ru/olamedia/olacraft/world/provider/WorldProvider.java
index b7984bc..9e76d1a 100644
--- a/src/ru/olamedia/olacraft/world/provider/WorldProvider.java
+++ b/src/ru/olamedia/olacraft/world/provider/WorldProvider.java
@@ -4,6 +4,7 @@ import ru.olamedia.olacraft.game.SpawnLocation;
import ru.olamedia.olacraft.world.WorldInfo;
import ru.olamedia.olacraft.world.block.Block;
import ru.olamedia.olacraft.world.chunk.Chunk;
+import ru.olamedia.olacraft.world.chunk.ChunkUnavailableException;
import ru.olamedia.olacraft.world.data.ChunkData;
import ru.olamedia.olacraft.world.data.RegionData;
import ru.olamedia.olacraft.world.dataProvider.AbstractChunkDataProvider;
@@ -58,34 +59,34 @@ public class WorldProvider {
return l;
}
- public boolean renderBottom(int x, int y, int z) {
+ public boolean renderBottom(int x, int y, int z) throws ChunkUnavailableException {
return (!isEmptyBlock(x, y, z)) && (isEmptyBlock(x, y - 1, z));
}
- public boolean renderTop(int x, int y, int z) {
+ public boolean renderTop(int x, int y, int z) throws ChunkUnavailableException {
// System.out.println("Check render top " + y + "[" + x + " " + y + " "
// + z + "]" + !isEmptyBlock(x, y, z)
// + " && " + isEmptyBlock(x, y + 1, z));
return (!isEmptyBlock(x, y, z)) && (isEmptyBlock(x, y + 1, z));
}
- public boolean renderLeft(int x, int y, int z) {
+ public boolean renderLeft(int x, int y, int z) throws ChunkUnavailableException {
return (!isEmptyBlock(x, y, z)) && (isEmptyBlock(x - 1, y, z));
}
- public boolean renderRight(int x, int y, int z) {
+ public boolean renderRight(int x, int y, int z) throws ChunkUnavailableException {
return (!isEmptyBlock(x, y, z)) && (isEmptyBlock(x + 1, y, z));
}
- public boolean renderBack(int x, int y, int z) {
+ public boolean renderBack(int x, int y, int z) throws ChunkUnavailableException {
return (!isEmptyBlock(x, y, z)) && (isEmptyBlock(x, y, z - 1));
}
- public boolean renderFront(int x, int y, int z) {
+ public boolean renderFront(int x, int y, int z) throws ChunkUnavailableException {
return (!isEmptyBlock(x, y, z)) && (isEmptyBlock(x, y, z + 1));
}
- public boolean isEmptyBlock(int x, int y, int z) {
+ public boolean isEmptyBlock(int x, int y, int z) throws ChunkUnavailableException {
BlockLocation blockLocation = new BlockLocation(x, y, z);
if (isChunkAvailable(blockLocation.getChunkLocation())) {
@@ -93,11 +94,10 @@ public class WorldProvider {
if (null != data) {
return data.isEmpty(blockLocation);
} else {
- // System.out.println("chunk null " + x + " " + y + " " + z);
+ System.out.println("chunk null " + x + " " + y + " " + z);
}
} else {
- // System.out.println("chunk not available " + x + " " + y + " " +
- // z);
+ throw new ChunkUnavailableException();
}
return true;
}