From 5ae985ced96d113590b079836553702a206c3306 Mon Sep 17 00:00:00 2001 From: EvilMuffinHa Date: Wed, 27 May 2020 11:49:28 -0400 Subject: [PATCH] small camera --- .idea/Helium-Engine.iml | 1 - .idea/artifacts/Helium_Engine_jar.xml | 152 ++++++++++++++++++++++ .idea/libraries/commons_io_2.xml | 15 --- src/Test.java | 7 +- src/org/hl/engine/graphics/Renderer.java | 4 +- src/org/hl/engine/math/lalg/Matrix4f.java | 23 +++- src/org/hl/engine/objects/Camera.java | 30 +++++ src/org/hl/engine/objects/GameObject.java | 3 +- src/resources/shaders/mainVertex.glsl | 3 +- 9 files changed, 211 insertions(+), 27 deletions(-) create mode 100644 .idea/artifacts/Helium_Engine_jar.xml delete mode 100644 .idea/libraries/commons_io_2.xml create mode 100644 src/org/hl/engine/objects/Camera.java diff --git a/.idea/Helium-Engine.iml b/.idea/Helium-Engine.iml index 60dfedb..0913bd0 100644 --- a/.idea/Helium-Engine.iml +++ b/.idea/Helium-Engine.iml @@ -8,6 +8,5 @@ - \ No newline at end of file diff --git a/.idea/artifacts/Helium_Engine_jar.xml b/.idea/artifacts/Helium_Engine_jar.xml new file mode 100644 index 0000000..8413fd3 --- /dev/null +++ b/.idea/artifacts/Helium_Engine_jar.xml @@ -0,0 +1,152 @@ + + + $PROJECT_DIR$/out/artifacts/Helium_Engine_jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/commons_io_2.xml b/.idea/libraries/commons_io_2.xml deleted file mode 100644 index 3cd1687..0000000 --- a/.idea/libraries/commons_io_2.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/Test.java b/src/Test.java index 5ec9c1e..cba6d27 100644 --- a/src/Test.java +++ b/src/Test.java @@ -3,6 +3,7 @@ import org.hl.engine.io.Display; import org.hl.engine.io.Input; import org.hl.engine.math.lalg.Vector3f; import org.hl.engine.math.lalg.Vector2f; +import org.hl.engine.objects.Camera; import org.hl.engine.objects.GameObject; import org.lwjgl.glfw.GLFW; @@ -29,9 +30,9 @@ public class Test extends Game { }, new Material(new Texture("resources/textures/b.png"))); - public GameObject testObject = new GameObject(mesh, new Vector3f(0, 0, -1), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1)); - + public GameObject testObject = new GameObject(mesh, new Vector3f(0, 0, 0 ), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1)); + public Camera camera = new Camera(new Vector3f(0, 0, 1), new Vector3f(0, 0, 0)); public void run() throws Exception { setup(); @@ -59,7 +60,7 @@ public class Test extends Game { // rendering the mesh - renderer.renderMesh(testObject); + renderer.renderMesh(testObject, camera); //swap buffers so the new one will appear display.swapBuffers(); } diff --git a/src/org/hl/engine/graphics/Renderer.java b/src/org/hl/engine/graphics/Renderer.java index 57616f2..03d7244 100644 --- a/src/org/hl/engine/graphics/Renderer.java +++ b/src/org/hl/engine/graphics/Renderer.java @@ -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); diff --git a/src/org/hl/engine/math/lalg/Matrix4f.java b/src/org/hl/engine/math/lalg/Matrix4f.java index f5914d7..0aeed3d 100644 --- a/src/org/hl/engine/math/lalg/Matrix4f.java +++ b/src/org/hl/engine/math/lalg/Matrix4f.java @@ -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)); diff --git a/src/org/hl/engine/objects/Camera.java b/src/org/hl/engine/objects/Camera.java new file mode 100644 index 0000000..d690dcf --- /dev/null +++ b/src/org/hl/engine/objects/Camera.java @@ -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; + } +} diff --git a/src/org/hl/engine/objects/GameObject.java b/src/org/hl/engine/objects/GameObject.java index f321819..3641d46 100644 --- a/src/org/hl/engine/objects/GameObject.java +++ b/src/org/hl/engine/objects/GameObject.java @@ -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() { diff --git a/src/resources/shaders/mainVertex.glsl b/src/resources/shaders/mainVertex.glsl index d7280c7..ea2c864 100644 --- a/src/resources/shaders/mainVertex.glsl +++ b/src/resources/shaders/mainVertex.glsl @@ -5,6 +5,7 @@ layout(location = 1) in vec3 color; layout(location = 2) in vec2 textureCoord; uniform mat4 model; +uniform mat4 view; uniform mat4 projection; layout(location = 0) out vec3 passColor; @@ -13,7 +14,7 @@ layout(location = 1) out vec2 passTextureCoord; void main() { - gl_Position = projection * model * vec4(position, 1.0); + gl_Position = model * view * projection * vec4(position, 1.0); passColor = color; passTextureCoord = textureCoord;