Advertisement
Guest User

b

a guest
Jul 27th, 2015
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.75 KB | None | 0 0
  1. public class Quad {
  2.  
  3. float a = 0, b = 0;
  4.  
  5. // Quad variables
  6. private int vaoId = 0;
  7. private int vboId = 0;
  8. private int vboiId = 0;
  9. private int indicesCount = 0;
  10. // Shader variables
  11. private int vsId = 0;
  12. private int fsId = 0;
  13. private int pId = 0;
  14. // Texture variables
  15. private int[] texIds = new int[] {0, 0};
  16. private int textureSelector = 0;
  17. // Update VBO variables
  18. private TexturedVertex[] vertices = null;
  19. private ByteBuffer vertexByteBuffer = null;
  20. private ByteBuffer verticesByteBuffer = null;
  21.  
  22. boolean move;
  23.  
  24. public Quad(boolean move, int aa) {
  25. this.move = move;
  26. this.vboId = aa;
  27. init();
  28. shaders();
  29. textures();
  30. }
  31.  
  32.  
  33.  
  34. public void update() {
  35. // Texture selection
  36. while(Keyboard.next()) {
  37. if (!Keyboard.getEventKeyState() && move) continue;
  38.  
  39. // Switch textures depending on the key released
  40. switch (Keyboard.getEventKey()) {
  41. case Keyboard.KEY_1:
  42. textureSelector = 0;
  43. break;
  44. case Keyboard.KEY_2:
  45. textureSelector = 1;
  46. break;
  47.  
  48.  
  49.  
  50. case Keyboard.KEY_LEFT:
  51. a-= 0.2f;
  52. break;
  53. case Keyboard.KEY_RIGHT:
  54. a+= 0.2f;
  55. break;
  56. case Keyboard.KEY_UP:
  57. b+= 0.2f;
  58. break;
  59. case Keyboard.KEY_DOWN:
  60. b-= 0.2f;
  61. break;
  62. }
  63. }
  64.  
  65. // Update vertices in the VBO, first bind the VBO
  66. GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
  67.  
  68. // Apply and update vertex data
  69. for (int i = 0; i < vertices.length; i++) {
  70. TexturedVertex vertex = vertices[i];
  71.  
  72. // Define offset
  73. float offsetX = (float) (Math.cos(Math.PI * Math.random()) * 0.1);
  74. float offsetY = (float) (Math.sin(Math.PI * Math.random()) * 0.1);
  75.  
  76. // Offset the vertex position
  77. float[] xyz = vertex.getXYZ();
  78. vertex.setXYZ(xyz[0] + a, xyz[1] + offsetY, xyz[2]);
  79.  
  80. // Put the new data in a ByteBuffer (in the view of a FloatBuffer)
  81. FloatBuffer vertexFloatBuffer = vertexByteBuffer.asFloatBuffer();
  82. vertexFloatBuffer.rewind();
  83. vertexFloatBuffer.put(vertex.getElements());
  84. vertexFloatBuffer.flip();
  85.  
  86. GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, i * TexturedVertex.stride,
  87. vertexByteBuffer);
  88.  
  89. // Restore the vertex data
  90. vertex.setXYZ(xyz[0], xyz[1], xyz[2]);
  91. }
  92.  
  93. // And of course unbind
  94. GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
  95.  
  96. this.exitOnGLError("logicCycle");
  97. }
  98.  
  99.  
  100.  
  101. public void render() {
  102. GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
  103.  
  104. GL20.glUseProgram(pId);
  105.  
  106. // Bind the texture
  107. GL13.glActiveTexture(GL13.GL_TEXTURE0);
  108. GL11.glBindTexture(GL11.GL_TEXTURE_2D, texIds[textureSelector]);
  109.  
  110. // Bind to the VAO that has all the information about the vertices
  111. GL30.glBindVertexArray(vaoId);
  112. GL20.glEnableVertexAttribArray(0);
  113. GL20.glEnableVertexAttribArray(1);
  114. GL20.glEnableVertexAttribArray(2);
  115.  
  116. // Bind to the index VBO that has all the information about the order of the vertices
  117. GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId);
  118.  
  119. // Draw the vertices
  120. GL11.glDrawElements(GL11.GL_TRIANGLES, indicesCount, GL11.GL_UNSIGNED_BYTE, 0);
  121.  
  122. // Put everything back to default (deselect)
  123. GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
  124. GL20.glDisableVertexAttribArray(0);
  125. GL20.glDisableVertexAttribArray(1);
  126. GL20.glDisableVertexAttribArray(2);
  127. GL30.glBindVertexArray(0);
  128.  
  129. GL20.glUseProgram(0);
  130.  
  131. this.exitOnGLError("renderCycle");
  132. }
  133.  
  134.  
  135.  
  136. private void init() {
  137. // We'll define our quad using 4 vertices of the custom 'TexturedVertex' class
  138. TexturedVertex v0 = new TexturedVertex();
  139. v0.setXYZ(-0.5f, 0.5f, 0); v0.setRGB(1, 0, 0); v0.setST(0, 0);
  140. TexturedVertex v1 = new TexturedVertex();
  141. v1.setXYZ(-0.5f, -0.5f, 0); v1.setRGB(0, 1, 0); v1.setST(0, 1);
  142. TexturedVertex v2 = new TexturedVertex();
  143. v2.setXYZ(0.5f, -0.5f, 0); v2.setRGB(0, 0, 1); v2.setST(1, 1);
  144. TexturedVertex v3 = new TexturedVertex();
  145. v3.setXYZ(0.5f, 0.5f, 0); v3.setRGB(1, 1, 1); v3.setST(1, 0);
  146.  
  147. vertices = new TexturedVertex[] {v0, v1, v2, v3};
  148.  
  149. // Create a FloatBufer of the appropriate size for one vertex
  150. vertexByteBuffer = BufferUtils.createByteBuffer(TexturedVertex.stride);
  151.  
  152. // Put each 'Vertex' in one FloatBuffer
  153. verticesByteBuffer = BufferUtils.createByteBuffer(vertices.length *
  154. TexturedVertex.stride);
  155.  
  156. FloatBuffer verticesFloatBuffer = verticesByteBuffer.asFloatBuffer();
  157. for (int i = 0; i < vertices.length; i++) {
  158. // Add position, color and texture floats to the buffer
  159. verticesFloatBuffer.put(vertices[i].getElements());
  160. }
  161. verticesFloatBuffer.flip();
  162.  
  163.  
  164. // OpenGL expects to draw vertices in counter clockwise order by default
  165. byte[] indices = {
  166. 0, 1, 2,
  167. 2, 3, 0
  168. };
  169. indicesCount = indices.length;
  170. ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesCount);
  171. indicesBuffer.put(indices);
  172. indicesBuffer.flip();
  173.  
  174. // Create a new Vertex Array Object in memory and select it (bind)
  175. vaoId = GL30.glGenVertexArrays();
  176. GL30.glBindVertexArray(vaoId);
  177.  
  178. // Create a new Vertex Buffer Object in memory and select it (bind)
  179. vboId = GL15.glGenBuffers();
  180. GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
  181. GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesFloatBuffer, GL15.GL_STREAM_DRAW);
  182.  
  183. // Put the position coordinates in attribute list 0
  184. GL20.glVertexAttribPointer(0, TexturedVertex.positionElementCount, GL11.GL_FLOAT,
  185. false, TexturedVertex.stride, TexturedVertex.positionByteOffset);
  186. // Put the color components in attribute list 1
  187. GL20.glVertexAttribPointer(1, TexturedVertex.colorElementCount, GL11.GL_FLOAT,
  188. false, TexturedVertex.stride, TexturedVertex.colorByteOffset);
  189. // Put the texture coordinates in attribute list 2
  190. GL20.glVertexAttribPointer(2, TexturedVertex.textureElementCount, GL11.GL_FLOAT,
  191. false, TexturedVertex.stride, TexturedVertex.textureByteOffset);
  192.  
  193. GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
  194.  
  195. // Deselect (bind to 0) the VAO
  196. GL30.glBindVertexArray(0);
  197.  
  198. // Create a new VBO for the indices and select it (bind) - INDICES
  199. vboiId = GL15.glGenBuffers();
  200. GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId);
  201. GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
  202. GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
  203.  
  204. this.exitOnGLError("setupQuad");
  205. }
  206.  
  207.  
  208. private void shaders() {
  209. // Load the vertex shader
  210. vsId = this.loadShader("shaders/vert.shdr", GL20.GL_VERTEX_SHADER);
  211. // Load the fragment shader
  212. fsId = this.loadShader("shaders/frag.shdr", GL20.GL_FRAGMENT_SHADER);
  213.  
  214. // Create a new shader program that links both shaders
  215. pId = GL20.glCreateProgram();
  216. GL20.glAttachShader(pId, vsId);
  217. GL20.glAttachShader(pId, fsId);
  218.  
  219. // Position information will be attribute 0
  220. GL20.glBindAttribLocation(pId, 0, "in_Position");
  221. // Color information will be attribute 1
  222. GL20.glBindAttribLocation(pId, 1, "in_Color");
  223. // Textute information will be attribute 2
  224. GL20.glBindAttribLocation(pId, 2, "in_TextureCoord");
  225.  
  226. GL20.glLinkProgram(pId);
  227. GL20.glValidateProgram(pId);
  228.  
  229. this.exitOnGLError("setupShaders");
  230. }
  231.  
  232.  
  233. private void textures() {
  234. texIds[0] = this.loadPNGTexture("res/a.png", GL13.GL_TEXTURE0);
  235. texIds[1] = this.loadPNGTexture("res/erza.png", GL13.GL_TEXTURE0);
  236.  
  237. this.exitOnGLError("setupTexture");
  238. }
  239.  
  240.  
  241. private int loadShader(String filename, int type) {
  242. StringBuilder shaderSource = new StringBuilder();
  243. int shaderID = 0;
  244.  
  245. try {
  246. BufferedReader reader = new BufferedReader(new FileReader(filename));
  247. String line;
  248. while ((line = reader.readLine()) != null) {
  249. shaderSource.append(line).append("\n");
  250. }
  251. reader.close();
  252. } catch (IOException e) {
  253. System.err.println("Could not read file.");
  254. e.printStackTrace();
  255. System.exit(-1);
  256. }
  257.  
  258. shaderID = GL20.glCreateShader(type);
  259. GL20.glShaderSource(shaderID, shaderSource);
  260. GL20.glCompileShader(shaderID);
  261.  
  262. if (GL20.glGetShader(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
  263. System.err.println("Could not compile shader.");
  264. System.exit(-1);
  265. }
  266.  
  267. this.exitOnGLError("loadShader");
  268.  
  269. return shaderID;
  270. }
  271.  
  272. private int loadPNGTexture(String filename, int textureUnit) {
  273. ByteBuffer buf = null;
  274. int tWidth = 0;
  275. int tHeight = 0;
  276.  
  277. try {
  278. // Open the PNG file as an InputStream
  279. InputStream in = new FileInputStream(filename);
  280. // Link the PNG decoder to this stream
  281. PNGDecoder decoder = new PNGDecoder(in);
  282.  
  283. // Get the width and height of the texture
  284. tWidth = decoder.getWidth();
  285. tHeight = decoder.getHeight();
  286.  
  287.  
  288. // Decode the PNG file in a ByteBuffer
  289. buf = ByteBuffer.allocateDirect(
  290. 4 * decoder.getWidth() * decoder.getHeight());
  291. decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
  292. buf.flip();
  293.  
  294. in.close();
  295. } catch (IOException e) {
  296. e.printStackTrace();
  297. System.exit(-1);
  298. }
  299.  
  300. // Create a new texture object in memory and bind it
  301. int texId = GL11.glGenTextures();
  302. GL13.glActiveTexture(textureUnit);
  303. GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
  304.  
  305. // All RGB bytes are aligned to each other and each component is 1 byte
  306. GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
  307.  
  308. // Upload the texture data and generate mip maps (for scaling)
  309. GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tWidth, tHeight, 0,
  310. GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
  311. GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
  312.  
  313. // Setup the ST coordinate system
  314. GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
  315. GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
  316.  
  317. // Setup what to do when the texture has to be scaled
  318. GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER,
  319. GL11.GL_LINEAR);
  320. GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,
  321. GL11.GL_LINEAR_MIPMAP_LINEAR);
  322.  
  323. this.exitOnGLError("loadPNGTexture");
  324.  
  325. return texId;
  326. }
  327.  
  328. private void exitOnGLError(String errorMessage) {
  329. int errorValue = GL11.glGetError();
  330.  
  331. if (errorValue != GL11.GL_NO_ERROR) {
  332. String errorString = GLU.gluErrorString(errorValue);
  333. System.err.println("ERROR - " + errorMessage + ": " + errorString);
  334.  
  335. if (Display.isCreated()) Display.destroy();
  336. System.exit(-1);
  337. }
  338. }
  339.  
  340.  
  341. public void destroy() {
  342. // Delete the texture
  343. GL11.glDeleteTextures(texIds[0]);
  344. GL11.glDeleteTextures(texIds[1]);
  345.  
  346. // Delete the shaders
  347. GL20.glUseProgram(0);
  348. GL20.glDetachShader(pId, vsId);
  349. GL20.glDetachShader(pId, fsId);
  350.  
  351. GL20.glDeleteShader(vsId);
  352. GL20.glDeleteShader(fsId);
  353. GL20.glDeleteProgram(pId);
  354.  
  355. // Select the VAO
  356. GL30.glBindVertexArray(vaoId);
  357.  
  358. // Disable the VBO index from the VAO attributes list
  359. GL20.glDisableVertexAttribArray(0);
  360. GL20.glDisableVertexAttribArray(1);
  361.  
  362. // Delete the vertex VBO
  363. GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
  364. GL15.glDeleteBuffers(vboId);
  365.  
  366. // Delete the index VBO
  367. GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
  368. GL15.glDeleteBuffers(vboiId);
  369.  
  370. // Delete the VAO
  371. GL30.glBindVertexArray(0);
  372. GL30.glDeleteVertexArrays(vaoId);
  373. }
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement