XUPWUP.NL
Login
Or create an account

ShaderProgram.java

0 Posted 21 Dec 2014
User avatar
Rick Hendricksen
Administrator
Posts: 2246
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import javax.media.opengl.GL2;
  6. import static javax.media.opengl.GL2ES2.*;
  7. /**
  8. *
  9. * @author Rick Hendricksen
  10. */
  11. public class ShaderProgram {
  12. public int program = 0;
  13. public ShaderProgram(String vert, String frag, GL2 gl) {
  14. int vertShader = genShader(vert, true, gl);
  15. int fragShader = genShader(frag, false, gl);
  16. if(vertShader == 0 || fragShader == 0){
  17. return;
  18. }
  19. program = gl.glCreateProgram();
  20. gl.glAttachShader(program, vertShader);
  21. gl.glAttachShader(program, fragShader);
  22. gl.glLinkProgram(program);
  23. int[] status = new int[1];
  24. gl.glGetProgramiv(program, GL_LINK_STATUS, status, 0);
  25. if(status[0] == GL_FALSE){
  26. System.out.println("Program linking failed.");
  27. int[] actualLength = new int[1];
  28. gl.glGetProgramiv(program, GL_INFO_LOG_LENGTH, actualLength, 0);
  29. byte[] buffer = new byte[actualLength[0]];
  30. int[] bytesWritten = new int[1];
  31. gl.glGetProgramInfoLog(program, buffer.length, bytesWritten, 0, buffer, 0);
  32. if(buffer.length != bytesWritten[0]){
  33. System.err.println("bytes written != bytes expected");
  34. }
  35. System.err.println(new String(buffer));
  36. gl.glDeleteProgram(program);
  37. program = 0;
  38. }
  39. }
  40. private int genShader(String code, boolean type, GL2 gl){
  41. int shader = gl.glCreateShader(type ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER);
  42. if(shader == 0){
  43. System.out.println("shader creation failed");
  44. }
  45. gl.glShaderSource(shader, 1, new String[]{code}, new int[]{code.length()}, 0);
  46. gl.glCompileShader(shader);
  47. int[] status = new int[1];
  48. gl.glGetShaderiv(shader, GL_COMPILE_STATUS, status, 0);
  49. if(status[0] == GL_FALSE){
  50. System.out.println((type ? "Vertex" : "Fragment") + " shader compilation failed.");
  51. int[] actualLength = new int[1];
  52. gl.glGetShaderiv(shader, GL_INFO_LOG_LENGTH, actualLength, 0);
  53. byte[] buffer = new byte[actualLength[0]];
  54. int[] bytesWritten = new int[1];
  55. gl.glGetShaderInfoLog(shader, buffer.length, bytesWritten, 0, buffer, 0);
  56. if(buffer.length != bytesWritten[0] + 1){
  57. System.err.println("bytes written != bytes expected");
  58. System.out.println(buffer.length);
  59. System.out.println(bytesWritten[0]);
  60. }
  61. System.err.println(new String(buffer, 0, buffer.length - 1));
  62. gl.glDeleteShader(shader);
  63. shader = 0;
  64. }
  65. return shader;
  66. }
  67. public static ShaderProgram fromStream(InputStream vert, InputStream frag, GL2 gl) throws IOException{
  68. BufferedReader brV = new BufferedReader(new InputStreamReader(vert));
  69. BufferedReader brF = new BufferedReader(new InputStreamReader(frag));
  70. StringBuilder sbV = new StringBuilder();
  71. StringBuilder sbF = new StringBuilder();
  72. String line;
  73. while((line = brV.readLine()) != null){
  74. sbV.append(line + "\n");
  75. }
  76. while((line = brF.readLine()) != null){
  77. sbF.append(line + "\n");
  78. }
  79. return new ShaderProgram(sbV.toString(), sbF.toString(), gl);
  80. }
  81. public void enable(GL2 gl){
  82. gl.glUseProgram(program);
  83. }
  84. public static void disable(GL2 gl){
  85. gl.glUseProgram(0);
  86. }
  87. public void delete(GL2 gl){
  88. gl.glDeleteProgram(program);
  89. }
  90. }
© Rick Hendricksen