开发者

How can a fragment shader use the color values of the previously rendered frame?

I am learning to use shaders in OpenGL ES.

As an example: Here's my playground fragment shader which takes the current video frame and makes it grayscale:

varying highp vec2 textureCoordinate;

uniform sampler2D videoFrame;

void main() {
    highp vec4 theColor = texture2D(videoFrame, textureCoordinate);
    highp float avrg = (theColor[0] + theColor[1] + theColor[2]) / 3.0;
    theColor[0] = avrg; // r
    theColor[1] = avrg; // g
    theColor[2] = avrg; // b
    gl_FragColor = theColor;
}

theColor represents the current pixel. It would be cool to also get access to the previous pixel at this same coordinate.

For sak开发者_如何学JAVAe of curiousity, I would like to add or multiply the color of the current pixel to the color of the pixel in the previous render frame.

How could I keep the previous pixels around and pass them in to my fragment shader in order to do something with them?

Note: It's OpenGL ES 2.0 on the iPhone.


You need to render the previous frame to a texture, using a Framebuffer Object (FBO), then you can read this texture in your fragment shader.


The dot intrinsic function that Damon refers to is a code implementation of the mathematical dot product. I'm not supremely familiar with OpenGL so I'm not sure what the exact function call is, but mathematically a dot product goes like this :

Given a vector a and a vector b, the 'dot' product a 'dot' b produces a scalar result c:

c = a.x * b.x + a.y * b.y + a.z * b.z

Most modern graphics hardware (and CPUs, for that matter) are capable of performing this kind of operation in one pass. In your particular case, you could compute your average easily with a dot product like so:

highp vec4 = (1/3, 1/3, 1/3, 0) //or zero

I always get the 4th component in homogeneous vectors and matrices mixed up for some reason.

highp float avg = theColor DOT vec4

This will multiple each component of theColor by 1/3 (and the 4th component by 0), and then add them together.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