PDA

View Full Version : Merging vertex shader in fragment shader



Hamdii
08-21-2015, 05:04 AM
Hi,
Is it possible to merge vertex shader into a fragment shader file ?

Best,
Hamdi

Dark Photon
08-21-2015, 06:06 AM
Yes. Have done that. And it definitely simplifies maintenance.
Do something like this:


#ifdef VERTEX_SHADER
...
#endif

#ifdef FRAGMENT_SHADER
...
#endif


Now in your program after you load this file before you compile it, simply prepend a short preamble with the #version and the #define to indicate which shader you're compiling. For example:


#version 300

#define VERTEX_SHADER


before you throw this at glCompileShader. You can do that either by manually prepending this string to the loaded shader source before feeding it to glShaderSource, or you can just feed these in as separate strings to glShaderSource and let the GLSL compiler do this internally.