Libgdx shaders not working.

Here are my shaders.

Vertex:


#version 120

attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;

uniform mat4 u_projTrans;
uniform vec4 shaderColor;

uniform float WIDTH, HEIGHT, SCALE, WINDOW_WIDTH, WINDOW_HEIGHT;

uniform float[384] lights;
uniform int lightingEnabled;
uniform int lightCount;

varying vec4 v_color;
varying vec2 v_texCoords;


float dist(vec2 v1, vec2 v2);
float shade(vec3 light);
vec3 light(vec3 light, vec3 lightColor);


void main() {
	
	vec4 lighting = vec4(1.0, 1.0, 1.0, 1.0);
	
	if(1 == 1) {
		lighting = vec4(0.0, 0.0, 0.0, 1.0);
		
		for(int i= 0; i < lightCount; i++) {
			int index = i * 6;
			vec3 _light = vec3(lights[index + 0], lights[index + 1], lights[index + 2]);
			vec3 _lightColor= vec3(lights[index + 3], lights[index + 4], lights[index + 5]);
			
			vec3 light = light(_light, _lightColor);
			
			if(light.r > lighting.r) lighting.r = light.r;
			if(light.g > lighting.g) lighting.g = light.g;
			if(light.b > lighting.b) lighting.b = light.b;
		}
		
	}
	
	vec4 pos = a_position;
	pos.x = pos.x / (WIDTH*SCALE) * WINDOW_WIDTH;
	pos.y = pos.y / (HEIGHT*SCALE) * WINDOW_HEIGHT;

	float tone = 2.0;
	v_color = a_color * vec4(tone, tone, tone, tone) * lighting;
	v_texCoords = a_texCoord0;
	gl_Position = u_projTrans * pos;
}


float dist(vec2 v1, vec2 v2) {
	float xd = v1.x - v2.x;
	float yd = v1.y - v2.y;
	
	if(xd < 0.0) xd = -xd;
	if(yd < 0.0) yd = -yd;
	
	return sqrt(xd * xd + yd * yd);
}

float shade(vec3 light) {
	float lx = light.x * SCALE;
	float ly = (HEIGHT - light.y) * SCALE;

	float dist = dist(vec2(lx, ly), a_position.xy) / light.z;

	float c = (1.0 - sqrt(dist * dist * 2.0) / 300.0);
	return c;
}

vec3 light(vec3 light, vec3 lightColor) {

	float c = shade(light);

	if(c < 0.0) c = 0.0;
	
	vec3 lighting = vec3(lightColor.r * c, lightColor.g * c, lightColor.b * c);
	
	return lighting;
}

Fragment:


#version 120

#ifdef GL_ES
    precision mediump float;
#endif

varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;


void main() {
	vec4 col = texture2D(u_texture, v_texCoords);
    gl_FragColor = v_color * col;
}

Here is the code where I send stuff to shaders.


	public void flushLights() {
				
		Game.shader.setUniformi("lightCount", lights.size());

		float[] lightData = new float[lights.size() * 6];
		
		for(int i=0;i<lights.size();i++) {
			int index = i * 6;
			Light light = lights.get(i);
			
			lightData[index + 0] = light.x;
			lightData[index + 1] = light.y;
			lightData[index + 2] = light.intensity;
			
			lightData[index + 3] = light.color.r;
			lightData[index + 4] = light.color.g;
			lightData[index + 5] = light.color.b;
			
		}
			
		Game.shader.setUniform1fv("lights", lightData, 0, lightData.length);
		
		lights.clear();
	}

Any ideas what I’m doing wrong here? This code works perfectly fine on 2 of newer computers. It doesn’t work on older laptops. Any ideas why?
I found that the problem is when I try to calculate lights by accessing lights uniform.
The only thing that shader.getInfoLog says is “Fragment shader linked, Vertex shader not linked”. What am I supposed to make out of that?..

If the laptop you are testing on came out in and around 2006, it will not work, your GLSL shaders are marked with version 120, which is a pre-processor of openGL 2.1, which came out in 2006. See: http://en.wikipedia.org/wiki/OpenGL_Shading_Language

It doesn’t work with either version :frowning:
If I remove the code which calculates the lights from uniforms, it works for some reason… Doesn’t seem to be the problem with sending data to shader.
I tried 110 and no version at all and it still doesn’t work…

What code are you using to run the shader programs?

What does that mean?
I do ShaderProgram shader = new ShaderProgram("", “”);
SpriteBatch.setShader(shader);

EDIT------------

