//-----------------------------------------------------------------------------------------------
// OpenGLShaders.cpp
//-----------------------------------------------------------------------------------------------
#include "GypsyPrecompiledHeader.hpp"
#include "Engine/Renderer/OpenGLShaders.hpp"

/*
/////////////////////////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------------------------
OpenGLShader::OpenGLShader( ShaderType shaderType, const std::string& shaderFileName, const std::string& shaderFilePath )
	: m_shaderFileName( shaderFileName )
	, m_shaderFilePath( shaderFilePath )
	, m_shaderType( shaderType )
	, m_shaderID( 0 )
{
	LoadFromFile();
}


//-----------------------------------------------------------------------------------------------
void OpenGLShader::LoadFromFile()
{
	unsigned char* shaderTextBuffer = NULL;
	int bytesRead = LoadFileToNewBuffer( m_shaderFilePath, shaderTextBuffer, true );
	if( bytesRead > 0 )
	{
		m_shaderID = glCreateShader( m_shaderType );
///		glShaderSource( m_shaderID, 1, (const GLchar**) &shaderText, NULL );
		char* shaderText = (char*) shaderTextBuffer;
		glShaderSource( m_shaderID, 1, &shaderText, NULL );
		glCompileShader( m_shaderID );

		GLint wasSuccessful;
		glGetShaderiv( m_shaderID, GL_COMPILE_STATUS, &wasSuccessful );
		if( wasSuccessful != GL_TRUE )
		{
			ReportShaderError( m_shaderType, m_shaderID, (char*) shaderText, m_shaderFileName, m_shaderFilePath );
		}
	}
	else
	{
		const char* shaderTypeName = NULL;
		switch( m_shaderType )
		{
			case GL_VERTEX_SHADER:		shaderTypeName = "vertex";		break;
			case GL_FRAGMENT_SHADER:	shaderTypeName = "fragment";	break;
			case GL_GEOMETRY_SHADER:	shaderTypeName = "geometry";	break;
			default:					shaderTypeName = "UNKNOWN";		break;
		}

		const char* errorTypePhrase = ( bytesRead == 0 ) ? "was empty" : "was not found";
		const std::string errorTitle = Stringf( "GLSL missing %s shader %s", shaderTypeName, m_shaderFileName.c_str() );
		const std::string errorMessage = Stringf( "ERROR: The GLSL %s shader \"%s\" %s.\n\nThe expected location was:\n  %s\n", shaderTypeName, m_shaderFileName.c_str(), errorTypePhrase, m_shaderFilePath.c_str() );
		FatalError( errorTitle, errorMessage );
	}

	delete[] shaderTextBuffer;
}





/////////////////////////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------------------------
OpenGLShaderProgram::OpenGLShaderProgram( OpenGLShader* vertexShader, OpenGLShader* fragmentShader )
	: m_vertexShader( vertexShader )
	, m_fragmentShader( fragmentShader )
	, m_programID( 0 )
{
	m_programID = glCreateProgram();
	glAttachShader( m_programID, m_vertexShader->m_shaderID );
	glAttachShader( m_programID, m_fragmentShader->m_shaderID );
	glLinkProgram( m_programID );

	GLint wasSuccessful;
	glGetProgramiv( m_programID, GL_LINK_STATUS, &wasSuccessful );
	if( wasSuccessful != GL_TRUE )
	{
		ReportShaderProgramError( m_programID, vertexShader, fragmentShader );
	}

	glUseProgram( m_programID );
}
*/

