ulfben icon

Mesh

ulfben | PRO | 03/25/21 08:09:48 PM UTC (Edited) | 0 ⭐ | 853 👁️ | Never ⏰ | []
Java |

2.85 KB

|

None

|

0 👍

/

0 👎

public class Mesh {
    public static final String TAG = "Mesh";
    public FloatBuffer _vertices;
    public IntBuffer _indices;
    public List<Texture> _textures = new ArrayList();
 
    public Vec3 _min = Vec3.Max();
    public Vec3 _max = Vec3.Min();
    public Vec3 _center = Vec3.Zero();
    public Vec3 _span = Vec3.Zero();
 
    public int[] VAO = new int[1];
    private final int[] VBO = new int[1];
    private final int[] EBO = new int[1];
    private final int _vertexCount;
    private final int _indexCount;
    public final int _mode = GLES30.GL_TRIANGLES;
 
    public Mesh(Vertex[] verts, int[] indices){
        _vertexCount = verts.length;
        _indexCount = indices.length;
        _indices = GfxUtils.makeIntBuffer(indices);
        _vertices = GfxUtils.makeFloatBuffer(verts);
        updateBounds();
        //centerAndScaleToDimensions(100.0f, 100.0f, 100.0f);
       // normalize();
        setupMesh();
    }
 
    public void render(final Shader s) {
        s.use();
        if(_textures.size() > 0){
            bindTextures(s);
        }
        GLES30.glBindVertexArray(VAO[0]);
        GLES30.glDrawElements(_mode, _indexCount, GLES30.GL_UNSIGNED_INT, 0);
        GLES30.glBindVertexArray(0);
        if(_textures.size() > 0) { //reset the texture unit, if used.
            GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
        }
        checkGLError("render()");
    }
 
    private void setupMesh() {
        _vertices.position(0);
        _indices.position(0);
 
        GLES30.glGenVertexArrays(1, VAO, 0);
        GLES30.glGenBuffers(1, VBO, 0);
        GLES30.glGenBuffers(1, EBO, 0);
 
        GLES30.glBindVertexArray(VAO[0]);
 
        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, VBO[0]);
        GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, getVertBufferSize(), _vertices, GLES30.GL_STATIC_DRAW);
 
        GLES30.glBindBuffer(GLES30.GL_ELEMENT_ARRAY_BUFFER, EBO[0]);
        GLES30.glBufferData(GLES30.GL_ELEMENT_ARRAY_BUFFER, getIndexBufferSize(), _indices, GLES30.GL_STATIC_DRAW);
 
        GLES30.glVertexAttribPointer(POS_INDEX, POS_SIZE, GLES30.GL_FLOAT, false, VERTEX_STRIDE, POS_OFFSET);
        GLES30.glEnableVertexAttribArray(POS_INDEX);
 
        GLES30.glVertexAttribPointer(NORMAL_INDEX, NORMAL_SIZE, GLES30.GL_FLOAT, false, VERTEX_STRIDE, NORMAL_OFFSET);
        GLES30.glEnableVertexAttribArray(NORMAL_INDEX);
 
        GLES30.glEnableVertexAttribArray(TEXCOORD_INDEX);
        GLES30.glVertexAttribPointer(TEXCOORD_INDEX, TEXCOORD_SIZE, GLES30.GL_FLOAT, false, VERTEX_STRIDE, TEXCOORD_OFFSET);
        GLES30.glBindVertexArray(0); //reset to default render context
        checkGLError("setupMesh");
    }
 
    private int getVertBufferSize() {
        return _vertexCount * Vertex.byteSize();
    }
 
    private int getIndexBufferSize() {
        return _indexCount * Integer.BYTES;
    }

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