I want to use the #include
directive in GLSL files so I don’t have to rewrite the same code for every GLSL shader I create. I tried to create my own small parser for this purpose, but I couldn’t find a good way to locate all #include
statements in a GLSL file. I want to extract the paths of the included files, read them, and write their content into the actual shader (not the original GLSL file that uses the #include directive, but the content read into a std::string
).
I attempted to find shader includes using the following code:
std::string include = "#include";
std::string shaderSource = ReadFile("shader.glsl");
std::uint32_t offset = 0, position = 0;
while ((position = shaderSource.find(include, offset)) != std::string::npos)
{
offset += include.size();
// parse the shader
}
However, I realize that this approach may not work in all scenarios, and I believe the solution should be more complex than what I’ve implemented.
I’d appreciate it if you could help me and let me know if there are any issues with my question in the comments instead of downvoting and closing it.
2