Sign in to follow this  
m41q

OpenGL Shaders: Lighting has different brightness on different PCs

Recommended Posts

m41q    151
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 [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img] )

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

Share this post


Link to post
Share on other sites
caldiar    715
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.

Share this post


Link to post
Share on other sites
m41q    151
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

Share this post


Link to post
Share on other sites
m41q    151

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.

Share this post


Link to post
Share on other sites
m41q    151

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
alh420    5995

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

Share this post


Link to post
Share on other sites
Ashaman73    13715

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 .

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
m41q    151

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
m41q    151

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 ;-)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By povilaslt2
      Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).
    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
       
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
  • Popular Now