Shader reload works
parent
c1a82427c0
commit
499eba60b7
|
@ -14,6 +14,5 @@ void main() {
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
// outColor = max(uColor.rgb, fragmentColor.rgb);
|
|
||||||
outColor = fragmentColor.rgb + uColor.rgb - uColor.rgb;
|
outColor = fragmentColor.rgb + uColor.rgb - uColor.rgb;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,10 +24,10 @@ RenderProgram::~RenderProgram() {
|
||||||
glDeleteProgram(mProgramId);
|
glDeleteProgram(mProgramId);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RenderProgram::Load() {
|
|
||||||
|
GLuint RenderProgram::CompileVertexShader() {
|
||||||
// Create the shaders
|
// Create the shaders
|
||||||
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
||||||
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
|
||||||
|
|
||||||
// Read the Vertex Shader code from the file
|
// Read the Vertex Shader code from the file
|
||||||
std::string VertexShaderCode;
|
std::string VertexShaderCode;
|
||||||
|
@ -40,23 +40,12 @@ bool RenderProgram::Load() {
|
||||||
}else{
|
}else{
|
||||||
gLog("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !", mVertexShaderFilename.c_str());
|
gLog("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !", mVertexShaderFilename.c_str());
|
||||||
getchar();
|
getchar();
|
||||||
return false;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
// Read the Fragment Shader code from the file
|
|
||||||
std::string FragmentShaderCode;
|
|
||||||
std::ifstream FragmentShaderStream(mFragmentShaderFilename.c_str(), std::ios::in);
|
|
||||||
if(FragmentShaderStream.is_open()){
|
|
||||||
std::stringstream sstr;
|
|
||||||
sstr << FragmentShaderStream.rdbuf();
|
|
||||||
FragmentShaderCode = sstr.str();
|
|
||||||
FragmentShaderStream.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GLint Result = GL_FALSE;
|
GLint Result = GL_FALSE;
|
||||||
int InfoLogLength;
|
int InfoLogLength;
|
||||||
|
|
||||||
|
|
||||||
// Compile Vertex Shader
|
// Compile Vertex Shader
|
||||||
gLog("Compiling shader : %s", mVertexShaderFilename.c_str());
|
gLog("Compiling shader : %s", mVertexShaderFilename.c_str());
|
||||||
char const * VertexSourcePointer = VertexShaderCode.c_str();
|
char const * VertexSourcePointer = VertexShaderCode.c_str();
|
||||||
|
@ -70,9 +59,28 @@ bool RenderProgram::Load() {
|
||||||
std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
|
std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
|
||||||
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
|
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
|
||||||
gLog("%s", &VertexShaderErrorMessage[0]);
|
gLog("%s", &VertexShaderErrorMessage[0]);
|
||||||
|
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return VertexShaderID;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLuint RenderProgram::CompileFragmentShader() {
|
||||||
|
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
|
|
||||||
|
// Read the Fragment Shader code from the file
|
||||||
|
std::string FragmentShaderCode;
|
||||||
|
std::ifstream FragmentShaderStream(mFragmentShaderFilename.c_str(), std::ios::in);
|
||||||
|
if(FragmentShaderStream.is_open()){
|
||||||
|
std::stringstream sstr;
|
||||||
|
sstr << FragmentShaderStream.rdbuf();
|
||||||
|
FragmentShaderCode = sstr.str();
|
||||||
|
FragmentShaderStream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
GLint Result = GL_FALSE;
|
||||||
|
int InfoLogLength;
|
||||||
|
|
||||||
// Compile Fragment Shader
|
// Compile Fragment Shader
|
||||||
gLog("Compiling shader : %s", mFragmentShaderFilename.c_str());
|
gLog("Compiling shader : %s", mFragmentShaderFilename.c_str());
|
||||||
|
@ -87,17 +95,24 @@ bool RenderProgram::Load() {
|
||||||
std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
|
std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
|
||||||
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
|
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
|
||||||
gLog("%s", &FragmentShaderErrorMessage[0]);
|
gLog("%s", &FragmentShaderErrorMessage[0]);
|
||||||
|
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return FragmentShaderID;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLuint RenderProgram::LinkProgram(GLuint vertex_shader, GLuint fragment_shader) {
|
||||||
// Link the program
|
// Link the program
|
||||||
gLog("Linking program");
|
gLog("Linking program");
|
||||||
GLuint ProgramID = glCreateProgram();
|
GLuint ProgramID = glCreateProgram();
|
||||||
glAttachShader(ProgramID, VertexShaderID);
|
glAttachShader(ProgramID, vertex_shader);
|
||||||
glAttachShader(ProgramID, FragmentShaderID);
|
glAttachShader(ProgramID, fragment_shader);
|
||||||
glLinkProgram(ProgramID);
|
glLinkProgram(ProgramID);
|
||||||
|
|
||||||
|
GLint Result = GL_FALSE;
|
||||||
|
int InfoLogLength;
|
||||||
|
|
||||||
// Check the program
|
// Check the program
|
||||||
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
|
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
|
||||||
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
||||||
|
@ -107,13 +122,35 @@ bool RenderProgram::Load() {
|
||||||
gLog("%s", &ProgramErrorMessage[0]);
|
gLog("%s", &ProgramErrorMessage[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
glDetachShader(ProgramID, VertexShaderID);
|
return ProgramID;
|
||||||
glDetachShader(ProgramID, FragmentShaderID);
|
}
|
||||||
|
|
||||||
glDeleteShader(VertexShaderID);
|
bool RenderProgram::Load() {
|
||||||
glDeleteShader(FragmentShaderID);
|
GLuint vertex_shader_id = CompileVertexShader();
|
||||||
|
if (vertex_shader_id == -1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLuint fragment_shader_id = CompileFragmentShader();
|
||||||
|
if (fragment_shader_id == -1) {
|
||||||
|
glDeleteShader(vertex_shader_id);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
mProgramId = LinkProgram(vertex_shader_id, fragment_shader_id);
|
||||||
|
if (mProgramId == -1) {
|
||||||
|
glDeleteShader(vertex_shader_id);
|
||||||
|
glDeleteShader(fragment_shader_id);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
glDetachShader(mProgramId, vertex_shader_id);
|
||||||
|
glDetachShader(mProgramId, fragment_shader_id);
|
||||||
|
|
||||||
|
glDeleteShader(vertex_shader_id);
|
||||||
|
glDeleteShader(fragment_shader_id);
|
||||||
|
|
||||||
mProgramId = ProgramID;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,6 +185,38 @@ void RenderProgram::RegisterFileModification() {
|
||||||
|
|
||||||
bool RenderProgram::OnFileChanged(const std::string& filename) {
|
bool RenderProgram::OnFileChanged(const std::string& filename) {
|
||||||
gLog("Renderprogram reload as file %s changed", filename.c_str());
|
gLog("Renderprogram reload as file %s changed", filename.c_str());
|
||||||
|
|
||||||
|
GLuint vertex_shader_id = CompileVertexShader();
|
||||||
|
if (vertex_shader_id == -1) {
|
||||||
|
gLog ("Reload failed: error when compiling vertex shader");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLuint fragment_shader_id = CompileFragmentShader();
|
||||||
|
if (fragment_shader_id == -1) {
|
||||||
|
glDeleteShader(vertex_shader_id);
|
||||||
|
gLog ("Reload failed: error when compiling fragment shader");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
mProgramId = LinkProgram(vertex_shader_id, fragment_shader_id);
|
||||||
|
if (mProgramId == -1) {
|
||||||
|
glDeleteShader(vertex_shader_id);
|
||||||
|
glDeleteShader(fragment_shader_id);
|
||||||
|
|
||||||
|
gLog ("Reload failed: error when linking the program");
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
glDetachShader(mProgramId, vertex_shader_id);
|
||||||
|
glDetachShader(mProgramId, fragment_shader_id);
|
||||||
|
|
||||||
|
glDeleteShader(vertex_shader_id);
|
||||||
|
glDeleteShader(fragment_shader_id);
|
||||||
|
|
||||||
|
gLog ("Reload successful");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -149,6 +149,10 @@ struct RenderProgram : AFileModificationListener {
|
||||||
|
|
||||||
bool Load();
|
bool Load();
|
||||||
|
|
||||||
|
GLuint CompileVertexShader();
|
||||||
|
GLuint CompileFragmentShader();
|
||||||
|
GLuint LinkProgram(GLuint vertex_shader, GLuint fragment_shader);
|
||||||
|
|
||||||
void RegisterFileModification();
|
void RegisterFileModification();
|
||||||
virtual bool OnFileChanged(const std::string& filename);
|
virtual bool OnFileChanged(const std::string& filename);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue