Skip to main content
GameDev.net gamedev.net
🔒 Locked

Shaders: Lighting has different brightness on different PCs

Started by GameDev.net Dec 15, 2012 at 11:46 AM 11 replies 4.3k views
Original Post
GameDev.net
GameDev.net
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
caldiar
caldiar
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.
Sik_the_hedgehog
Sik_the_hedgehog

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

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.

Ashaman73
Ashaman73


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 .

Sik_the_hedgehog
Sik_the_hedgehog


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.
Waterlimon
Waterlimon
Yeah its pretty annoying to have drivers that could probably handle shaders written in HTML, making you unable to fix them on your computer...
o3o
Sik_the_hedgehog
Sik_the_hedgehog


Btw, what do you think of the overall graphics quality?
My friend always wants to know what others think of his work.

Eh, looks a bit too dark to me, although don't trust much this monitor (it shows some shades really dark). Source material is fine though if the first pic (the non-lit one) is anything to go by, it's the lighting that would need to be tweaked. Not much brighter, just a bit. Of course depends what you want to do, I just find it hard to look around there (trees appear practically black - I seriously need a new monitor...).

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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.