Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
GameNoodle/src/game/graphics/Shader.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
91 lines (72 sloc)
1.88 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package game.graphics; | |
import static org.lwjgl.opengl.GL20.*; | |
import java.util.HashMap; | |
import java.util.Map; | |
import game.math.Matrix4f; | |
import game.math.Vector3f; | |
import game.utils.ShaderUtils; | |
public class Shader { | |
public static final int VERTEX_ATTRIB = 0; | |
public static final int TCOORD_ATTRIB = 1; | |
public static Shader SMOOTH_FLOOR; | |
private boolean enabled = false; | |
private final int ID; | |
private Map<String, Integer> locationCache = new HashMap<String, Integer>(); | |
public Shader(String vertex, String fragment) { | |
ID = ShaderUtils.load(vertex, fragment); | |
} | |
public static void loadAll() { | |
SMOOTH_FLOOR = new Shader("shaders/SmoothFloor.vert", | |
"shaders/SmoothFloor.frag"); | |
} | |
public int getUniform(String name) { | |
if (locationCache.containsKey(name)) { | |
return locationCache.get(name); | |
} | |
int result = glGetUniformLocation(ID, name); | |
if (result == -1) { | |
System.err.println("Could not find uniform variable '" + name + "'!"); | |
} else { | |
locationCache.put(name, result); | |
} | |
return result; | |
} | |
public void setUniform1i(String name, int value) { | |
if (!enabled) { | |
enable(); | |
} | |
glUniform1i(getUniform(name), value); | |
} | |
public void setUniform1f(String name, float value) { | |
if (!enabled) { | |
enable(); | |
} | |
glUniform1f(getUniform(name), value); | |
} | |
public void setUniform2f(String name, float x, float y) { | |
if (!enabled) { | |
enable(); | |
} | |
glUniform2f(getUniform(name), x, y); | |
} | |
public void setUniform3f(String name, Vector3f vector) { | |
if (!enabled) { | |
enable(); | |
} | |
glUniform3f(getUniform(name), vector.x, vector.y, vector.z); | |
} | |
public void setUniformMat4f(String name, Matrix4f matrix) { | |
if (!enabled) { | |
enable(); | |
} | |
glUniformMatrix4fv(getUniform(name), false, matrix.toFloatBuffer()); | |
} | |
public void enable() { | |
glUseProgram(ID); | |
enabled = true; | |
} | |
public void disable() { | |
glUseProgram(0); | |
enabled = false; | |
} | |
} |