我已经查看了许多教程和示例程序,但我仍然无法弄清楚我的统一块有什么问题。我试图使用viewMatrix渲染一些模型,当它在一个统一块之外时工作正常,但是将统一变量移动到一个块中会导致立方体完全透明。 (尽管如此,它似乎仍然会影响深度测试。)
作为概述,我现在创建一个UBO,将UBO绑定到预先选择的绑定点,然后对统一块执行相同操作。
以下是相关代码:
初始化:
Loader loader = new Loader();
Camera camera = new Camera();
MasterRenderer renderer = new MasterRenderer(loader);
renderer.prepareCameraUBO(camera, loader);
相关加载程序代码:
public int createUBO(int floatCount) {
//Generate UBO
System.out.println("Generating UBO.");
int ubo = GL15.glGenBuffers();
//Resize it and then unbind it
GL15.glBindBuffer(GL31.GL_UNIFORM_BUFFER, ubo);
GL15.glBufferData(ubo, floatCount * 4, GL15.GL_DYNAMIC_DRAW);
GL15.glBindBuffer(GL31.GL_UNIFORM_BUFFER, 0);
return ubo;
}
public void bindUBOToPoint(int uboID, int bindingPoint) {
GL15.glBindBuffer(GL31.GL_UNIFORM_BUFFER, uboID);
GL30.glBindBufferBase(GL31.GL_UNIFORM_BUFFER, bindingPoint, uboID);
GL15.glBindBuffer(GL31.GL_UNIFORM_BUFFER, 0);
}
public void updateUBOData(int ubo, int offset, float[] data) {
GL15.glBindBuffer(GL31.GL_UNIFORM_BUFFER, ubo);
GL15.glBufferSubData(GL31.GL_UNIFORM_BUFFER, offset, storeDataInBuffer(data));
GL15.glBindBuffer(GL31.GL_UNIFORM_BUFFER, 0);
}
相关摄像头代码:
public void move(Player player, Loader loader) {
//We follow the player around and calculate the view matrix
updateViewMatrixUBO(loader);
}
public void updateViewMatrixUBO(Loader loader) {
loader.updateUBOData(uboID, 0, VC.convertMatrixToFloatArray(viewMatrix));
}
相关的MasterRenderer代码:
public MasterRenderer(Loader loader) {
enableCulling();
createProjectionMatrix();
entityShader = new EntityShader();
terrainShader = new TerrainShader();
entityRenderer = new EntityRenderer(entityShader, projectionMatrix, loader);
terrainRenderer = new TerrainRenderer(terrainShader, projectionMatrix);
}
public void prepareCameraUBO(Camera camera, Loader loader) {
//Create a UBO for the camera matrices.
int bindingPoint = 0;
int cameraUBO = loader.createUBO(16);
loader.bindUBOToPoint(cameraUBO, bindingPoint);
camera.setUBOID(cameraUBO);
entityShader.bindUniformBlockToPoint(cameraUBO, bindingPoint, "Camera");
terrainShader.bindUniformBlockToPoint(cameraUBO, bindingPoint, "Camera");
}
相关着色器程序(Java)代码:
public void bindUniformBlockToPoint(int bindingPoint, String blockName) {
GL20.glUseProgram(programID);
GL31.glUniformBlockBinding(programID, GL31.glGetUniformBlockIndex(programID, blockName), bindingPoint);
GL20.glUseProgram(0);
}
相关着色器(GLSL)代码:
layout (std140, row_major) uniform Camera {
mat4 viewMatrix;
};