⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠ You can decompress Drawing data with the command palette: ‘Decompress current Excalidraw file’. For more info check in plugin settings under ‘Saving’
Excalidraw Data
Text Elements
float[] vertices = {
-0.5f, 0.5f, 0f,
-0.5f, -0.5f, 0f,
0.5f, -0.5f, 0f,
0.5f, 0.5f, 0f,
};
int[] indices = {0,1,3,3,1,2};
RawModel rawModel = loader.loadToVAO(vertices,indices);
renderer.render(rawModel);
传入四个顶点,和索引缓冲对象
public RawModel loadToVAO(float[] positions, int[] indices){
int vaoIO = createVAO(); // create VAO
bindIndicesBuffer(indices); // bind VAO
storeDataInAttributeList(0,positions);
unBindVAO(); // unbind after stored data in VAO
return new RawModel(vaoIO, indices.length);
}
// in the VAO
private void storeDataInAttributeList(int attributeNumber, float[] data){
int vboID = GL30.glGenBuffers(); // create an empty VBO
vbos.add(vboID);
GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER,vboID); // specify the type of VBO and bind it
FloatBuffer buffer = storeDataInFloatBuffer(data);
// store into the VBO
GL30.glBufferData(GL30.GL_ARRAY_BUFFER,buffer,GL30.GL_STATIC_DRAW);
// put the vbo into VAO in one of the attribute
// parameters: 3, is the length of each vertex
// 设置顶点属性指针:glVertexAttribPointer(0, 3, ...) 表示一个顶点由 3 个 float 构成(x,y,z)
GL20.glVertexAttribPointer(attributeNumber, 3, GL30.GL_FLOAT, false, 0, 0);
GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER,0);
} ^g2eOtI2F
// data has to be stored into a VBO as a float buffer
private FloatBuffer storeDataInFloatBuffer(float[] data){
FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
buffer.put(data);
buffer.flip(); // prepare the buffer to be read
return buffer;
}
public void render(RawModel model){
GL30.glBindVertexArray(model.getVaoID());
GL30.glEnableVertexAttribArray(0);
GL30.glDrawElements(GL11.GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0); // 使用索引缓冲区进行绘制
GL30.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0); // 解绑
}