camera movement finished

This commit is contained in:
EvilMuffinHa 2020-05-27 15:10:29 -04:00
parent 5ae985ced9
commit 9a6b5ec048
9 changed files with 232 additions and 25 deletions

View File

@ -50,9 +50,21 @@ public class Test extends Game {
//First updating
int frames = display.update();
//testObject.update();
display.setWindowName(display.getWindowName().substring(0, 4) + " (Frames : " + frames + ")");
float ms = 0.05f;
Vector3f cameraPos = camera.getPosition();
Vector3f cameraRot = camera.getRotation();
if (i.isKeyDown(GLFW.GLFW_KEY_A)) cameraPos = Vector3f.add(cameraPos, new Vector3f(-ms, 0, 0));
if (i.isKeyDown(GLFW.GLFW_KEY_D)) cameraPos = Vector3f.add(cameraPos, new Vector3f(ms, 0, 0));
if (i.isKeyDown(GLFW.GLFW_KEY_W)) cameraPos = Vector3f.add(cameraPos, new Vector3f(0, 0, -ms));
if (i.isKeyDown(GLFW.GLFW_KEY_S)) cameraPos = Vector3f.add(cameraPos, new Vector3f(0, 0, ms));
if (i.isKeyDown(GLFW.GLFW_KEY_SPACE)) cameraPos = Vector3f.add(cameraPos, new Vector3f(0, ms, 0));
if (i.isKeyDown(GLFW.GLFW_KEY_LEFT_SHIFT)) cameraPos = Vector3f.add(cameraPos, new Vector3f(0, -ms, 0));
camera.setPosition(cameraPos);
camera.setRotation(cameraRot);
i.reset();

View File

@ -34,9 +34,9 @@ public class Renderer {
shader.bind();
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()));
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);

View File

@ -26,6 +26,17 @@ public class Matrix4f {
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();
@ -73,8 +84,8 @@ public class Matrix4f {
result.set(0, 0, 1.0f / (aspectRatio * tan));
result.set(1, 1, 1.0f / tan);
result.set(2, 2, -((far + near) / range));
result.set(2, 3, -1.0f);
result.set(3, 2, -(2.0f*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;
@ -96,15 +107,15 @@ public class Matrix4f {
public static Matrix4f multiply(Matrix4f first, Matrix4f second) {
Matrix4f result = Matrix4f.identity();
Matrix4f result = Matrix4f.zeroes();
for (int i = 0; i < SIZE; i ++ ) {
for (int j = 0; j < SIZE; j ++) {
result.set(i, j,
first.get(i, 0) * second.get(0, j) +
first.get(i, 1) * second.get(1, j) +
first.get(i, 2) * second.get(2, j) +
first.get(i, 3) * second.get(3, 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)
);
}
}

View File

@ -1,5 +1,7 @@
package org.hl.engine.math.lalg;
import java.util.Objects;
public class Vector2f {
private float x;
private float y;
@ -16,9 +18,63 @@ public class Vector2f {
}
public void add(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() {

View File

@ -1,6 +1,8 @@
package org.hl.engine.math.lalg;
import java.util.Objects;
public class Vector3f {
private float x;
private float y;
@ -19,10 +21,65 @@ public class Vector3f {
this.z = z;
}
public void add(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() {

View File

@ -1,5 +1,7 @@
package org.hl.engine.math.lalg;
import java.util.Objects;
public class Vector4f {
private float x;
private float y;
@ -27,6 +29,68 @@ public class Vector4f {
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;

View File

@ -1,9 +1,7 @@
package org.hl.engine.objects;
import org.hl.engine.graphics.Mesh;
import org.hl.engine.math.lalg.Vector3f;
public class Camera{
public class Camera {
private Vector3f position;
private Vector3f rotation;
@ -12,6 +10,8 @@ public class Camera{
this.rotation = rotation;
}
public Vector3f getPosition() {
return position;
}

View File

@ -14,11 +14,6 @@ public class GameObject {
this.mesh = mesh;
}
public void update() {
position.add(0, 0, -0.01F);
rotation.add(0, 0.001F, 0);
}
public Vector3f getPosition() {
return position;
}
@ -34,4 +29,16 @@ public class GameObject {
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;
}
}

View File

@ -14,7 +14,7 @@ layout(location = 1) out vec2 passTextureCoord;
void main() {
gl_Position = model * view * projection * vec4(position, 1.0);
gl_Position = projection * view * model * vec4(position, 1.0);
passColor = color;
passTextureCoord = textureCoord;