TextureRenderer.java 3.0 KB

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