From 7288a7ee99ac8f2c1901657550bb26075a277177 Mon Sep 17 00:00:00 2001 From: EvilMuffinHa Date: Mon, 25 May 2020 18:18:51 -0400 Subject: [PATCH] uniforms! --- src/org/hl/engine/graphics/Renderer.java | 2 + src/org/hl/engine/graphics/Shader.java | 48 ++++++++++++++++++++++- src/org/hl/engine/math/lalg/Matrix4f.java | 26 ++++++++++++ src/resources/shaders/mainFragment.glsl | 10 ++--- src/resources/shaders/mainVertex.glsl | 10 ++--- 5 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 src/org/hl/engine/math/lalg/Matrix4f.java diff --git a/src/org/hl/engine/graphics/Renderer.java b/src/org/hl/engine/graphics/Renderer.java index 421f5e4..a22126b 100644 --- a/src/org/hl/engine/graphics/Renderer.java +++ b/src/org/hl/engine/graphics/Renderer.java @@ -29,6 +29,8 @@ public class Renderer { shader.bind(); + shader.setUniform("scale", 2.0F); + GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_UNSIGNED_INT, 0); shader.unbind(); diff --git a/src/org/hl/engine/graphics/Shader.java b/src/org/hl/engine/graphics/Shader.java index 7e87109..937d2a3 100644 --- a/src/org/hl/engine/graphics/Shader.java +++ b/src/org/hl/engine/graphics/Shader.java @@ -1,8 +1,15 @@ package org.hl.engine.graphics; +import org.hl.engine.math.lalg.Matrix4f; +import org.hl.engine.math.lalg.Vector2f; +import org.hl.engine.math.lalg.Vector3f; +import org.hl.engine.math.lalg.Vector4f; import org.hl.engine.utils.FileUtils; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL20; +import org.lwjgl.system.MemoryUtil; + +import java.nio.FloatBuffer; public class Shader { private String vertexFile; @@ -66,9 +73,40 @@ public class Shader { return; } - GL20.glDeleteShader(vertexID); - GL20.glDeleteShader(fragmentID); + } + public int getUniformLocation(String name) { + return GL20.glGetUniformLocation(programID, name); + } + + public void setUniform(String name, float value) { + GL20.glUniform1f(getUniformLocation(name), value); + } + + public void setUniform(String name, int value) { + GL20.glUniform1i(getUniformLocation(name), value); + } + + public void setUniform(String name, boolean value) { + GL20.glUniform1i(getUniformLocation(name), value ? 1 : 0); + } + public void setUniform(String name, Vector2f value) { + GL20.glUniform2f(getUniformLocation(name), value.getX(), value.getY()); + + } + + public void setUniform(String name, Vector3f value) { + GL20.glUniform3f(getUniformLocation(name), value.getX(), value.getY(), value.getZ()); + } + + public void setUniform(String name, Vector4f value) { + GL20.glUniform4f(getUniformLocation(name), value.getX(), value.getY(), value.getZ(), value.getA()); + } + + public void setUniform(String name, Matrix4f value) { + FloatBuffer matrix = MemoryUtil.memAllocFloat(Matrix4f.SIZE * Matrix4f.SIZE); + matrix.put(value.convertTo1D()).flip(); + GL20.glUniformMatrix4fv(getUniformLocation(name), true, matrix); } // Bind so we can use the shader @@ -83,6 +121,12 @@ public class Shader { // Destroy the program public void destroy() { + GL20.glDetachShader(programID, vertexID); + GL20.glDetachShader(programID, fragmentID); + + GL20.glDeleteShader(vertexID); + GL20.glDeleteShader(fragmentID); + GL20.glDeleteProgram(programID); } } diff --git a/src/org/hl/engine/math/lalg/Matrix4f.java b/src/org/hl/engine/math/lalg/Matrix4f.java new file mode 100644 index 0000000..43bdfd1 --- /dev/null +++ b/src/org/hl/engine/math/lalg/Matrix4f.java @@ -0,0 +1,26 @@ +package org.hl.engine.math.lalg; + +public class Matrix4f { + public static final int SIZE = 4; + private float[][] elements; + 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 float[] convertTo1D() { + float[] returnedArray = new float[SIZE*SIZE]; + int sizeOfRow = elements[0].length; + for (int i = 0; i < elements.length; i ++) { + for (int j = 0; j < sizeOfRow; j ++) { + returnedArray[i*SIZE + j] = elements[i][j]; + } + } + return returnedArray; + } +} diff --git a/src/resources/shaders/mainFragment.glsl b/src/resources/shaders/mainFragment.glsl index ba10a51..8bbda4e 100644 --- a/src/resources/shaders/mainFragment.glsl +++ b/src/resources/shaders/mainFragment.glsl @@ -1,12 +1,12 @@ #version 410 core +in vec3 passColor; +in vec2 passTextureCoord; +out vec4 outColor; -layout(location = 0) in vec3 passColor; -layout(location = 1) in vec2 passTextureCoord; - -layout(location = 0) out vec4 outColor; +uniform sampler2D tex; void main() { - outColor = vec4(passColor, 1.0); + outColor = texture(tex, passTextureCoord); } diff --git a/src/resources/shaders/mainVertex.glsl b/src/resources/shaders/mainVertex.glsl index c795fa8..22f512e 100644 --- a/src/resources/shaders/mainVertex.glsl +++ b/src/resources/shaders/mainVertex.glsl @@ -1,7 +1,5 @@ #version 410 core - - layout(location = 0) in vec3 position; layout(location = 1) in vec3 color; layout(location = 2) in vec2 textureCoord; @@ -12,10 +10,12 @@ layout(location = 2) in vec2 textureCoord; layout(location = 0) out vec3 passColor; layout(location = 1) out vec2 passTextureCoord; +uniform float scale; + void main() { - gl_Position = vec4(position, 1.0); - passColor = color; + gl_Position = vec4(position, 1.0) * vec4(scale, scale, scale, 1); + passColor = color; - passTextureCoord = textureCoord; + passTextureCoord = textureCoord; } \ No newline at end of file