c++ – How to set separate fragment shaders for different objects on the scene?


How to define separate fragment shaders for different objects on the scene (sets of vertices) (their own for each, let's say the "type" of the object)?

There are several types of objects on the scene (which should be displayed), but one object does not have any textures at all, the second has cube-textures, the third has three textures, etc. Writing a universal shader – as I read somewhere, is wrong, tk. It is very bad to insert ifs into a fragment shader, the speed drops.

Please explain on your fingers "How to give different fragment shaders for different objects" ?, or make a mego-shader-universal.


It's simple.

You can compile many shader programs, each with its own pair of vertex and fragment shaders. They usually do not repeat because a unique set of parameters is passed from the vertex shader to the fragment shader. If you have a standard vertex shader, and the fragment shaders differ only in code (the set of parameters is the same), then you can compile many pairs using one vertex shader with different fragment shaders.

Next, draw each object with its own shader (shader program). Separately.

Or, if there are several objects in each type, then you expose the shader, draw all the objects using it, change the shader, draw another type of objects, etc.

Scroll to Top