Guest User

Untitled

a guest
Mar 25th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.FileInputStream;
  4. import java.io.InputStreamReader;
  5.  
  6. import org.lwjgl.opengl.ARBFragmentShader;
  7. import org.lwjgl.opengl.ARBShaderObjects;
  8. import org.lwjgl.opengl.ARBVertexShader;
  9. import org.lwjgl.opengl.GL11;
  10.  
  11. /**
  12. * The vertex and fragment shaders are setup when the box object is
  13. * constructed. They are applied to the GL state prior to the box
  14. * being drawn, and released from that state after drawing.
  15. * @author Stephen Jones
  16. */
  17. public class Box {
  18.  
  19. /*
  20. * if the shaders are setup ok we can use shaders, otherwise we just
  21. * use default settings
  22. */
  23. private boolean useShader;
  24.  
  25. /*
  26. * program shader, to which is attached a vertex and fragment shaders.
  27. * They are set to 0 as a check because GL will assign unique int
  28. * values to each
  29. */
  30. private int program=0;
  31.  
  32. public Box(){
  33. int vertShader = 0, fragShader = 0;
  34.  
  35. try {
  36. vertShader = createShader("res/screen.vert",ARBVertexShader.GL_VERTEX_SHADER_ARB);
  37. fragShader = createShader("res/screen.frag",ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
  38. }
  39. catch(Exception exc) {
  40. exc.printStackTrace();
  41. return;
  42. }
  43. finally {
  44. if(vertShader == 0 || fragShader == 0)
  45. return;
  46. }
  47.  
  48. program = ARBShaderObjects.glCreateProgramObjectARB();
  49.  
  50. if(program == 0)
  51. return;
  52.  
  53. /*
  54. * if the vertex and fragment shaders setup sucessfully,
  55. * attach them to the shader program, link the sahder program
  56. * (into the GL context I suppose), and validate
  57. */
  58. ARBShaderObjects.glAttachObjectARB(program, vertShader);
  59. ARBShaderObjects.glAttachObjectARB(program, fragShader);
  60.  
  61. ARBShaderObjects.glLinkProgramARB(program);
  62. if (ARBShaderObjects.glGetObjectParameteriARB(program, ARBShaderObjects.GL_OBJECT_LINK_STATUS_ARB) == GL11.GL_FALSE) {
  63. System.err.println(getLogInfo(program));
  64. return;
  65. }
  66.  
  67. ARBShaderObjects.glValidateProgramARB(program);
  68. if (ARBShaderObjects.glGetObjectParameteriARB(program, ARBShaderObjects.GL_OBJECT_VALIDATE_STATUS_ARB) == GL11.GL_FALSE) {
  69. System.err.println(getLogInfo(program));
  70. return;
  71. }
  72.  
  73. useShader = true;
  74. }
  75.  
  76. /*
  77. * If the shader was setup succesfully, we use the shader. Otherwise
  78. * we run normal drawing code.
  79. */
  80. public void draw(){
  81. if(useShader)
  82. ARBShaderObjects.glUseProgramObjectARB(program);
  83.  
  84. GL11.glLoadIdentity();
  85. GL11.glTranslatef(0.0f, 0.0f, -10.0f);
  86. GL11.glColor3f(1.0f, 1.0f, 1.0f);//white
  87.  
  88. GL11.glBegin(GL11.GL_QUADS);
  89. GL11.glVertex3f(-1.0f, 1.0f, 0.0f);
  90. GL11.glVertex3f(1.0f, 1.0f, 0.0f);
  91. GL11.glVertex3f(1.0f, -1.0f, 0.0f);
  92. GL11.glVertex3f(-1.0f, -1.0f, 0.0f);
  93. GL11.glEnd();
  94.  
  95. //release the shader
  96. if(useShader)
  97. ARBShaderObjects.glUseProgramObjectARB(0);
  98.  
  99. }
  100.  
  101. /*
  102. * With the exception of syntax, setting up vertex and fragment shaders
  103. * is the same.
  104. * @param the name and path to the vertex shader
  105. */
  106. private int createShader(String filename, int shaderType) throws Exception {
  107. int shader = 0;
  108. try {
  109. shader = ARBShaderObjects.glCreateShaderObjectARB(shaderType);
  110.  
  111. if(shader == 0)
  112. return 0;
  113.  
  114. ARBShaderObjects.glShaderSourceARB(shader, readFileAsString(filename));
  115. ARBShaderObjects.glCompileShaderARB(shader);
  116.  
  117. if (ARBShaderObjects.glGetObjectParameteriARB(shader, ARBShaderObjects.GL_OBJECT_COMPILE_STATUS_ARB) == GL11.GL_FALSE)
  118. throw new RuntimeException("Error creating shader: " + getLogInfo(shader));
  119.  
  120. return shader;
  121. }
  122. catch(Exception exc) {
  123. ARBShaderObjects.glDeleteObjectARB(shader);
  124. throw exc;
  125. }
  126. }
  127.  
  128. private static String getLogInfo(int obj) {
  129. return ARBShaderObjects.glGetInfoLogARB(obj, ARBShaderObjects.glGetObjectParameteriARB(obj, ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB));
  130. }
  131.  
  132. private String readFileAsString(String filename) throws Exception {
  133. StringBuilder source = new StringBuilder();
  134.  
  135. FileInputStream in = new FileInputStream(filename);
  136.  
  137. Exception exception = null;
  138.  
  139. BufferedReader reader;
  140. try{
  141. reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
  142.  
  143. Exception innerExc= null;
  144. try {
  145. String line;
  146. while((line = reader.readLine()) != null)
  147. source.append(line).append('\n');
  148. }
  149. catch(Exception exc) {
  150. exception = exc;
  151. }
  152. finally {
  153. try {
  154. reader.close();
  155. }
  156. catch(Exception exc) {
  157. if(innerExc == null)
  158. innerExc = exc;
  159. else
  160. exc.printStackTrace();
  161. }
  162. }
  163.  
  164. if(innerExc != null)
  165. throw innerExc;
  166. }
  167. catch(Exception exc) {
  168. exception = exc;
  169. }
  170. finally {
  171. try {
  172. in.close();
  173. }
  174. catch(Exception exc) {
  175. if(exception == null)
  176. exception = exc;
  177. else
  178. exc.printStackTrace();
  179. }
  180.  
  181. if(exception != null)
  182. throw exception;
  183. }
  184.  
  185. return source.toString();
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment