#include "color_quad.h"

/* Draws a colored quad. */
void
color_quad_draw(Renderer* renderer, Shader* shader, mat4 model, vec2 size, vec4 color)
{
	mat4 view;
	mat4 projection;

	f32 vertices[4][3] = 
	{
		{0.0f   , 0.0f   , 0.0f},
		{0.0f   , size[1], 0.0f},
		{size[0], size[1], 0.0f},
		{size[0], 0.0f   , 0.0f},
	};

	u32 indices[2][3] = 
	{
		{1, 2, 3},
		{0, 1, 3}
	};

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	camera_view_get(&renderer->camera, view);
	camera_projection_get(&renderer->camera, projection);

	vao_bind(&renderer->vao);

	vbo_bind(&renderer->vbo);
	vbo_buffer(&renderer->vbo, sizeof(vertices), vertices);

	vbo_bind(&renderer->ebo);
	vbo_buffer(&renderer->ebo, sizeof(indices), indices);

	vertex_attribute_set(0, 3, GL_FLOAT, 3 * sizeof(float), 0);

	vao_bind(&renderer->vao);

	renderer_shader_use(renderer, shader);

	shader_uniform_mat4_set(shader, COLOR_QUAD_UNIFORM_VIEW, view);
	shader_uniform_mat4_set(shader, COLOR_QUAD_UNIFORM_PROJECTION, projection);
	shader_uniform_mat4_set(shader, COLOR_QUAD_UNIFORM_MODEL, model);
	shader_uniform_vec4_set(shader, COLOR_QUAD_UNIFORM_COLOR, color);

	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
	
	vao_unbind();
}	
