|
@@ -13,37 +13,62 @@ import static org.lwjgl.opengl.GL20.*;
|
|
public final class Shader
|
|
public final class Shader
|
|
{
|
|
{
|
|
private static int program = -1;
|
|
private static int program = -1;
|
|
- private static String[][] lightStrings;
|
|
|
|
|
|
|
|
private static final List<Runnable> TASKS = new LinkedList<>();
|
|
private static final List<Runnable> TASKS = new LinkedList<>();
|
|
|
|
|
|
protected static boolean initDone = false;
|
|
protected static boolean initDone = false;
|
|
|
|
|
|
|
|
+ // uniform stuff
|
|
|
|
+ private static int unifViewMatrix = -1;
|
|
|
|
+ private static int unifModelMatrix = -1;
|
|
|
|
+ private static MatrixStack modelMatrix;
|
|
|
|
+
|
|
|
|
+ private static int unifDepth = -1;
|
|
|
|
+
|
|
|
|
+ private static int unifAmbientLight = -1;
|
|
|
|
+ private static int[][] unifLight;
|
|
|
|
+
|
|
|
|
+ private static int unifUseTexture = -1;
|
|
|
|
+ private static int unifUseColor = -1;
|
|
|
|
+ private static int unifUseLight = -1;
|
|
|
|
+
|
|
protected static void init()
|
|
protected static void init()
|
|
{
|
|
{
|
|
program = createShaderProgram("shaders/vertex.vs", "shaders/fragment.fs");
|
|
program = createShaderProgram("shaders/vertex.vs", "shaders/fragment.fs");
|
|
glUseProgram(program);
|
|
glUseProgram(program);
|
|
|
|
|
|
- sendMatrix();
|
|
|
|
- setCamera(0.0f, 0.0f);
|
|
|
|
|
|
+ unifViewMatrix = glGetUniformLocation(program, "viewMatrix");
|
|
|
|
+ updateViewMatrix();
|
|
|
|
+
|
|
|
|
+ unifModelMatrix = glGetUniformLocation(program, "modelMatrix");
|
|
|
|
+ modelMatrix = new MatrixStack(20);
|
|
|
|
+ updateMatrix();
|
|
|
|
+
|
|
|
|
+ unifDepth = glGetUniformLocation(program, "depth");
|
|
setDepth(0.0f);
|
|
setDepth(0.0f);
|
|
|
|
+
|
|
|
|
+ unifAmbientLight = glGetUniformLocation(program, "ambientLight");
|
|
setAmbientLight(1.0f, 1.0f, 1.0f);
|
|
setAmbientLight(1.0f, 1.0f, 1.0f);
|
|
|
|
|
|
- lightStrings = new String[32][3];
|
|
|
|
- for(int index = 0; index < lightStrings.length; index++)
|
|
|
|
|
|
+ unifLight = new int[32][3];
|
|
|
|
+ for(int index = 0; index < unifLight.length; index++)
|
|
{
|
|
{
|
|
- lightStrings[index][0] = "lights[" + index + "].color";
|
|
|
|
- lightStrings[index][1] = "lights[" + index + "].pos";
|
|
|
|
- lightStrings[index][2] = "lights[" + index + "].strength";
|
|
|
|
-
|
|
|
|
|
|
+ unifLight[index][0] = glGetUniformLocation(program, "lights[" + index + "].color");
|
|
|
|
+ unifLight[index][1] = glGetUniformLocation(program, "lights[" + index + "].pos");
|
|
|
|
+ unifLight[index][2] = glGetUniformLocation(program, "lights[" + index + "].strength");
|
|
setLightColor(index, 0.0f, 0.0f, 0.0f);
|
|
setLightColor(index, 0.0f, 0.0f, 0.0f);
|
|
setLightLocation(index, 0.0f, 0.0f);
|
|
setLightLocation(index, 0.0f, 0.0f);
|
|
setLightStrength(index, 0.0f);
|
|
setLightStrength(index, 0.0f);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ unifUseTexture = glGetUniformLocation(program, "useTexture");
|
|
setTextureUsing(false);
|
|
setTextureUsing(false);
|
|
|
|
+
|
|
|
|
+ unifUseColor = glGetUniformLocation(program, "useColor");
|
|
|
|
+ setColorUsing(false);
|
|
|
|
+
|
|
|
|
+ unifUseLight = glGetUniformLocation(program, "useLight");
|
|
setLightUsing(false);
|
|
setLightUsing(false);
|
|
- setCameraUsing(false);
|
|
|
|
|
|
|
|
initDone = true;
|
|
initDone = true;
|
|
}
|
|
}
|
|
@@ -62,7 +87,7 @@ public final class Shader
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private static void sendMatrix()
|
|
|
|
|
|
+ private static void updateViewMatrix()
|
|
{
|
|
{
|
|
FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
|
|
FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
|
|
|
|
|
|
@@ -88,27 +113,57 @@ public final class Shader
|
|
|
|
|
|
buffer.flip();
|
|
buffer.flip();
|
|
|
|
|
|
- glUniformMatrix4fv(glGetUniformLocation(program, "matrix"), false, buffer);
|
|
|
|
|
|
+ glUniformMatrix4fv(unifViewMatrix, false, buffer);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void updateMatrix()
|
|
|
|
+ {
|
|
|
|
+ glUniformMatrix4fv(unifModelMatrix, false, modelMatrix.getData());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void pushMatrix()
|
|
|
|
+ {
|
|
|
|
+ modelMatrix.push();
|
|
}
|
|
}
|
|
|
|
|
|
- public static void setCamera(float x, float y)
|
|
|
|
|
|
+ public static void popMatrix()
|
|
{
|
|
{
|
|
- glUniform2f(glGetUniformLocation(program, "camera"), x, y);
|
|
|
|
|
|
+ modelMatrix.pop();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void translate(float tx, float ty)
|
|
|
|
+ {
|
|
|
|
+ modelMatrix.translate(tx, ty);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void translateTo(float tx, float ty)
|
|
|
|
+ {
|
|
|
|
+ modelMatrix.translateTo(tx, ty);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void scale(float sx, float sy)
|
|
|
|
+ {
|
|
|
|
+ modelMatrix.scale(sx, sy);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void rotate(float angle)
|
|
|
|
+ {
|
|
|
|
+ modelMatrix.rotate(angle);
|
|
}
|
|
}
|
|
|
|
|
|
public static void setDepth(float depth)
|
|
public static void setDepth(float depth)
|
|
{
|
|
{
|
|
- glUniform1f(glGetUniformLocation(program, "depth"), depth);
|
|
|
|
|
|
+ glUniform1f(unifDepth, depth);
|
|
}
|
|
}
|
|
|
|
|
|
public static void setAmbientLight(float r, float g, float b)
|
|
public static void setAmbientLight(float r, float g, float b)
|
|
{
|
|
{
|
|
- glUniform3f(glGetUniformLocation(program, "ambientLight"), r, g, b);
|
|
|
|
|
|
+ glUniform3f(unifAmbientLight, r, g, b);
|
|
}
|
|
}
|
|
|
|
|
|
private static void checkLightIndex(int index)
|
|
private static void checkLightIndex(int index)
|
|
{
|
|
{
|
|
- if(index < 0 || index > lightStrings.length)
|
|
|
|
|
|
+ if(index < 0 || index > unifLight.length)
|
|
{
|
|
{
|
|
throw new ShaderException("'" + index + "' is not a valid light index");
|
|
throw new ShaderException("'" + index + "' is not a valid light index");
|
|
}
|
|
}
|
|
@@ -117,34 +172,34 @@ public final class Shader
|
|
public static void setLightColor(int index, float r, float g, float b)
|
|
public static void setLightColor(int index, float r, float g, float b)
|
|
{
|
|
{
|
|
checkLightIndex(index);
|
|
checkLightIndex(index);
|
|
- glUniform3f(glGetUniformLocation(program, lightStrings[index][0]), r, g, b);
|
|
|
|
|
|
+ glUniform3f(unifLight[index][0], r, g, b);
|
|
}
|
|
}
|
|
|
|
|
|
public static void setLightLocation(int index, float x, float y)
|
|
public static void setLightLocation(int index, float x, float y)
|
|
{
|
|
{
|
|
checkLightIndex(index);
|
|
checkLightIndex(index);
|
|
- glUniform2f(glGetUniformLocation(program, lightStrings[index][1]), x, y);
|
|
|
|
|
|
+ glUniform2f(unifLight[index][1], x, y);
|
|
}
|
|
}
|
|
|
|
|
|
public static void setLightStrength(int index, float strength)
|
|
public static void setLightStrength(int index, float strength)
|
|
{
|
|
{
|
|
checkLightIndex(index);
|
|
checkLightIndex(index);
|
|
- glUniform1f(glGetUniformLocation(program, lightStrings[index][2]), strength);
|
|
|
|
|
|
+ glUniform1f(unifLight[index][2], strength);
|
|
}
|
|
}
|
|
|
|
|
|
public static void setTextureUsing(boolean use)
|
|
public static void setTextureUsing(boolean use)
|
|
{
|
|
{
|
|
- glUniform1i(glGetUniformLocation(program, "useTexture"), use ? 1 : 0);
|
|
|
|
|
|
+ glUniform1i(unifUseTexture, use ? 1 : 0);
|
|
}
|
|
}
|
|
|
|
|
|
- public static void setLightUsing(boolean use)
|
|
|
|
|
|
+ public static void setColorUsing(boolean use)
|
|
{
|
|
{
|
|
- glUniform1i(glGetUniformLocation(program, "useLight"), use ? 1 : 0);
|
|
|
|
|
|
+ glUniform1i(unifUseColor, use ? 1 : 0);
|
|
}
|
|
}
|
|
|
|
|
|
- public static void setCameraUsing(boolean use)
|
|
|
|
|
|
+ public static void setLightUsing(boolean use)
|
|
{
|
|
{
|
|
- glUniform1i(glGetUniformLocation(program, "useCameraOffset"), use ? 1 : 0);
|
|
|
|
|
|
+ glUniform1i(unifUseLight, use ? 1 : 0);
|
|
}
|
|
}
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
// -------------------------------------------------------------------------
|
|
@@ -245,7 +300,7 @@ public final class Shader
|
|
compiled = glGetProgrami(vfprogram, GL_LINK_STATUS);
|
|
compiled = glGetProgrami(vfprogram, GL_LINK_STATUS);
|
|
if(compiled != 1)
|
|
if(compiled != 1)
|
|
{
|
|
{
|
|
- throw new ShaderException("failed linking shaders '" + vertex + "' and '" + fragment + "' " + compiled);
|
|
|
|
|
|
+ throw new ShaderException("failed linking shaders '" + vertex + "' and '" + fragment + "' " + compiled + " " + glGetProgramInfoLog(vfprogram));
|
|
}
|
|
}
|
|
|
|
|
|
glDeleteShader(vShader);
|
|
glDeleteShader(vShader);
|