You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

144 lines
3.2 KiB

#include "Shader.h"
#include <string>
#include "ExternalLib/glad.h"
/******************
* Constructors *
******************/
Shader::Shader(GLenum const &shaderType_)
{
if (shaderType_ == GL_VERTEX_SHADER) //shaderType <=> const unsigned int &
shaderType = VERTEX;
else if (shaderType_ == GL_FRAGMENT_SHADER)
shaderType = FRAGMENT;
else if (shaderType_ == GL_GEOMETRY_SHADER)
shaderType = GEOMETRY;
else // unknown shader type
std::cerr << "ERROR::SHADER UNKNOWN SHADER TYPE : " << shaderType << std::endl;
shaderId = glCreateShader(shaderType_); // Careful Shader must be initialized withing a glContext
}
/******************
* Destructor *
******************/
Shader::~Shader()
{
// DO NOT CALL ANY GL CONTEXT DEPENDANT METHOD HERE !
// AS ANONYMOUS INSTANCES OF SHADER COULD PRODUCE UNEXPECTED BEHAVIORS
}
void Shader::unload()
{
/**
* \brief mark shader for deletion (for it to be effective, shader must be detached first)
*/
glDeleteShader(shaderId);
}
bool Shader::loadFromFile(std::string const &filename)
{
/**
* \brief Load shader Code contained in "filename" file
* \param filename location of the shader source code file
*/
shaderSourceCodeLocation = filename; // keep sourceCode location in a variable (in order to reload it faster)
std::fstream shaderCode;
// Openning shader source file
shaderCode.open(filename);
if ( !shaderCode.good() )
{
std::cerr << "ERROR::SHADER::" << filename << " SOURCE CODE INVALID OR NOT FOUND @ " << filename << std::endl;
return false;
}
// Reading shader source file
std::stringstream sstream;
sstream << shaderCode.rdbuf();
shaderCode.close();
// Linking shader Code with Shader ID
shaderSourceCode = sstream.str();
const char *shaderSourceCodeArray = shaderSourceCode.c_str(); //TODO for some reason need intermediate variable to store string into char* array
glShaderSource(shaderId, 1, &shaderSourceCodeArray, nullptr );
// validate shader loading
return true;
}
bool Shader::compile()
{
/**
* \brief compile and test shader
* Assume sourceCode linked before
*/
// Compile shader
glCompileShader(shaderId);
// checking for compileShader errors
GLint success;
GLchar infoLog[512];
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &success);
if(!success)
{
//print compiling errors
glGetShaderInfoLog(shaderId, 512, nullptr, infoLog);
std::cerr << "ERROR::SHADER COMPILATION::" << shaderSourceCodeLocation << "\n" << infoLog << std::endl;
return false;
}
return true;
}
GLuint Shader::getId()
{
/**
* \brief return GLuint corresponding to the shader's ID
*/
return shaderId;
}
ShaderType Shader::getType()
{
/**
* \brief return the type of shader (Vertex, Fragment, Geometry)
*/
return shaderType;
}
std::string Shader::getSource()
{
/**
* \brief return sourceCode of shader in a string
*/
return shaderSourceCode;
}
std::string Shader::getSourceLocation()
{
/**
* \brief return sourceCode of shader in a string
*/
return shaderSourceCodeLocation;
}