morgancodes
morgancodes

Reputation: 25285

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?

Upvotes: 3

Views: 2285

Answers (1)

Chris Dodd
Chris Dodd

Reputation: 126527

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...

Upvotes: 8

Related Questions