我试图在VBO中获取顶点以生成拟合边界框,但从glMapBuffer返回的值是随机的。
这是我的代码:
//Data (Quad):
FloatBuffer vertexData = FloatBuffer.wrap(new float[] {
-0.5f, -0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
});
存储顶点:
//Generating IDs
int vaoID = glGenVertexArrays();
glBindVertexArray(vaoID);
int vboID = glGenBuffers();
//Storing vertex data in attribute list
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, vertexData , GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
//Unbinding
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
加载顶点:
//Binding VAO and VBO
glBindVertexArray(vaoID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
ByteBuffer vertexBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
//Printing values
//vertices.flip(); //Do I have to flip it? No, right?
System.out.println("Capacity: " + vertices.capacity() + ", Limit: " + vertices.limit());
//Capacity: 48, Limit: 48, Position: 0
while(vertices.remaining() != 0) {
System.out.print("x: " + vertices.get());
System.out.print(", y: " + vertices.get());
System.out.println(", z: " + vertices.get());
}
//Unbinding
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
输出是随机值(大多数只是零但有时是随机的)。 GlGetError()返回0(无错误)。
这是生成边界框还是愚蠢的合理方法?为什么值是随机的?
答案 0 :(得分:0)
除非这是你没有展示的内容:glMapBuffer
返回ByteBuffer
,所以你不是一次得到一个组成每个浮点数的4个字节吗?因此,请尝试使用FloatBuffer
将其投放到vertices.asFloatBuffer
,或使用getFloat()
代替get()
。