带浮点数和字节的交错 VBO
Interleaved VBO with Float and Byte
我想在交错的 OpenGL vbo 中存储三个浮点值和两个字节值。
不幸的是,渲染的数据显然不正确。
当我用两个不同的 VBO 渲染相同的数据时,一切都很好,所以我不认为我的着色器有问题。
/*
* 20^3 blocks per chunk, 6 faces per block, 3 vertices per face
* max. every second face can be visible
*/
private static final int MAX_FLOAT_AMOUNT = 20 * 20 * 20 * 6 * 3 / 2;
/*
* 20^3 blocks per chunk, 6 faces per block, 2 bytes per face
* max. every second face can be visible
*/
private static final int MAX_BYTE_AMOUNT = 20 * 20 * 20 * 6 * 2 / 2;
private int dataVboIndex;
protected int vaoId;
protected int indicesCount;
protected boolean isInitialized = false;
public static ByteBuffer dataFloatBuffer = BufferUtils.createByteBuffer(4 * MAX_FLOAT_AMOUNT + MAX_BYTE_AMOUNT);
DefaultChunkVao(int indiciesVboId) {
init();
}
DefaultChunkVao(boolean initialize) {
if(initialize) init();
}
private void init() {
isInitialized = true;
// bind vao
vaoId = glGenVertexArrays();
glBindVertexArray(vaoId);
//create vbo
dataVboIndex = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, dataVboIndex);
dataFloatBuffer.clear();
dataFloatBuffer.position(4 * MAX_FLOAT_AMOUNT + MAX_BYTE_AMOUNT);
dataFloatBuffer.flip();
// allocate memory
glBufferData(GL_ARRAY_BUFFER, dataFloatBuffer, GL_STREAM_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * 4 + 2, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 1, GL_UNSIGNED_BYTE, false, 3 * 4 + 2, 3 * 4);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_UNSIGNED_BYTE, false, 3 * 4 + 2, 3 * 4 + 1);
// unbind vao
glBindVertexArray(0);
}
public void updateData(float[] data) {
if(!isInitialized) init();
dataFloatBuffer.clear();
for(int counter = 0; counter < data.length; counter+=0) {
dataFloatBuffer.putFloat(data[counter++]);
dataFloatBuffer.putFloat(data[counter++]);
dataFloatBuffer.putFloat(data[counter++]);
dataFloatBuffer.put((byte) data[counter++]);
dataFloatBuffer.put((byte) data[counter++]);
}
dataFloatBuffer.flip();
glBindBuffer(GL_ARRAY_BUFFER, dataVboIndex);
glBufferSubData(GL_ARRAY_BUFFER, 0, dataFloatBuffer);
glBindBuffer(GL_ARRAY_BUFFER, 0);
this.indicesCount = data.length / 5;
}
MAX_FLOAT_AMOUNT
和 MAX_BYTE_AMOUNT
常量分别包含浮点数。每个 VBO 的字节数。我假设在确定 ByteBuffer
的容量时必须将浮点数乘以 4 是否正确,因为每个浮点数都有 4 个字节?
我做错了什么,我的字节值在着色器中始终为 0?
编辑:我能够用一个更简单的例子重现这个问题。这里我想在 VBO 中存储位置和两个字节。在片段着色器中,我检查 byte1 值是否正确传递。如果是这样,着色器将形状渲染为绿色,否则为蓝色。不幸的是,我的形状呈现为蓝色,因此我认为 byte1 值未正确传递。
顶点着色器
#version 400 core
in vec3 position;
in int byte1;
in int byte2;
flat out int p_byte1;
flat out int p_byte2;
void main(void) {
gl_Position = vec4(position, 1);
p_byte1 = byte1;
p_byte2 = byte2;
}
片段着色器:
#version 400 core
flat in int p_byte1;
flat in int p_byte2;
out vec3 out_color;
void main(void) {
if(p_byte1 == 45) {
out_color = vec3(0, 1, 0);
} else out_color = vec3(0, 0, 1);
}
正在创建 VAO:
vaoId = glGenVertexArrays();
glBindVertexArray(vaoId);
final int vbo = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
float[] data = {0, 0, 0, 20f, 20f, 1, 1, 1, 20f, 20f, 1, 0, 1, 20f, 20f, 0, 1, 1, 20f, 20f};
ByteBuffer dataBuffer = BufferUtils.createByteBuffer(4 * (3 * 4) + 1 * (2 * 4));
for(int counter = 0; counter < data.length; counter+=0) {
dataBuffer.putFloat(data[counter++]);
dataBuffer.putFloat(data[counter++]);
dataBuffer.putFloat(data[counter++]);
dataBuffer.put((byte) data[counter++]);
dataBuffer.put((byte) data[counter++]);
}
dataBuffer.flip();
glBufferData(GL_ARRAY_BUFFER, dataBuffer, GL_STREAM_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * 4 + 2, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 1, GL_UNSIGNED_BYTE, false, 3 * 4 + 2, 3 * 4);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_UNSIGNED_BYTE, false, 3 * 4 + 2, 3 * 4 + 1);
glBindVertexArray(0);
我找到了问题的解决方案。对于整数数据类型,必须使用 glVertexAttribIPointer
而不是 glVertexAttribPointer
。
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * 4 + 2, 0);
glEnableVertexAttribArray(1);
glVertexAttribIPointer(1, 1, GL_UNSIGNED_BYTE, 3 * 4 + 2, 3 * 4);
glEnableVertexAttribArray(2);
glVertexAttribIPointer(2, 1, GL_UNSIGNED_BYTE, 3 * 4 + 2, 3 * 4 + 1);
我想在交错的 OpenGL vbo 中存储三个浮点值和两个字节值。 不幸的是,渲染的数据显然不正确。 当我用两个不同的 VBO 渲染相同的数据时,一切都很好,所以我不认为我的着色器有问题。
/*
* 20^3 blocks per chunk, 6 faces per block, 3 vertices per face
* max. every second face can be visible
*/
private static final int MAX_FLOAT_AMOUNT = 20 * 20 * 20 * 6 * 3 / 2;
/*
* 20^3 blocks per chunk, 6 faces per block, 2 bytes per face
* max. every second face can be visible
*/
private static final int MAX_BYTE_AMOUNT = 20 * 20 * 20 * 6 * 2 / 2;
private int dataVboIndex;
protected int vaoId;
protected int indicesCount;
protected boolean isInitialized = false;
public static ByteBuffer dataFloatBuffer = BufferUtils.createByteBuffer(4 * MAX_FLOAT_AMOUNT + MAX_BYTE_AMOUNT);
DefaultChunkVao(int indiciesVboId) {
init();
}
DefaultChunkVao(boolean initialize) {
if(initialize) init();
}
private void init() {
isInitialized = true;
// bind vao
vaoId = glGenVertexArrays();
glBindVertexArray(vaoId);
//create vbo
dataVboIndex = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, dataVboIndex);
dataFloatBuffer.clear();
dataFloatBuffer.position(4 * MAX_FLOAT_AMOUNT + MAX_BYTE_AMOUNT);
dataFloatBuffer.flip();
// allocate memory
glBufferData(GL_ARRAY_BUFFER, dataFloatBuffer, GL_STREAM_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * 4 + 2, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 1, GL_UNSIGNED_BYTE, false, 3 * 4 + 2, 3 * 4);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_UNSIGNED_BYTE, false, 3 * 4 + 2, 3 * 4 + 1);
// unbind vao
glBindVertexArray(0);
}
public void updateData(float[] data) {
if(!isInitialized) init();
dataFloatBuffer.clear();
for(int counter = 0; counter < data.length; counter+=0) {
dataFloatBuffer.putFloat(data[counter++]);
dataFloatBuffer.putFloat(data[counter++]);
dataFloatBuffer.putFloat(data[counter++]);
dataFloatBuffer.put((byte) data[counter++]);
dataFloatBuffer.put((byte) data[counter++]);
}
dataFloatBuffer.flip();
glBindBuffer(GL_ARRAY_BUFFER, dataVboIndex);
glBufferSubData(GL_ARRAY_BUFFER, 0, dataFloatBuffer);
glBindBuffer(GL_ARRAY_BUFFER, 0);
this.indicesCount = data.length / 5;
}
MAX_FLOAT_AMOUNT
和 MAX_BYTE_AMOUNT
常量分别包含浮点数。每个 VBO 的字节数。我假设在确定 ByteBuffer
的容量时必须将浮点数乘以 4 是否正确,因为每个浮点数都有 4 个字节?
我做错了什么,我的字节值在着色器中始终为 0?
编辑:我能够用一个更简单的例子重现这个问题。这里我想在 VBO 中存储位置和两个字节。在片段着色器中,我检查 byte1 值是否正确传递。如果是这样,着色器将形状渲染为绿色,否则为蓝色。不幸的是,我的形状呈现为蓝色,因此我认为 byte1 值未正确传递。
顶点着色器
#version 400 core
in vec3 position;
in int byte1;
in int byte2;
flat out int p_byte1;
flat out int p_byte2;
void main(void) {
gl_Position = vec4(position, 1);
p_byte1 = byte1;
p_byte2 = byte2;
}
片段着色器:
#version 400 core
flat in int p_byte1;
flat in int p_byte2;
out vec3 out_color;
void main(void) {
if(p_byte1 == 45) {
out_color = vec3(0, 1, 0);
} else out_color = vec3(0, 0, 1);
}
正在创建 VAO:
vaoId = glGenVertexArrays();
glBindVertexArray(vaoId);
final int vbo = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
float[] data = {0, 0, 0, 20f, 20f, 1, 1, 1, 20f, 20f, 1, 0, 1, 20f, 20f, 0, 1, 1, 20f, 20f};
ByteBuffer dataBuffer = BufferUtils.createByteBuffer(4 * (3 * 4) + 1 * (2 * 4));
for(int counter = 0; counter < data.length; counter+=0) {
dataBuffer.putFloat(data[counter++]);
dataBuffer.putFloat(data[counter++]);
dataBuffer.putFloat(data[counter++]);
dataBuffer.put((byte) data[counter++]);
dataBuffer.put((byte) data[counter++]);
}
dataBuffer.flip();
glBufferData(GL_ARRAY_BUFFER, dataBuffer, GL_STREAM_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * 4 + 2, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 1, GL_UNSIGNED_BYTE, false, 3 * 4 + 2, 3 * 4);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_UNSIGNED_BYTE, false, 3 * 4 + 2, 3 * 4 + 1);
glBindVertexArray(0);
我找到了问题的解决方案。对于整数数据类型,必须使用 glVertexAttribIPointer
而不是 glVertexAttribPointer
。
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * 4 + 2, 0);
glEnableVertexAttribArray(1);
glVertexAttribIPointer(1, 1, GL_UNSIGNED_BYTE, 3 * 4 + 2, 3 * 4);
glEnableVertexAttribArray(2);
glVertexAttribIPointer(2, 1, GL_UNSIGNED_BYTE, 3 * 4 + 2, 3 * 4 + 1);