ShaderProgram.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package eu.tankernn.gameEngine.shaders;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.nio.FloatBuffer;
  7. import org.lwjgl.opengl.GL11;
  8. import org.lwjgl.opengl.GL20;
  9. import org.lwjgl.BufferUtils;
  10. import org.lwjgl.util.vector.Matrix4f;
  11. import org.lwjgl.util.vector.Vector2f;
  12. import org.lwjgl.util.vector.Vector3f;
  13. import org.lwjgl.util.vector.Vector4f;
  14. public abstract class ShaderProgram {
  15. private int programID;
  16. private int vertexShaderID;
  17. private int fragmentShaderID;
  18. private static FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
  19. public ShaderProgram(String vertexFile, String fragmentFile) {
  20. vertexShaderID = loadShader(vertexFile, GL20.GL_VERTEX_SHADER);
  21. fragmentShaderID = loadShader(fragmentFile, GL20.GL_FRAGMENT_SHADER);
  22. programID = GL20.glCreateProgram();
  23. GL20.glAttachShader(programID, vertexShaderID);
  24. GL20.glAttachShader(programID, fragmentShaderID);
  25. bindAttributes();
  26. GL20.glLinkProgram(programID);
  27. GL20.glValidateProgram(programID);
  28. getAllUniformLocations();
  29. }
  30. protected abstract void getAllUniformLocations();
  31. protected int getUniformLocation(String uniformName) {
  32. return GL20.glGetUniformLocation(programID, uniformName);
  33. }
  34. public void start() {
  35. GL20.glUseProgram(programID);
  36. }
  37. public void stop() {
  38. GL20.glUseProgram(0);
  39. }
  40. public void cleanUp() {
  41. stop();
  42. GL20.glDetachShader(programID, vertexShaderID);
  43. GL20.glDetachShader(programID, fragmentShaderID);
  44. GL20.glDeleteShader(vertexShaderID);
  45. GL20.glDeleteShader(fragmentShaderID);
  46. GL20.glDeleteProgram(programID);
  47. }
  48. protected abstract void bindAttributes();
  49. protected void bindAttribute(int attribute, String variableName) {
  50. GL20.glBindAttribLocation(programID, attribute, variableName);
  51. }
  52. protected void loadFloat(int location, float value) {
  53. GL20.glUniform1f(location, value);
  54. }
  55. protected void loadInt(int location, int value) {
  56. GL20.glUniform1i(location, value);
  57. }
  58. protected void loadVector(int location, Vector4f vector) {
  59. GL20.glUniform4f(location, vector.x, vector.y, vector.z, vector.w);
  60. }
  61. protected void loadVector(int location, Vector3f vector) {
  62. GL20.glUniform3f(location, vector.x, vector.y, vector.z);
  63. }
  64. protected void load2DVector(int location, Vector2f vector) {
  65. GL20.glUniform2f(location, vector.x, vector.y);
  66. }
  67. protected void loadBoolean(int location, boolean value) {
  68. float toLoad = 0;
  69. if (value)
  70. toLoad = 1;
  71. GL20.glUniform1f(location, toLoad);
  72. }
  73. protected void loadMatrix(int location, Matrix4f matrix) {
  74. matrix.store(matrixBuffer);
  75. matrixBuffer.flip();
  76. GL20.glUniformMatrix4(location, false, matrixBuffer);
  77. }
  78. private static int loadShader(String file, int type) {
  79. StringBuilder shaderSource = new StringBuilder();
  80. try {
  81. InputStream in = ShaderProgram.class.getResourceAsStream(file);
  82. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  83. String line;
  84. while ((line = reader.readLine()) != null) {
  85. shaderSource.append(line).append('\n');
  86. }
  87. reader.close();
  88. } catch (IOException | NullPointerException e) {
  89. System.err.println("Could not read file: " + file);
  90. e.printStackTrace();
  91. System.exit(-1);
  92. }
  93. int shaderID = GL20.glCreateShader(type);
  94. GL20.glShaderSource(shaderID, shaderSource);
  95. GL20.glCompileShader(shaderID);
  96. if (GL20.glGetShaderi(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
  97. System.out.println(GL20.glGetShaderInfoLog(shaderID, 500));
  98. System.err.println("Could not compile shader.");
  99. System.exit(-1);
  100. }
  101. return shaderID;
  102. }
  103. }