Vbo.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package eu.tankernn.gameEngine.renderEngine;
  2. import java.nio.FloatBuffer;
  3. import java.nio.IntBuffer;
  4. import org.lwjgl.BufferUtils;
  5. import org.lwjgl.opengl.GL15;
  6. public class Vbo {
  7. private final int vboId;
  8. private final int type;
  9. private final int usage;
  10. private Vbo(int vboId, int type, int usage){
  11. this.vboId = vboId;
  12. this.type = type;
  13. this.usage = usage;
  14. }
  15. private Vbo(int vboId, int type, int usage, int size) {
  16. this(vboId, type, usage);
  17. bind();
  18. GL15.glBufferData(type, size * 4, usage);
  19. unbind();
  20. }
  21. public static Vbo create(int type, int usage, int size) {
  22. int id = GL15.glGenBuffers();
  23. return new Vbo(id, type, usage, size);
  24. }
  25. public static Vbo create(int type, int usage){
  26. int id = GL15.glGenBuffers();
  27. return new Vbo(id, type, usage);
  28. }
  29. public static Vbo create(int type) {
  30. return create(type, GL15.GL_STATIC_DRAW);
  31. }
  32. public void bind(){
  33. GL15.glBindBuffer(type, vboId);
  34. }
  35. public void unbind(){
  36. GL15.glBindBuffer(type, vboId);
  37. }
  38. public void storeData(float[] data){
  39. FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
  40. buffer.put(data);
  41. buffer.flip();
  42. storeData(buffer);
  43. }
  44. public void storeData(FloatBuffer data){
  45. GL15.glBufferData(type, data, usage);
  46. }
  47. public void storeData(int[] data){
  48. IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
  49. buffer.put(data);
  50. buffer.flip();
  51. storeData(buffer);
  52. }
  53. public void storeData(IntBuffer data){
  54. GL15.glBufferData(type, data, usage);
  55. }
  56. public void updateData(float[] data) {
  57. updateData(data, BufferUtils.createFloatBuffer(data.length));
  58. }
  59. public void updateData(float[] data, FloatBuffer buffer) {
  60. bind();
  61. buffer.clear();
  62. buffer.put(data);
  63. buffer.flip();
  64. GL15.glBindBuffer(type, vboId);
  65. GL15.glBufferData(type, buffer.capacity() * 4, usage);
  66. GL15.glBufferSubData(type, 0, buffer);
  67. GL15.glBindBuffer(type, 0);
  68. unbind();
  69. }
  70. public void delete(){
  71. GL15.glDeleteBuffers(vboId);
  72. }
  73. }