2016-09-06 13:15:29 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <fstream>
|
|
|
|
#include <algorithm>
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <GL/glew.h>
|
|
|
|
|
|
|
|
#include "shader.hpp"
|
|
|
|
|
2016-10-31 15:19:12 +00:00
|
|
|
GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_path){
|
2016-09-06 13:15:29 +00:00
|
|
|
|
|
|
|
// Create the shaders
|
|
|
|
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
|
|
|
|
// Read the Vertex Shader code from the file
|
|
|
|
std::string VertexShaderCode;
|
|
|
|
std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
|
|
|
|
if(VertexShaderStream.is_open()){
|
|
|
|
std::string Line = "";
|
|
|
|
while(getline(VertexShaderStream, Line))
|
|
|
|
VertexShaderCode += "\n" + Line;
|
|
|
|
VertexShaderStream.close();
|
2016-09-08 15:41:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string message("File not found: ");
|
|
|
|
message += std::string(vertex_file_path);
|
|
|
|
throw std::invalid_argument(message.c_str());
|
2016-09-06 13:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the Fragment Shader code from the file
|
|
|
|
std::string FragmentShaderCode;
|
|
|
|
std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
|
|
|
|
if(FragmentShaderStream.is_open()){
|
|
|
|
std::string Line = "";
|
|
|
|
while(getline(FragmentShaderStream, Line))
|
|
|
|
FragmentShaderCode += "\n" + Line;
|
|
|
|
FragmentShaderStream.close();
|
|
|
|
}
|
2016-09-08 15:41:26 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string message("File not found: ");
|
|
|
|
message += std::string(fragment_file_path);
|
|
|
|
throw std::invalid_argument(message.c_str());
|
|
|
|
}
|
2016-09-06 13:15:29 +00:00
|
|
|
|
|
|
|
GLint Result = GL_FALSE;
|
|
|
|
int InfoLogLength;
|
|
|
|
|
|
|
|
|
|
|
|
// Compile Vertex Shader
|
|
|
|
char const * VertexSourcePointer = VertexShaderCode.c_str();
|
|
|
|
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
|
|
|
|
glCompileShader(VertexShaderID);
|
|
|
|
|
|
|
|
// Check Vertex Shader
|
|
|
|
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
|
|
|
|
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
|
|
|
if ( InfoLogLength > 0 ){
|
|
|
|
std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
|
|
|
|
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
|
2016-09-08 15:41:26 +00:00
|
|
|
throw std::invalid_argument(VertexShaderErrorMessage.data());
|
2016-09-06 13:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Compile Fragment Shader
|
|
|
|
char const * FragmentSourcePointer = FragmentShaderCode.c_str();
|
|
|
|
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
|
|
|
|
glCompileShader(FragmentShaderID);
|
|
|
|
|
|
|
|
// Check Fragment Shader
|
|
|
|
glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
|
|
|
|
glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
|
|
|
if ( InfoLogLength > 0 ){
|
|
|
|
std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
|
|
|
|
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
|
2016-09-08 15:41:26 +00:00
|
|
|
throw std::invalid_argument(FragmentShaderErrorMessage.data());
|
2016-09-06 13:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Link the program
|
|
|
|
GLuint ProgramID = glCreateProgram();
|
|
|
|
glAttachShader(ProgramID, VertexShaderID);
|
|
|
|
glAttachShader(ProgramID, FragmentShaderID);
|
|
|
|
glLinkProgram(ProgramID);
|
|
|
|
|
|
|
|
// Check the program
|
|
|
|
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
|
|
|
|
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
|
|
|
if ( InfoLogLength > 0 ){
|
|
|
|
std::vector<char> ProgramErrorMessage(InfoLogLength+1);
|
|
|
|
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
|
2016-09-08 15:41:26 +00:00
|
|
|
throw std::invalid_argument(ProgramErrorMessage.data());
|
2016-09-06 13:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
glDetachShader(ProgramID, VertexShaderID);
|
|
|
|
glDetachShader(ProgramID, FragmentShaderID);
|
|
|
|
|
|
|
|
glDeleteShader(VertexShaderID);
|
|
|
|
glDeleteShader(FragmentShaderID);
|
|
|
|
|
|
|
|
return ProgramID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|