Shaders: Lighting has different brightness on different PCs

Started by
11 comments, last by GameDev.net 11 years, 2 months ago
Hi,

I´m currently developing an online multiplayer game using OpenGL.
Until recently, I used the fixed OpenGL rendering pipeline, but I (nearly) successfully switched to shaders (it looks MUCH better wink.png )

But there is an issue with the lighting:
On my computer everything is fine (I guess because I took suitable values for the lights) ;
but on two other computers, the lightning is pretty much brighter than on mine. Therefore we can not figure out some good looking values for the light...
Could it have something to do with me having ATI and my friend Geforce?
Anyway, I´ll attach two images of the situation to this post.

Now for the code:
As far as the shader goes, here is the code of the fragmentshader:
[source lang="java"]varying vec4 diffuse, ambient;
varying vec3 normal, lightDir, halfVector;
varying vec4 glColor;

uniform sampler2D tex0;
uniform sampler2D tex1;
uniform sampler2D tex2;

uniform int lighting;
uniform int texturing;
uniform int alphamap;

void main (void)
{
vec3 n,halfV;
float NdotL,NdotHV;
vec4 color = glColor;

if (lighting==1) {color*=2*ambient;color.a=1;
} else if (lighting==2) {
color*=ambient;color.a=1;
n = normal;

NdotL = dot(n,lightDir);
if (true) {
NdotL+=0.6f;
NdotL/=1.6f;
color += diffuse * NdotL;
}
}

switch (texturing) {
case 0: gl_FragColor=color;break;
case 1: gl_FragColor=color*texture2D(tex0,gl_TexCoord[0].st);break;
case 2:
vec4 texture0 = texture2D(tex0,gl_TexCoord[0].st);
vec4 texture1 = texture2D(tex1,gl_TexCoord[1].st);
vec4 texture2 = texture2D(tex2,gl_TexCoord[2].st);
if (alphamap==0) {gl_FragColor = color*mix(texture1,texture2,texture0.g);
} else if (alphamap==1) {gl_FragColor = color*mix(texture1,texture2,texture0.r);}
else {gl_FragColor= vec4(1,1,1,1);}break;
}
}
[/source]
Usually lighting equals 2 and texturing equals 1.
There are some calculations in the vertexshader as well:

[source lang="java"] if (lighting>=1) {
ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;
}
if (lighting==2) {
normal = normalize(gl_NormalMatrix * gl_Normal);
lightDir = normalize(vec3(gl_LightSource[0].position));
halfVector = normalize(gl_LightSource[0].halfVector.xyz);
diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
}[/source]

Finally, I set up the lighting in the program itself:

[source lang="java"]//Lighting
GL11.glLightf(GL11.GL_LIGHT0,GL11.GL_SPOT_DIRECTION,180);
GL11.glLightf(GL11.GL_LIGHT0,GL11.GL_CONSTANT_ATTENUATION,1);
GL11.glLightf(GL11.GL_LIGHT0,GL11.GL_LINEAR_ATTENUATION,0.0f);
GL11.glLightf(GL11.GL_LIGHT0,GL11.GL_QUADRATIC_ATTENUATION,0f);

FloatBuffer lightPosition = BufferUtils.createFloatBuffer(4);
lightPosition.put(0.0f).put(1.0f).put(0.0f).put(0.0f).flip();
GL11.glLight(GL11.GL_LIGHT0,GL11.GL_POSITION,lightPosition);
FloatBuffer whiteLight = BufferUtils.createFloatBuffer(4);
whiteLight.put(0.7f).put(0.7f).put(0.7f).put(1.0f).flip();
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_SPECULAR, whiteLight);
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, whiteLight);

FloatBuffer lModelAmbient = BufferUtils.createFloatBuffer(4);
lModelAmbient.put(1.4f).put(1.4f).put(1.4f).put(1.0f).flip();
GL11.glLightModel(GL11.GL_LIGHT_MODEL_AMBIENT,lModelAmbient);
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_LIGHT0);

GL11.glEnable(GL11.GL_COLOR_MATERIAL);
GL11.glColorMaterial(GL11.GL_FRONT_AND_BACK,GL11.GL_AMBIENT_AND_DIFFUSE);[/source]
Basically, the scene consists of a simple ambient light and one directional light (representing the sun).
Now, I get that 1.4f may be a pretty high value for the ambient light, but when I turn it down, the scene gets very dark (at least on my computer).

Do you have a clue of where to look for the mistake?
Is there some kind of function or calculation that may be treated differently by different graphic cards or even by different versions of GLSL?
It´s like one computer adds a certain value to the light and the other doesn´t...

