projection matrix
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package org.hl.engine.graphics;
|
||||
|
||||
import org.hl.engine.io.Display;
|
||||
import org.hl.engine.math.lalg.Matrix4f;
|
||||
import org.hl.engine.objects.GameObject;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
import org.lwjgl.opengl.GL15;
|
||||
@@ -8,30 +11,32 @@ import org.lwjgl.opengl.GL30;
|
||||
public class Renderer {
|
||||
|
||||
private Shader shader;
|
||||
private Display display;
|
||||
|
||||
public Renderer(Shader shader) {
|
||||
public Renderer(Display display, Shader shader) {
|
||||
this.shader = shader;
|
||||
this.display = display;
|
||||
}
|
||||
|
||||
|
||||
public void renderMesh(Mesh mesh) {
|
||||
public void renderMesh(GameObject object) {
|
||||
|
||||
// Renders the mesh by drawing it using triangles (least complicated)
|
||||
GL30.glBindVertexArray(mesh.getVertexArrayObject());
|
||||
GL30.glBindVertexArray(object.getMesh().getVertexArrayObject());
|
||||
GL30.glEnableVertexAttribArray(0);
|
||||
GL30.glEnableVertexAttribArray(1);
|
||||
GL30.glEnableVertexAttribArray(2);
|
||||
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, mesh.getIndicesBufferObject());
|
||||
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, object.getMesh().getIndicesBufferObject());
|
||||
|
||||
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
||||
|
||||
GL13.glBindTexture(GL11.GL_TEXTURE_2D, mesh.getMaterial().getTexture().getId());
|
||||
GL13.glBindTexture(GL11.GL_TEXTURE_2D, object.getMesh().getMaterial().getTexture().getId());
|
||||
|
||||
shader.bind();
|
||||
|
||||
shader.setUniform("scale", 2.0F);
|
||||
shader.setUniform("model", Matrix4f.transform(object.getPosition(), object.getRotation(), object.getScale()));
|
||||
shader.setUniform("projection", display.getProjectionMatrix());
|
||||
|
||||
GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_UNSIGNED_INT, 0);
|
||||
GL11.glDrawElements(GL11.GL_TRIANGLES, object.getMesh().getIndices().length, GL11.GL_UNSIGNED_INT, 0);
|
||||
|
||||
shader.unbind();
|
||||
GL30.glDisableVertexAttribArray(0);
|
||||
|
||||
@@ -17,6 +17,9 @@ public class Shader {
|
||||
|
||||
private int vertexID, fragmentID, programID;
|
||||
|
||||
public static final String VERTEXSHADER = "/resources/shaders/mainVertex.glsl";
|
||||
public static final String FRAGSHADER = "/resources/shaders/mainFragment.glsl";
|
||||
|
||||
public Shader(String vertexPath, String fragmentPath) {
|
||||
vertexFile = FileUtils.loadAsString(vertexPath);
|
||||
fragmentFile = FileUtils.loadAsString(fragmentPath);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.hl.engine.io;
|
||||
|
||||
import org.hl.engine.math.lalg.Matrix4f;
|
||||
import org.hl.engine.math.lalg.Vector3f;
|
||||
import org.lwjgl.glfw.GLFWErrorCallback;
|
||||
import org.lwjgl.glfw.GLFWVidMode;
|
||||
@@ -29,15 +30,16 @@ public class Display {
|
||||
private int savedPosY;
|
||||
private int savedWidth;
|
||||
private int savedHeight;
|
||||
|
||||
private Matrix4f projection;
|
||||
|
||||
|
||||
|
||||
// Constructor to create the display
|
||||
public Display (int width, int height, String windowName) {
|
||||
public Display (int width, int height, String windowName, float fov, float near, float far) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.windowName = windowName;
|
||||
projection = Matrix4f.projection(fov, (float)this.width / (float) this.height, near, far);
|
||||
}
|
||||
|
||||
// Change the window name
|
||||
@@ -67,6 +69,10 @@ public class Display {
|
||||
return isFullscreen;
|
||||
}
|
||||
|
||||
public Matrix4f getProjectionMatrix() {
|
||||
return projection;
|
||||
}
|
||||
|
||||
// Makes the screen fullscreen or not based on the argument
|
||||
public void setFullscreen(boolean fullscreen) {
|
||||
isFullscreen = fullscreen;
|
||||
|
||||
@@ -2,17 +2,130 @@ package org.hl.engine.math.lalg;
|
||||
|
||||
public class Matrix4f {
|
||||
public static final int SIZE = 4;
|
||||
private float[][] elements;
|
||||
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}
|
||||
};
|
||||
Matrix4f identity = new Matrix4f(identityArray);
|
||||
return identity;
|
||||
}
|
||||
|
||||
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 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}
|
||||
};
|
||||
Matrix4f result = new Matrix4f(rotArray);
|
||||
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(2, 3, -1.0f);
|
||||
result.set(3, 2, -(2.0f*far*near/range));
|
||||
result.set(3, 3, 0f);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Matrix4f multiply(Matrix4f first, Matrix4f second) {
|
||||
|
||||
Matrix4f result = Matrix4f.identity();
|
||||
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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 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));
|
||||
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;
|
||||
|
||||
@@ -15,6 +15,12 @@ public class Vector2f {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
|
||||
public void add(float x, float y) {
|
||||
this.x += x;
|
||||
this.y += y;
|
||||
}
|
||||
|
||||
public float getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,12 @@ 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 float getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,13 @@ public class Vector4f {
|
||||
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 float getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.hl.engine.objects;
|
||||
|
||||
import org.hl.engine.graphics.Mesh;
|
||||
import org.hl.engine.math.lalg.Vector3f;
|
||||
|
||||
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 void update() {
|
||||
position.add(0, 0, -0.1F);
|
||||
}
|
||||
|
||||
public Vector3f getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public Vector3f getRotation() {
|
||||
return rotation;
|
||||
}
|
||||
|
||||
public Vector3f getScale() {
|
||||
return scale;
|
||||
}
|
||||
|
||||
public Mesh getMesh() {
|
||||
return mesh;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user