How to transfer lighting code from vertex shader to fragment shader?

Vertex shader:


#version 120

attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord;

uniform vec3[64] lights;
uniform vec3[64] 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);

	for(int i=0;i<lightCount;i++) {
		lighting.rgb = max(light(lights[i], lightColors[i]), lighting.rgb);
	}
	
	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;

	v_color = a_color * lighting;
	v_texCoords = a_texCoord;
	gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * pos;
}

Fragment:


#version 120

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

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

This code works properly, but is per vertex so it has some not nice features as one might expect. How do I move lighting calculations to fragment shader? I know I’m asking for quite a lot, but I’m kinda lost here…

Whats your actual question? How glsl works at all? You will have to specify this a little more…
You can use a varying position vector for every vertex to give each fragment its own position (possibly there is a more direct way, no idea, however).
A varying variable varies for each fragment, depending on how close it is to the vertices, in case you did not already know.

Yea maybe I have no idea how glsl works, sorry…

So lets say I make a varying vec2 v_position.
It will be different for each pixel on the screen somehow??

edit ---------------
Here is the problem…

If you look closely you can see entities that shouldn’t be seen, because they are pretty big.
Also, with this code you can see where the vertices are at certain light combinations. (When lighting difference is very extreme)

If you pass the fragment shader the position from the vertex shader and then calculate the lightning in the fragment shader your problem should be solved. GLSL interpolates values from the vertex shader to the fragment shader. Haven’t you ever wondered why you can pass the fragment shader a bunch of texture coordinates and it suddenly outputs the correct fragment for each pixel in the image? ::slight_smile:

If you feel this is stupid question, just ignore it. I’m playing LoL atm…

Can I have same uniforms in fragment shader as in vertex shader?

You can.

i don’t know what to say…

Try working through these tutorials before you try to implement any kind of per-fragment lighting.
http://www.lighthouse3d.com/tutorials/glsl-tutorial/

This might be off topic, but:

[quote]Full DirectX® 9.0 support (Vertex Shader version 2.0 and Pixel Shader version 2.0)
[/quote]
This is from my very old laptop specs.
Does this mean it has opengl version 2.0 support, meaning glsl supported version is 1.10?

No that’s completely a DirectX thing. There is a loose equivalence between DirectX shaders versions and OpenGL shader versions but A) I don’t know it so you’d have to look it up and B) it’s only a rough and ready rule and might not be accurate.

hmmm…almost all integrated graphics chips in the last 6 years support shaders. At least the ones from intel. Not sure if you are using any special or more modern extensions/versions but generally DX9 means at least opengl 2.something and dx11 means at least opengl 3.something.

Also, why not google how to check what your computer supports? Or even better, use the bindings to opengl to find out.