Uniforms
This commit is contained in:
@@ -1,12 +1,16 @@
|
|||||||
package Shaders;
|
package Shaders;
|
||||||
|
|
||||||
|
import org.lwjgl.BufferUtils;
|
||||||
import org.lwjgl.opengl.GL11;
|
import org.lwjgl.opengl.GL11;
|
||||||
import org.lwjgl.opengl.GL20;
|
import org.lwjgl.opengl.GL20;
|
||||||
|
import org.lwjgl.util.vector.Matrix4f;
|
||||||
|
import org.lwjgl.util.vector.Vector3f;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.FloatBuffer;
|
||||||
|
|
||||||
public abstract class ShaderProgram
|
public abstract class ShaderProgram
|
||||||
{
|
{
|
||||||
@@ -14,6 +18,8 @@ public abstract class ShaderProgram
|
|||||||
private int vertexShaderID;
|
private int vertexShaderID;
|
||||||
private int fragmentShaderID;
|
private int fragmentShaderID;
|
||||||
|
|
||||||
|
private static FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
|
||||||
|
|
||||||
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);
|
||||||
@@ -24,6 +30,14 @@ public abstract class ShaderProgram
|
|||||||
bindAttributes();
|
bindAttributes();
|
||||||
GL20.glLinkProgram(programID);
|
GL20.glLinkProgram(programID);
|
||||||
GL20.glValidateProgram(programID);
|
GL20.glValidateProgram(programID);
|
||||||
|
getAllUniformLocations();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void getAllUniformLocations();
|
||||||
|
|
||||||
|
protected int getUniformLocation(String uniformName)
|
||||||
|
{
|
||||||
|
return GL20.glGetUniformLocation(programID, uniformName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start()
|
public void start()
|
||||||
@@ -53,6 +67,35 @@ 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 vector)
|
||||||
|
{
|
||||||
|
GL20.glUniform3f(location, vector.x, vector.y, vector.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void loadBoolean(int location, boolean value)
|
||||||
|
{
|
||||||
|
if(value)
|
||||||
|
{
|
||||||
|
GL20.glUniform1f(location, 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
GL20.glUniform1f(location, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void loadMatrix(int location, Matrix4f matrix)
|
||||||
|
{
|
||||||
|
matrix.store(matrixBuffer);
|
||||||
|
matrixBuffer.flip();
|
||||||
|
GL20.glUniformMatrix4(location, false, matrixBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
private static int loadShader(String file, int type)
|
private static int loadShader(String file, int type)
|
||||||
{
|
{
|
||||||
StringBuilder shaderSource = new StringBuilder();
|
StringBuilder shaderSource = new StringBuilder();
|
||||||
|
|||||||
@@ -1,18 +1,32 @@
|
|||||||
package Shaders;
|
package Shaders;
|
||||||
|
|
||||||
|
import org.lwjgl.util.vector.Matrix4f;
|
||||||
|
|
||||||
public class StaticShader extends ShaderProgram
|
public class StaticShader extends ShaderProgram
|
||||||
{
|
{
|
||||||
private static final String vertexFile = "src/shaders/vertexShader.glsl", fragmentFile = "src/shaders/fragmentShader.glsl";
|
private static final String vertexFile = "src/shaders/vertexShader.glsl", fragmentFile = "src/shaders/fragmentShader.glsl";
|
||||||
|
private int locationTransform;
|
||||||
|
|
||||||
public StaticShader()
|
public StaticShader()
|
||||||
{
|
{
|
||||||
super(vertexFile, fragmentFile);
|
super(vertexFile, fragmentFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void getAllUniformLocations()
|
||||||
|
{
|
||||||
|
locationTransform = getUniformLocation("transformationMatrix");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void bindAttributes()
|
protected void bindAttributes()
|
||||||
{
|
{
|
||||||
bindAttribute(0, "position");
|
bindAttribute(0, "position");
|
||||||
bindAttribute(1, "uvs");
|
bindAttribute(1, "uvs");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void loadTransformMatrix(Matrix4f matrix)
|
||||||
|
{
|
||||||
|
loadMatrix(locationTransform, matrix);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ in vec2 uvs;
|
|||||||
out vec3 color;
|
out vec3 color;
|
||||||
out vec2 passUvs;
|
out vec2 passUvs;
|
||||||
|
|
||||||
|
uniform mat4 transformationMatrix;
|
||||||
|
|
||||||
void main(void)
|
void main(void)
|
||||||
{
|
{
|
||||||
gl_Position = vec4(position.x, position.y, position.z, 1.0);
|
gl_Position = vec4(position.x, position.y, position.z, 1.0) * transformationMatrix;
|
||||||
passUvs = uvs;
|
passUvs = uvs;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package Tools;
|
||||||
|
|
||||||
|
import org.lwjgl.util.vector.Matrix4f;
|
||||||
|
import org.lwjgl.util.vector.Vector3f;
|
||||||
|
|
||||||
|
public class Maths
|
||||||
|
{
|
||||||
|
public static Matrix4f createTransformMatrix(Vector3f translation, float rX, float rY, float rZ, float scale)
|
||||||
|
{
|
||||||
|
Matrix4f matrix = new Matrix4f();
|
||||||
|
matrix.setIdentity();
|
||||||
|
Matrix4f.translate(translation, matrix, matrix);
|
||||||
|
Matrix4f.rotate((float)Math.toRadians(rX), new Vector3f(1, 0, 0), matrix, matrix);
|
||||||
|
Matrix4f.rotate((float)Math.toRadians(rY), new Vector3f(0, 1, 0), matrix, matrix);
|
||||||
|
Matrix4f.rotate((float)Math.toRadians(rZ), new Vector3f(0, 0, 1), matrix, matrix);
|
||||||
|
Matrix4f.scale(new Vector3f(scale, scale, scale), matrix, matrix);
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user