TextureRenderer.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package me.hammerle.snuviengine.api;
  2. import java.nio.FloatBuffer;
  3. import org.lwjgl.BufferUtils;
  4. import static org.lwjgl.opengl.GL11.*;
  5. import static org.lwjgl.opengl.GL15.*;
  6. import static org.lwjgl.opengl.GL20.*;
  7. import static org.lwjgl.opengl.GL30.*;
  8. public class TextureRenderer {
  9. private final int vertexArray;
  10. private final int vertexBuffer;
  11. private int triangles = 0;
  12. private FloatBuffer buffer;
  13. private boolean built = false;
  14. public TextureRenderer(int triangles) {
  15. buffer = BufferUtils.createFloatBuffer(triangles * 12);
  16. vertexArray = glGenVertexArrays();
  17. vertexBuffer = glGenBuffers();
  18. GLHelper.glBindVertexArray(vertexArray);
  19. GLHelper.glBindBuffer(vertexBuffer);
  20. glEnableVertexAttribArray(0);
  21. glVertexAttribPointer(0, 2, GL_FLOAT, false, 16, 0);
  22. glEnableVertexAttribArray(1);
  23. glVertexAttribPointer(1, 2, GL_FLOAT, false, 16, 8);
  24. }
  25. public void addTriangle(float x1, float y1, float x2, float y2, float x3, float y3, float tx1, float ty1, float tx2, float ty2, float tx3, float ty3) {
  26. if(buffer.position() + 12 > buffer.capacity()) {
  27. FloatBuffer newBuffer = BufferUtils.createFloatBuffer(buffer.capacity() * 2);
  28. buffer.flip();
  29. newBuffer.put(buffer);
  30. buffer = newBuffer;
  31. }
  32. addToBuffer(x1, y1, tx1, ty1);
  33. addToBuffer(x2, y2, tx2, ty2);
  34. addToBuffer(x3, y3, tx3, ty3);
  35. triangles++;
  36. }
  37. private void addToBuffer(float x, float y, float tx, float ty) {
  38. buffer.put(x);
  39. buffer.put(y);
  40. buffer.put(tx);
  41. buffer.put(ty);
  42. }
  43. public void addRectangle(float minX, float minY, float maxX, float maxY, float tMinX, float tMinY, float tMaxX, float tMaxY) {
  44. addTriangle(minX, maxY, minX, minY, maxX, maxY, tMinX, tMaxY, tMinX, tMinY, tMaxX, tMaxY);
  45. addTriangle(maxX, maxY, minX, minY, maxX, minY, tMaxX, tMaxY, tMinX, tMinY, tMaxX, tMinY);
  46. }
  47. public boolean isBuilt() {
  48. return built;
  49. }
  50. public void build() {
  51. if(triangles == 0 || built) {
  52. return;
  53. }
  54. buffer.flip();
  55. GLHelper.glBindVertexArray(vertexArray);
  56. GLHelper.glBindBuffer(vertexBuffer);
  57. glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
  58. buffer.limit(buffer.capacity());
  59. built = true;
  60. }
  61. public void clear() {
  62. triangles = 0;
  63. buffer.clear();
  64. built = false;
  65. }
  66. public void draw() {
  67. if(triangles == 0 || !built) {
  68. return;
  69. }
  70. GLHelper.glBindVertexArray(vertexArray);
  71. GLHelper.glBindBuffer(vertexBuffer);
  72. glDrawArrays(GL_TRIANGLES, 0, triangles * 3);
  73. }
  74. public void delete() {
  75. buffer = null;
  76. glDeleteVertexArrays(vertexArray);
  77. glDeleteBuffers(vertexBuffer);
  78. built = false;
  79. }
  80. }