Thanks in advance for any clues
Advertisement
Check the value of your lighting variable on the other computer. From the code you've posted, only ambient lighting is applied when 'lighting' is equal to 1 (both ambient, and directional are applied when lighting is equal to 2).

I don't see where lighting is actually set anywhere in the code you posted but that's a good starting point to take a look at for you.

To reiterate, the reason it looks brighter on the other machine is probably because you're only applying ambient lighting when it runs on that machine.
Check the value of your lighting variable on the other computer.
ok, checked
Usually, the value of the lighting-variable should be 2 nearly all the time;
but to be certain, I changed the code of the shaders to make them do always the same, regardless of the value of this variable.
When we tested it on the problematic computers, no differences was to be found...

Also, I tested it the other way round, now on my computer:
I set the shaders to only act like lighting was 1 all the time. No great differences, just slightly darker and no directional light.

So the lighting variable should act like it is supposed to act, thanks for the suggestion anyway.
I think there may be some calculation of some vector or something that is done differently on both computers.

To eliminate as many sources for errors as possible, I checked the version of OpenGL on the computers.
We both use the same version, 4.2.

i still have not found the cause of the problem, but I think I at least was able to reduce the consequences of the bug:
Now the player can decide for himself, what brightness suits him best.

But still,

if anyone knows why there are different calculations on different pcs when using shader, an answer would be much appreciated.

It has been a while since I worked on the graphics (have been busy with other functionalities), but this bug still affects the game.

Every one that works on a computer with this bug is able to set the brightness to 50% and the overall lightness is good. But it doesn´t create the effect I wanted:

Now everything looks the same, regarding brightness. There are no bright or dark parts like those on the computer where this bug doesnt occur...

What could POSSIBLY make the SAME shader work DIFFER ENT on different machines?

Are there ANY possible circumstances that influence the calculations?

Still thanks if anyone knows anything...

Undefined behavior can. In fact, going by the first image, it looks like lighting isn't being applied at all, and considering the way lighting is set up, it looks like the only reason it works is because you're still using some leftover functionality from the fixed pipeline (instead of passing all lighting variables directly to the shader as uniforms).

By the way, shouldn't gl_FragColor = vec4(1,1,1,1); actually be gl_FragColor = vec4(1.0f,1.0f,1.0f,1.0f);? Silly catch, but I recall GLSL doesn't support implicit casting between integers and floats and this can mess up the shader compilation (dunno if newer versions rectify this, really didn't get any time to work on them).

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

Just to shed some more light on the general problem, without (unfortunately) being able to help much:

When the shader is loaded, it is compiled by the driver before it's sent to the graphics card.

This compilation is the obvious place where things "go wrong" between hardware vendors.

Some compilers are less strict then others, so if you wrote some un-standard code (probably without realizing), it's highly likely that it will behave differently on different hardware.

One good practice is to always specify shader version in the shader program, this generally makes the compiler behave more predictable.

Also don't forget to read the output of the compiler with glGetShaderInfoLog during debug, to catch any warnings.

What could POSSIBLY make the SAME shader work DIFFER ENT on different machines?

Are there ANY possible circumstances that influence the calculations?

I think you are looking in the wrong direction. From the screenshots I would guess, that you use a multi-pass deferred shader. I would guess, that one or more shaders do not compile on different PCs, resulting in seeing the un-postprocessed color-buffer, at least this happens to me.

Some tips:

1. If you develop on nvidia, get your hands on an AMD/ATI videocard. NVidia has a very lax handling of the GLSL specification resulting often in shaders not working on other video chips.

2. Output shader logs, when a shader does not compile or work, your program will not stop working, the shader will only not be applied, leaving black triangles or un-processed buffers .

1. If you develop on nvidia, get your hands on an AMD/ATI videocard. NVidia has a very lax handling of the GLSL specification resulting often in shaders not working on other video chips.

In the first post he said he's using AMD hardware that the computer where it has problems has Nvidia hardware. Yeah, that one makes this situation really odd since it's the Nvidia drivers that are lenient usually. It's probably always a good idea to test on both anyway.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

Thx @all for the answers!

Ok, I´ll just finish my current ToDo and then will test all the things you suggested.

It´s quite possible that I messed up something in the code and did not write it in the "standard-way" of doing it since this is my first time using GLSL.

It looks like this language is a bit nasty compared to Java and its convenient Exceptionswink.png.

Btw, what do you think of the overall graphics quality?

My friend always wants to know what others think of his workwink.png.

This topic is closed to new replies.

Advertisement