简体   繁体   中英

LWJGL - Uniform buffer object doesn't work

I tried to implement uniform buffer object to my game, but for some reason I haven't got it working. I don't think it currently sends anything to shaders. Before UBO everything worked fine. This is fully temporary system. I just wanted to get it working.

My UBO class:

private int ubo;

public void createUBO() {

    ubo = GL15.glGenBuffers();

}

public void allocate(int bytes) {

    bindBuffer();

    GL15.glBufferData(GL31.GL_UNIFORM_BUFFER, bytes, GL15.GL_STATIC_DRAW);

    unbindBuffer();

}

public void updateUBO(FloatBuffer buffer) {

    buffer.flip();

    bindBuffer();

    GL15.glBufferSubData(GL31.GL_UNIFORM_BUFFER, 0, buffer);

    unbindBuffer();

}

My gameloop:

public void init() {

    ubo = new UBO();

    ubo.createUBO();
    ubo.allocate(64);

}

public void render() {

    basicEntityShader.start();
    basicEntityShader.loadFog(fogColor, fogDensity, fogGradient);
    basicEntityShader.loadLights(lights);

    Matrix4f viewMatrix = Maths.createViewMatrix(camera);
    FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
    viewMatrix.storeTranspose(buffer);

    ubo.updateUBO(buffer);

    basicEntityShader.setUBO(ubo);
    basicEntityShader.bindUniformBlocks();
    entityRenderer.render(entities, camera);

}

Shader:

layout (std140) uniform Matrices  {

    mat4 viewMatrix;

};

Bindings:

GL30.glBindBufferRange(GL31.GL_UNIFORM_BUFFER, 0, ubo.getUbo(), 0, 64);

int block = GL31.glGetUniformBlockIndex(programID, "Matrices");

GL31.glUniformBlockBinding(programID, block, 0);

Before implementing a lot of sugar code, make sure you got it working and then shift step by step to util classes and so on.

So, get rid of the UBO class and in the init:

  • glGenBuffers , glBufferData , glBindBufferBase (later -range , it involves some additional offset stuff)

  • then set up your shader, be sure is >= 0 and glGetUniformBlockIndex != -1 and then glUniformBlockBinding

  • get rid also of:

     basicEntityShader.setUBO(ubo); basicEntityShader.bindUniformBlocks(); 
  • allocate a floatbuffer in the init, don't do it every time in the render loop.

Now, in the render:

  • better to avoid consuming your buffers and deal with position set/reset or flip/rewind in order to reduce error exposure. Then do a for and save your viewMatrix inside your buffer.

  • bind your buffer, glBufferSubData and unbind

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