/*
PolyGun

Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "game/renderer/gl/gl_mesh.hpp"

#include "game/renderer/gl/opengl.hpp"

using namespace polygun::renderer;

GLMesh::GLMesh() :
	m_vao(0),
	m_vbo(0),
	m_tbo(0),
	m_nbo(0),
	m_ebo(0),
	m_element_count(0),
	m_custom_buffers()
{
	if(GLEW_ARB_vertex_array_object) {
		glGenVertexArrays(1, &m_vao);
		CHECK_GL;
	}
}

GLMesh::~GLMesh() {
	if(GLEW_ARB_vertex_array_object) {
		glDeleteVertexArrays(1, &m_vao);
		CHECK_GL;
	}
	if(m_vbo>0) {
		glDeleteBuffers(1, &m_vbo);
		CHECK_GL;
	}
	if(m_ebo>0) {
		glDeleteBuffers(1, &m_ebo);
		CHECK_GL;
	}
	if(m_nbo>0) {
		glDeleteBuffers(1, &m_nbo);
		CHECK_GL;
	}
	for(MeshBuffer& buffer : m_custom_buffers) {
		glDeleteBuffers(1, &buffer.m_id);
		CHECK_GL;
	}
}

void GLMesh::add_custom_buffer(const void* data, unsigned size, unsigned char data_length, misc::VarType data_type) {
	if(GLEW_ARB_vertex_array_object) {
		glBindVertexArray(m_vao);
		CHECK_GL;
	}
	unsigned id;
	glGenBuffers(GL_ARRAY_BUFFER, &id);
	CHECK_GL;
	glBindBuffer(GL_ARRAY_BUFFER, id);
	CHECK_GL;
	glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
	CHECK_GL;
	if(GLEW_ARB_vertex_array_object) {
		unsigned attribute_index = calculate_custom_buffer_attribute_index(m_custom_buffers.size()-1);
		glVertexAttribPointer(attribute_index, data_length, opengl::var_type_to_glenum(data_type), GL_FALSE, data_length*misc::var_type_size(data_type), (void*)0);
		CHECK_GL;
		glEnableVertexAttribArray(attribute_index);
		CHECK_GL;
	}
	else
		m_custom_buffers.push_back(MeshBuffer{id, data_length, data_type});
}

unsigned GLMesh::calculate_custom_buffer_attribute_index(unsigned buffer_index) {
	unsigned result = 1;
	if(has_texture_uvs())
		result++;
	if(has_normals())
		result++;
	return result+buffer_index;
}

void GLMesh::load_vertices(const std::vector<float>& vertices) {
	if(GLEW_ARB_vertex_array_object) {
		glBindVertexArray(m_vao);
		CHECK_GL;
	}

	if(m_vbo==0) {
		glGenBuffers(1, &m_vbo);
		CHECK_GL;
	}
	glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
	CHECK_GL;
	glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(float), vertices.data(), GL_STATIC_DRAW);
	CHECK_GL;
	if(GLEW_ARB_vertex_array_object) {
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
		CHECK_GL;
		glEnableVertexAttribArray(0);
		CHECK_GL;
	}
}

void GLMesh::load_indices(const std::vector<unsigned>& indices) {
	if(m_ebo==0) {
		glGenBuffers(1, &m_ebo);
		CHECK_GL;
	}
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
	CHECK_GL;
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(unsigned), indices.data(), GL_STATIC_DRAW);
	CHECK_GL;
	m_element_count = indices.size();
}

void GLMesh::load_uvs(const std::vector<float>& uvs) {
	if(m_tbo==0) {
		glGenBuffers(1, &m_tbo);
		CHECK_GL;
	}
	glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
	CHECK_GL;
	glBufferData(GL_ARRAY_BUFFER, uvs.size()*sizeof(float), uvs.data(), GL_STATIC_DRAW);
	CHECK_GL;
	if(GLEW_ARB_vertex_array_object) {
		glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), (void*)0);
		CHECK_GL;
		glEnableVertexAttribArray(1);
		CHECK_GL;
	}
}

void GLMesh::load_normals(const std::vector<float>& normals) {
	if(m_nbo==0) {
		glGenBuffers(1, &m_nbo);
		CHECK_GL;
	}
	glBindBuffer(GL_ARRAY_BUFFER, m_nbo);
	CHECK_GL;
	glBufferData(GL_ARRAY_BUFFER, normals.size()*sizeof(float), normals.data(), GL_STATIC_DRAW);
	CHECK_GL;
	if(GLEW_ARB_vertex_array_object) {
		glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
		CHECK_GL;
		glEnableVertexAttribArray(2);
		CHECK_GL;
	}
}
