问题
I'm trying to understand how post processing work.
From what I understood, I need to to something like that:
-> Bind render texture
-> Draw my scene
-> Unbind render texture
-> Draw a quad full screen using the resulting texture using a shader to apply an effect (blur, …)
The problem with that system is: How can I apply multiple effects on the resulting quad?
In my mind, I think applying the "effects" (shaders) on the resulting texture then drawing the quad his probably the best solution, but I don't know if it is possible.
Can I apply shaders on texture directly?
PS: Here is what I've done for the moment, I can draw all my scene in a texture currently:
A PostEffect class (an effect to apply)
EffectManager (create the output texture and have a method "add(PostEffect*)"
回答1:
I don't know what you mean with "directly applying" shaders to a texture, but to render a texture with a special effect you need to render a fullscreen quad to the screen with the shader you need, and of course feed the texture into the shader. To have multiple effects you need to use two framebuffers (ping-ponging), like this:
-> Bind 1st render texture
-> Draw scene
-> Bind 2nd render texture
-> Feed 1st render texture to a shader and draw quad
-> Bind 1st render texture
-> Feed 2nd render texture to another shader and draw quad
-> Bind 2nd render texture
-> Feed 1st render texture to a third shader and draw quad
...
-> Unbind
-> Draw quad
You cannot render two times in a row to the same framebuffer, which is why you need to do this way (see https://www.opengl.org/wiki/Framebuffer_Object#Feedback_Loops).
If you can, try to pack as many effects as possible into the same shader to minimize overhead when feeding uniforms and drawing.
回答2:
Post processing tecniques can vary depending on what you want to accomplish. You can either write a single shader that applies multiple post processing effects and render to the texture a single time, or if the renderings are sequential in nature then you need a double buffer:
render to texture 1
use texture one and render to texture 2
use texture 2 and render to texture 1
etc
etc
and do that until all of your post effects are applied. But then you should render the final pass straight to your screen render buffer to avoid a costly 'copying' of the texture to the screen render buffer.
Can I apply shaders on texture directly?
Not sure what you even mean by that. The only time a shader is utilized is when you do a draw call via glDrawArrays or glDrawElements assumming you created your FBO and RBOs correctly. So yes, you must use a full screen rendering technique of some sort.
来源:https://stackoverflow.com/questions/28550225/post-processing-and-resulting-texture