TextureRenderer.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. {
  10. private int vao;
  11. private int vbo;
  12. private int triangles = 0;
  13. private FloatBuffer buffer;
  14. private boolean built = false;
  15. public TextureRenderer(int triangles)
  16. {
  17. buffer = BufferUtils.createFloatBuffer(triangles * 12);
  18. Shader.addTask(() ->
  19. {
  20. vao = glGenVertexArrays();
  21. vbo = glGenBuffers();
  22. GLHelper.glBindVertexArray(vao);
  23. GLHelper.glBindBuffer(vbo);
  24. glEnableVertexAttribArray(0);
  25. glVertexAttribPointer(0, 2, GL_FLOAT, false, 16, 0);
  26. glEnableVertexAttribArray(1);
  27. glVertexAttribPointer(1, 2, GL_FLOAT, false, 16, 8);
  28. });
  29. }
  30. 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)
  31. {
  32. if(buffer.position() + 12 > buffer.capacity())
  33. {
  34. FloatBuffer newBuffer = BufferUtils.createFloatBuffer(buffer.capacity() * 2);
  35. buffer.flip();
  36. newBuffer.put(buffer);
  37. buffer = newBuffer;
  38. }
  39. buffer.put(x1);
  40. buffer.put(y1);
  41. buffer.put(tx1);
  42. buffer.put(ty1);
  43. buffer.put(x2);
  44. buffer.put(y2);
  45. buffer.put(tx2);
  46. buffer.put(ty2);
  47. buffer.put(x3);
  48. buffer.put(y3);
  49. buffer.put(tx3);
  50. buffer.put(ty3);
  51. triangles++;
  52. }
  53. public void addRectangle(float minX, float minY, float maxX, float maxY, float tMinX, float tMinY, float tMaxX, float tMaxY)
  54. {
  55. addTriangle(minX, maxY, minX, minY, maxX, maxY, tMinX, tMaxY, tMinX, tMinY, tMaxX, tMaxY);
  56. addTriangle(maxX, maxY, minX, minY, maxX, minY, tMaxX, tMaxY, tMinX, tMinY, tMaxX, tMinY);
  57. }
  58. public boolean isBuilt()
  59. {
  60. return built;
  61. }
  62. public void build()
  63. {
  64. if(!Shader.initDone)
  65. {
  66. throw new ShaderException("build called too early");
  67. }
  68. if(triangles == 0 || built)
  69. {
  70. return;
  71. }
  72. buffer.flip();
  73. GLHelper.glBindVertexArray(vao);
  74. GLHelper.glBindBuffer(vbo);
  75. glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
  76. buffer.limit(buffer.capacity());
  77. built = true;
  78. }
  79. public void clear()
  80. {
  81. triangles = 0;
  82. built = false;
  83. }
  84. public void draw()
  85. {
  86. if(triangles == 0)
  87. {
  88. return;
  89. }
  90. if(!built)
  91. {
  92. throw new ShaderException("build must be called before drawing");
  93. }
  94. GLHelper.glBindVertexArray(vao);
  95. GLHelper.glBindBuffer(vbo);
  96. glDrawArrays(GL_TRIANGLES, 0, buffer.limit() / 4);
  97. }
  98. public void delete()
  99. {
  100. buffer = null;
  101. glDeleteVertexArrays(vao);
  102. glDeleteBuffers(vbo);
  103. vao = -1;
  104. vbo = -1;
  105. built = false;
  106. }
  107. }