projection matrix

This commit is contained in:
EvilMuffinHa 2020-05-26 12:04:13 -04:00
parent 7288a7ee99
commit 1285842b55
12 changed files with 233 additions and 46 deletions

BIN
resources/textures/b.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

6
src/Game.java Normal file
View File

@ -0,0 +1,6 @@
abstract public class Game {
abstract public void setup() throws Exception;
abstract public void loop() throws Exception;
abstract public void close() throws Exception;
}

View File

@ -3,13 +3,14 @@ import org.hl.engine.io.Display;
import org.hl.engine.io.Input; import org.hl.engine.io.Input;
import org.hl.engine.math.lalg.Vector3f; import org.hl.engine.math.lalg.Vector3f;
import org.hl.engine.math.lalg.Vector2f; import org.hl.engine.math.lalg.Vector2f;
import org.hl.engine.objects.GameObject;
import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFW;
public class Test { public class Test extends Game {
// Defining original parts of the game // Defining original parts of the game
public final static int WIDTH = 640, HEIGHT = 480; public final static int WIDTH = 1280, HEIGHT = 760;
public final String windowName = "Game!"; public final String windowName = "Game!";
public Display display; public Display display;
public Input i; public Input i;
@ -26,7 +27,9 @@ public class Test {
0, 1, 2, 0, 1, 2,
0, 2, 3 0, 2, 3
}, new Material(new Texture("resources/textures/thonk.png"))); }, 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));
@ -42,24 +45,39 @@ public class Test {
} }
public void loop(){ public void loop(){
update();
render();
//First updating
int frames = display.update();
//testObject.update();
display.setWindowName(display.getWindowName().substring(0, 4) + " (Frames : " + frames + ")");
i.reset();
// Now Render!
// rendering the mesh
renderer.renderMesh(testObject);
//swap buffers so the new one will appear
display.swapBuffers();
} }
public void setup() throws Exception { public void setup() throws Exception {
//First, set up the display //First, set up the display
display = new Display(WIDTH, HEIGHT, windowName); display = new Display(WIDTH, HEIGHT, windowName, 70, 0.1f, 1000f);
display.create(); display.create();
// Open the shaders (not necessary there are textures) // Open the shaders
shader = new Shader("/resources/shaders/mainVertex.glsl", "/resources/shaders/mainFragment.glsl"); shader = new Shader(Shader.VERTEXSHADER, Shader.FRAGSHADER);
// Set up the renderer // Set up the renderer
renderer = new Renderer(shader); renderer = new Renderer(display, shader);
// Changing the background color // Changing the background color
display.setBackgroundColor(1F, 1F, 1F); display.setBackgroundColor(0F, 0F, 0F);
// Creating / displaying the mesh // Creating / displaying the mesh
mesh.create(); mesh.create();
@ -69,25 +87,7 @@ public class Test {
} }
// Updating public void close() {
private void update() {
// Just updating lmfao
int frames = display.update();
display.setWindowName(display.getWindowName().substring(0, 4) + " (Frames : " + frames + ")");
i.reset();
}
private void render() {
// rendering the mesh
renderer.renderMesh(mesh);
//swap buffers so the new one will appear
display.swapBuffers();
}
private void close() {
// Removing everything // Removing everything
display.destroy(); display.destroy();
mesh.destroy(); mesh.destroy();

View File

@ -1,5 +1,8 @@
package org.hl.engine.graphics; 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.GL11;
import org.lwjgl.opengl.GL13; import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL15; import org.lwjgl.opengl.GL15;
@ -8,30 +11,32 @@ import org.lwjgl.opengl.GL30;
public class Renderer { public class Renderer {
private Shader shader; private Shader shader;
private Display display;
public Renderer(Shader shader) { public Renderer(Display display, Shader shader) {
this.shader = shader; this.shader = shader;
this.display = display;
} }
public void renderMesh(GameObject object) {
public void renderMesh(Mesh mesh) {
// Renders the mesh by drawing it using triangles (least complicated) // Renders the mesh by drawing it using triangles (least complicated)
GL30.glBindVertexArray(mesh.getVertexArrayObject()); GL30.glBindVertexArray(object.getMesh().getVertexArrayObject());
GL30.glEnableVertexAttribArray(0); GL30.glEnableVertexAttribArray(0);
GL30.glEnableVertexAttribArray(1); GL30.glEnableVertexAttribArray(1);
GL30.glEnableVertexAttribArray(2); 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.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.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(); shader.unbind();
GL30.glDisableVertexAttribArray(0); GL30.glDisableVertexAttribArray(0);

View File

@ -17,6 +17,9 @@ public class Shader {
private int vertexID, fragmentID, programID; 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) { public Shader(String vertexPath, String fragmentPath) {
vertexFile = FileUtils.loadAsString(vertexPath); vertexFile = FileUtils.loadAsString(vertexPath);
fragmentFile = FileUtils.loadAsString(fragmentPath); fragmentFile = FileUtils.loadAsString(fragmentPath);

View File

@ -1,5 +1,6 @@
package org.hl.engine.io; package org.hl.engine.io;
import org.hl.engine.math.lalg.Matrix4f;
import org.hl.engine.math.lalg.Vector3f; import org.hl.engine.math.lalg.Vector3f;
import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode; import org.lwjgl.glfw.GLFWVidMode;
@ -29,15 +30,16 @@ public class Display {
private int savedPosY; private int savedPosY;
private int savedWidth; private int savedWidth;
private int savedHeight; private int savedHeight;
private Matrix4f projection;
// Constructor to create the display // 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.width = width;
this.height = height; this.height = height;
this.windowName = windowName; this.windowName = windowName;
projection = Matrix4f.projection(fov, (float)this.width / (float) this.height, near, far);
} }
// Change the window name // Change the window name
@ -67,6 +69,10 @@ public class Display {
return isFullscreen; return isFullscreen;
} }
public Matrix4f getProjectionMatrix() {
return projection;
}
// Makes the screen fullscreen or not based on the argument // Makes the screen fullscreen or not based on the argument
public void setFullscreen(boolean fullscreen) { public void setFullscreen(boolean fullscreen) {
isFullscreen = fullscreen; isFullscreen = fullscreen;

View File

@ -2,17 +2,130 @@ package org.hl.engine.math.lalg;
public class Matrix4f { public class Matrix4f {
public static final int SIZE = 4; 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) { public float get(int x, int y) {
return elements[x][y]; return elements[x][y];
} }
public void set(int x, int y, float value) { public void set(int x, int y, float value) {
elements[x][y] = value; elements[x][y] = value;
} }
public float[][] getAll() { public float[][] getAll() {
return elements; 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() { public float[] convertTo1D() {
float[] returnedArray = new float[SIZE*SIZE]; float[] returnedArray = new float[SIZE*SIZE];
int sizeOfRow = elements[0].length; int sizeOfRow = elements[0].length;

View File

@ -15,6 +15,12 @@ public class Vector2f {
this.y = y; this.y = y;
} }
public void add(float x, float y) {
this.x += x;
this.y += y;
}
public float getX() { public float getX() {
return x; return x;
} }

View File

@ -19,6 +19,12 @@ public class Vector3f {
this.z = z; this.z = z;
} }
public void add(float x, float y, float z) {
this.x += x;
this.y += y;
this.z += z;
}
public float getX() { public float getX() {
return x; return x;
} }

View File

@ -21,6 +21,13 @@ public class Vector4f {
this.a = a; 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() { public float getX() {
return x; return x;
} }

View File

@ -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;
}
}

View File

@ -4,17 +4,16 @@ layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color; layout(location = 1) in vec3 color;
layout(location = 2) in vec2 textureCoord; layout(location = 2) in vec2 textureCoord;
uniform mat4 model;
uniform mat4 projection;
layout(location = 0) out vec3 passColor; layout(location = 0) out vec3 passColor;
layout(location = 1) out vec2 passTextureCoord; layout(location = 1) out vec2 passTextureCoord;
uniform float scale;
void main() { void main() {
gl_Position = vec4(position, 1.0) * vec4(scale, scale, scale, 1); gl_Position = projection * model * vec4(position, 1.0);
passColor = color; passColor = color;
passTextureCoord = textureCoord; passTextureCoord = textureCoord;