/// Copyright (c) 2009 Ruben Penalva. All rights reserved.
/// Redistribution and use in source and binary forms, with or without modification, 
/// are permitted provided that the following conditions are met:
///    1. Redistributions of source code must retain the above copyright notice, this 
///    list of conditions and the following disclaimer.
///    2. Redistributions in binary form must reproduce the above copyright notice,
///    this list of conditions and the following disclaimer in the documentation and/or
///    other materials provided with the distribution.
///    3. The name of the author may not be used to endorse or promote products derived
///    from this software without specific prior written permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
/// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
/// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 
/// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
/// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
/// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
/// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/// @file OpenglGpuShader.cpp
/// This source file is part of Gpu Quadratic Curve Render Test.
/// The file holds the OpenglGpuShader class implementation.
/// @author Ruben Penalva (ruben.penalva@rpenalva.com)

#include "OpenglGpuShader.h"

#include "UnicodeString.h"
#include <fstream>

using namespace GpuQuadraticCurveRender;

OpenglGpuShader::OpenglGpuShader(const std::wstring& shaderFile, 
                                 GpuShaderStage stage) :	m_glShader(LoadShaderFromFile(shaderFile, stage)) 
{
}

OpenglGpuShader::~OpenglGpuShader(void)
{
  glDeleteShader(m_glShader);
}
  
GLint OpenglGpuShader::GetGlShaderId(void) const
{
  return m_glShader;
}

GLint OpenglGpuShader::LoadShaderFromFile(const std::wstring& shaderFile, GpuShaderStage stage)
{
	std::string source;
	ReadSourceFile(shaderFile, source);

	GLenum shaderType;
	if (stage == Vertex)
	{
		shaderType = GL_VERTEX_SHADER;
	}
	else if (stage == Pixel)
	{
		shaderType = GL_FRAGMENT_SHADER;
	}

	GLuint shaderId = glCreateShader(shaderType);

	int size = (int)source.size();
	const char* sourceChar = source.c_str();
	glShaderSource(shaderId, 1, (const GLchar**)&sourceChar, (GLint*)&size);
	glCompileShader(shaderId);

	// Check if the compilation was ok
	int compileStatus;
	glGetShaderiv(shaderId, GL_COMPILE_STATUS, &compileStatus);
	if (!compileStatus)
	{
		int length = 0;
		glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &length);

		char* infoLog = new char[length];

		glGetShaderInfoLog(shaderId, length, &length, infoLog);

		std::wstring exceptionError(ConvertUTF8ToUTF16(infoLog));
		exceptionError = L"[ERROR] Compiling shader error: " + exceptionError;
		
		delete[] infoLog;

		throw exceptionError;
	}

	return shaderId;
}

void OpenglGpuShader::ReadSourceFile(const std::wstring& shaderFile, std::string& source)
{
 /// Load the file
	std::ifstream fileStream(shaderFile.c_str());
	if (!fileStream)
	{
		throw L"[ERROR] Shader file couldn't be loaded. Method ReadSourceFile";
	}

	std::string line;
	while (std::getline(fileStream, line))
	{
		source += "\n" + line;
	}
}