Question

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 sake 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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top