I'm working on a small engine in OpenTK right now, and I've got shaders working so far. I wonder though , how it is possible to apply a shader to an entire scene!?. I've seen this done in minecraft for example, where someone created a shader that warped the entire scene. But since every object is rendered with its own shader active, how would I achieve this?
-
I don't know MinceCraft, but it is possible that the runtime composes the actual shader from some fragments. E.g. a basic lighting step, some more work, user defined steps, etc. After all, you can compile shaders from strings. – Nico Schertler Jul 18 '14 at 12:33
4 Answers
You seem to be referring to a technique called post processing. The way it works is that you first render the entire scene to a texture using the shaders you already have. You can then render this texture to the screen using a fragment shader to apply various effects like motion blur, warping or depth of field.

- 1,201
- 6
- 17
-
-
Well, in that case what you were asking for does not exist. Post processing is the closest you are going to get to being able to apply multiple shaders to the same objects. – fintelia Jul 18 '14 at 18:18
"But since every object is rendered with its own shader active"
That's not how OpenGL works. In fact there's no such thing as "models" (what you probably mean by "object") in OpenGL. OpenGL draws primitives (points, lines and triangles) one at a time. Furthermore there's no hard association between a set of primitives and the shaders being used.
It's trivial to just bind a single shader program at the beginning of a batch and every primitive of that batch is subjected to this shader. If the batch consists of the whole scene, then the whole scene uses that shader.

- 159,371
- 13
- 185
- 298
AFAIK, you can only bind one vertex shader at a time.
What you may want to try is to render to a texture first then rerender the texture onto the screen but applying some changes to it (warping it for example). You can also extract the depth buffer and use it if you have a more complex change that you want to apply.

- 11,863
- 22
- 26
If you bind the shader you want before the render loop, it would effect all items until you un-bind it (i.e. binding id #0) or disable GL_TEXTURE_2D via glEnable()/glDisable().

- 189
- 1
- 1
- 11