TextureRenderer.java 2.9 KB

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