1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <string>
#include <fstream>
#include <vector>
bool fileToString(std::ifstream& iStream, std::string& shaderSource)
{
//Validate stream status
if(!iStream.good())
return false;
//Loop through each line & append to string
std::string line;
while(!iStream.eof())
{
std::getline(iStream, line);
shaderSource.append(line);
}
return true;
}
bool validateCompilation(GLuint shader)
{
//Check if there was compile error
int success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
bool compileError = (success == GL_FALSE ? true : false);
//Print compile error info
if(compileError)
{
//Get log length
int infoLength = 512;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLength);
//Get info log message & print it
char info[infoLength];
glGetShaderInfoLog(shader, infoLength, NULL, info);
std::cout << "SHADER COMPILE ERROR: " << info << std::endl;
}
//Return true if no compile error and false otherwise
return !compileError;
}
template <GLenum SHADER_TYPE>
bool compileShader(GLuint& shader, std::string shaderFile)
{
//Read shader
std::ifstream shaderIStream(shaderFile);
std::string shaderSource;
if(!fileToString(shaderIStream, shaderSource))
return false;
//Create shader obj
shader = glCreateShader(SHADER_TYPE);
//Compile shader
const char* constShaderSource = shaderSource.c_str();
glShaderSource(shader, 1, &constShaderSource, NULL);
glCompileShader(SHADER_TYPE);
//Validate shader
if(!validateCompilation(shader))
return false;
return true;
}
| |