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?..