[DEV] tutorial 7 'uniform' implementation
This commit is contained in:
parent
e5467419ec
commit
5d2acb70c7
@ -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;
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
18
src/toolbox/Maths.java
Normal file
18
src/toolbox/Maths.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user