#include "uniform_buffer.h"


bool init_uniform_buffer(Uniform_Buffer *uniform_buffer) {
  glGenBuffers(1, &uniform_buffer->ubo_buffer);
  glBindBuffer(GL_UNIFORM_BUFFER, uniform_buffer->ubo_buffer);
  glBufferData(GL_UNIFORM_BUFFER, 2 * sizeof(Matrix4), nullptr, GL_STATIC_DRAW);
  glBindBuffer(GL_UNIFORM_BUFFER, 0);

  return true;
}

void upload_uniform_buffer_data(Uniform_Buffer *uniform_buffer, Matrix4 view_matrix, Matrix4 projection_matrix) {
  glBindBuffer(GL_UNIFORM_BUFFER, uniform_buffer->ubo_buffer);
  glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(Matrix4), glm::value_ptr(view_matrix));
  glBufferSubData(GL_UNIFORM_BUFFER, sizeof(Matrix4), sizeof(Matrix4), glm::value_ptr(projection_matrix));
  glBindBufferRange(GL_UNIFORM_BUFFER, 0, uniform_buffer->ubo_buffer, 0, 2 * sizeof(Matrix4));
  glBindBuffer(GL_UNIFORM_BUFFER, 0);
}

void cleanup(Uniform_Buffer *uniform_buffer) {
  glDeleteBuffers(1, &uniform_buffer->ubo_buffer);
}
