small camera

This commit is contained in:
2020-05-27 11:49:28 -04:00
parent 1285842b55
commit 5ae985ced9
9 changed files with 211 additions and 27 deletions
+3 -1
View File
@@ -2,6 +2,7 @@ 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.GL13;
@@ -18,7 +19,7 @@ public class Renderer {
this.display = display;
}
public void renderMesh(GameObject object) {
public void renderMesh(GameObject object, Camera camera) {
// Renders the mesh by drawing it using triangles (least complicated)
GL30.glBindVertexArray(object.getMesh().getVertexArrayObject());
@@ -35,6 +36,7 @@ public class Renderer {
shader.setUniform("model", Matrix4f.transform(object.getPosition(), object.getRotation(), object.getScale()));
shader.setUniform("projection", display.getProjectionMatrix());
shader.setUniform("view", Matrix4f.view(camera.getPosition(), camera.getRotation()));
GL11.glDrawElements(GL11.GL_TRIANGLES, object.getMesh().getIndices().length, GL11.GL_UNSIGNED_INT, 0);
+18 -5
View File
@@ -1,5 +1,7 @@
package org.hl.engine.math.lalg;
public class Matrix4f {
public static final int SIZE = 4;
private float[][] elements = new float[SIZE][SIZE];
@@ -21,8 +23,7 @@ public class Matrix4f {
{0 , 0 , 1 , 0},
{0 , 0 , 0 , 1}
};
Matrix4f identity = new Matrix4f(identityArray);
return identity;
return new Matrix4f(identityArray);
}
public static Matrix4f translate(Vector3f translate) {
@@ -49,8 +50,7 @@ public class Matrix4f {
{z*x*C-y*sin , z*y*C + x*sin , cos + z*z*C , 0},
{0 , 0 , 0 , 1}
};
Matrix4f result = new Matrix4f(rotArray);
return result;
return new Matrix4f(rotArray);
}
public static Matrix4f scale(Vector3f scaleVec) {
@@ -80,6 +80,20 @@ public class Matrix4f {
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(rotationYMatrix, rotationXMatrix));
return Matrix4f.multiply(translationMatrix, rotMat);
}
public static Matrix4f multiply(Matrix4f first, Matrix4f second) {
Matrix4f result = Matrix4f.identity();
@@ -112,7 +126,6 @@ public class Matrix4f {
}
public static Matrix4f transform(Vector3f position, Vector3f rotation, Vector3f scale) {
Matrix4f result = identity();
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));
+30
View File
@@ -0,0 +1,30 @@
package org.hl.engine.objects;
import org.hl.engine.graphics.Mesh;
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) {
this.position = position;
}
public Vector3f getRotation() {
return rotation;
}
public void setRotation(Vector3f rotation) {
this.rotation = rotation;
}
}
+2 -1
View File
@@ -15,7 +15,8 @@ public class GameObject {
}
public void update() {
position.add(0, 0, -0.1F);
position.add(0, 0, -0.01F);
rotation.add(0, 0.001F, 0);
}
public Vector3f getPosition() {