[solved] Burnt colors on Android

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

[solved] Burnt colors on Android

Post by Ats »

As you know, I spent some time with GLES shaders to display Omeganaut correctly on PC and Android phones. But recently I tried working on the light...
So the light was at position (0, 10, 0). I moved it to (10000, 10000, 10000) and the thing looks good on PC.
But on Android, all the colors are burned. It looks like the distance of the light is exploding the intensity of it on Android:
burnt_colors.png
burnt_colors.png (100.5 KiB) Viewed 6220 times

So I managed to reduce that problem by replacing:
vec4 Idiff = LDiffuse * max(dot(N,L), 0.0);
per:
vec4 Idiff = LDiffuse * clamp(dot(N,L), 0.0, 1.0);
in:

Code: Select all

#ifdef GL_ES
  precision mediump float;
#endif

uniform mat4 modelViewMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;

attribute vec4 position, color; // vertex position & color
attribute vec3 normal;   // vertex normal

varying vec3 N;
varying vec3 v;
varying vec4 v_Color;

void main(void)
{
  v = vec3(modelViewMatrix * position);
  N = normalize(normalMatrix * normal);
  v_Color = color;
  gl_Position = modelViewProjectionMatrix * position;
}

Code: Select all

#ifdef GL_ES
  precision mediump float;
#endif

varying vec3 N;
varying vec3 v;
varying vec4 v_Color;

uniform vec4 globalColor; // = Material.Color
uniform mat4 lightPosition;
uniform mat4 lightColorAmbient;
uniform mat4 fogColor;

void main (void)
{
  // Light position
  vec3 LPosition = vec3(lightPosition[0][0], lightPosition[0][1], lightPosition[0][2]);
  vec3 L = normalize(LPosition.xyz - v);

  // calculate Diffuse Term
  vec4 LAmbient = vec4(lightColorAmbient[0][0], lightColorAmbient[0][1], lightColorAmbient[0][2], 1.0);
  vec4 LDiffuse = globalColor * LAmbient;

  // clamp to avoid burned colors on Android?
//  vec4 Idiff = LDiffuse * max(dot(N,L), 0.0);
  vec4 Idiff = LDiffuse * clamp(dot(N,L), 0.0, 1.0);

  gl_FragColor = mix(v_Color, Idiff, 0.3);
}

It's better, but the result still isn't the same on PC and Android. It's way more contrasted on Android for the ships, and the planets keeps burning, and I really don't know why...

PC:
contrast_pc.png
contrast_pc.png (117.44 KiB) Viewed 6220 times
Android:
contrast_android.png
contrast_android.png (197.47 KiB) Viewed 6220 times
Last edited by Ats on Thu Jul 21, 2022 8:07 pm, edited 1 time in total.
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: Burnt colors on Android

Post by Kjell »

Hi Ats,
Ats wrote: Thu Jul 21, 2022 9:37 amIt's better, but the result still isn't the same on PC and Android. It's way more contrasted on Android for the ships, and the planets keeps burning, and I really don't know why...
Make sure to normalize the light vector before doing any calculations with it ( instead of clamping the result of your dot product ). The difference comes from the fact that on desktop ( GL ) the vector is automatically normalized while on mobile ( GL ES ) it is not.

K
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Burnt colors on Android

Post by Ats »

Thanks Kjell.

I'm starting to get somewhere after doing this:
vec3 LPosition = normalize(vec3(lightPosition[0][0], lightPosition[0][1], lightPosition[0][2]));

Now I'm going to work on my contrasts again :wink:


mmm... Do I need to normalize this too?
v = normalize(vec3(modelViewMatrix * position));
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: Burnt colors on Android

Post by Kjell »

Hi Ats,
Ats wrote: Thu Jul 21, 2022 1:25 pmI'm starting to get somewhere after doing this:
vec3 LPosition = normalize(vec3(lightPosition[0][0], lightPosition[0][1], lightPosition[0][2]));
I'd recommend normalizing your vector in a ZExpression before using it in your shader, that way you only do it once instead of for every pixel.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" LightPosition="0 1 2" FileVersion="2">
  <OnLoaded>
    <ZExpression>
      <Expression>
<![CDATA[// Calculate vector length & normalize

float x, y, z, s;

x = App.LightPosition.X;
y = App.LightPosition.Y;
z = App.LightPosition.Z;

s = sqrt(x*x+y*y+z*z);

App.LightPosition /= s;]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
</ZApplication>
K
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Burnt colors on Android

Post by Ats »

Oh, I have never thought of that. Thanks for the clever tip, Kjell!
Post Reply