Vao.java 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package eu.tankernn.gameEngine.renderEngine;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.lwjgl.opengl.GL11;
  5. import org.lwjgl.opengl.GL15;
  6. import org.lwjgl.opengl.GL20;
  7. import org.lwjgl.opengl.GL30;
  8. public class Vao {
  9. protected static final int BYTES_PER_FLOAT = 4;
  10. private static final int BYTES_PER_INT = 4;
  11. public final int id;
  12. private List<Vbo> dataVbos = new ArrayList<Vbo>();
  13. private Vbo indexVbo;
  14. private int indexCount;
  15. public static Vao create() {
  16. int id = GL30.glGenVertexArrays();
  17. return new Vao(id);
  18. }
  19. private Vao(int id) {
  20. this.id = id;
  21. }
  22. public Vao(int id, int indexCount) {
  23. this(id);
  24. this.indexCount = indexCount;
  25. }
  26. public int getIndexCount(){
  27. return indexCount;
  28. }
  29. public void bind(int... attributes){
  30. bind();
  31. for (int i : attributes) {
  32. GL20.glEnableVertexAttribArray(i);
  33. }
  34. }
  35. public void unbind(int... attributes){
  36. for (int i : attributes) {
  37. GL20.glDisableVertexAttribArray(i);
  38. }
  39. unbind();
  40. }
  41. public void createIndexBuffer(int[] indices){
  42. this.indexVbo = Vbo.create(GL15.GL_ELEMENT_ARRAY_BUFFER);
  43. indexVbo.bind();
  44. indexVbo.storeData(indices);
  45. this.indexCount = indices.length;
  46. }
  47. public void createAttribute(int attribute, float[] data, int attrSize){
  48. Vbo dataVbo = Vbo.create(GL15.GL_ARRAY_BUFFER);
  49. dataVbo.bind();
  50. dataVbo.storeData(data);
  51. GL20.glVertexAttribPointer(attribute, attrSize, GL11.GL_FLOAT, false, attrSize * BYTES_PER_FLOAT, 0);
  52. dataVbo.unbind();
  53. dataVbos.add(dataVbo);
  54. }
  55. public void createIntAttribute(int attribute, int[] data, int attrSize){
  56. Vbo dataVbo = Vbo.create(GL15.GL_ARRAY_BUFFER);
  57. dataVbo.bind();
  58. dataVbo.storeData(data);
  59. GL30.glVertexAttribIPointer(attribute, attrSize, GL11.GL_INT, attrSize * BYTES_PER_INT, 0);
  60. dataVbo.unbind();
  61. dataVbos.add(dataVbo);
  62. }
  63. public void delete() {
  64. GL30.glDeleteVertexArrays(id);
  65. for(Vbo vbo : dataVbos){
  66. vbo.delete();
  67. }
  68. indexVbo.delete();
  69. }
  70. private void bind() {
  71. GL30.glBindVertexArray(id);
  72. }
  73. private void unbind() {
  74. GL30.glBindVertexArray(0);
  75. }
  76. }