Using multiple shader programs per step

This may be answered somewhere, but I haven’t found it. I’m wondering if it is possible to switch between different shader programs within a single rendering step. I’ll explain what I mean with an example. I have a “ground” mesh and a “sky” mesh, and I want to use different shader code to render each one. One way to use the same code for both meshes is to pass in a value to a shader variable (for example: “ground = 1”) to tell the shader program to use the part of the shader code for the ground. Then I can check this value in the shader code itself to determine whether to use the ground shader code or the sky shader code. But it’s inefficiency to check this value per vertex (or per pixel).

What I’d like to do instead is use glUseProgram (or something) to use a shader program just before drawing the ground mesh and then to use a different shader program just before drawing the sky mesh. I’ve been able to get the shader programs to work individually, but I don’t know how to switch between them in a single step. Is this possible? If so, how do I do it? I hope you understand what I’m trying to ask.

Thanks in advance for your help. (This forum has always been so very helpful to me.)

Okay. I actually figured it out! It’s very simple. You just use glUseProgram(program_id) switch between shaders during the run-time step. When I initially tried this, it didn’t work, but that was because I had set some uniform variables without first calling glUseProgram. So, you have to make sure you call glUseProgram before defining the variables. Now it works!

Hello, I had the same problem (I used one shader program for a terrain mesh and another for an animated water mesh). Your solution solved my problem, thanks!