javierMarquez
javierMarquez

Reputation: 350

Best way to use multiple shaders

Few weeks I learn OpenGL on famous http://learnopengl.com and have question about a organise multiple materials (shaders). In this tutorial is using Shader class which compose Shader program from list of vert/frag shaders:

Shader shader("vert.glsl", "frag.glsl");

After that for shader I create uniforms:

    glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
    glUniformMatrix4fv(glGetUniformLocation(shader.Program, "view"), 1, GL_FALSE, glm::value_ptr(view));
etc.

And at the end we receive one Shader program. How in this case I can make multiple shaders? I try to make:

Shader shader("vertDiff.glsl", "fragDiff.glsl");
Shader shaderGlass ("vertGlass.glsl", "fragGlass.glsl");
Shader shaderLight("vertLight.glsl", "fragLight.glsl")

And after that i make uniforms for .glsl for each shader :

glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(glGetUniformLocation(shaderGlass.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));

But shading doesn't work correctly. The same uniforms needs to be declared few times for each shader and that make program incorrect. How is a correctly to make this part? Thank you and sorry for my stupid question.:)

Upvotes: 3

Views: 2937

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474406

Of course that doesn't work. The glUniform* calls change the currently bound program's uniform state. So whatever program was passed to glUseProgram most recently.

If you want to modify a uniform in another program, you have to switch to the other program. Or use glProgramUniform*.

Upvotes: 6

Related Questions