[英]Is there a way in Opengl es 2.0 fragment shader, to get a previous fragment color
My code is drawing a background image and drawing some other images (particles) on top of that image.我的代码正在绘制背景图像并在该图像之上绘制一些其他图像(粒子)。 I want to make the particles have some blending effects, like darken, lighten, burn... the same as Canvas globalcompositeoperation does.
我想让粒子有一些混合效果,比如变暗、变亮、燃烧……就像Canvas 全局复合操作一样。 So in the fragment shader, i need to get the previous fragment color and blend it with the new color.
所以在片段着色器中,我需要获取以前的片段颜色并将其与新颜色混合。 But i could not find a way to do it.
但我找不到办法做到这一点。
No, there is no possibility within the standard.不,标准内没有可能性。 However, with the extensions EXT_shader_framebuffer_fetch (non-Mali devices) and ARM_shader_framebuffer_fetch ( Mali devices) a value from the framebuffer can be read (since OpenGL 2.0 and for OpenGL ES 2.0 / 3.0):
但是,通过扩展EXT_shader_framebuffer_fetch (非 Mali 设备)和ARM_shader_framebuffer_fetch ( Mali设备),可以从帧缓冲区中读取一个值(从 OpenGL 2.0 开始,对于 OpenGL ES 2.0):
This extension provides a mechanism whereby a fragment shader may read existing framebuffer data as input.
这个扩展提供了一种机制,片段着色器可以读取现有的帧缓冲区数据作为输入。 This can be used to implement compositing operations that would have been inconvenient or impossible with fixed-function blending.
这可用于实现固定功能混合不方便或不可能的合成操作。 It can also be used to apply a function to the framebuffer color, by writing a shader which uses the existing framebuffer color as its only input.
它还可以用于将 function 应用到帧缓冲区颜色,方法是编写一个使用现有帧缓冲区颜色作为其唯一输入的着色器。
Note that there is no guarantee that hardware will support an extension.请注意,不能保证硬件将支持扩展。 You need to test whether the extension is supported or not at runtime.
您需要在运行时测试扩展是否受支持。
If you want to read fragments from the previous rendering, the usual way is to implement multiple rendering passes and render to a texture.如果你想从之前的渲染中读取片段,通常的方法是实现多个渲染通道并渲染到一个纹理。 See also LearnOpenGL - Deferred Shading .
另请参阅LearnOpenGL - 延迟着色。
In many cases, there is no need to read fragments in the fragment shader.在很多情况下,不需要在片段着色器中读取片段。 A lot of rendering effects can be implemented using the standard Blending functionality.
使用标准混合功能可以实现许多渲染效果。 The blend function can be changed with
glBlendFunc
and the blend equation can be changed with glBlendEquation
.可以使用
glBlendFunc
更改混合 function 并且可以使用glBlendEquation
更改混合方程。
Yes it is possible.对的,这是可能的。 First, render result to the texturebuffer.
首先,将结果渲染到纹理缓冲区。 Then send the texturebuffer to the second shader to apply the effects.
然后将纹理缓冲区发送到第二个着色器以应用效果。
to find out how to do this, look for the keywords GLES20.glBindFramebuffer (GL_FRAMEBUFFER, mFramebuff);要了解如何执行此操作,请查找关键字 GLES20.glBindFramebuffer (GL_FRAMEBUFFER, mFramebuff);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.