Externally Define Preprocessor Macros in GLSL

≡放荡痞女 提交于 2019-12-02 19:04:35

You don't really need to prepend it to the code you loaded. That's why there are multiple strings in the glShaderSourceARB API.

Something like the following does what you are looking for:

char *sources[2] = { "#define FOO\n", sourceFromFile };
glShaderSourceARB(shader, 2, sources, NULL);

Here is what I use to avoid #version problems. It is Java, and not optimized for perfo, but it works perfectly:

// extract shader sourcecode from file
StringBuilder shaderSource = Tools.readTextRessource(shaderSrcFile.url);

// apply defined values
int versionIndex = shaderSource.indexOf("#version");
if(versionIndex == -1) err("missing #version xxx in shader "+shaderSrcFile.name());
int defineInsertPoint = shaderSource.indexOf("\n", versionIndex)+1;

for (int i = defined.size() - 1; i >= 0; --i)
    shaderSource.insert(defineInsertPoint, "#define " + defined.get(i) + "\n");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!