AHhhhhh behhh I guess I will just have to implement FBO version of lighting somehow…

If you are using older versions (can’t remember what one) you will have to tell the shader program about attributes before hand in the shader creation and linkage code. Looks something like this using LWJGL:

// program is program handle
// location is the attribute number (start with 0 and increment)
// attribute is a string of the attribute name
GL20.glBindAttribLocation(program, location, attribute);

Edit: I think that this is required before V3.3 GLSL #version 330

You should print the log after creating a shader as it will give you a clue.

You also have a pretty ridiculous number of lights, I wouldn’t be surprised if some devices don’t support that many uniforms.

Further, some drivers don’t support dynamic indexing.

Ps longarmx this is only necessary if you want custom attribute locations. It’s unrelated in the OPs case… Also it has to be done before linking, which I’m not sure LibGDX even supports.

@davedes

Its not really that big. 1 light is x,y,intensity,r,g,b. So 384/6 = 64 lights.
Maybe it really doesn’t support dynamic indexing… That would be bad… Very bad…

Query GL_MAX_VERTEX_UNIFORM_COMPONENTS on those machines to see if you’re going over the max.

There are some quirks with this query, see here:
http://www.opengl.org/wiki/GLSL_Uniform

Instead of a single flat array, try using a vec3 array to define the light position, and another parallel vec4 array to define their colours. This should clean up code and remove the need for dynamic indexing.

Also I’m just quickly glancing at your code; but you should avoid if statements and try using built-ins like length(), distance(), abs(), clamp(), max(), etc. as it will lead to a big boost in performance.

If you really feel like you need that many lights, maybe a deferred solution would be more suitable.

I have some tutorials on shaders that you might find useful. They cover some basics like the built-in smoothstep() and other useful functions.

There is also a section on lighting, and you’ll probably be able to take some aspects of it (like falloff etc).

I came to conclusion that older stuff doesn’t support dynamic indexing…

Here is vertex shader I wrote. This one seems to work


#version 120

attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;

uniform mat4 u_projTrans;

uniform vec3[16] lights;
uniform vec3[16] lightColors;
uniform int lightingEnabled;
uniform int lightCount;

varying vec4 v_color;
varying vec2 v_texCoords;


float shade(vec3 light) {
	float lx = light.x;
	float ly = light.y;
	float dist = distance(a_position.xy, vec2(lx, ly)) / light.z;
	float c = (1 - sqrt(dist * dist * 2) / 300);
	return c;
}

vec3 light(vec3 light, vec3 lightColor) {
	float c = shade(light);
	if(c < 0) c = 0;
	
	vec3 l = vec3(lightColor.r * c, lightColor.g * c, lightColor.b * c);
		
	return l;
}

vec4 calculateLighting() {
	vec4 lighting=vec4(0.0, 0.0, 0.0, 1.0);
	
	int counter = 0;
	
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[0], lightColors[0])); } else return lighting;
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[1], lightColors[1])); } else return lighting;
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[2], lightColors[2])); } else return lighting;
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[3], lightColors[3])); } else return lighting;
	
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[4], lightColors[4])); } else return lighting;
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[5], lightColors[5])); } else return lighting;
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[6], lightColors[6])); } else return lighting;
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[7], lightColors[7])); } else return lighting;
	
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[8], lightColors[8])); } else return lighting;
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[9], lightColors[9])); } else return lighting;
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[10], lightColors[10])); } else return lighting;
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[11], lightColors[11])); } else return lighting;
	
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[12], lightColors[12])); } else return lighting;
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[13], lightColors[13])); } else return lighting;
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[14], lightColors[14])); } else return lighting;
	if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[15], lightColors[15])); } else return lighting;
	
	
	return lighting;
}

void main() {
	
	vec4 lighting = vec4(1.0, 1.0, 1.0, 1.0);
	
	if(lightingEnabled == 1) {
	
		lighting = vec4(0.0, 0.0, 0.0, 1.0);		
		
		lighting = calculateLighting();
		
	}
	
	vec4 pos = a_position;
	pos.x = pos.x;
	pos.y = pos.y;

	float tone = 2.0;
	v_color = a_color * vec4(tone, tone, tone, tone) * lighting;
	v_texCoords = a_texCoord0;
	gl_Position = u_projTrans * pos;
}

Light from different sourec are additive why you are doign those complex branchess?

When I do additive, stuff starts looking too bright… This looks a lot better, but has the drawback of making circle-like dark patterns when 2 lights add up. Will need to fix it somehow.