/*
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/legacy_gl/legacy_gl_mesh_renderer.hpp"

#include "game/renderer/camera.hpp"
#include "game/renderer/legacy_gl/legacy_gl_mesh.hpp"
#include "game/renderer/gl/gl_texture.hpp"
#include "game/renderer/gl/opengl.hpp"
#include "common/logger.hpp"

using namespace polygun::renderer;

LegacyGLMeshRenderer::LegacyGLMeshRenderer() :
	m_prev_texture(nullptr),
	m_color_buffer(nullptr),
	m_prev_color()
{}

LegacyGLMeshRenderer::~LegacyGLMeshRenderer() {
	if(m_color_buffer)
		delete[] m_color_buffer;
}

void LegacyGLMeshRenderer::set_color_mod(const glm::vec4& color, Mesh* mesh) {
	LegacyGLMesh* gl_mesh = static_cast<LegacyGLMesh*>(mesh);
	if(m_color_buffer)
		delete[] m_color_buffer;
	m_color_buffer = new float[gl_mesh->m_vertices_length/3*4];
	for(size_t  i = 0; i<gl_mesh->m_vertices_length/3; i++) {
		m_color_buffer[i*4] = color.r;
		m_color_buffer[i*4+1] = color.g;
		m_color_buffer[i*4+2] = color.b;
		m_color_buffer[i*4+3] = color.a;
	}
	glColorPointer(4, GL_FLOAT, 0, m_color_buffer);
	CHECK_GL;
	m_prev_color = color;
}

void LegacyGLMeshRenderer::render(Mesh* mesh, const glm::vec4& color) {
	LegacyGLMesh* gl_mesh = static_cast<LegacyGLMesh*>(mesh);
	glEnableClientState(GL_VERTEX_ARRAY);
	CHECK_GL;
	glVertexPointer(3, GL_FLOAT, 0, gl_mesh->m_vertices);
	CHECK_GL;
	glEnableClientState(GL_COLOR_ARRAY);
	CHECK_GL;
	if(color!=m_prev_color)
		set_color_mod(color, mesh);
	if(mesh->has_normals()) {
		glEnableClientState(GL_NORMAL_ARRAY);
		CHECK_GL;
		glNormalPointer(GL_FLOAT, 0, gl_mesh->m_normals);
		CHECK_GL;
	}
	glMatrixMode(GL_MODELVIEW);
	CHECK_GL;
	glLoadMatrixf(&m_transform[0][0]);
	CHECK_GL;
	glMatrixMode(GL_PROJECTION);
	CHECK_GL;
	glLoadMatrixf(&m_camera->get_projection()[0][0]);
	CHECK_GL;
	glMultMatrixf(&m_camera->get_view()[0][0]);
	CHECK_GL;
	glDrawElements(GL_TRIANGLES, gl_mesh->m_indices_length, GL_UNSIGNED_INT, gl_mesh->m_indices);
	CHECK_GL;
	if(mesh->has_normals()) {
		glDisableClientState(GL_NORMAL_ARRAY);
		CHECK_GL;
	}
	glDisableClientState(GL_VERTEX_ARRAY);
	CHECK_GL;
	glDisableClientState(GL_COLOR_ARRAY);
	CHECK_GL;
	m_transform = glm::mat4(1);
}

void LegacyGLMeshRenderer::render_textured(Mesh* mesh, Texture* texture) {
	if(!mesh->has_texture_uvs()) {
		LOG_ERROR("Attempt to render mesh without texture as textured!");
		return;
	}
	LegacyGLMesh* gl_mesh = static_cast<LegacyGLMesh*>(mesh);
	glEnableClientState(GL_VERTEX_ARRAY);
	CHECK_GL;
	glVertexPointer(3, GL_FLOAT, 0, gl_mesh->m_vertices);
	CHECK_GL;
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	CHECK_GL;
	glTexCoordPointer(2, GL_FLOAT, 0, gl_mesh->m_uvs);
	CHECK_GL;
	if(mesh->has_normals()) {
		glEnableClientState(GL_NORMAL_ARRAY);
		CHECK_GL;
		glNormalPointer(GL_FLOAT, 0, gl_mesh->m_normals);
		CHECK_GL;
	}
	glEnable(GL_TEXTURE_2D);
	CHECK_GL;
	if(texture!=m_prev_texture) {
		GLTexture* gl_texture = static_cast<GLTexture*>(texture);
		gl_texture->bind();
		m_prev_texture = texture;
	}
	glMatrixMode(GL_MODELVIEW);
	CHECK_GL;
	glLoadMatrixf(&m_transform[0][0]);
	CHECK_GL;
	glMatrixMode(GL_PROJECTION);
	CHECK_GL;
	glLoadMatrixf(&m_camera->get_projection()[0][0]);
	CHECK_GL;
	glMultMatrixf(&m_camera->get_view()[0][0]);
	CHECK_GL;
	glDrawElements(GL_TRIANGLES, gl_mesh->m_indices_length, GL_UNSIGNED_INT, gl_mesh->m_indices);
	CHECK_GL;
	glDisable(GL_TEXTURE_2D);
	CHECK_GL;
	if(mesh->has_normals()) {
		glDisableClientState(GL_NORMAL_ARRAY);
		CHECK_GL;
	}
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	CHECK_GL;
	glDisableClientState(GL_VERTEX_ARRAY);
	CHECK_GL;
	m_transform = glm::mat4(1);
}

void LegacyGLMeshRenderer::set_3d_rendering_mode(bool enable) {
	if(enable)
		glEnable(GL_DEPTH_TEST);
	else
		glDisable(GL_DEPTH_TEST);
	CHECK_GL;
}

void LegacyGLMeshRenderer::set_alpha_blending_mode(bool enable) {
	if(enable) {
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	}
	else
		glDisable(GL_BLEND);
}
