Advertisement
Guest User

a

a guest
Jul 27th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. public class GL3 {
  2. // Entry point for the application
  3. public static void main(String[] args) {
  4. new GL3();
  5. }
  6.  
  7. // Setup variables
  8. private final String WINDOW_TITLE = "The Quad: Update VBO";
  9. private final int WIDTH = 320;
  10. private final int HEIGHT = 320;
  11.  
  12.  
  13. Quad rect, square;
  14.  
  15.  
  16. public GL3() {
  17. setupOpenGL();
  18. rect = new Quad(true, 0);
  19. square = new Quad(false, 1);
  20.  
  21. while (!Display.isCloseRequested()) {
  22. // Do a single loop (logic/render)
  23. loopCycle();
  24.  
  25. // Force a maximum FPS of about 60
  26. Display.sync(60);
  27. // Let the CPU synchronize with the GPU if GPU is tagging behind
  28. Display.update();
  29. }
  30.  
  31. // Destroy OpenGL (Display)
  32. this.destroyOpenGL();
  33. }
  34.  
  35.  
  36.  
  37. private void setupOpenGL() {
  38. // Setup an OpenGL context with API version 3.2
  39. try {
  40. PixelFormat pixelFormat = new PixelFormat();
  41. ContextAttribs contextAtrributes = new ContextAttribs(3, 2)
  42. .withForwardCompatible(true)
  43. .withProfileCore(true);
  44.  
  45. Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
  46. Display.setTitle(WINDOW_TITLE);
  47. Display.create(pixelFormat, contextAtrributes);
  48.  
  49. GL11.glViewport(0, 0, WIDTH, HEIGHT);
  50. } catch (LWJGLException e) {
  51. e.printStackTrace();
  52. System.exit(-1);
  53. }
  54.  
  55. // Setup an XNA like background color
  56. GL11.glClearColor(0.4f, 0.6f, 0.9f, 0f);
  57.  
  58. // Map the internal OpenGL coordinate system to the entire screen
  59. GL11.glViewport(0, 0, WIDTH, HEIGHT);
  60.  
  61. this.exitOnGLError("setupOpenGL");
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68. private void loopCycle() {
  69. rect.update();
  70. square.update();
  71. rect.render();
  72.  
  73.  
  74. square.render();
  75.  
  76. this.exitOnGLError("loopCycle");
  77. }
  78.  
  79. private void destroyOpenGL() {
  80.  
  81.  
  82. this.exitOnGLError("destroyOpenGL");
  83. rect.destroy();
  84. square.destroy();
  85. Display.destroy();
  86. }
  87.  
  88. private int loadShader(String filename, int type) {
  89. StringBuilder shaderSource = new StringBuilder();
  90. int shaderID = 0;
  91.  
  92. try {
  93. BufferedReader reader = new BufferedReader(new FileReader(filename));
  94. String line;
  95. while ((line = reader.readLine()) != null) {
  96. shaderSource.append(line).append("\n");
  97. }
  98. reader.close();
  99. } catch (IOException e) {
  100. System.err.println("Could not read file.");
  101. e.printStackTrace();
  102. System.exit(-1);
  103. }
  104.  
  105. shaderID = GL20.glCreateShader(type);
  106. GL20.glShaderSource(shaderID, shaderSource);
  107. GL20.glCompileShader(shaderID);
  108.  
  109. if (GL20.glGetShader(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
  110. System.err.println("Could not compile shader.");
  111. System.exit(-1);
  112. }
  113.  
  114. this.exitOnGLError("loadShader");
  115.  
  116. return shaderID;
  117. }
  118.  
  119. private int loadPNGTexture(String filename, int textureUnit) {
  120. ByteBuffer buf = null;
  121. int tWidth = 0;
  122. int tHeight = 0;
  123.  
  124. try {
  125. // Open the PNG file as an InputStream
  126. InputStream in = new FileInputStream(filename);
  127. // Link the PNG decoder to this stream
  128. PNGDecoder decoder = new PNGDecoder(in);
  129.  
  130. // Get the width and height of the texture
  131. tWidth = decoder.getWidth();
  132. tHeight = decoder.getHeight();
  133.  
  134.  
  135. // Decode the PNG file in a ByteBuffer
  136. buf = ByteBuffer.allocateDirect(
  137. 4 * decoder.getWidth() * decoder.getHeight());
  138. decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
  139. buf.flip();
  140.  
  141. in.close();
  142. } catch (IOException e) {
  143. e.printStackTrace();
  144. System.exit(-1);
  145. }
  146.  
  147. // Create a new texture object in memory and bind it
  148. int texId = GL11.glGenTextures();
  149. GL13.glActiveTexture(textureUnit);
  150. GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
  151.  
  152. // All RGB bytes are aligned to each other and each component is 1 byte
  153. GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
  154.  
  155. // Upload the texture data and generate mip maps (for scaling)
  156. GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tWidth, tHeight, 0,
  157. GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
  158. GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
  159.  
  160. // Setup the ST coordinate system
  161. GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
  162. GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
  163.  
  164. // Setup what to do when the texture has to be scaled
  165. GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER,
  166. GL11.GL_LINEAR);
  167. GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,
  168. GL11.GL_LINEAR_MIPMAP_LINEAR);
  169.  
  170. this.exitOnGLError("loadPNGTexture");
  171.  
  172. return texId;
  173. }
  174.  
  175. private void exitOnGLError(String errorMessage) {
  176. int errorValue = GL11.glGetError();
  177.  
  178. if (errorValue != GL11.GL_NO_ERROR) {
  179. String errorString = GLU.gluErrorString(errorValue);
  180. System.err.println("ERROR - " + errorMessage + ": " + errorString);
  181.  
  182. if (Display.isCreated()) Display.destroy();
  183. System.exit(-1);
  184. }
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement