During OpenGL initialization, the program is supposed to do something like:
<Get Shader Source Code>
<Create Shader>
<Attach Source Code To Shader>
<Compile Shader>
Getting source code could be as simple as putting it in a string like: (Example taken from SuperBible, 6th Edition)
static const char * vs_source[] =
{
"#version 420 core \n"
" \n"
"void main(void) \n"
"{ \n"
" gl_Position = vec4(0.0, 0.0, 0.0, 1.0); \n"
"} \n"
};
The problem is that it is hard to edit, debug and maintain GLSL shaders directly in a string. So getting the source code in a string from a file is easier for development:
std::ifstream vertexShaderFile("vertex.glsl");
std::ostringstream vertexBuffer;
vertexBuffer << vertexShaderFile.rdbuf();
std::string vertexBufferStr = vertexBuffer.str();
// Warning: safe only until vertexBufferStr is destroyed or modified
const GLchar *vertexSource = vertexBufferStr.c_str();
The problem now is how to ship the shaders with your program? Indeed, shipping source code with your application may be a problem. OpenGL supports "pre-compiled binary shaders" but the Open Wiki states that:
Program binary formats are not intended to be transmitted. It is not reasonable to expect different hardware vendors to accept the same binary formats. It is not reasonable to expect different hardware from the same vendor to accept the same binary formats. [...]
How to practically ship GLSL shaders with your C++ software?
With c++11, you can also use the new feature of raw string literals. Put this source code in a separate file named shader.vs
:
R"(
#version 420 core
void main(void)
{
gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
}
)"
and then import it as a string like this:
const std::string vs_source =
#include "shader.vs"
;
The advantage is that its easy to maintain and debug, and you get correct line numbers in case of errors from the OpenGL shader compiler. And you still don't need to ship separate shaders.
The only disadvantage I can see is the added lines on the top and bottom of the file (R")
and )"
) and the syntax that is a little bit strange for getting the string into C++ code.