uniforms!
This commit is contained in:
parent
fca84badb8
commit
7288a7ee99
|
@ -29,6 +29,8 @@ public class Renderer {
|
|||
|
||||
shader.bind();
|
||||
|
||||
shader.setUniform("scale", 2.0F);
|
||||
|
||||
GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_UNSIGNED_INT, 0);
|
||||
|
||||
shader.unbind();
|
||||
|
|
|
@ -1,8 +1,15 @@
|
|||
package org.hl.engine.graphics;
|
||||
|
||||
import org.hl.engine.math.lalg.Matrix4f;
|
||||
import org.hl.engine.math.lalg.Vector2f;
|
||||
import org.hl.engine.math.lalg.Vector3f;
|
||||
import org.hl.engine.math.lalg.Vector4f;
|
||||
import org.hl.engine.utils.FileUtils;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
import org.lwjgl.system.MemoryUtil;
|
||||
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
public class Shader {
|
||||
private String vertexFile;
|
||||
|
@ -66,9 +73,40 @@ public class Shader {
|
|||
return;
|
||||
}
|
||||
|
||||
GL20.glDeleteShader(vertexID);
|
||||
GL20.glDeleteShader(fragmentID);
|
||||
}
|
||||
|
||||
public int getUniformLocation(String name) {
|
||||
return GL20.glGetUniformLocation(programID, name);
|
||||
}
|
||||
|
||||
public void setUniform(String name, float value) {
|
||||
GL20.glUniform1f(getUniformLocation(name), value);
|
||||
}
|
||||
|
||||
public void setUniform(String name, int value) {
|
||||
GL20.glUniform1i(getUniformLocation(name), value);
|
||||
}
|
||||
|
||||
public void setUniform(String name, boolean value) {
|
||||
GL20.glUniform1i(getUniformLocation(name), value ? 1 : 0);
|
||||
}
|
||||
public void setUniform(String name, Vector2f value) {
|
||||
GL20.glUniform2f(getUniformLocation(name), value.getX(), value.getY());
|
||||
|
||||
}
|
||||
|
||||
public void setUniform(String name, Vector3f value) {
|
||||
GL20.glUniform3f(getUniformLocation(name), value.getX(), value.getY(), value.getZ());
|
||||
}
|
||||
|
||||
public void setUniform(String name, Vector4f value) {
|
||||
GL20.glUniform4f(getUniformLocation(name), value.getX(), value.getY(), value.getZ(), value.getA());
|
||||
}
|
||||
|
||||
public void setUniform(String name, Matrix4f value) {
|
||||
FloatBuffer matrix = MemoryUtil.memAllocFloat(Matrix4f.SIZE * Matrix4f.SIZE);
|
||||
matrix.put(value.convertTo1D()).flip();
|
||||
GL20.glUniformMatrix4fv(getUniformLocation(name), true, matrix);
|
||||
}
|
||||
|
||||
// Bind so we can use the shader
|
||||
|
@ -83,6 +121,12 @@ public class Shader {
|
|||
|
||||
// Destroy the program
|
||||
public void destroy() {
|
||||
GL20.glDetachShader(programID, vertexID);
|
||||
GL20.glDetachShader(programID, fragmentID);
|
||||
|
||||
GL20.glDeleteShader(vertexID);
|
||||
GL20.glDeleteShader(fragmentID);
|
||||
|
||||
GL20.glDeleteProgram(programID);
|
||||
}
|
||||
}
|
||||
|
|
26
src/org/hl/engine/math/lalg/Matrix4f.java
Normal file
26
src/org/hl/engine/math/lalg/Matrix4f.java
Normal file
|
@ -0,0 +1,26 @@
|
|||
package org.hl.engine.math.lalg;
|
||||
|
||||
public class Matrix4f {
|
||||
public static final int SIZE = 4;
|
||||
private float[][] elements;
|
||||
public float get(int x, int y) {
|
||||
return elements[x][y];
|
||||
}
|
||||
public void set(int x, int y, float value) {
|
||||
elements[x][y] = value;
|
||||
}
|
||||
public float[][] getAll() {
|
||||
return elements;
|
||||
}
|
||||
|
||||
public float[] convertTo1D() {
|
||||
float[] returnedArray = new float[SIZE*SIZE];
|
||||
int sizeOfRow = elements[0].length;
|
||||
for (int i = 0; i < elements.length; i ++) {
|
||||
for (int j = 0; j < sizeOfRow; j ++) {
|
||||
returnedArray[i*SIZE + j] = elements[i][j];
|
||||
}
|
||||
}
|
||||
return returnedArray;
|
||||
}
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
#version 410 core
|
||||
|
||||
in vec3 passColor;
|
||||
in vec2 passTextureCoord;
|
||||
|
||||
out vec4 outColor;
|
||||
|
||||
layout(location = 0) in vec3 passColor;
|
||||
layout(location = 1) in vec2 passTextureCoord;
|
||||
|
||||
layout(location = 0) out vec4 outColor;
|
||||
uniform sampler2D tex;
|
||||
|
||||
void main() {
|
||||
outColor = vec4(passColor, 1.0);
|
||||
outColor = texture(tex, passTextureCoord);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
#version 410 core
|
||||
|
||||
|
||||
|
||||
layout(location = 0) in vec3 position;
|
||||
layout(location = 1) in vec3 color;
|
||||
layout(location = 2) in vec2 textureCoord;
|
||||
|
@ -12,10 +10,12 @@ layout(location = 2) in vec2 textureCoord;
|
|||
layout(location = 0) out vec3 passColor;
|
||||
layout(location = 1) out vec2 passTextureCoord;
|
||||
|
||||
uniform float scale;
|
||||
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(position, 1.0);
|
||||
passColor = color;
|
||||
gl_Position = vec4(position, 1.0) * vec4(scale, scale, scale, 1);
|
||||
passColor = color;
|
||||
|
||||
passTextureCoord = textureCoord;
|
||||
passTextureCoord = textureCoord;
|
||||
}
|
Reference in New Issue
Block a user