From 5d2acb70c7e0513ad9fcaf2c7d59cb4094201830 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Sat, 18 Apr 2020 22:53:42 +0200 Subject: [PATCH] [DEV] tutorial 7 'uniform' implementation --- src/module-info.java | 1 + src/shaders/ShaderProgram.java | 35 +++++++++++++++++++++++++++++++++- src/shaders/StaticShader.java | 12 ++++++++++++ src/shaders/vertexShader.txt | 4 +++- src/toolbox/Maths.java | 18 +++++++++++++++++ 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/toolbox/Maths.java diff --git a/src/module-info.java b/src/module-info.java index 613a246..879c9ef 100644 --- a/src/module-info.java +++ b/src/module-info.java @@ -17,6 +17,7 @@ open module Tutorial { requires transitive org.lwjgl.stb.natives; requires transitive org.lwjgl.opengl; requires transitive org.lwjgl.opengl.natives; + requires org.lwjgl.openvr; requires java.desktop; requires pngdecoder; diff --git a/src/shaders/ShaderProgram.java b/src/shaders/ShaderProgram.java index b50aeb1..4f8b783 100644 --- a/src/shaders/ShaderProgram.java +++ b/src/shaders/ShaderProgram.java @@ -3,7 +3,12 @@ package shaders; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; +import java.nio.FloatBuffer; +import javax.vecmath.Matrix4f; +import javax.vecmath.Vector3f; + +import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL20; @@ -12,6 +17,7 @@ public abstract class ShaderProgram { private int programID; private int vertexShaderID; private int fragmentShaderID; + public ShaderProgram (String vertexFile, String fragmentFile) { vertexShaderID = loadShader(vertexFile, GL20.GL_VERTEX_SHADER); @@ -19,11 +25,17 @@ public abstract class ShaderProgram { programID = GL20.glCreateProgram(); GL20.glAttachShader(programID, vertexShaderID); GL20.glAttachShader(programID, fragmentShaderID); + bindAttributes(); GL20.glLinkProgram(programID); GL20.glValidateProgram(programID); - bindAttributes(); + getAllUniformLocations(); } + protected abstract void getAllUniformLocations(); + protected int getUniformLocation(String uniformName) { + return GL20.glGetUniformLocation(programID, uniformName); + } + public void start() { GL20.glUseProgram(programID); } @@ -44,6 +56,27 @@ public abstract class ShaderProgram { protected void bindAttribute(int attribute, String variableName) { GL20.glBindAttribLocation(programID, attribute, variableName); } + + protected void loadFloat(int location, float value) { + GL20.glUniform1f(location, value); + } + + protected void loadVector(int location, Vector3f value) { + GL20.glUniform3f(location, value.x, value.y, value.z); + } + protected void loadBoolean(int location, boolean value) { + GL20.glUniform1f(location, value?1.0f:0.0f); + } + + protected void loadMatrix(int location, Matrix4f value) { + float[] values = new float[] { + value.m00, value.m01, value.m02, value.m03, + value.m10, value.m11, value.m12, value.m13, + value.m20, value.m21, value.m22, value.m23, + value.m30, value.m31, value.m32, value.m33 + }; + GL20.glUniformMatrix4fv(location, false, values); + } private static int loadShader(String file, int type) { StringBuilder shaderSource = new StringBuilder(); diff --git a/src/shaders/StaticShader.java b/src/shaders/StaticShader.java index ae6d9f9..30d7c3b 100644 --- a/src/shaders/StaticShader.java +++ b/src/shaders/StaticShader.java @@ -1,8 +1,12 @@ package shaders; +import javax.vecmath.Matrix4f; + public class StaticShader extends ShaderProgram { private static final String VERTEX_FILE = "src/shaders/vertexShader.txt"; private static final String FRAGMENT_FILE = "src/shaders/fragmentShader.txt"; + + private int location_transformationMatrix; public StaticShader() { super(VERTEX_FILE, FRAGMENT_FILE); @@ -13,5 +17,13 @@ public class StaticShader extends ShaderProgram { super.bindAttribute(0, "position"); super.bindAttribute(1, "textureCoords"); } + + @Override + protected void getAllUniformLocations() { + location_transformationMatrix = super.getUniformLocation("transformationMatrix"); + } + public void loadTransformationMatrix(Matrix4f value) { + super.loadMatrix(location_transformationMatrix, value); + } } diff --git a/src/shaders/vertexShader.txt b/src/shaders/vertexShader.txt index ee4b5e1..43600fc 100644 --- a/src/shaders/vertexShader.txt +++ b/src/shaders/vertexShader.txt @@ -5,9 +5,11 @@ in vec2 textureCoords; out vec2 pass_textureCoords; +uniform mat4 transformationMatrix; + void main(void) { - gl_Position = vec4(position, 1.0); + gl_Position = transformationMatrix * vec4(position, 1.0); pass_textureCoords = textureCoords; } diff --git a/src/toolbox/Maths.java b/src/toolbox/Maths.java new file mode 100644 index 0000000..351f6a1 --- /dev/null +++ b/src/toolbox/Maths.java @@ -0,0 +1,18 @@ +package toolbox; + +import javax.vecmath.Matrix4f; +import javax.vecmath.Vector3f; + +public class Maths { + + public static Matrix4f createTransformationMatrix(Vector3f translation, Vector3f rotation, float scale) { + Matrix4f matrix = new Matrix4f(); + matrix.setIdentity(); + matrix.setTranslation(translation); + matrix.rotX(rotation.x); + matrix.rotY(rotation.y); + matrix.rotZ(rotation.z); + matrix.setScale(scale); + return matrix; + } +}