Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: shaders texture sample lookup  (Read 2231 times)

0 Members and 1 Guest are viewing this topic.


  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
shaders texture sample lookup
« on: March 18, 2016, 04:42:29 pm »
Hi, I've been trying for some time to get a dynamic amount of lights into my light shader instead of using a fixed array.  I tried setting the arrays to a max length and then sending a uniform int at runtime to say where the last used indices was but it just wont work due to it not being a constant.

So after much looking around and a comment on here that i saw by Nexus, I found out about using texture sampling instead of an array. So i've been trying that without much luck.

example of how i make the texture and the shader logic:

mdataTexture.create(10, 1);

for (int i = 0; i < 10; i++)
                sf::RectangleShape r;
                r.setSize(sf::Vector2f(1, 1));
                r.setPosition(i, 0);
                sf::Vector2f vec2 = static_cast<sf::Vector2f>(mWindow.mapCoordsToPixel(vec[i], mCam));
                vec2.y = mWindow.getSize().y - vec2.y + 8;
                vec2.x += 8;
                r.setFillColor(sf::Color(vec2.x, vec2.y, 0, 255));

for(int i = 0; i <  10; i++)
                vec4 pixel = texture2D(uData, vec2(float(i)/10.0, 0.0));
                distances[i] = distance(vec2(pixel.x , pixel.y), vec2(gl_FragCoord.x,  gl_FragCoord.y));

when this is run, the data returned (pixel.x, pixel.y) always equal zero. and I just can't understand why  :(

When instead of trying to use the color data as coordinates for my lights, I instead use the color data to set the fragColor, it works.  So the texture is definitely, there, loaded and functioning to the extent that i can take colours from it, but when i try and take the color as floats to use as coordinates it just gives me zero.

Such a weird problem, any help would be appreciated.

thanks for your time.


I have changed the fragment shader to use texelFetch instead of texture2d. This is easier for me because it doesn't use normalised values, so i can be sure im accessing the right pixel when using a for loop index.

this changes screen color as i'd expect:
vec4 pixel = texelFetch(uData, ivec2(5, 0), 0);
gl_FragColor = pixel;
so does this:

gl_FragColor.r = pixel.r;
gl_FragColor.g = pixel.g;
gl_FragColor.b = pixel.b;
gl_FragColor.a = pixel.a;
however, this doesn't work and seems to always be zero (even though it works as a color)
vec4 pixel = texelFetch(uData, ivec2(5, 0), 0);
float dist = distance(vec2(pixel.x, pixel.y), gl_FragCoord.xy);
modColor.r +=  vColor.r * uColor.r / dist;
modColor.g += vColor.g * uColor.g / dist;
modColor.b += vColor.b * uColor.b / dist;
modColor.a += vColor.a * uColor.a / dist;
gl_FragColor = texture2D(uTexture, vTexCoord) * modColor ;
« Last Edit: March 18, 2016, 07:52:33 pm by ka0s420 »