Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use different shader programs in OpenGL?

Tags:

c++

opengl

glsl

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?

like image 321
nurgan Avatar asked Oct 07 '16 18:10

nurgan


1 Answers

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.

like image 74
Dietrich Epp Avatar answered Dec 20 '22 19:12

Dietrich Epp