I’m having trouble drawing objects with uniformbuffers multiple times per frame (mapping the buffer calling glDrawElements, mapping to the buffer and calling glDrawElements again).
Generating the buffer:
public UniformBuffer(GL4 gl, int buffer_draw, int binding_index, float[] data)
{
m_handle = new int[1];
FloatBuffer buffer_data = SNumberUtils.convertToFloatBuffer(data);
gl.glGenBuffers(1, m_handle, 0);
gl.glBindBuffer(GL4.GL_UNIFORM_BUFFER, m_handle[0]);
gl.glBufferData(GL4.GL_UNIFORM_BUFFER, data.length * Buffers.SIZEOF_FLOAT, buffer_data, buffer_draw);
gl.glBindBuffer(GL4.GL_UNIFORM_BUFFER, 0);
}
Mapping the buffer:
gl.glBindBufferBase(GL4.GL_UNIFORM_BUFFER, block_index, m_handle[0]);
gl.glUniformBlockBinding(program, bind_location, block_index);
gl.glMapBuffer(GL4.GL_UNIFORM_BUFFER, GL4.GL_WRITE_ONLY).asFloatBuffer().put(data);
gl.glUnmapBuffer(GL4.GL_UNIFORM_BUFFER);
Then binding before glDrawElements():
gl.glBindBufferBase(GL4.GL_UNIFORM_BUFFER, block_index, m_handle[0]);
gl.glUniformBlockBinding(program, bind_location, block_index);
render:
public void render(GL4 gl, GLU glu)
{
if(m_ibo == null) return;
bindTextures(gl);
map(gl);
gl.glPolygonMode(m_face, m_mode);
gl.glEnable(GL4.GL_DEPTH_TEST);
gl.glEnable(GL4.GL_CULL_FACE);
gl.glEnable(GL4.GL_BLEND);
gl.glBlendFunc(GL4.GL_SRC_ALPHA, GL4.GL_ONE_MINUS_SRC_ALPHA);
Matrix4f projection_mat = m_camera.getProjectionMatrix();
float[] projection_array = projection_mat.get(new float[16], 0);
Matrix4f view_mat = new Matrix4f().identity();
float[] view_array = view_mat.get(new float[16], 0);
m_model_ubo.bind(gl, m_shader.getUniformBlockLocation("model_matrix_buff"), UniformBuffer.MODEL_INDEX, m_shader.getHandle());
m_joint_ubo.bind(gl, m_shader.getUniformBlockLocation("anim_joint_buff"), UniformBuffer.JOINT_INDEX, m_shader.getHandle());
m_inv_bind_ubo.bind(gl, m_shader.getUniformBlockLocation("inv_bind_buff"), UniformBuffer.INVERSE_BIND_INDEX, m_shader.getHandle());
m_vao.bind(gl);
m_ibo.bind(gl);
m_shader.update(gl);
m_shader.setUniformMatrix4f(gl, false, "projection_matrix", projection_array);
m_shader.setUniformMatrix4f(gl, false, "view_matrix", view_array);
gl.glDrawElements(GL4.GL_TRIANGLES, m_vert_count, GL4.GL_UNSIGNED_INT, 0);
m_ibo.unbind(gl);
m_vao.unbind(gl);
m_inv_bind_ubo.unbind(gl);
m_joint_ubo.unbind(gl);
m_model_ubo.unbind(gl);
//gl.glFinish();
}
This way the only objects that end up on screen are the last ones to get drawn, if I add glFinish() to the end of each render pass then I get all of the objects on screen.
Edit: code formatting