Guest User

VAO.class

a guest
Dec 22nd, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. public class VAO {
  2.     public static List<Integer> vaos = new ArrayList<>();
  3.     public static List<Integer> vbos = new ArrayList<>();
  4.    
  5.     int vaoID;
  6.     int vCount;
  7.    
  8.     public VAO(float[] vertices, float[] normals, float[] texCoords, int[] indices) {
  9.         vaoID = createVAO();
  10.         vCount = indices.length;
  11.         bindIndices(indices);
  12.         storeDataInVAO(0, 3, vertices);
  13.         storeDataInVAO(1, 3, normals);
  14.         storeDataInVAO(2, 2, texCoords);
  15.         glBindVertexArray(0);
  16.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  17.     }
  18.    
  19.     private int createVAO() {
  20.         int vao = glGenVertexArrays();
  21.         vaos.add(vao);
  22.         glBindVertexArray(vao);
  23.         return vao;
  24.     }
  25.    
  26.     private void bindIndices(int[] indices) {
  27.         int vboiID = glGenBuffers();
  28.         vbos.add(vboiID);
  29.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboiID);
  30.         glBufferData(GL_ELEMENT_ARRAY_BUFFER, asIntBuffer(indices), GL_STATIC_DRAW);
  31.     }
  32.    
  33.     private void storeDataInVAO(int index, int coordsize, float[] data) {
  34.         int vboID = glGenBuffers();
  35.         vbos.add(vboID);
  36.         glBindBuffer(GL_ARRAY_BUFFER, vboID);
  37.         glBufferData(GL_ARRAY_BUFFER, asFloatBuffer(data), GL_STATIC_DRAW);
  38.         glVertexAttribPointer(index, coordsize, GL_FLOAT, false, 0, 0);
  39.         glBindBuffer(GL_ARRAY_BUFFER, 0);
  40.     }
  41.    
  42.     public int getVaoID() {
  43.         return vaoID;
  44.     }
  45.    
  46.     public int getVertexCount() {
  47.         return vCount;
  48.     }
  49.    
  50.     public static void cleanUp() {
  51.         for(int i = 0; i < vaos.size(); i++) {
  52.             glDeleteVertexArrays(i);
  53.             System.out.println("[MODEL] Deleted VAO #"+(i+1));
  54.         }
  55.         System.out.println("--------------------");
  56.         for(int i = 0; i < vbos.size(); i++) {
  57.             glDeleteBuffers(i);
  58.             System.out.println("[MODEL] Deleted VBO #"+(i+1));
  59.         }
  60.         System.out.println("--------------------");
  61.         for(int i = 0; i < Render.textures.size(); i++) {
  62.             glDeleteTextures(i);
  63.             Render.textures.get(i).release();
  64.             System.out.println("[MODEL] Deleted texture #"+(i+1));
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment