Skip to content

Commit

Permalink
added camera destinations + pop ups on editor
Browse files Browse the repository at this point in the history
  • Loading branch information
rpereira committed Jan 23, 2018
1 parent eba7af0 commit 97bb198
Show file tree
Hide file tree
Showing 23 changed files with 512 additions and 176 deletions.
3 changes: 3 additions & 0 deletions VoxelEngine/src/com/grillecube/client/opengl/ImageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class ImageUtils {

/** return an image for this file */
public static BufferedImage readImage(String filepath) {
if (filepath == null) {
return (null);
}
BufferedImage image = null;
try {
image = ImageIO.read(new File(filepath));
Expand Down
165 changes: 96 additions & 69 deletions VoxelEngine/src/com/grillecube/client/renderer/camera/Camera.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,99 +14,126 @@

package com.grillecube.client.renderer.camera;

import java.util.LinkedList;

@SuppressWarnings("rawtypes")
public abstract class Camera {

private int _state;
private LinkedList<CameraDestination<?>> destinations;
private CameraDestination curDestination;
private int state;

/** aspect_ratio is WIDTH / HEIGHT */
public Camera() {
this._state = 0;
this.state = 0;
this.destinations = null;
this.curDestination = null;
}

public boolean hasState(int state) {
return ((this._state & state) == state);
return ((this.state & state) == state);
}

public void setState(int state) {
this._state = this._state | state;
this.state = this.state | state;
}

public void unsetState(int state) {
this._state = this._state & ~(state);
this.state = this.state & ~(state);
}

public void switchState(int state) {
this._state = this._state ^ (state);
this.state = this.state ^ (state);
}

public void resetState() {
this._state = 0;
this.state = 0;
}

@SuppressWarnings("unchecked")
public void update() {

if (this.curDestination != null) {
float dt = 1 / 60.0f;
if (this.curDestination.update(this, dt)) {
this.curDestination = null;
}
} else if (this.destinations != null) {
this.curDestination = this.destinations.pop();
this.curDestination.init(this);
if (this.destinations.size() == 0) {
this.destinations = null;
}
}
}

public final void addDestination(CameraDestination destination) {
if (this.destinations == null) {
this.destinations = new LinkedList<CameraDestination<?>>();
}
this.destinations.addLast(destination);
}

public abstract Camera clone();


// /** return true if the given point is inside the frustum of the camera */
// public boolean isInFrustum(float x, float y, float z, float imprecision) {
//
// // get the vector which point to the given point
// float vx = x - this._pos.x;
// float vy = y - this._pos.y;
// float vz = z - this._pos.z;
//
// // get it length
// float length = Vector3f.length(vx, vy, vz);
//
// // normalize the vector
// vx /= length;
// vy /= length;
// vz /= length;
//
// double dot = vx * this._look_vec.x + vy * this._look_vec.y + vz * this._look_vec.z;
// double angle = Math.toDegrees(Math.acos(dot));
// return (angle < (this._fov + imprecision) / 2);
// }
//
// public boolean isInFrustum(Vector3f point, float imprecision) {
// return (this.isInFrustum(point.x, point.y, point.z));
// }
//
// public boolean isInFrustum(float x, float y, float z) {
// return (this.isInFrustum(x, y, z, 0));
// }
//
// public boolean isInFrustum(Vector3f point) {
// return (this.isInFrustum(point.x, point.y, point.z));
// }
//
// /**
// * return true if the box of center "center" with dimension x, y, z is in
// * camera frustum
// */
// public boolean isBoxInFrustum(Vector3f center, float x, float y, float z) {
// return (this.isInFrustum(center.x + x, center.y + y, center.z + z)
// || this.isInFrustum(center.x - x, center.y + y, center.z + z)
// || this.isInFrustum(center.x + x, center.y + y, center.z - z)
// || this.isInFrustum(center.x - x, center.y + y, center.z - z)
// || this.isInFrustum(center.x + x, center.y - y, center.z + z)
// || this.isInFrustum(center.x - x, center.y - y, center.z + z)
// || this.isInFrustum(center.x + x, center.y - y, center.z - z)
// || this.isInFrustum(center.x - x, center.y - y, center.z - z));
// }
//
// public boolean isBoxInFrustum(Vector3f center, float demisize) {
// return (this.isBoxInFrustum(center, demisize, demisize, demisize));
// }
//
// public boolean isBoxInFrustum(Vector3f center, Vector3f demisize) {
// return (this.isBoxInFrustum(center, demisize.x, demisize.y, demisize.z));
// }
//
// public boolean isBoxInFrustum(BoundingBox box) {
// return (this.isBoxInFrustum(box.getCenter(), box.getSize().x / 2.0f, box.getSize().y / 2.0f, box.getSize().z / 2.0f));
// }
// /** return true if the given point is inside the frustum of the camera */
// public boolean isInFrustum(float x, float y, float z, float imprecision) {
//
// // get the vector which point to the given point
// float vx = x - this._pos.x;
// float vy = y - this._pos.y;
// float vz = z - this._pos.z;
//
// // get it length
// float length = Vector3f.length(vx, vy, vz);
//
// // normalize the vector
// vx /= length;
// vy /= length;
// vz /= length;
//
// double dot = vx * this._look_vec.x + vy * this._look_vec.y + vz *
// this._look_vec.z;
// double angle = Math.toDegrees(Math.acos(dot));
// return (angle < (this._fov + imprecision) / 2);
// }
//
// public boolean isInFrustum(Vector3f point, float imprecision) {
// return (this.isInFrustum(point.x, point.y, point.z));
// }
//
// public boolean isInFrustum(float x, float y, float z) {
// return (this.isInFrustum(x, y, z, 0));
// }
//
// public boolean isInFrustum(Vector3f point) {
// return (this.isInFrustum(point.x, point.y, point.z));
// }
//
// /**
// * return true if the box of center "center" with dimension x, y, z is in
// * camera frustum
// */
// public boolean isBoxInFrustum(Vector3f center, float x, float y, float z) {
// return (this.isInFrustum(center.x + x, center.y + y, center.z + z)
// || this.isInFrustum(center.x - x, center.y + y, center.z + z)
// || this.isInFrustum(center.x + x, center.y + y, center.z - z)
// || this.isInFrustum(center.x - x, center.y + y, center.z - z)
// || this.isInFrustum(center.x + x, center.y - y, center.z + z)
// || this.isInFrustum(center.x - x, center.y - y, center.z + z)
// || this.isInFrustum(center.x + x, center.y - y, center.z - z)
// || this.isInFrustum(center.x - x, center.y - y, center.z - z));
// }
//
// public boolean isBoxInFrustum(Vector3f center, float demisize) {
// return (this.isBoxInFrustum(center, demisize, demisize, demisize));
// }
//
// public boolean isBoxInFrustum(Vector3f center, Vector3f demisize) {
// return (this.isBoxInFrustum(center, demisize.x, demisize.y, demisize.z));
// }
//
// public boolean isBoxInFrustum(BoundingBox box) {
// return (this.isBoxInFrustum(box.getCenter(), box.getSize().x / 2.0f,
// box.getSize().y / 2.0f, box.getSize().z / 2.0f));
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.grillecube.client.renderer.camera;

public abstract class CameraDestination<T extends Camera> {

private final float duration;
private float accumulator;

public CameraDestination(float duration) {
this.duration = duration;
}

public final void init(T camera) {
this.accumulator = 0;
this.onInit(camera);
}

protected abstract void onInit(T camera);

/**
*
* @param cameraView
* @param dt
* @return true if the destination is reached
*/
public boolean update(T camera, float dt) {
this.accumulator += dt;
if (this.accumulator >= this.duration) {
this.onReached(camera);
return (true);
}
this.onUpdate(camera, this.accumulator / this.duration);
return (false);
}

/** a callback on destination update (ratio is in [0, 1] */
protected abstract void onUpdate(T camera, float ratio);

/** a callback whenever the destination is reached */
protected abstract void onReached(T camera);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.grillecube.client.renderer.camera;

public class CameraDestinationCenter extends CameraDestination<CameraPerspectiveWorldCentered> {

public final float desiredDistance;
public final float desiredPhi;
public final float desiredTheta;

public float camDistance;
public float camPhi;
public float camTheta;

/**
*
* @param desiredPosition
* : future position of the camera
* @param desiredRotation
* : future rotation of the camera
* @param duration
* : duration of the transition
*/
public CameraDestinationCenter(float desiredDistance, float desiredPhi, float desiredTheta, float duration) {
super(duration);
this.desiredDistance = desiredDistance;
this.desiredPhi = desiredPhi;
this.desiredTheta = desiredTheta;
}

@Override
protected final void onInit(CameraPerspectiveWorldCentered camera) {
this.camDistance = camera.getR();
this.camPhi = camera.getPhi();
this.camTheta = camera.getTheta();
}

@Override
protected final void onUpdate(CameraPerspectiveWorldCentered camera, float ratio) {
camera.setR(this.camDistance * (1.0f - ratio) + this.desiredDistance * ratio);
camera.setPhi(this.camPhi * (1.0f - ratio) + this.desiredPhi * ratio);
camera.setTheta(this.camTheta * (1.0f - ratio) + this.desiredTheta * ratio);
}

@Override
protected final void onReached(CameraPerspectiveWorldCentered camera) {
camera.setR(this.desiredDistance);
camera.setPhi(this.desiredPhi);
camera.setTheta(this.desiredTheta);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.grillecube.client.renderer.camera;

import com.grillecube.common.maths.Quaternion;
import com.grillecube.common.maths.Vector3f;

public class CameraDestinationRaw extends CameraDestination<CameraView> {

public final Vector3f desiredPosition;
public final Quaternion desiredRotation;

private Vector3f camPos;
private Quaternion camRot;

/**
*
* @param desiredPosition
* : future position of the camera
* @param desiredRotation
* this.accumulator = 0;
*
* : future rotation of the camera
* @param duration
* : duration of the transition
*/
public CameraDestinationRaw(Vector3f desiredPosition, Quaternion desiredRotation, float duration) {
super(duration);
this.desiredPosition = desiredPosition;
this.desiredRotation = desiredRotation;
}

@Override
protected final void onInit(CameraView camera) {
this.camRot = new Quaternion();
this.camPos = new Vector3f();
this.camPos.set(camera.getPosition());
this.camRot.set(Quaternion.toQuaternion(camera.getRot()));
}

@Override
protected final void onUpdate(CameraView camera, float ratio) {
camera.setPosition(Vector3f.interpolate(this.camPos, this.desiredPosition, ratio, null));
camera.setRot(Quaternion.toEulerAngle(Quaternion.interpolate(this.camRot, this.desiredRotation, ratio)));
}

@Override
protected final void onReached(CameraView camera) {
camera.setPosition(this.desiredPosition);
camera.setRot(Quaternion.toEulerAngle(this.desiredRotation));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,9 @@ public float distance(Vector3f point) {
public CameraPerspectiveWorld(GLFWWindow window) {
super(window);
super.setPosition(0, 0, 16);
super.setPositionVelocity(0, 0, 0);
super.setRotationVelocity(0, 0, 0);
this.setRotX(0);
this.setRotY(0);
this.setRotZ(0);
super.setSpeed(0.2f);
super.setRotSpeed(1);

this.planes = new CameraPlane[6];
for (int i = 0; i < this.planes.length; i++) {
Expand All @@ -81,9 +77,7 @@ public CameraPerspectiveWorld(GLFWWindow window) {
public Camera clone() {
CameraPerspectiveWorld camera = new CameraPerspectiveWorld(null);
camera.setAspect(this.getAspect());
camera.setRotX(this.getRotX());
camera.setRotY(this.getRotZ());
camera.setRotZ(this.getRotY());
camera.setRot(this.getRot());
camera.setFov(this.getFov());
camera.setNearDistance(this.getNearDistance());
camera.setFarDistance(this.getFarDistance());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ public CameraPerspectiveWorldFree(GLFWWindow window) {
@Override
public void update() {
super.update();
this.setSpeed(0.5f);
this.updateMove();
}

protected void updateMove() {
Vector3f vel = this.getPositionVelocity();
Vector3f vel = new Vector3f();
if (this.hasState(STATE_MOVE_FORWARD)) {
vel.setX(this.getViewVector().x);
vel.setY(this.getViewVector().y);
Expand All @@ -48,7 +47,7 @@ protected void updateMove() {
vel.setZ(0);
}

this.move(vel.scale(2.0F));
this.move(vel, 2.0f);
}

@Override
Expand Down
Loading

0 comments on commit 97bb198

Please sign in to comment.