简体   繁体   中英

Incorrectly updated uniform buffer

I have a uniform buffer in shaders that looks like this:

layout (std140) uniform transform {
     mat4 projection;
     mat4 cam;
     bool enabledCam;
};

Subsequently, I use a variable "enabledCam" to determine whether to apply the camera matrix to the object.

if (enabledCam) {
    gl_Position = projection * cam * model * vec4(pos, 1.0f);
} else {
    gl_Position = projection * model * vec4(pos, 1.0f);
}

Until "enabledCam" was just a uniform variable, everything worked, but when transferring it to the buffer does not work as it should. I update the variable in the code like this:

public void setEnabledCam(boolean enabledCam) {
    glBindBuffer(GL_UNIFORM_BUFFER, bufferID);
    ByteBuffer bf = BufferUtils.createByteBuffer(4);
    bf.putInt(enabledCam ? 1 : 0);
    bf.flip();
    glBufferSubData(GL_UNIFORM_BUFFER, 128, bf);
}

Buffer initialization:

int blockID = glGetUniformBlockIndex(programId, "transform");
if (blockID != -1) {
    GL31.glUniformBlockBinding(programId, blockID, 0);
    bufferID = glGenBuffers();
    glBindBuffer(GL_UNIFORM_BUFFER, bufferID);
    glBufferData(GL_UNIFORM_BUFFER, 132, GL_STATIC_DRAW);
    GL30.glBindBufferBase(GL_UNIFORM_BUFFER, 0, bufferID); 
}

It updates, but for all objects at once, although I update it before rendering each object. Either everyone is affected by the camera or not. Two other variables from the buffer - matrices work fine.

See OpenGL 4.6 API Core Profile Specification - 7.6.2.1 Uniform Buffer Object Storage :

Members of type bool , int, uint, float, and double are respectively extracted from a buffer object by reading a single uint , int, uint, float, or double value at the specified offset.

This means that you need to set a 4-byte value (unsigned 32-bit) in the buffer for a Boolean value. Convert a Boolean value to a 32-bit integral value 0 or 1 and store it in the buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM