[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.stb.natives;
|
||||||
requires transitive org.lwjgl.opengl;
|
requires transitive org.lwjgl.opengl;
|
||||||
requires transitive org.lwjgl.opengl.natives;
|
requires transitive org.lwjgl.opengl.natives;
|
||||||
|
|
||||||
requires org.lwjgl.openvr;
|
requires org.lwjgl.openvr;
|
||||||
requires java.desktop;
|
requires java.desktop;
|
||||||
requires pngdecoder;
|
requires pngdecoder;
|
||||||
|
@ -3,7 +3,12 @@ package shaders;
|
|||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
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.GL11;
|
||||||
import org.lwjgl.opengl.GL20;
|
import org.lwjgl.opengl.GL20;
|
||||||
|
|
||||||
@ -13,15 +18,22 @@ public abstract class ShaderProgram {
|
|||||||
private int vertexShaderID;
|
private int vertexShaderID;
|
||||||
private int fragmentShaderID;
|
private int fragmentShaderID;
|
||||||
|
|
||||||
|
|
||||||
public ShaderProgram (String vertexFile, String fragmentFile) {
|
public ShaderProgram (String vertexFile, String fragmentFile) {
|
||||||
vertexShaderID = loadShader(vertexFile, GL20.GL_VERTEX_SHADER);
|
vertexShaderID = loadShader(vertexFile, GL20.GL_VERTEX_SHADER);
|
||||||
fragmentShaderID = loadShader(fragmentFile, GL20.GL_FRAGMENT_SHADER);
|
fragmentShaderID = loadShader(fragmentFile, GL20.GL_FRAGMENT_SHADER);
|
||||||
programID = GL20.glCreateProgram();
|
programID = GL20.glCreateProgram();
|
||||||
GL20.glAttachShader(programID, vertexShaderID);
|
GL20.glAttachShader(programID, vertexShaderID);
|
||||||
GL20.glAttachShader(programID, fragmentShaderID);
|
GL20.glAttachShader(programID, fragmentShaderID);
|
||||||
|
bindAttributes();
|
||||||
GL20.glLinkProgram(programID);
|
GL20.glLinkProgram(programID);
|
||||||
GL20.glValidateProgram(programID);
|
GL20.glValidateProgram(programID);
|
||||||
bindAttributes();
|
getAllUniformLocations();
|
||||||
|
}
|
||||||
|
protected abstract void getAllUniformLocations();
|
||||||
|
|
||||||
|
protected int getUniformLocation(String uniformName) {
|
||||||
|
return GL20.glGetUniformLocation(programID, uniformName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
@ -45,6 +57,27 @@ public abstract class ShaderProgram {
|
|||||||
GL20.glBindAttribLocation(programID, attribute, 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) {
|
private static int loadShader(String file, int type) {
|
||||||
StringBuilder shaderSource = new StringBuilder();
|
StringBuilder shaderSource = new StringBuilder();
|
||||||
try {
|
try {
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
package shaders;
|
package shaders;
|
||||||
|
|
||||||
|
import javax.vecmath.Matrix4f;
|
||||||
|
|
||||||
public class StaticShader extends ShaderProgram {
|
public class StaticShader extends ShaderProgram {
|
||||||
private static final String VERTEX_FILE = "src/shaders/vertexShader.txt";
|
private static final String VERTEX_FILE = "src/shaders/vertexShader.txt";
|
||||||
private static final String FRAGMENT_FILE = "src/shaders/fragmentShader.txt";
|
private static final String FRAGMENT_FILE = "src/shaders/fragmentShader.txt";
|
||||||
|
|
||||||
|
private int location_transformationMatrix;
|
||||||
|
|
||||||
public StaticShader() {
|
public StaticShader() {
|
||||||
super(VERTEX_FILE, FRAGMENT_FILE);
|
super(VERTEX_FILE, FRAGMENT_FILE);
|
||||||
}
|
}
|
||||||
@ -14,4 +18,12 @@ public class StaticShader extends ShaderProgram {
|
|||||||
super.bindAttribute(1, "textureCoords");
|
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;
|
out vec2 pass_textureCoords;
|
||||||
|
|
||||||
|
uniform mat4 transformationMatrix;
|
||||||
|
|
||||||
void main(void) {
|
void main(void) {
|
||||||
|
|
||||||
gl_Position = vec4(position, 1.0);
|
gl_Position = transformationMatrix * vec4(position, 1.0);
|
||||||
pass_textureCoords = textureCoords;
|
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