Guest User

Untitled

a guest
Oct 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. package com.exoterragame.client;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.nio.FloatBuffer;
  6. import java.nio.IntBuffer;
  7.  
  8. import org.lwjgl.BufferUtils;
  9. import org.lwjgl.LWJGLException;
  10. import org.lwjgl.opengl.Display;
  11. import org.lwjgl.opengl.DisplayMode;
  12. import org.lwjgl.opengl.GL11;
  13. import org.lwjgl.opengl.GL13;
  14. import org.lwjgl.opengl.GL15;
  15. import org.lwjgl.opengl.GL20;
  16. import org.lwjgl.opengl.GL21;
  17. import org.newdawn.slick.opengl.Texture;
  18. import org.newdawn.slick.opengl.TextureLoader;
  19. import org.newdawn.slick.util.ResourceLoader;
  20.  
  21. import com.exoterragame.client.util.FileUtility;
  22.  
  23. public class Main {
  24.  
  25. public void start() {
  26. try {
  27. Display.setDisplayMode(new DisplayMode(800, 600));
  28. Display.create();
  29. } catch (LWJGLException e) {
  30. e.printStackTrace();
  31. System.exit(0);
  32. }
  33.  
  34.  
  35. GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  36.  
  37. FloatBuffer vert_b = BufferUtils.createFloatBuffer(8);
  38. IntBuffer idx_b = BufferUtils.createIntBuffer(4);
  39. float[] verts = {
  40. -1.0f, -1.0f,
  41. 1.0f, -1.0f,
  42. -1.0f, 1.0f,
  43. 1.0f, 1.0f
  44. };
  45. int[] idx = {
  46. 0, 1, 2, 3
  47. };
  48.  
  49. vert_b.put(verts).rewind();
  50. idx_b.put(idx).rewind();
  51.  
  52. int buffer_id = GL15.glGenBuffers();
  53. GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffer_id);
  54. GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vert_b, GL15.GL_STATIC_DRAW);
  55.  
  56. int idx_buffer_id = GL15.glGenBuffers();
  57. GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, idx_buffer_id);
  58. GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, idx_b, GL15.GL_STATIC_DRAW);
  59.  
  60. int vert_shader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
  61. String vert_source = "";
  62. try {
  63. vert_source = FileUtility.loadTextFile("res/shaders/default.v.glsl");
  64. } catch (FileNotFoundException e) {
  65. e.printStackTrace();
  66. System.exit(0);
  67. }
  68. GL20.glShaderSource(vert_shader, vert_source);
  69. GL20.glCompileShader(vert_shader);
  70. if (GL20.glGetShader(vert_shader, GL20.GL_COMPILE_STATUS) == 0) {
  71. System.exit(0);
  72. }
  73.  
  74. int frag_shader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
  75. String frag_source = "";
  76. try {
  77. frag_source = FileUtility.loadTextFile("res/shaders/default.f.glsl");
  78. } catch (FileNotFoundException e) {
  79. e.printStackTrace();
  80. System.exit(0);
  81. }
  82. GL20.glShaderSource(frag_shader, frag_source);
  83. GL20.glCompileShader(frag_shader);
  84. if (GL20.glGetShader(frag_shader, GL20.GL_COMPILE_STATUS) == 0) {
  85. System.exit(0);
  86. }
  87.  
  88. int program = GL20.glCreateProgram();
  89. GL20.glAttachShader(program, vert_shader);
  90. GL20.glAttachShader(program, frag_shader);
  91. GL20.glLinkProgram(program);
  92. if (GL20.glGetProgram(program, GL20.GL_LINK_STATUS) == 0) {
  93. System.exit(0);
  94. }
  95.  
  96. int tex_uniform = GL20.glGetUniformLocation(program, "texture");
  97. int pos_attribute = GL20.glGetAttribLocation(program, "position");
  98.  
  99. Texture texture = null;
  100. try {
  101. texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/tex0.png"));
  102. } catch (IOException e) {
  103. // TODO Auto-generated catch block
  104. e.printStackTrace();
  105. }
  106.  
  107. while (!Display.isCloseRequested()) {
  108. GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
  109.  
  110. GL20.glUseProgram(program);
  111.  
  112. if (texture != null) {
  113. GL13.glActiveTexture(GL13.GL_TEXTURE0);
  114. texture.bind();
  115. GL20.glUniform1i(tex_uniform, 0);
  116. }
  117.  
  118. GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffer_id);
  119. GL20.glEnableVertexAttribArray(pos_attribute);
  120. GL20.glVertexAttribPointer(pos_attribute, 2, false, Float.SIZE * 2, vert_b);
  121. GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, idx_buffer_id);
  122.  
  123. GL11.glDrawElements(GL11.GL_TRIANGLE_STRIP, idx_b);
  124.  
  125. GL20.glDisableVertexAttribArray(pos_attribute);
  126. GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
  127. GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
  128.  
  129. Display.update();
  130. }
  131. GL15.glDeleteBuffers(buffer_id);
  132. GL15.glDeleteBuffers(idx_buffer_id);
  133. }
  134.  
  135. public static void main(String[] args) {
  136. Main main = new Main();
  137. main.start();
  138. }
  139.  
  140. }
Add Comment
Please, Sign In to add comment