Jump to content

  • Log In with Google      Sign In   
  • Create Account


Shaders: Lighting has different brightness on different PCs


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 m41q   Members   -  Reputation: 151

Like
0Likes
Like

Posted 15 December 2012 - 05:46 AM

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 Posted Image )

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

Attached Thumbnails

  • bright.jpg
  • dark.jpg


Sponsor:

#2 caldiar   Members   -  Reputation: 701

Like
1Likes
Like

Posted 20 December 2012 - 08:41 PM

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.

#3 m41q   Members   -  Reputation: 151

Like
0Likes
Like

Posted 25 December 2012 - 10:23 AM

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.

Edited by m41q, 25 December 2012 - 10:24 AM.


#4 m41q   Members   -  Reputation: 151

Like
0Likes
Like

Posted 28 December 2012 - 05:04 AM

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.



#5 m41q   Members   -  Reputation: 151

Like
0Likes
Like

Posted 10 February 2013 - 06:50 AM

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



#6 Sik_the_hedgehog   Crossbones+   -  Reputation: 1483

Like
1Likes
Like

Posted 11 February 2013 - 07:38 AM

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.

#7 Olof Hedman   Crossbones+   -  Reputation: 2647

Like
1Likes
Like

Posted 11 February 2013 - 08:39 AM

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.


Edited by Olof Hedman, 11 February 2013 - 08:45 AM.


#8 Ashaman73   Crossbones+   -  Reputation: 6685

Like
4Likes
Like

Posted 12 February 2013 - 03:30 AM

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 .



#9 Sik_the_hedgehog   Crossbones+   -  Reputation: 1483

Like
0Likes
Like

Posted 12 February 2013 - 07:05 AM

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.

#10 m41q   Members   -  Reputation: 151

Like
0Likes
Like

Posted 12 February 2013 - 10:13 AM

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.



#11 Waterlimon   Crossbones+   -  Reputation: 2355

Like
0Likes
Like

Posted 12 February 2013 - 11:51 AM

Yeah its pretty annoying to have drivers that could probably handle shaders written in HTML, making you unable to fix them on your computer...

Waterlimon (imagine this is handwritten please)


#12 Sik_the_hedgehog   Crossbones+   -  Reputation: 1483

Like
0Likes
Like

Posted 12 February 2013 - 02:56 PM

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.

#13 m41q   Members   -  Reputation: 151

Like
0Likes
Like

Posted 12 February 2013 - 03:09 PM

Source material is fine

 

ok, good to hear
 

trees appear practically black

yeah... they don´t do that on my screen ^^.
Anyway, because of this bug I tried a workaround with a brightness option, so even your screen would probably be fine ;-)





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS