Adding a Vertex to a libgdx Mesh

感情迁移 提交于 2019-12-11 06:24:16

问题


the title basically.

You can reserve more than you need when creating a mesh

mesh = new Mesh(false, 100, 0, new VertexAttribute(Usage.Position, 3, "a_position"));

But there is no method for adding a vertex. You can get the FloatBuffer and add to that, but I get strange results. I also tried the mesh.setVertices with offset but that does not work either.

I debugged with drawing points. Works until I try adding a vertex by any means (even if I tweak offsets to account for 3 floats in one vertex)

Copied code segment:

mesh.setVertices(new float[] {
                -0.5f, -0.5f, 0,
                0.5f, -0.5f, 0/*,
                -0.5f,0.5f,0.f*/});//works if I uncoment this
mesh.setVertices(new float[]{-0.5f,0.5f,0.f}, 6, 3);//but comment this out

I also tried

squareMesh.setVertices(new float[]{-0.5f,0.5f,0.f}, 2, 3);

Thanks :)


回答1:


Adding is a bad idea because the underlying buffers must be recreated and that takes up a lot of time. Instead, one should allocate meshes in bulk and only render what is currently used.

For example mesh.render(GL10.GL_TRIANGLES,0,num_triangles);

As for updating the buffer:

FloatBuffer fbuftmp = mesh.getVerticesBuffer(); 
BufferUtils.copy(buf,fbuftmp,fbuftmp.capacity(),0);

Where buf is an float array.

Use BufferUtils.copy for reason explained here



来源:https://stackoverflow.com/questions/10042255/adding-a-vertex-to-a-libgdx-mesh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!