beta release: textures, 3D models, Cameras
This commit is contained in:
@@ -12,12 +12,21 @@ import java.nio.IntBuffer;
|
||||
public class Mesh {
|
||||
private Vertex[] vertices;
|
||||
private int[] indices;
|
||||
private int vertexArrayObject, positionBufferObject, indicesBufferObject, colorBufferObject;
|
||||
private int vertexArrayObject, positionBufferObject, indicesBufferObject, colorBufferObject, textureBufferObject;
|
||||
private Material material;
|
||||
private boolean type;
|
||||
|
||||
// A group of vertices combined based on the indexes
|
||||
public Mesh(Vertex[] vertices, int[] indices) {
|
||||
public Mesh(Vertex[] vertices, int[] indices, Material material, String type) throws Exception {
|
||||
this.vertices = vertices;
|
||||
this.indices = indices;
|
||||
this.material = material;
|
||||
if (!type.equals("texture") && !type.equals("color")) {
|
||||
throw new Exception("Type must be either texture or color. ");
|
||||
}
|
||||
this.type = type.equals("texture");
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Destroy the mesh
|
||||
@@ -108,4 +117,12 @@ public class Mesh {
|
||||
|
||||
return bufferID;
|
||||
}
|
||||
|
||||
public boolean isType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setMaterial(Material material) {
|
||||
this.material = material;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.hl.engine.graphics;
|
||||
|
||||
import org.hl.engine.math.lalg.Vector2f;
|
||||
import org.hl.engine.math.lalg.Vector3f;
|
||||
import org.lwjgl.assimp.*;
|
||||
|
||||
public class ModelLoader {
|
||||
public static Mesh loadModel(String path, String texture) throws Exception {
|
||||
AIScene scene = Assimp.aiImportFile(path, Assimp.aiProcess_JoinIdenticalVertices | Assimp.aiProcess_Triangulate);
|
||||
|
||||
if (scene == null) {
|
||||
throw new Exception("Couldn't load model at " + path);
|
||||
}
|
||||
|
||||
AIMesh mesh = AIMesh.create(scene.mMeshes().get(0));
|
||||
int vertexCount = mesh.mNumVertices();
|
||||
|
||||
AIVector3D.Buffer vertices = mesh.mVertices();
|
||||
AIVector3D.Buffer normals = mesh.mNormals();
|
||||
|
||||
Vertex[] vertexList = new Vertex[vertexCount];
|
||||
|
||||
for (int i = 0; i < vertexCount; i ++) {
|
||||
AIVector3D vertex = vertices.get(i);
|
||||
Vector3f engineVertex = new Vector3f(vertex.x(), vertex.y(), vertex.z());
|
||||
|
||||
AIVector3D normal = normals.get(i);
|
||||
Vector3f engineNormal = new Vector3f(normal.x(), normal.y(), normal.z());
|
||||
|
||||
Vector2f meshTextureCoord = new Vector2f(0, 0);
|
||||
|
||||
if (mesh.mNumUVComponents().get(0) != 0) {
|
||||
AIVector3D tex = mesh.mTextureCoords(0).get(i);
|
||||
meshTextureCoord.setX(tex.x());
|
||||
meshTextureCoord.setY(tex.y());
|
||||
}
|
||||
|
||||
vertexList[i] = new Vertex(engineVertex, meshTextureCoord, engineNormal);
|
||||
|
||||
}
|
||||
|
||||
int faceCount = mesh.mNumFaces();
|
||||
AIFace.Buffer indices = mesh.mFaces();
|
||||
int[] indicesList = new int[faceCount*3];
|
||||
|
||||
for (int i = 0; i < faceCount; i ++) {
|
||||
AIFace face = indices.get(i);
|
||||
indicesList[i * 3 + 0] = face.mIndices().get(0);
|
||||
indicesList[i * 3 + 1] = face.mIndices().get(1);
|
||||
indicesList[i * 3 + 2] = face.mIndices().get(2);
|
||||
|
||||
}
|
||||
|
||||
return new Mesh(vertexList, indicesList, new Material(new Texture(texture)), "texture");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package org.hl.engine.graphics;
|
||||
|
||||
import org.hl.engine.io.Display;
|
||||
import org.hl.engine.math.lalg.Matrix4f;
|
||||
import org.hl.engine.objects.Camera;
|
||||
import org.hl.engine.objects.GameObject;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL15;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
@@ -12,8 +16,7 @@ public class Renderer {
|
||||
this.shader = shader;
|
||||
}
|
||||
|
||||
|
||||
public void renderMesh(Mesh mesh) {
|
||||
public void renderMesh(GameObject object, Camera camera) {
|
||||
|
||||
// Renders the mesh by drawing it using triangles (least complicated)
|
||||
GL30.glBindVertexArray(mesh.getVertexArrayObject());
|
||||
@@ -23,7 +26,12 @@ public class Renderer {
|
||||
|
||||
shader.bind();
|
||||
|
||||
GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_UNSIGNED_INT, 0);
|
||||
shader.setUniform("type", object.getMesh().isType());
|
||||
shader.setUniform("projection", display.getProjectionMatrix());
|
||||
shader.setUniform("view", Matrix4f.view(camera.getPosition(), camera.getRotation()));
|
||||
shader.setUniform("model", Matrix4f.transform(object.getPosition(), object.getRotation(), object.getScale()));
|
||||
|
||||
GL11.glDrawElements(GL11.GL_TRIANGLES, object.getMesh().getIndices().length, GL11.GL_UNSIGNED_INT, 0);
|
||||
|
||||
shader.unbind();
|
||||
GL30.glDisableVertexAttribArray(0);
|
||||
|
||||
@@ -6,10 +6,31 @@ public class Vertex {
|
||||
|
||||
// Just a vertex
|
||||
|
||||
private Vector3f position;
|
||||
private Vector3f position, normal;
|
||||
private Vector3f color;
|
||||
private Vector2f textureCoords;
|
||||
private boolean type;
|
||||
|
||||
public Vertex (Vector3f position, Vector3f color) {
|
||||
public Vertex(Vector3f position, Vector2f textureCoords) {
|
||||
this.position = position;
|
||||
this.textureCoords = textureCoords;
|
||||
this.color = new Vector3f(1, 1, 1);
|
||||
}
|
||||
|
||||
public Vertex(Vector3f position, Vector2f textureCoords, Vector3f normal) {
|
||||
this.position = position;
|
||||
this.textureCoords = textureCoords;
|
||||
this.normal = normal;
|
||||
this.color = new Vector3f(1, 1, 1);
|
||||
}
|
||||
|
||||
public Vertex(Vector3f position, Vector3f color) {
|
||||
this.position = position;
|
||||
this.color = color;
|
||||
this.textureCoords = new Vector2f(0, 0);
|
||||
}
|
||||
|
||||
public Vertex(Vector3f position, Vector3f color, Vector2f textureCoords) {
|
||||
this.position = position;
|
||||
this.color = color;
|
||||
}
|
||||
@@ -21,4 +42,16 @@ public class Vertex {
|
||||
public Vector3f getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public Vector2f getTextureCoords() {
|
||||
return textureCoords;
|
||||
}
|
||||
|
||||
public boolean isType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public Vector3f getNormal() {
|
||||
return normal;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,12 @@ public class Display {
|
||||
private int savedPosY;
|
||||
private int savedWidth;
|
||||
private int savedHeight;
|
||||
|
||||
private Matrix4f projection;
|
||||
private boolean isLocked;
|
||||
private double[] cursorX = new double[1];
|
||||
private double[] cursorY = new double[1];
|
||||
private double[] enabledCursorX = new double[1];
|
||||
private double[] enabledCursorY = new double[1];
|
||||
|
||||
|
||||
|
||||
@@ -86,6 +91,26 @@ public class Display {
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseState(boolean lock) {
|
||||
if (lock != isLocked) {
|
||||
if (lock) {
|
||||
glfwGetCursorPos(window, enabledCursorX, enabledCursorY);
|
||||
glfwSetCursorPos(window, cursorX[0], cursorY[0]);
|
||||
isLocked = lock;
|
||||
glfwSetInputMode(window, GLFW_CURSOR, lock ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
|
||||
} else {
|
||||
glfwGetCursorPos(window, cursorX, cursorY);
|
||||
isLocked = lock;
|
||||
glfwSetInputMode(window, GLFW_CURSOR, lock ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
|
||||
glfwSetCursorPos(window, enabledCursorX[0], enabledCursorY[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isLocked() {
|
||||
return isLocked;
|
||||
}
|
||||
|
||||
// resized getter
|
||||
|
||||
public boolean isResized() {
|
||||
@@ -101,9 +126,13 @@ public class Display {
|
||||
System.err.println("Failed to initialize GLFW! ");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
if(System.getProperty("os.name").contains("Mac")) {
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
|
||||
} else {
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
|
||||
}
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL11.GL_TRUE);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
@@ -122,6 +151,8 @@ public class Display {
|
||||
windowYPos[0] = (videoMode.height() - this.height ) / 2;
|
||||
glfwSetWindowPos(window, windowXPos[0], windowYPos[0]);
|
||||
|
||||
glfwSetCursorPos(window, 0, 0);
|
||||
|
||||
|
||||
// Graphics
|
||||
glfwMakeContextCurrent(window);
|
||||
@@ -209,4 +240,8 @@ public class Display {
|
||||
public void setBackgroundColor(float r, float g, float b) {
|
||||
background.setVector(r, g, b);
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
swapBuffers();
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package org.hl.engine.math;
|
||||
|
||||
public class Vector3f {
|
||||
private float x;
|
||||
private float y;
|
||||
private float z;
|
||||
|
||||
// Just a vector if you know what I mean
|
||||
public Vector3f (float x, float y, float z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public void setVector(float x, float y, float z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public float getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(float x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public float getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(float y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public float getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public void setZ(float z) {
|
||||
this.z = z;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
package org.hl.engine.math.lalg;
|
||||
|
||||
|
||||
|
||||
public class Matrix4f {
|
||||
public static final int SIZE = 4;
|
||||
private float[][] elements = new float[SIZE][SIZE];
|
||||
|
||||
|
||||
public Matrix4f() {
|
||||
|
||||
}
|
||||
|
||||
public Matrix4f(float[][] values) {
|
||||
this.elements = values;
|
||||
}
|
||||
|
||||
|
||||
public static Matrix4f identity() {
|
||||
float[][] identityArray = {
|
||||
{1 , 0 , 0 , 0},
|
||||
{0 , 1 , 0 , 0},
|
||||
{0 , 0 , 1 , 0},
|
||||
{0 , 0 , 0 , 1}
|
||||
};
|
||||
return new Matrix4f(identityArray);
|
||||
}
|
||||
|
||||
public static Matrix4f zeroes() {
|
||||
float[][] zeroMatrix = {
|
||||
{0 , 0 , 0 , 0},
|
||||
{0 , 0 , 0 , 0},
|
||||
{0 , 0 , 0 , 0},
|
||||
{0 , 0 , 0 , 0}
|
||||
};
|
||||
return new Matrix4f(zeroMatrix);
|
||||
|
||||
}
|
||||
|
||||
public static Matrix4f translate(Vector3f translate) {
|
||||
Matrix4f result = Matrix4f.identity();
|
||||
|
||||
result.set(3, 0, translate.getX());
|
||||
result.set(3, 1, translate.getY());
|
||||
result.set(3, 2, translate.getZ());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public static Matrix4f flip(Matrix4f matrix) {
|
||||
Matrix4f flippedMatrix = matrix;
|
||||
for (int i = 0; i < SIZE; i ++) {
|
||||
for (int j = 0; j < SIZE; j ++ ) {
|
||||
flippedMatrix.set(i, j, matrix.get(j, i));
|
||||
}
|
||||
}
|
||||
return flippedMatrix;
|
||||
}
|
||||
|
||||
// This one doesn't work
|
||||
|
||||
/* public static Matrix4f rotate(float angle, Vector3f axis) {
|
||||
|
||||
float cos = (float)Math.cos(Math.toDegrees(angle));
|
||||
float sin = (float)Math.sin(Math.toDegrees(angle));
|
||||
float C = 1 - cos;
|
||||
float x = axis.getX();
|
||||
float y = axis.getY();
|
||||
float z = axis.getZ();
|
||||
float[][] rotArray = {
|
||||
{cos + x*x*C , x*y*C - z*sin , x*z*C + y*sin , 0},
|
||||
{y*z*C + z*sin , cos + y*y*C , y*z*C - x*sin , 0},
|
||||
{z*x*C-y*sin , z*y*C + x*sin , cos + z*z*C , 0},
|
||||
{0 , 0 , 0 , 1}
|
||||
};
|
||||
|
||||
return new Matrix4f(rotArray);
|
||||
} */
|
||||
public static Matrix4f rotate(float angle, Vector3f axis) {
|
||||
Matrix4f result = Matrix4f.identity();
|
||||
|
||||
float cos = (float) Math.cos(Math.toRadians(angle));
|
||||
float sin = (float) Math.sin(Math.toRadians(angle));
|
||||
float C = 1 - cos;
|
||||
|
||||
result.set(0, 0, cos + axis.getX() * axis.getX() * C);
|
||||
result.set(0, 1, axis.getX() * axis.getY() * C - axis.getZ() * sin);
|
||||
result.set(0, 2, axis.getX() * axis.getZ() * C + axis.getY() * sin);
|
||||
result.set(1, 0, axis.getY() * axis.getX() * C + axis.getZ() * sin);
|
||||
result.set(1, 1, cos + axis.getY() * axis.getY() * C);
|
||||
result.set(1, 2, axis.getY() * axis.getZ() * C - axis.getX() * sin);
|
||||
result.set(2, 0, axis.getZ() * axis.getX() * C - axis.getY() * sin);
|
||||
result.set(2, 1, axis.getZ() * axis.getY() * C + axis.getX() * sin);
|
||||
result.set(2, 2, cos + axis.getZ() * axis.getZ() * C);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Matrix4f scale(Vector3f scaleVec) {
|
||||
|
||||
Matrix4f result = Matrix4f.identity();
|
||||
|
||||
result.set(0, 0, scaleVec.getX());
|
||||
result.set(1, 1, scaleVec.getY());
|
||||
result.set(2, 2, scaleVec.getZ());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Matrix4f projection( float fov, float aspectRatio, float near, float far) {
|
||||
Matrix4f result = Matrix4f.identity();
|
||||
|
||||
float tan = (float)Math.tan(Math.toRadians(fov / 2));
|
||||
float range = far - near;
|
||||
|
||||
result.set(0, 0, 1.0f / (aspectRatio * tan));
|
||||
result.set(1, 1, 1.0f / tan);
|
||||
result.set(2, 2, -((far + near) / range));
|
||||
result.set(3, 2, -1.0f);
|
||||
result.set(2, 3, -(2.0f*far*near/range));
|
||||
result.set(3, 3, 0f);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Matrix4f view(Vector3f position, Vector3f rotation) {
|
||||
|
||||
Vector3f negative = new Vector3f(-position.getX(), -position.getY(), -position.getZ());
|
||||
Matrix4f translationMatrix = Matrix4f.translate(negative);
|
||||
Matrix4f rotationXMatrix = Matrix4f.rotate(-rotation.getX(), new Vector3f(1, 0, 0));
|
||||
Matrix4f rotationYMatrix = Matrix4f.rotate(-rotation.getY(), new Vector3f(0, 1, 0));
|
||||
Matrix4f rotationZMatrix = Matrix4f.rotate(-rotation.getZ(), new Vector3f(0, 0, 1));
|
||||
|
||||
Matrix4f rotMat = Matrix4f.multiply(rotationZMatrix, Matrix4f.multiply(rotationXMatrix, rotationYMatrix));
|
||||
|
||||
return Matrix4f.multiply(rotMat, translationMatrix);
|
||||
|
||||
}
|
||||
|
||||
public static Matrix4f multiply(Matrix4f first, Matrix4f second) {
|
||||
|
||||
Matrix4f result = Matrix4f.zeroes();
|
||||
|
||||
for (int i = 0; i < SIZE; i ++ ) {
|
||||
for (int j = 0; j < SIZE; j ++) {
|
||||
result.set(i, j,
|
||||
first.get(0, i) * second.get(j, 0) +
|
||||
first.get(1, i) * second.get(j, 1) +
|
||||
first.get(2, i) * second.get(j, 2) +
|
||||
first.get(3, i) * second.get(j, 3)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
public float get(int x, int y) {
|
||||
return elements[x][y];
|
||||
}
|
||||
|
||||
public void set(int x, int y, float value) {
|
||||
elements[x][y] = value;
|
||||
}
|
||||
|
||||
public float[][] getAll() {
|
||||
return elements;
|
||||
}
|
||||
|
||||
public static Matrix4f transform(Vector3f position, Vector3f rotation, Vector3f scale) {
|
||||
Matrix4f translationMatrix = Matrix4f.translate(position);
|
||||
Matrix4f rotationXMatrix = Matrix4f.rotate(rotation.getX(), new Vector3f(1, 0, 0));
|
||||
Matrix4f rotationYMatrix = Matrix4f.rotate(rotation.getY(), new Vector3f(0, 1, 0));
|
||||
Matrix4f rotationZMatrix = Matrix4f.rotate(rotation.getZ(), new Vector3f(0, 0, 1));
|
||||
Matrix4f scaleMatrix = Matrix4f.scale(scale);
|
||||
|
||||
Matrix4f rotMat = Matrix4f.multiply(rotationXMatrix, Matrix4f.multiply(rotationYMatrix, rotationZMatrix));
|
||||
|
||||
return Matrix4f.multiply(translationMatrix, Matrix4f.multiply(rotMat, scaleMatrix));
|
||||
|
||||
}
|
||||
|
||||
|
||||
public float[] convertTo1D() {
|
||||
float[] returnedArray = new float[SIZE*SIZE];
|
||||
int sizeOfRow = elements[0].length;
|
||||
for (int i = 0; i < elements.length; i ++) {
|
||||
for (int j = 0; j < sizeOfRow; j ++) {
|
||||
returnedArray[i*SIZE + j] = elements[i][j];
|
||||
}
|
||||
}
|
||||
return returnedArray;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package org.hl.engine.math.lalg;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Vector2f {
|
||||
private float x;
|
||||
private float y;
|
||||
|
||||
// Just a vector if you know what I mean
|
||||
public Vector2f (float x, float y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public void setVector(float x, float y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
|
||||
public static Vector2f add(Vector2f first, Vector2f second) {
|
||||
return new Vector2f(first.getX() + second.getX(), first.getY() + second.getY());
|
||||
}
|
||||
|
||||
public static Vector2f sub(Vector2f first, Vector2f second) {
|
||||
return new Vector2f(first.getX() - second.getX(), first.getY() - second.getY());
|
||||
}
|
||||
|
||||
public static Vector2f mul(Vector2f first, Vector2f second) {
|
||||
return new Vector2f(first.getX() * second.getX(), first.getY() * second.getY());
|
||||
}
|
||||
|
||||
public static Vector2f div(Vector2f first, Vector2f second) {
|
||||
return new Vector2f(first.getX() / second.getX(), first.getY() / second.getY());
|
||||
}
|
||||
|
||||
public static float dot(Vector2f first, Vector2f second) {
|
||||
return first.getX()*second.getX() + first.getY()*second.getY();
|
||||
}
|
||||
|
||||
public static float magnitude(Vector2f vector) {
|
||||
return (float)Math.sqrt(vector.getX()*vector.getX() + vector.getY()*vector.getY());
|
||||
}
|
||||
|
||||
public static Vector2f normalize(Vector2f vector) {
|
||||
float len = Vector2f.magnitude(vector);
|
||||
return Vector2f.div(vector, new Vector2f(len, len));
|
||||
}
|
||||
|
||||
public static Vector2f scale(Vector2f vector, float scalar) {
|
||||
return new Vector2f(vector.getX()*scalar, vector.getY()*scalar);
|
||||
}
|
||||
|
||||
public static Vector2f projection(Vector2f projecting, Vector2f projectedOnto) {
|
||||
return Vector2f.scale(projectedOnto, ( Vector2f.dot(projecting, projectedOnto) / Vector2f.dot(projectedOnto, projectedOnto) ));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Vector2f vector2f = (Vector2f) o;
|
||||
return Float.compare(vector2f.getX(), getX()) == 0 &&
|
||||
Float.compare(vector2f.getY(), getY()) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getX(), getY());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Vector2f{" +
|
||||
"x=" + x +
|
||||
", y=" + y +
|
||||
'}';
|
||||
}
|
||||
|
||||
public float getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(float x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public float getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(float y) {
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
Executable
+107
@@ -0,0 +1,107 @@
|
||||
package org.hl.engine.math;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Vector3f {
|
||||
private float x;
|
||||
private float y;
|
||||
private float z;
|
||||
|
||||
// Just a vector if you know what I mean
|
||||
public Vector3f (float x, float y, float z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public void setVector(float x, float y, float z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public static Vector3f add(Vector3f first, Vector3f second) {
|
||||
return new Vector3f(first.getX() + second.getX(), first.getY() + second.getY(), first.getZ() + second.getZ());
|
||||
}
|
||||
|
||||
public static Vector3f sub(Vector3f first, Vector3f second) {
|
||||
return new Vector3f(first.getX() - second.getX(), first.getY() - second.getY(), first.getZ() - second.getZ());
|
||||
}
|
||||
|
||||
public static Vector3f mul(Vector3f first, Vector3f second) {
|
||||
return new Vector3f(first.getX() * second.getX(), first.getY() * second.getY(), first.getZ() * second.getZ());
|
||||
}
|
||||
|
||||
public static Vector3f div(Vector3f first, Vector3f second) {
|
||||
return new Vector3f(first.getX() / second.getX(), first.getY() / second.getY(), first.getZ() / second.getZ());
|
||||
}
|
||||
|
||||
public static float dot(Vector3f first, Vector3f second) {
|
||||
return first.getX()*second.getX() + first.getY()*second.getY() + first.getZ()*second.getZ();
|
||||
}
|
||||
|
||||
public static float magnitude(Vector3f vector) {
|
||||
return (float)Math.sqrt(vector.getX()*vector.getX() + vector.getY()*vector.getY() + vector.getZ()*vector.getZ());
|
||||
}
|
||||
|
||||
public static Vector3f normalize(Vector3f vector) {
|
||||
float len = Vector3f.magnitude(vector);
|
||||
return Vector3f.div(vector, new Vector3f(len, len, len));
|
||||
}
|
||||
|
||||
public static Vector3f scale(Vector3f vector, float scalar) {
|
||||
return new Vector3f(vector.getX()*scalar, vector.getY()*scalar, vector.getZ()*scalar);
|
||||
}
|
||||
|
||||
public static Vector3f projection(Vector3f projecting, Vector3f projectedOnto) {
|
||||
return Vector3f.scale(projectedOnto, ( Vector3f.dot(projecting, projectedOnto) / Vector3f.dot(projectedOnto, projectedOnto) ));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Vector3f vector3f = (Vector3f) o;
|
||||
return Float.compare(vector3f.getX(), getX()) == 0 &&
|
||||
Float.compare(vector3f.getY(), getY()) == 0 &&
|
||||
Float.compare(vector3f.getZ(), getZ()) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getX(), getY(), getZ());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Vector3f{" +
|
||||
"x=" + x +
|
||||
", y=" + y +
|
||||
", z=" + z +
|
||||
'}';
|
||||
}
|
||||
|
||||
public float getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(float x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public float getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(float y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public float getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public void setZ(float z) {
|
||||
this.z = z;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package org.hl.engine.math.lalg;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Vector4f {
|
||||
private float x;
|
||||
private float y;
|
||||
private float z;
|
||||
private float a;
|
||||
|
||||
// Just a vector if you know what I mean
|
||||
public Vector4f (float x, float y, float z, float a) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public void setVector(float x, float y, float z, float a) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public void add(float x, float y, float z, float a) {
|
||||
this.x += x;
|
||||
this.y += y;
|
||||
this.z += z;
|
||||
this.a += a;
|
||||
}
|
||||
public static Vector4f add(Vector4f first, Vector4f second) {
|
||||
return new Vector4f(first.getX() + second.getX(), first.getY() + second.getY(), first.getZ() + second.getZ(), first.getA() + second.getA());
|
||||
}
|
||||
|
||||
public static Vector4f sub(Vector4f first, Vector4f second) {
|
||||
return new Vector4f(first.getX() - second.getX(), first.getY() - second.getY(), first.getZ() - second.getZ(), first.getA() - second.getA());
|
||||
}
|
||||
|
||||
public static Vector4f mul(Vector4f first, Vector4f second) {
|
||||
return new Vector4f(first.getX() * second.getX(), first.getY() * second.getY(), first.getZ() * second.getZ(), first.getA() * second.getA());
|
||||
}
|
||||
|
||||
public static Vector4f div(Vector4f first, Vector4f second) {
|
||||
return new Vector4f(first.getX() / second.getX(), first.getY() / second.getY(), first.getZ() / second.getZ(), first.getA() / second.getA());
|
||||
}
|
||||
|
||||
public static float dot(Vector4f first, Vector4f second) {
|
||||
return first.getX()*second.getX() + first.getY()*second.getY() + first.getZ()*second.getZ() + first.getA()*second.getA();
|
||||
}
|
||||
|
||||
public static float magnitude(Vector4f vector) {
|
||||
return (float)Math.sqrt(vector.getX()*vector.getX() + vector.getY()*vector.getY() + vector.getZ()*vector.getZ() + vector.getA()*vector.getA());
|
||||
}
|
||||
|
||||
public static Vector4f normalize(Vector4f vector) {
|
||||
float len = Vector4f.magnitude(vector);
|
||||
return Vector4f.div(vector, new Vector4f(len, len, len, len));
|
||||
}
|
||||
|
||||
public static Vector4f scale(Vector4f vector, float scalar) {
|
||||
return new Vector4f(vector.getX()*scalar, vector.getY()*scalar, vector.getZ()*scalar, vector.getA()*scalar);
|
||||
}
|
||||
|
||||
public static Vector4f projection(Vector4f projecting, Vector4f projectedOnto) {
|
||||
return Vector4f.scale(projectedOnto, ( Vector4f.dot(projecting, projectedOnto) / Vector4f.dot(projectedOnto, projectedOnto) ));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Vector4f vector4f = (Vector4f) o;
|
||||
return Float.compare(vector4f.getX(), getX()) == 0 &&
|
||||
Float.compare(vector4f.getY(), getY()) == 0 &&
|
||||
Float.compare(vector4f.getZ(), getZ()) == 0 &&
|
||||
Float.compare(vector4f.getA(), getA()) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getX(), getY(), getZ(), getA());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Vector4f{" +
|
||||
"x=" + x +
|
||||
", y=" + y +
|
||||
", z=" + z +
|
||||
", a=" + a +
|
||||
'}';
|
||||
}
|
||||
|
||||
public float getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(float x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public float getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(float y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public float getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public void setZ(float z) {
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public float getA() {
|
||||
return a;
|
||||
}
|
||||
|
||||
public void setA(float a) {
|
||||
this.a = a;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.hl.engine.objects;
|
||||
import org.hl.engine.math.lalg.Vector3f;
|
||||
|
||||
public class Camera {
|
||||
private Vector3f position;
|
||||
private Vector3f rotation;
|
||||
|
||||
public Camera(Vector3f position, Vector3f rotation) {
|
||||
this.position = position;
|
||||
this.rotation = rotation;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Vector3f getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public void setPosition(Vector3f position) throws Exception {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public Vector3f getRotation() {
|
||||
return rotation;
|
||||
}
|
||||
|
||||
public void setRotation(Vector3f rotation) throws Exception {
|
||||
this.rotation = rotation;
|
||||
}
|
||||
|
||||
public void movePosition(float offsetX, float offsetY, float offsetZ) {
|
||||
if(offsetZ != 0) {
|
||||
position.setZ(position.getZ() + (float) Math.cos(Math.toRadians(rotation.getY())) * offsetZ);
|
||||
position.setX(position.getX() + (float) Math.sin(Math.toRadians(rotation.getY())) * -offsetZ);
|
||||
}
|
||||
if(offsetX != 0) {
|
||||
position.setX(position.getX() + (float) Math.cos(Math.toRadians(rotation.getY())) * offsetX) ;
|
||||
position.setZ(position.getZ() + (float) Math.sin(Math.toRadians(rotation.getY())) * offsetX);
|
||||
}
|
||||
|
||||
position.setY(position.getY() + offsetY);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package org.hl.engine.objects;
|
||||
|
||||
import org.hl.engine.io.Display;
|
||||
import org.hl.engine.io.Input;
|
||||
import org.hl.engine.math.lalg.Vector3f;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
public class FirstPersonCamera extends Camera {
|
||||
private Input i;
|
||||
private float moveSpeed;
|
||||
private float sensitivity;
|
||||
|
||||
private double oldMouseX, oldMouseY = 0;
|
||||
private double newMouseX, newMouseY;
|
||||
|
||||
public FirstPersonCamera(Vector3f position, Vector3f rotation, float moveSpeed, float sensitivity) {
|
||||
super(position, rotation);
|
||||
this.moveSpeed = moveSpeed;
|
||||
this.sensitivity = sensitivity;
|
||||
}
|
||||
|
||||
public void create(Input i) {
|
||||
this.i = i;
|
||||
}
|
||||
|
||||
public void standardKeybindUpdate () throws Exception {
|
||||
|
||||
newMouseX = i.getMouseX();
|
||||
newMouseY = i.getMouseY();
|
||||
|
||||
|
||||
Vector3f cameraPos = getPosition();
|
||||
Vector3f cameraRot = getRotation();
|
||||
|
||||
|
||||
float dx = (float) ((float)newMouseX - oldMouseX);
|
||||
float dy = (float) ((float)newMouseY - oldMouseY);
|
||||
oldMouseX = newMouseX;
|
||||
oldMouseY = newMouseY;
|
||||
|
||||
cameraRot = Vector3f.add(cameraRot, new Vector3f(dy*sensitivity, dx*sensitivity, 0));
|
||||
|
||||
if (i.isKeyDown(GLFW.GLFW_KEY_A)) movePosition(-moveSpeed, 0, 0);
|
||||
if (i.isKeyDown(GLFW.GLFW_KEY_D)) movePosition(moveSpeed, 0, 0);
|
||||
if (i.isKeyDown(GLFW.GLFW_KEY_W)) movePosition(0, 0, -moveSpeed);
|
||||
if (i.isKeyDown(GLFW.GLFW_KEY_S)) movePosition(0, 0, moveSpeed);
|
||||
if (i.isKeyDown(GLFW.GLFW_KEY_SPACE)) movePosition(0, moveSpeed, 0);
|
||||
if (i.isKeyDown(GLFW.GLFW_KEY_LEFT_SHIFT)) movePosition(0, -moveSpeed, 0);
|
||||
if (i.keyPress(GLFW.GLFW_KEY_R)) moveSpeed = 3*moveSpeed;
|
||||
if (i.keyReleased(GLFW.GLFW_KEY_R)) moveSpeed = moveSpeed / 3;
|
||||
|
||||
setRotation(cameraRot);
|
||||
setPosition(cameraPos);
|
||||
|
||||
i.reset();
|
||||
}
|
||||
|
||||
public void moveForward() {
|
||||
movePosition(0, 0, -moveSpeed);
|
||||
}
|
||||
public void moveBackward() {
|
||||
movePosition(0, 0, moveSpeed);
|
||||
}
|
||||
public void moveLeft() {
|
||||
movePosition(-moveSpeed, 0, 0);
|
||||
}
|
||||
public void moveRight() {
|
||||
movePosition(moveSpeed, 0, 0);
|
||||
}
|
||||
public void moveUp() {
|
||||
movePosition(0, moveSpeed, 0);
|
||||
}
|
||||
public void moveDown() {
|
||||
movePosition(0, -moveSpeed, 0);
|
||||
}
|
||||
|
||||
public float getMoveSpeed() {
|
||||
return moveSpeed;
|
||||
}
|
||||
|
||||
public void setMoveSpeed(float moveSpeed) {
|
||||
this.moveSpeed = moveSpeed;
|
||||
}
|
||||
|
||||
public void rotateCamera(float dx, float dy) throws Exception {
|
||||
|
||||
Vector3f cameraRot = getRotation();
|
||||
cameraRot = Vector3f.add(cameraRot, new Vector3f(dy*sensitivity, dx*sensitivity, 0));
|
||||
|
||||
|
||||
setRotation(cameraRot);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.hl.engine.objects;
|
||||
|
||||
import org.hl.engine.math.lalg.Vector3f;
|
||||
|
||||
public class FixedCamera extends Camera {
|
||||
public FixedCamera(Vector3f position, Vector3f rotation) {
|
||||
super(position, rotation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRotation(Vector3f rotation) throws Exception {
|
||||
throw new Exception("You cannot rotate a fixed camera! ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(Vector3f rotation) throws Exception {
|
||||
throw new Exception("You cannot move a fixed camera! ");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package org.hl.engine.objects;
|
||||
|
||||
import org.hl.engine.graphics.*;
|
||||
import org.hl.engine.math.lalg.Vector2f;
|
||||
import org.hl.engine.math.lalg.Vector3f;
|
||||
import org.hl.engine.objects.yloaders.YMesh;
|
||||
import org.hl.engine.objects.yloaders.YPoint;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.constructor.Constructor;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
public class GameObject {
|
||||
private Vector3f position, rotation, scale;
|
||||
private Mesh mesh;
|
||||
|
||||
public GameObject(Mesh mesh, Vector3f position, Vector3f rotation, Vector3f scale) {
|
||||
this.position = position;
|
||||
this.rotation = rotation;
|
||||
this.scale = scale;
|
||||
this.mesh = mesh;
|
||||
}
|
||||
|
||||
public GameObject(String meshFileName, Vector3f position, Vector3f rotation, Vector3f scale) throws Exception {
|
||||
if (!meshFileName.endsWith(".mesh")) { throw new Exception("Wrong file type! "); }
|
||||
Yaml yaml = new Yaml();
|
||||
FileInputStream inputStream = new FileInputStream(meshFileName);
|
||||
YMesh yMesh = yaml.loadAs(inputStream, YMesh.class);
|
||||
Integer[] cull = yMesh.getCull().toArray(new Integer[0]);
|
||||
String type = yMesh.getType();
|
||||
String texture = yMesh.getTexture();
|
||||
YPoint[] vertices = yMesh.getVertices().toArray(new YPoint[0]);
|
||||
Vertex[] meshFormat = new Vertex[vertices.length];
|
||||
|
||||
for (YPoint vertex : vertices) {
|
||||
if (vertex.getVertex().size() != 3) {
|
||||
throw new Exception("Incorrect number of coordinates. ");
|
||||
}
|
||||
if (vertex.getColor().size() != 3) {
|
||||
throw new Exception("Incorrect number of color values. ");
|
||||
}
|
||||
if (vertex.getTexture().size() != 2) {
|
||||
throw new Exception("Incorrect number of texture coordinates. ");
|
||||
}
|
||||
}
|
||||
if (!type.equals("texture") && !type.equals("color")) {
|
||||
throw new Exception("Incorrect type. Type can only be texture or color. ");
|
||||
}
|
||||
for (int i = 0; i < vertices.length; i ++) {
|
||||
Vertex value = new Vertex(
|
||||
new Vector3f(vertices[i].getVertex().get(0), vertices[i].getVertex().get(1), vertices[i].getVertex().get(2)),
|
||||
new Vector3f(vertices[i].getColor().get(0), vertices[i].getColor().get(1), vertices[i].getColor().get(2)),
|
||||
new Vector2f(vertices[i].getTexture().get(0), vertices[i].getTexture().get(1)));
|
||||
meshFormat[i] = value;
|
||||
}
|
||||
int[] indices = new int[cull.length];
|
||||
for (int j = 0; j < cull.length; j ++) {
|
||||
indices[j] = cull[j];
|
||||
}
|
||||
this.mesh = new Mesh(meshFormat, indices, new Material(new Texture(texture)), type);
|
||||
this.position = position;
|
||||
this.scale = scale;
|
||||
this.rotation = rotation;
|
||||
|
||||
}
|
||||
public GameObject(String meshFileName, String texturePath, Vector3f position, Vector3f rotation, Vector3f scale) throws Exception {
|
||||
this.mesh = ModelLoader.loadModel(meshFileName, texturePath);
|
||||
this.position = position;
|
||||
this.scale = scale;
|
||||
this.rotation = rotation;
|
||||
}
|
||||
|
||||
|
||||
public void create() {
|
||||
mesh.create();
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
mesh.destroy();
|
||||
}
|
||||
|
||||
public Vector3f getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public Vector3f getRotation() {
|
||||
return rotation;
|
||||
}
|
||||
|
||||
public Vector3f getScale() {
|
||||
return scale;
|
||||
}
|
||||
|
||||
public Mesh getMesh() {
|
||||
return mesh;
|
||||
}
|
||||
|
||||
public void setPosition(Vector3f position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public void setRotation(Vector3f rotation) {
|
||||
this.rotation = rotation;
|
||||
}
|
||||
|
||||
public void setScale(Vector3f scale) {
|
||||
this.scale = scale;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
GameObject that = (GameObject) o;
|
||||
return Objects.equals(getPosition(), that.getPosition()) &&
|
||||
Objects.equals(getRotation(), that.getRotation()) &&
|
||||
Objects.equals(getScale(), that.getScale()) &&
|
||||
Objects.equals(getMesh(), that.getMesh());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getPosition(), getRotation(), getScale(), getMesh());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
package org.hl.engine.objects;
|
||||
|
||||
import org.hl.engine.io.Input;
|
||||
import org.hl.engine.math.lalg.Vector3f;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
public class ThirdPersonCamera extends Camera {
|
||||
|
||||
private GameObject object;
|
||||
private Input i;
|
||||
private float distance;
|
||||
private float angle = 0;
|
||||
private float horizAngle = 0;
|
||||
private float vertAngle = 0;
|
||||
private float moveSpeed;
|
||||
private float sensitivity;
|
||||
private float near;
|
||||
private float far;
|
||||
private boolean clickToMove;
|
||||
private boolean clickToZoom;
|
||||
private boolean zoomEnabled;
|
||||
|
||||
private double oldMouseX, oldMouseY = 0;
|
||||
private double newMouseX, newMouseY;
|
||||
|
||||
public ThirdPersonCamera(Vector3f position, Vector3f rotation, GameObject object, float sensitivity, float distance, float near, float far, boolean clickToMove, boolean clickToZoom, boolean zoomEnabled) {
|
||||
super(position, rotation);
|
||||
this.object = object;
|
||||
this.sensitivity = sensitivity;
|
||||
this.clickToMove = clickToMove;
|
||||
this.clickToZoom = clickToZoom;
|
||||
this.zoomEnabled = zoomEnabled;
|
||||
this.near = near;
|
||||
this.far = far;
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
public void create(Input i) throws Exception {
|
||||
|
||||
this.i = i;
|
||||
setRotation(getRotation());
|
||||
setPosition(getPosition());
|
||||
}
|
||||
|
||||
|
||||
public void standardKeybindUpdate() throws Exception {
|
||||
|
||||
newMouseX = i.getMouseX();
|
||||
newMouseY = i.getMouseY();
|
||||
|
||||
|
||||
float dx = (float) ((float)newMouseX - oldMouseX);
|
||||
float dy = (float) ((float)newMouseY - oldMouseY);
|
||||
|
||||
if (clickToMove && clickToZoom) {
|
||||
if (i.isButtonDown(GLFW.GLFW_MOUSE_BUTTON_LEFT)) {
|
||||
vertAngle -= dy * sensitivity;
|
||||
horizAngle += dx * sensitivity;
|
||||
}
|
||||
|
||||
if (i.isButtonDown(GLFW.GLFW_MOUSE_BUTTON_RIGHT) && zoomEnabled) {
|
||||
if (distance > 0) {
|
||||
distance += dy * sensitivity;
|
||||
}
|
||||
else {
|
||||
distance = near;
|
||||
}
|
||||
if (distance > far) {
|
||||
distance = far;
|
||||
}
|
||||
}
|
||||
} else if (clickToMove) {
|
||||
|
||||
if (i.isButtonDown(GLFW.GLFW_MOUSE_BUTTON_LEFT)) {
|
||||
vertAngle -= dy * sensitivity;
|
||||
horizAngle += dx * sensitivity;
|
||||
}
|
||||
if (zoomEnabled) {
|
||||
if (distance > 0) {
|
||||
distance += dy * sensitivity;
|
||||
} else {
|
||||
distance = near;
|
||||
}
|
||||
if (distance > far) {
|
||||
distance = far;
|
||||
}
|
||||
}
|
||||
|
||||
} else if (clickToZoom) {
|
||||
|
||||
vertAngle -= dy * sensitivity;
|
||||
horizAngle += dx * sensitivity;
|
||||
|
||||
if (i.isButtonDown(GLFW.GLFW_MOUSE_BUTTON_RIGHT) && zoomEnabled) {
|
||||
if (distance > 0) {
|
||||
distance += dy * sensitivity;
|
||||
} else {
|
||||
distance = near;
|
||||
}
|
||||
if (distance > far) {
|
||||
distance = far;
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
vertAngle -= dy * sensitivity;
|
||||
horizAngle += dx * sensitivity;
|
||||
if (zoomEnabled) {
|
||||
if (distance > 0) {
|
||||
distance += dy * sensitivity;
|
||||
} else {
|
||||
distance = near;
|
||||
}
|
||||
if (distance > far) {
|
||||
distance = far;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
oldMouseX = newMouseX;
|
||||
oldMouseY = newMouseY;
|
||||
|
||||
float horizDistance = (float) (distance * Math.cos(Math.toRadians(vertAngle)));
|
||||
float vertDistance = (float) (distance * Math.sin(Math.toRadians(vertAngle)));
|
||||
|
||||
float xOffset = (float) (horizDistance * Math.sin(Math.toRadians(-horizAngle)));
|
||||
float zOffset = (float) (horizDistance * Math.cos(Math.toRadians(-horizAngle)));
|
||||
|
||||
setPosition(new Vector3f(object.getPosition().getX() + xOffset, object.getPosition().getY() - vertDistance, object.getPosition().getZ() + zOffset));
|
||||
setRotation(new Vector3f(-vertAngle, horizAngle,0));
|
||||
|
||||
i.reset();
|
||||
|
||||
}
|
||||
public void movePosition (float dy, float dx) throws Exception {
|
||||
vertAngle -= dy * sensitivity;
|
||||
horizAngle += dx * sensitivity;
|
||||
|
||||
float horizDistance = (float) (distance * Math.cos(Math.toRadians(vertAngle)));
|
||||
float vertDistance = (float) (distance * Math.sin(Math.toRadians(vertAngle)));
|
||||
|
||||
float xOffset = (float) (horizDistance * Math.sin(Math.toRadians(-horizAngle)));
|
||||
float zOffset = (float) (horizDistance * Math.cos(Math.toRadians(-horizAngle)));
|
||||
|
||||
setPosition(new Vector3f(object.getPosition().getX() + xOffset, object.getPosition().getY() - vertDistance, object.getPosition().getZ() + zOffset));
|
||||
setRotation(new Vector3f(-vertAngle, horizAngle,0));
|
||||
}
|
||||
public void zoom (float dy) throws Exception {
|
||||
if (zoomEnabled) {
|
||||
if (distance > 0) {
|
||||
distance += dy * sensitivity;
|
||||
} else {
|
||||
distance = near;
|
||||
}
|
||||
if (distance > far) {
|
||||
distance = far;
|
||||
}
|
||||
}
|
||||
|
||||
float horizDistance = (float) (distance * Math.cos(Math.toRadians(vertAngle)));
|
||||
float vertDistance = (float) (distance * Math.sin(Math.toRadians(vertAngle)));
|
||||
|
||||
float xOffset = (float) (horizDistance * Math.sin(Math.toRadians(-horizAngle)));
|
||||
float zOffset = (float) (horizDistance * Math.cos(Math.toRadians(-horizAngle)));
|
||||
|
||||
setPosition(new Vector3f(object.getPosition().getX() + xOffset, object.getPosition().getY() - vertDistance, object.getPosition().getZ() + zOffset));
|
||||
setRotation(new Vector3f(-vertAngle, horizAngle,0));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.hl.engine.objects;
|
||||
|
||||
import org.hl.engine.math.lalg.Vector3f;
|
||||
|
||||
public class TopDownCamera extends FixedCamera {
|
||||
public TopDownCamera(Vector3f position) {
|
||||
super(position, new Vector3f(90, 0, 0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.hl.engine.objects.yloaders ;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class YMesh implements java.io.Serializable {
|
||||
private String type;
|
||||
private ArrayList<YPoint> vertices;
|
||||
private ArrayList<Integer> cull;
|
||||
private String texture;
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public ArrayList<YPoint> getVertices() {
|
||||
return vertices;
|
||||
}
|
||||
|
||||
public void setVertices(ArrayList<YPoint> vertices) {
|
||||
this.vertices = vertices;
|
||||
}
|
||||
|
||||
public ArrayList<Integer> getCull() {
|
||||
return cull;
|
||||
}
|
||||
|
||||
public void setCull(ArrayList<Integer> cull) {
|
||||
this.cull = cull;
|
||||
}
|
||||
|
||||
public String getTexture() {
|
||||
return texture;
|
||||
}
|
||||
|
||||
public void setTexture(String texture) {
|
||||
this.texture = texture;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.hl.engine.objects.yloaders;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class YPoint implements java.io.Serializable {
|
||||
private ArrayList<Float> vertex;
|
||||
private ArrayList<Float> texture;
|
||||
private ArrayList<Float> color;
|
||||
|
||||
public ArrayList<Float> getVertex() {
|
||||
return vertex;
|
||||
}
|
||||
|
||||
public void setVertex(ArrayList<Float> vertex) {
|
||||
this.vertex = vertex;
|
||||
}
|
||||
|
||||
public ArrayList<Float> getTexture() {
|
||||
return texture;
|
||||
}
|
||||
|
||||
public void setTexture(ArrayList<Float> texture) {
|
||||
this.texture = texture;
|
||||
}
|
||||
|
||||
public ArrayList<Float> getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(ArrayList<Float> color) {
|
||||
this.color = color;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user