ulfben

Mesh

Mar 25th, 2021 (edited)
109
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Mesh {
  2.     public static final String TAG = "Mesh";
  3.     public FloatBuffer _vertices;
  4.     public IntBuffer _indices;
  5.     public List<Texture> _textures = new ArrayList();
  6.  
  7.     public Vec3 _min = Vec3.Max();
  8.     public Vec3 _max = Vec3.Min();
  9.     public Vec3 _center = Vec3.Zero();
  10.     public Vec3 _span = Vec3.Zero();
  11.  
  12.     public int[] VAO = new int[1];
  13.     private final int[] VBO = new int[1];
  14.     private final int[] EBO = new int[1];
  15.     private final int _vertexCount;
  16.     private final int _indexCount;
  17.     public final int _mode = GLES30.GL_TRIANGLES;
  18.  
  19.     public Mesh(Vertex[] verts, int[] indices){
  20.         _vertexCount = verts.length;
  21.         _indexCount = indices.length;
  22.         _indices = GfxUtils.makeIntBuffer(indices);
  23.         _vertices = GfxUtils.makeFloatBuffer(verts);
  24.         updateBounds();
  25.         //centerAndScaleToDimensions(100.0f, 100.0f, 100.0f);
  26.        // normalize();
  27.         setupMesh();
  28.     }
  29.  
  30.     public void render(final Shader s) {
  31.         s.use();
  32.         if(_textures.size() > 0){
  33.             bindTextures(s);
  34.         }
  35.         GLES30.glBindVertexArray(VAO[0]);
  36.         GLES30.glDrawElements(_mode, _indexCount, GLES30.GL_UNSIGNED_INT, 0);
  37.         GLES30.glBindVertexArray(0);
  38.         if(_textures.size() > 0) { //reset the texture unit, if used.
  39.             GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
  40.         }
  41.         checkGLError("render()");
  42.     }
  43.  
  44.     private void setupMesh() {
  45.         _vertices.position(0);
  46.         _indices.position(0);
  47.  
  48.         GLES30.glGenVertexArrays(1, VAO, 0);
  49.         GLES30.glGenBuffers(1, VBO, 0);
  50.         GLES30.glGenBuffers(1, EBO, 0);
  51.  
  52.         GLES30.glBindVertexArray(VAO[0]);
  53.  
  54.         GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, VBO[0]);
  55.         GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, getVertBufferSize(), _vertices, GLES30.GL_STATIC_DRAW);
  56.  
  57.         GLES30.glBindBuffer(GLES30.GL_ELEMENT_ARRAY_BUFFER, EBO[0]);
  58.         GLES30.glBufferData(GLES30.GL_ELEMENT_ARRAY_BUFFER, getIndexBufferSize(), _indices, GLES30.GL_STATIC_DRAW);
  59.  
  60.         GLES30.glVertexAttribPointer(POS_INDEX, POS_SIZE, GLES30.GL_FLOAT, false, VERTEX_STRIDE, POS_OFFSET);
  61.         GLES30.glEnableVertexAttribArray(POS_INDEX);
  62.  
  63.         GLES30.glVertexAttribPointer(NORMAL_INDEX, NORMAL_SIZE, GLES30.GL_FLOAT, false, VERTEX_STRIDE, NORMAL_OFFSET);
  64.         GLES30.glEnableVertexAttribArray(NORMAL_INDEX);
  65.  
  66.         GLES30.glEnableVertexAttribArray(TEXCOORD_INDEX);
  67.         GLES30.glVertexAttribPointer(TEXCOORD_INDEX, TEXCOORD_SIZE, GLES30.GL_FLOAT, false, VERTEX_STRIDE, TEXCOORD_OFFSET);
  68.         GLES30.glBindVertexArray(0); //reset to default render context
  69.         checkGLError("setupMesh");
  70.     }
  71.  
  72.     private int getVertBufferSize() {
  73.         return _vertexCount * Vertex.byteSize();
  74.     }
  75.  
  76.     private int getIndexBufferSize() {
  77.         return _indexCount * Integer.BYTES;
  78.     }
RAW Paste Data