GLSL: indexing an array with a variable?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
arundel
Prole
Posts: 31
Joined: Tue Sep 21, 2010 8:49 pm
Location: The Netherlands

GLSL: indexing an array with a variable?

Post by arundel »

I've been attempting the whole day to get some simple shader glsl code to work. The shader is supposed to make some stripey colored lines from a one dimensional Lua color containing table. The random colors are set up in Lua in the 'voxel_table', which is sent to the shader with shader:send("voxel_table",unpack(voxel_table)).

The voxel_table format is: { {0.93456,0.235466,0.112356}, {0.767734,0.342211,0.324235}, ... }.

I already checked that the values are random.

This is my shader code (the Lua part of the script isn't important):

Code: Select all

shader = love.graphics.newShader([[

	extern vec4 voxel_table[1024];
    int index;
	
	vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pc)
	{
	    vec4 pixel = Texel(texture, texture_coords); // current pixel 
            index=int(pixel.x);
	    pixel.rgba = vec4(voxel_table[index][0],voxel_table[index][1],voxel_table[index][2],voxel_table[index][3]);
		return pixel;
	}
	
]])
(Ignore the formatting, I don't know how to fix it in the code box.)

So I heard that indexing arrays in GLSL with a variable is glitchy or not possible at all. I don't know anything about GLSL. There is no error, but all pixels are colored one color, which is not supposed to happen (remember, I need stripes).

My suspicion is that the 'index' variable in the GLSL code doesn't want to update/assign to pixel.x for some reason. I back this statement up with the fact that when I change index to: "index = int(0)" or "index = int(5)", etc, all pixels will change to one color (but no stripes, of course). Any help would be appreciated.
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: GLSL: indexing an array with a variable?

Post by slime »

Color component values in GLSL generally range from 0-1 (rather than 0-255 or whatever).
arundel
Prole
Posts: 31
Joined: Tue Sep 21, 2010 8:49 pm
Location: The Netherlands

Re: GLSL: indexing an array with a variable?

Post by arundel »

@slime,
The voxel_table format is: { {0.93456,0.235466,0.112356}, {0.767734,0.342211,0.324235}, ... }.
They're all numbers between 0 and 1 so an invalid number range can't be the problem. Btw, the alpha values I forgot to show in this example, but in the script they're there and have a value of 1.

Here's a real sample of the color data:

Code: Select all

{0.71330755881738,0.64333958057407,0.55644722314076,1},
{0.98340533117127,0.080882800977306,0.61399115222498,1},
{0.71892828799496,0.67113520766849,0.41385694171876,1},
{0.41414169485696,0.11421212746685,0.054865220034606,1},
{0.96593241413237,0.64833716160417,0.17505142248292,1},
{0.30352098977889,0.27341280051114,0.34674434271287,1},
{0.18805207175919,0.62801892832059,0.54395377838293,1},
{0.9983730318303,0.43653441900413,0.18101641582311,1},
{0.69238095072853,0.20960586939474,0.56193529326864,1},
{0.27699530632821,0.73787316581446,0.55078619676,1},
{0.024791359869007,0.74223803941095,0.049589119057454,1},
{0.96670251542343,0.99844848994509,0.73573427439504,1},
{0.8196732295864,0.77461497689554,0.79629619194785,1},
{0.17447516086355,0.062995176547141,0.34744917059718,1},
{0.97831930168776,0.77292802089191,0.62639559381135,1},
{0.51299269223209,0.66678672909988,0.11750785528735,1},
{0.1490499012792,0.24742464642855,0.47881382396959,1},
{0.98646707443502,0.94024869763087,0.75493512452393,1},
{0.95950453636212,0.90514517559151,0.76916045066238,1},
{0.33416278959471,0.26749790828798,0.60437741880659,1},
{0.74387813371788,0.010494259505764,0.053914118863764,1},
{0.98040636997507,0.67178137725765,0.81898661650514,1},
{0.15398619580456,0.94685566088072,0.29056101921696,1},
{0.1299967189081,0.56596236937401,0.81703084196372,1},
{0.99294267376928,0.61824395663255,0.061909868352354,1},
{0.79585157800486,0.93775559000353,0.81171091357863,1},
{0.44981183036292,0.44810768302735,0.17515961761755,1},
{0.69512870999538,0.99635466553483,0.14707180988107,1},
{0.83924148888348,0.78004977676428,0.26796062170575,1},
{0.38965397699217,0.53482197019675,0.20059570849054,1},
{0.14921794859704,0.6945364591615,0.051813180476955,1},
{0.97312342430692,0.52052194748871,0.14430177124006,1},
{0.20333847938934,0.50135574136454,0.37698663000816,1},
{0.60887929632108,0.34949858304048,0.63134997226429,1},
{0.98306090164661,0.230328686004,0.072592092150711,1},
{0.69481594204992,0.010872005803834,0.5225516708377,1},
{0.18479710615305,0.19701065923995,0.073027609060307,1},
{0.79322773992732,0.18444964974964,0.63176141667622,1},
arundel
Prole
Posts: 31
Joined: Tue Sep 21, 2010 8:49 pm
Location: The Netherlands

Re: GLSL: indexing an array with a variable?

Post by arundel »

I fixed the problem! Apparantly I shouldn't have used index = int(pixel.x), but index = int(pc.x) instead (screen coordinates). Now it makes stripey colorful stripes. :3

The fire is extinguished, nothing to see here people, move on. <:)
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests