将顶点添加到libgdx网格 [英] Adding a Vertex to a libgdx Mesh
问题描述
基本上是标题.
您可以保留创建网格时所需的更多空间
You can reserve more than you need when creating a mesh
mesh = new Mesh(false, 100, 0, new VertexAttribute(Usage.Position, 3, "a_position"));
但是没有添加顶点的方法.您可以获取FloatBuffer并将其添加到其中,但是我得到了奇怪的结果.我还尝试了带有offset的mesh.setVertices,但这也不起作用.
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.
我调试了绘图点.有效,直到我尝试以任何方式添加顶点(即使我调整偏移量以在一个顶点中考虑3个浮点)
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)
复制的代码段:
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
我也尝试过
squareMesh.setVertices(new float[]{-0.5f,0.5f,0.f}, 2, 3);
谢谢:)
推荐答案
添加是一个坏主意,因为必须重新创建基础缓冲区,这会花费大量时间.取而代之的是,应该批量分配网格,并且仅渲染当前使用的网格.
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.
例如mesh.render(GL10.GL_TRIANGLES,0,num_triangles);
关于更新缓冲区:
FloatBuffer fbuftmp = mesh.getVerticesBuffer();
BufferUtils.copy(buf,fbuftmp,fbuftmp.capacity(),0);
buf
是浮点数组.
出于说明原因使用BufferUtils.copy 此处
Use BufferUtils.copy for reason explained here
这篇关于将顶点添加到libgdx网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!