Jump to content

  • Log In with Google      Sign In   
  • Create Account


Kripke

Member Since 22 Jun 2009
Offline Last Active Nov 27 2012 08:39 AM
-----

Topics I've Started

Rendering highlights on 2d map based on grayscale texture

22 November 2012 - 08:15 AM

Hello,
Long time lurker, first time poster.

Using Java and LWJGL (2d), I am trying to render highlights on specific parts (areas) of a screen sized textured quad:

Posted Image




The areas are as you can see irregular and further defined by another image, grayscale, where each area has a unique color value:

Posted Image

The purpose is to through the gray scale image and a variably sized collection of grayscale color values (e.g byte[] barr = {56, 59, 61, 46, 51}), specify what areas that should be highlighted.

I have something that works somewhat, using shaders, but is horribly slow.

It involves sending an array with the areas (gray scale color values) I want highlighted to the shader, setting the color of those areas to for instance green in the shader, and the rest to alpha 0, iterating over the array with a for-loop.

When the shader is done, a screen sized quad is rendered, alpha blended with the original color map. This gives the result I want but suffers greatly from increasing the size variable. Highlighting 50 areas gives a framerate of ~5-10.

The fragment shader looks something like this:

[source lang="cpp"]uniform sampler2d texture;uniform int size;uniform float cols[100];void main(){ vec4 pixel = texture2d(texture,vec2(pos)); for(int i = 0; i < size; i++) { if(pixel.r == (1.0/255.0) * cols[i]) { // set color to transparent green break; } else { // set color to invisible } }}[/source]


It seems as the for-loop is the culprit, looping over 1 element for instance is ok, but still slow.
Another way would be to create a 1d texture instead of an array to send to the shader, but I guess I still would need to loop over the texels of that texture and do the comparison. Or is it possible to check if the pixel's color "exists" in the 1d texture/array? Is there another way of going about this effect with or without shaders? Some fancy blending technique?

Any ideas would be very welcome, and let me know if you need any more information.

PARTNERS