Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share preprocessor macros across c++ and glsl code?

I'm setting up lighting for an openGL program. I'd like to be able to easily tweak the number of lighting sources in C++ without having to touch my shader

In my C++ code:

#define NUM_LIGHTS 5
GLfloat lightposn [4 * NUM_LIGHTS];

In my glsl code:

 uniform vec4 lightposn[NUM_LIGHTS];

How can I pass this NUM_LIGHTS value to my shader? Is it possible to use a macro defined in a c++ file in a shader? Is there another easy way to set NUM_LIGHTS across both my c++ code and my glsl code?

like image 619
morgancodes Avatar asked Aug 22 '26 18:08

morgancodes


1 Answers

Read the header file with the #defines in it into a string, and 'prepend' it to the shader by passing it to glShaderSource first

char *shader_src[3];
shader_src[0] = "#version ...\n";
shader_src[1] = ReadHeaderFile(....);
shader_src[2] = ReadShaderSourceFile(....);
glShaderSource(shader, 3, shader_src, NULL);
...compile, link, and check for errors...
like image 109
Chris Dodd Avatar answered Aug 25 '26 08:08

Chris Dodd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!