我需要用新顶点更新我的网格。我这样创建VBO(最初创建时只有一个顶点):
public Mesh(float[] vertex, int size)
{
texture = null;
meshType = 1; //will draw lines
FloatBuffer verticesBuffer = null;
IntBuffer indicesBuffer = null;
int vboID;
try
{
vertexCount = size;
vaoID = glGenVertexArrays();
glBindVertexArray(vaoID);
vboIDList = new ArrayList<>();
// Vertices VBO generation
vboID = glGenBuffers();
vboIDList.add(vboID);
verticesBuffer = MemoryUtil.memAllocFloat(size * 3); // !!! Must Be manually freed!
verticesBuffer.put(vertex).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
vertexAttrArrCount += 1;
// Indices VBO generation
vboID = glGenBuffers();
vboIDList.add(vboID);
indicesBuffer = MemoryUtil.memAllocInt(size); // !!! Must be manually freed!
indicesBuffer.put(new int[]{0}).flip();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
// unbinding
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
finally
{
if (verticesBuffer != null)
{
MemoryUtil.memFree(verticesBuffer); // Freeing vertex buffer
}
if (indicesBuffer != null)
{
MemoryUtil.memFree(indicesBuffer); // Freeing indices buffer
}
}
}
然后我要更新VBO缓冲区,并将新顶点写入其中。请注意,我创建VBO是为了给我的新顶点留有足够的空间,并且我控制它不会被过度填充。我还控制每个渲染调用绘制多少元素,因此我不会绘制空的0/0/0顶点。
我的问题是,这段代码可以工作:
public void updateVBO(float[] vertices, int[] indices, int size)
{
if (meshType == 1)
{
lineCount = size;
FloatBuffer subDataF = null;
IntBuffer subDataI = null;
int vboID;
try
{
//System.out.printf("Adding vertex (%f, %f, %f) to position %d\n",vertex.x,vertex.y,vertex.z,position);
vboID = vboIDList.get(0);
//float[] vert = new float[]{vertex.x, vertex.y, vertex.z};
subDataF = MemoryUtil.memAllocFloat(vertices.length); // !!! Must Be manually freed!
subDataF.put(vertices).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, subDataF, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
vboID = vboIDList.get(1);
//int[] index = new int[]{ position };
subDataI = MemoryUtil.memAllocInt(indices.length); // !!! Must Be manually freed!
subDataI.put(indices).flip();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, subDataI, GL_STATIC_DRAW);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
finally
{
if (subDataF != null)
{
MemoryUtil.memFree(subDataF);
}
if (subDataI != null)
{
MemoryUtil.memFree(subDataI);
}
}
}
}
因此,当我传递整个顶点数组,从头开始重新分配VBO内存时,它就会提取出我所需要的内容。但是我希望使用glBufferSubData,这样我就不会在每次添加新顶点时重新分配momory。这段代码不起作用:
public void addVertex(Vector3f vertex, int position)
{
if (meshType == 1)
{
FloatBuffer subDataF = null;
IntBuffer subDataI = null;
int vboID;
lineCount = position+1;
try
{
System.out.printf("Adding vertex (%f, %f, %f) to position %d\n",vertex.x,vertex.y,vertex.z,position);
vboID = vboIDList.get(0);
float[] vert = new float[]{vertex.x, vertex.y, vertex.z};
subDataF = MemoryUtil.memAllocFloat(3); // !!! Must Be manually freed!
subDataF.put(vert).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferSubData(GL_ARRAY_BUFFER, position * 3 * 4, subDataF);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
vboID = vboIDList.get(1);
int[] index = new int[]{ position };
subDataI = MemoryUtil.memAllocInt(1); // !!! Must Be manually freed!
subDataI.put(index).flip();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, position * 4, subDataI);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
finally
{
if (subDataF != null)
{
MemoryUtil.memFree(subDataF);
}
if (subDataI != null)
{
MemoryUtil.memFree(subDataI);
}
}
}
}
我也知道它没有优化我创建floatbuffer和intbuffer的方式,我只是想在清理之前让它工作。我尝试了很多东西,所以最后一段代码很奇怪。
但我还是不明白我做错了什么。我确实检查了我正确地传递了所有的数据,并且位置(和偏移量)似乎是如何计算他们应该是。而当我使用glBufferData时,它会绘制任何东西。
谁能解释一下我错在哪?
在所有的建议之后,下面是我最后得到的,但它仍然完全不起作用:
public void addVertex(Vector3f vertex, int position)
{
if (meshType == 1)
{
FloatBuffer subDataF = null;
IntBuffer subDataI = null;
int vboID;
lineCount = position+1;
try
{
System.out.printf("Adding vertex (%f, %f, %f) to position %d\n",vertex.x,vertex.y,vertex.z,position);
vboID = vboIDList.get(0);
float[] vert = new float[]{vertex.x, vertex.y, vertex.z};
subDataF = MemoryUtil.memAllocFloat(3); // !!! Must Be manually freed!
subDataF.put(vert).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferSubData(GL_ARRAY_BUFFER, (long)(position * 3 * 4), (FloatBuffer)subDataF);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
vboID = vboIDList.get(1);
int[] index = new int[]{ position };
subDataI = MemoryUtil.memAllocInt(1); // !!! Must Be manually freed!
subDataI.put(index).flip();
glBindVertexArray(vaoID);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, (long)(position * 4), (IntBuffer)subDataI);
}
finally
{
if (subDataF != null)
{
MemoryUtil.memFree(subDataF);
}
if (subDataI != null)
{
MemoryUtil.memFree(subDataI);
}
}
}
}
我确实检查了VAO ID是否正确。
正如我所想的那样,这是一件愚蠢的事情,与VAO绑定之类的根本没有联系。
问题是,当我最初创建VBO时,我是这样做的:
// Vertices VBO generation
vboID = glGenBuffers();
vboIDList.add(vboID);
verticesBuffer = MemoryUtil.memAllocFloat(size * 3); // !!! Must Be manually freed!
verticesBuffer.put(vertex).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
vertexAttrArrCount += 1;
我假设,因为我为大小*3的浮点分配了缓冲区,所以它的大小将是这个大小,并且当我将它放入VBO中时,它将分配大小*3*4字节,即足够用于大小*3的浮点。
结果没有!因为我只将一个顶点(3个浮点)放入缓冲区--它只会分配那么多的空间。所以当我以后尝试使用glBufferSubData时--它在GPU上只有3个浮点的空格,自然不会把值放在我需要的地方。我真的很惊讶它没有完全撞到我身上。
为了解决这个问题,我现在做了这样的事情:
// Vertices VBO generation
...
verticesBuffer.put(vertex).put(new float[size*3 - 3]).flip();
...
所以基本上,我手动将一个空数组放入FloatBuffer中,这样可以确保缓冲区的大小正确。
结果如下:构造函数:
public Mesh(float[] vertex, int size)
{
texture = null;
meshType = 1; //will draw lines
FloatBuffer verticesBuffer = null;
IntBuffer indicesBuffer = null;
int vboID;
try
{
vertexCount = size;
vaoID = glGenVertexArrays();
glBindVertexArray(vaoID);
vboIDList = new ArrayList<>();
// Vertices VBO generation
vboID = glGenBuffers();
vboIDList.add(vboID);
verticesBuffer = MemoryUtil.memAllocFloat(size * 3); // !!! Must Be manually freed!
verticesBuffer.put(vertex).put(new float[size*3 - 3]).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
vertexAttrArrCount += 1;
// Indices VBO generation
vboID = glGenBuffers();
vboIDList.add(vboID);
indicesBuffer = MemoryUtil.memAllocInt(size); // !!! Must be manually freed!
indicesBuffer.put(new int[size]).flip(); // I need the first element 0 anyway
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
// unbinding
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
finally
{
if (verticesBuffer != null)
{
MemoryUtil.memFree(verticesBuffer); // Freeing vertex buffer
}
if (indicesBuffer != null)
{
MemoryUtil.memFree(indicesBuffer); // Freeing indices buffer
}
}
}
然后更新:
public void addVertex(Vector3f vertex, int position)
{
if (meshType == 1)
{
FloatBuffer subDataF = null;
IntBuffer subDataI = null;
int vboID;
lineCount = position+1;
try
{
vboID = vboIDList.get(0);
float[] vert = new float[]{vertex.x, vertex.y, vertex.z};
subDataF = MemoryUtil.memAllocFloat(vert.length); // !!! Must Be manually freed!
subDataF.put(vert).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferSubData(GL_ARRAY_BUFFER, (long)(position * 3 * 4), (FloatBuffer)subDataF);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
vboID = vboIDList.get(1);
int[] index = new int[]{ position };
subDataI = MemoryUtil.memAllocInt(index.length); // !!! Must Be manually freed!
subDataI.put(index).flip();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, (long)(position * 4), (IntBuffer)subDataI);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
finally
{
if (subDataF != null)
{
MemoryUtil.memFree(subDataF);
}
if (subDataI != null)
{
MemoryUtil.memFree(subDataI);
}
}
}
}
而且起作用了。请注意,代码有点脏,我没有清理它之前张贴一个答案。
我真的无法解决这个问题。首先,我从STS创建了一个Spring模板项目(Spring MVC)。 然后我完全复制了http://blog.terrencemiao.com/archives/spring-3-shacks-up-jsf-2-the-maverick-way报告的代码,它对我不起作用。 我认为那段代码中至少有一个错误,那就是“应用程序onfigure.xml应该是应用程序ontext
我的gradle构建: 处理器不在单独的模块中。 处理器不做任何事情,在< code>#process中,它只是抛出,看看它是否在工作。 但绝对没有发生任何事情。没有错误,什么都没有。我怎样才能使它工作?
一切工作正常,正如预期的那样,但是当我添加@Builder注释时: 在生成过程中,我得到以下错误: 这是龙目虫还是我做错了什么?
问题内容: 我有以下课程: :和: :但是当我用: :我收到错误消息: :有人能对此有所启发吗? 问题答案: 从那以后我就知道了。您需要先运行: java -DoutputDirectory =。-cp。; activejdbc-instrumentation.jar; javassist.jar; activejdbc.jar; activejdbc.instrumentation.Main
我现在正在尝试复制一个名为的数据库。在MySQL命令行客户机中,我正在执行我得到错误消息: 说吧,告诉我我很蠢。我只是需要一些帮助,拜托。