Commit 327f86f3 authored by Robert Menzel's avatar Robert Menzel

added method to set multiple shader strings per shader and added more verbose error messages

parent 0d0a8a15
......@@ -65,10 +65,14 @@ public:
// ==================================================================================================== \/
public:
bool setFromFile (const std::string &_filename);
bool setSource (const std::string &_source );
bool setSource (const std::string &_source, bool _checkForCompileErrors = true);
bool setSources (const std::vector<std::string> &_sources, bool _checkForCompileErrors = true );
protected:
bool compile (const char *_pProgramText) const;
bool compile () const;
// get a log and a bool whether the log contains an error (or just a warning), not done
// automatically by compile() but called by all public source setting functions:
void getCompileLog( std::string &_log, bool &_wasErrorLog ) const;
// =================================================================================================== \/
// ============================================================================================ FIELDS \/
......
......@@ -37,17 +37,77 @@ bool Shader::setFromFile(const std::string& _filename)
return false;
}
return setSource(fileContent);
bool compileErrors = true;
if ( setSource(fileContent, false) ) { // don't check for errors, we will do that on our own:
std::string compileLog;
getCompileLog( compileLog, compileErrors );
if (compileLog.size() > 0) {
if (compileErrors) {
error() << "\nIn file: " << _filename << ": \n" << compileLog << "\n" << std::endl;
} else {
warning() << "\nIn file: " << _filename << ": \n" << compileLog << "\n" << std::endl;
}
}
}
return !compileErrors; // return true iff there were no errors
}
bool Shader::setSource(const std::string& _source)
bool Shader::setSource(const std::string& _source, bool _checkForCompileErrors)
{
return compile(_source.c_str());
const char *pProgramString = _source.c_str();
glShaderSource(mObjectName, 1, &pProgramString, NULL); // can't create OpenGL errors
if (!compile()) {
return false;
}
// the compile call should work even if there are compile errors itself:
bool compileErrors = false;
if (_checkForCompileErrors) {
std::string compileLog;
getCompileLog( compileLog, compileErrors );
if (compileLog.size() > 0) {
if (compileErrors) {
error() << compileLog << std::endl;
} else {
warning() << compileLog << std::endl;
}
}
}
return !compileErrors; // return true iff there were NO errors
}
bool Shader::compile(const char* _pProgramText) const
bool Shader::setSources(const std::vector<std::string> &_sources, bool _checkForCompileErrors )
{
unsigned int numberOfStrings = _sources.size();
const char **pProgramStrings = new const char*[ numberOfStrings ];
for (unsigned int i = 0; i < numberOfStrings; ++i) {
pProgramStrings[i] = _sources[i].c_str();
}
glShaderSource(mObjectName, numberOfStrings, pProgramStrings, NULL); // can't create OpenGL errors
delete[] pProgramStrings;
if (!compile()) {
return false;
}
// the compile call should work even if there are compile errors itself:
bool compileErrors = false;
if (_checkForCompileErrors) {
std::string compileLog;
getCompileLog( compileLog, compileErrors );
if (compileLog.size() > 0) {
if (compileErrors) {
error() << compileLog << std::endl;
} else {
warning() << compileLog << std::endl;
}
}
}
return !compileErrors; // return true iff there were NO errors
}
bool Shader::compile() const
{
glShaderSource(mObjectName, 1, &_pProgramText, NULL); // can't create OpenGL errors
glCompileShader(mObjectName);
#ifdef ACGL_CHECK_CRITICAL_GL_ERRORS
......@@ -59,30 +119,35 @@ bool Shader::compile(const char* _pProgramText) const
error() << "glCompileShader failed, that can only mean, that the object name used is not a valid shader object!" << std::endl;
return false;
}
#endif
return true;
}
void Shader::getCompileLog( std::string &_log, bool &_wasErrorLog ) const
{
// check for shader compile errors:
GLint shaderError;
glGetShaderiv(mObjectName, GL_COMPILE_STATUS, &shaderError);
if(shaderError != GL_TRUE)
{
// yes, errors
error() << "Shader compile error: " << std::endl;
_wasErrorLog = true;
} else {
_wasErrorLog = false;
}
// a log gets always printed (could be warnings)
// the log could be warnings:
GLsizei length = 0;
glGetShaderiv(mObjectName, GL_INFO_LOG_LENGTH, &length);
if(length > 1)
if(length > 1) // null terminated, so 1 could also be empty
{
// a compile log can get produced even if there were no errors, but warnings!
GLchar* pInfo = new char[length + 1];
GLchar* pInfo = new char[length];
glGetShaderInfoLog(mObjectName, length, &length, pInfo);
error() << "Compile log: " << std::string(pInfo) << std::endl;
//error() << "Compile log: " << std::string(pInfo) << std::endl;
_log = std::string( pInfo );
delete[] pInfo;
} else {
_log = "";
}
openGLRareError(); // glGetShader- calls normaly shoudn't create errors
return (shaderError == GL_TRUE); // return true if we encountered no errors
#else
return true; // if no error checking was done we assume it went ok
#endif
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment