简体   繁体   English

LWJGL-统一缓冲区对象不起作用

[英]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. 在UBO之前一切正常。 This is fully temporary system. 这是完全临时的系统。 I just wanted to get it working. 我只是想让它工作。

My UBO class: 我的UBO课程:

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. 在实施大量的糖代码之前,请确保您已将其工作,然后逐步转移至util类,依此类推。

So, get rid of the UBO class and in the init: 因此,摆脱UBO类并在init中:

  • glGenBuffers , glBufferData , glBindBufferBase (later -range , it involves some additional offset stuff) glGenBuffersglBufferDataglBindBufferBase (稍后-range ,它涉及一些其他偏移量的东西)

  • then set up your shader, be sure is >= 0 and glGetUniformBlockIndex != -1 and then glUniformBlockBinding 然后设置您的着色器,确保> = 0并设置glGetUniformBlockIndex != -1,然后设置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. 在初始化中分配一个float缓冲区,不要每次在render循环中都这样做。

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. 然后执行for并将viewMatrix保存在缓冲区中。

  • bind your buffer, glBufferSubData and unbind 绑定缓冲区glBufferSubData并取消绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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