I have to use two different shader programs in OpenGL for different objects.
I found that I have to use glUseProgram()
to switch between different shader programs, but not much information on that.
How does generating and binding VAOs and VBOs work for each shader program (how & when) given that I have two different shader programs I use for different objects?
When you render objects in OpenGL, your code will look like this:
Bind program with glUseProgram
, set uniforms with glUniform4fv
, glUniformMatrix4fv
, etc.
Bind the vertex array with glBindVertexArray
.
Bind any textures you need with glActiveTexture
and glBindTexture
.
Change any other state, e.g., glEnable
, glDisable
, glBlendFunc
.
Draw with glDrawArrays
or glDrawElements
.
If you want, reset state back to defaults.
These are all the things that you tend to do in vanilla OpenGL 3 code. You should already have this part working.
If you need to write multiple objects with different shader programs, you just do the above steps multiple times. State changes can be omitted if you are going to use the same state for multiple programs (except uniforms, which are saved separately for each program). For example, you might use the same VAO, the same textures, the same blending function, et cetera.
There are many tutorials on how OpenGL 3 drawing commands work, if you are looking for more detailed examples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With