I am trying to figure out a way to implement a 'highlighting' feature for my OpenGL image viewer. The idea is that I want the user to be able to tap the image to highlight an area, this has gone well so far. The problem I am facing is when the user taps a second time. I want to keep both highlights rendering over the image where they were placed. At the moment when the second tap arrives, the first highlight disappears.
This is my current fragment shader:
precision mediump float;
uniform vec4 vColor;
varying vec2 v_TexCoordinate;
uniform sampler2D u_Image;
uniform float u_touchX;
uniform float u_touchY;
void main() {
float radius = 0.1;
float a = v_TexCoordinate.x - u_touchX;
float b = v_TexCoordinate.y - u_touchY;
float d = sqrt(abs(a*a + b*b));
if(d < radius){
gl_FragColor = texture2D(u_Image, v_TexCoordinate) + vColor;
}else{
gl_FragColor = texture2D(u_Image, v_TexCoordinate);
}
}
Fairly simple, I pass in the image, highlight color and the x and y of where the user tapped the screen. If the fragment is within a certain distance of that point I add the highlight. Works great thus far.
Obviously though, once that touch point moves the original highlight is gone.
My goal here is to have as much of this feature running GPU side as possible.
Here are a few thoughts I've had so far but I'm hoping that someone more clever than I can show me a better way to do this.
There has to be a better way but I'm currently coming up empty and I'm hoping someone can point me in the right direction.
If your highlight is simply additive, then create a second texture and render the highlight regions into it. Simply render that with additive blending over the underlying layer. It's probably far easier to do that that trying to create a database of highlight regions in GLSL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With