PDA

View Full Version : Fragment shader for detecting contour pixels



lucafr
01-12-2012, 12:14 PM
Hi,

i have the need to do the following thing. I am working with a 2D engine called cocos2D-x. This engine draws the sprite of the game as textured quads.
Texture have alpha component. Pixels with alpha=255 (or 1.0f) are totally opaque, while pixels with alpha=0 are totally transparent.

I would like to add the following functionality: when drawing a sprite, i want to be able to
detect pixels that are contour pixels. A contour pixel is a pixel that is totally opaque and that it is adjacent to at least a totally transparent pixel. Once detected, i want to change its color in some ways.

First question: can this be done with OpenGL ES?
Second question: can someone show me a simple way to do this? Even a starting point would be really useful.

What concerns me is the possibility to access adjacent pixels, that is, given pixel at coordinates (x,y), i need to check 4-neighboring or 8-neighboring. For example, for 4-neighboring that means checking the alpha of the following pixels:

(x+1,y)
(x-1,y)
(x,y-1)
(x,y+1)


Third question: is OpenGL ES 2.0 in wide use or should avoid it? At my company we are writing a simple game for iPhone/Android platforms. While iPhone support 2.0 ES, i am wondering if Android phones are already there and if supporting OpenGL ES 2.0 is not a premature thing to do.

Thank you.

Xmas
01-13-2012, 04:19 AM
OpenGL ES 2.0 is widely supported on Android smartphones today.

It's certainly doable, you just sample the sprite texture several times in your fragment shader with texture coordinates adjusted so they represent a +/-1 pixel offset. That said, if the sprites are static I think you should really do this contour detection as a preprocess pass generating a contour mask texture (which you could potentially combine with the alpha information, depending on the pixel format used).

lucafr
01-13-2012, 02:23 PM
Thx, it was not difficult, but it was absolutely my first shader at all!
And obviously yes, i will do that job off-line. Thx for your advice!