Sign in to follow this  

strange problem different colors on different devices

This topic is 823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

since i cant take good image of the result in ogles 2.0 which is what i target i post similar result in es 2.0 emulator.

 

111111111.jpg

 

 

 

 

now problem is in shaders but i am not quite sure why, has anybody encoutered similar problem? how to fix this? what may cause such behavior?

 

vertex shader

[spoiler]

attribute vec3 Vpos;
attribute vec2 Vtexcoord;
attribute vec3 Vnormal;
attribute vec4 Vcolor;

uniform vec4 MVP1;
uniform vec4 MVP2;
uniform vec4 MVP3;
uniform vec4 MVP4;


vec4 vertexClip;
varying vec3 vertex_position;
varying vec3 v_normal;
varying vec2 texcoord;


float dp43(vec4 matrow, vec3 p)
{
return ( (matrow.x*p.x) + (matrow.y*p.y) + (matrow.z*p.z) + matrow.w );
}


 
void main()
{

texcoord = Vtexcoord;

vertexClip.x = dp43(MVP1, Vpos);
vertexClip.y = dp43(MVP2, Vpos);
vertexClip.z = dp43(MVP3, Vpos);
vertexClip.w = dp43(MVP4, Vpos);
v_normal = Vnormal;


vertex_position = Vpos;

gl_Position = vertexClip;






}

 

[/spoiler]

 

[spoiler]


const float Eta = 0.15; 

varying vec3 vertex_position;
varying vec3 v_normal;
varying vec2 texcoord;

uniform vec3 u_cameraPosition;
uniform vec3 LPOS;

uniform sampler2D normal_tex;
uniform sampler2D height_tex;


uniform float min_height;
uniform float height_length;
uniform float time;

uniform vec3 sky_color;
uniform vec3 sun_color;

vec3 vectorAB(vec3 A, vec3 B)
{
return vec3(B.x-A.x, B.y-A.y, B.z-A.z);
}



vec3 vectorcross(vec3 v1, vec3 v2)
{
	vec3 crossproduct;
	crossproduct.x  = (v1.y*v2.z)-(v1.z*v2.y);
	crossproduct.y  = (v1.z*v2.x)-(v1.x*v2.z);
	crossproduct.z  = (v1.x*v2.y)-(v1.y*v2.x);
	
	return crossproduct;
}


vec3 Normal(vec3 p1, vec3 p2, vec3 p3)
{
	vec3 v1 = vectorAB(p2, p1);
	vec3 v2 = vectorAB(p3, p1);
	return normalize( cross(v1, v2) );	
}




float PhongReflection(vec3 DirFromVertToLight, vec3 surfnormal, vec3 PerfectlyReflectedDir, vec3 DirVertexCam)
{
float eep = dot(PerfectlyReflectedDir, DirVertexCam);
float sign = 1.0;
if (eep < 0.0) sign = -1.0;

return 0.5*dot(DirFromVertToLight,surfnormal) + 0.8*sign*pow(eep, 7.0);
}




vec3 return_color(vec3 worldNormal)
{
vec3 lightDir = normalize(vectorAB(LPOS, vertex_position));

vec3 refraction 	= refract(lightDir, worldNormal, Eta);
vec3 reflection_vec = reflect(lightDir, worldNormal);
        
vec3 cam_vert = normalize( vectorAB( u_cameraPosition, vertex_position) );

float REFLECTIONi = max(0.0, -dot(cam_vert, reflection_vec));
float REFRACTIONi = max(0.0, 1.0-dot(cam_vert, refraction));


vec3 refractionColor =  sky_color * REFRACTIONi;
vec3 reflectionColor =  sun_color * REFLECTIONi;

float fresnel = Eta + (1.0 - Eta) * pow(max(0.6, 1.0 - dot(-lightDir, worldNormal)), 5.0);

vec3 ambient_light = vec3(0.0, 0.0, 0.3);
vec3 vert_light = normalize( vectorAB(vertex_position, LPOS) );
vec3 vert_cam = normalize( vectorAB( vertex_position, u_cameraPosition) );

float phong_r = PhongReflection(vert_light, worldNormal, reflection_vec, vert_cam);
 // if (isnan(phong_r)) phong_r = 0.0;

phong_r = max( phong_r, 0.0 );

phong_r = clamp( phong_r, 0.0, 256.0 );
phong_r = clamp( phong_r, 0.0, 1.0 );
    float intensity = min(1.0, max(0.0, fresnel*phong_r));


vec3 res_color = mix(refractionColor, reflectionColor, intensity);

               vec3 sth = (sky_color + res_color);
               sth = clamp(sth, 0.0, 1.0);

        
return sth;
}

float n3ddistance(vec3 first_point, vec3 second_point)
{
float x = first_point.x-second_point.x;
float y = first_point.y-second_point.y;
float z = first_point.z-second_point.z;
float val = x*x + y*y + z*z;
return sqrt(val);
}

void main()
{     
	vec3 finalColor;	
	vec3 adot = vectorAB(u_cameraPosition, vertex_position);
	float dst = sqrt(dot(adot,adot));
	float mipmapintens = dst/320.0;	
	
if ( n3ddistance(u_cameraPosition, vertex_position) >= 660.0 )
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
else
{
//	float4 relfectColor = cubeTexture.Sample(SampleType, reflect(nnormal, nviewdirection));
	
vec3 nnormal = texture2D(normal_tex, texcoord ).rgb*2.0 - 1.0;
vec3 vertex_color = return_color(v_normal);
vec3 texture_color= return_color(nnormal);




    finalColor = (vertex_color + texture_color) / 2.0; //tutaj mozna by bylo zrobic jeszcze rozjasnianie



	
	float intens = 1.0 - dst/500.0;
	finalColor = finalColor * intens + (1.0-intens)*sky_color;	
gl_FragColor = vec4(finalColor, 1.0);
}
} 

[/spoiler]

 

 

 

note that strange things happen in vec3 return_color(vec3 worldNormal)

 

input normal data which are parameters for that function call are the same for both (well i used eyes to verify that tongue.png can be sure if this is precision problem or what, normals seem to be the same, sane for light position, camera position here i post again but only the code of that function

 

[spoiler]


vec3 return_color(vec3 worldNormal)
{
vec3 lightDir = normalize(vectorAB(LPOS, vertex_position));

vec3 refraction 	= refract(lightDir, worldNormal, Eta);
vec3 reflection_vec = reflect(lightDir, worldNormal);
        
vec3 cam_vert = normalize( vectorAB( u_cameraPosition, vertex_position) );

float REFLECTIONi = max(0.0, -dot(cam_vert, reflection_vec));
float REFRACTIONi = max(0.0, 1.0-dot(cam_vert, refraction));


vec3 refractionColor =  sky_color * REFRACTIONi;
vec3 reflectionColor =  sun_color * REFLECTIONi;

float fresnel = Eta + (1.0 - Eta) * pow(max(0.6, 1.0 - dot(-lightDir, worldNormal)), 5.0);

vec3 ambient_light = vec3(0.0, 0.0, 0.3);
vec3 vert_light = normalize( vectorAB(vertex_position, LPOS) );
vec3 vert_cam = normalize( vectorAB( vertex_position, u_cameraPosition) );

float phong_r = PhongReflection(vert_light, worldNormal, reflection_vec, vert_cam);
 // if (isnan(phong_r)) phong_r = 0.0;

phong_r = max( phong_r, 0.0 );

phong_r = clamp( phong_r, 0.0, 256.0 );
phong_r = clamp( phong_r, 0.0, 1.0 );
    float intensity = min(1.0, max(0.0, fresnel*phong_r));


vec3 res_color = mix(refractionColor, reflectionColor, intensity);

               vec3 sth = (sky_color + res_color);
               sth = clamp(sth, 0.0, 1.0);

        
return sth;
} 

[/spoiler]

Edited by ?W ?I ?R ?E ?D ? ?C ?A ?T

Share this post


Link to post
Share on other sites

Is that really the full shader code that's getting passed into OpenGLES? I thought you had to have some sort of precision specification in the fragment shader (IIRC, vertex shader defaults to highp, fragment shader doesn't have a default value for floats, so you either need to declare a default, or you need to specify precision for every float you declare).

 

Anyway, I'd say the most likely problems are either:

 

1. Insufficient precision when calculating lightDir

2. Calling pow with a negative value of eep in PhongReflection (that's undefined behaviour).

Share this post


Link to post
Share on other sites

Damn! i didint see that pow has such stupid thing, anyway it didin't fix the problem you said something about insufficient precision calcing lightDir

 

vec3 lightDir = normalize(vectorAB(LPOS, vertex_position));

i've made both:

varying highp vec3 vertex_position;
varying highp vec3 v_normal;
varying highp vec2 texcoord;
 
coz i didint know how else could i do that (didint fix it too ofc)
 
es 2.0 how it should look like:
es20_2.jpg
 
 
es 3.0 how it looks like :/
es30_2.jpg
 
 
 
 
 
VS
[spoiler]

attribute vec3 Vpos;
attribute vec2 Vtexcoord;
attribute vec3 Vnormal;
attribute vec4 Vcolor;

uniform vec4 MVP1;
uniform vec4 MVP2;
uniform vec4 MVP3;
uniform vec4 MVP4;


vec4 vertexClip;
varying highp vec3 vertex_position;
varying highp vec3 v_normal;
varying highp vec2 texcoord;


float dp43(vec4 matrow, vec3 p)
{
return ( (matrow.x*p.x) + (matrow.y*p.y) + (matrow.z*p.z) + matrow.w );
}


 
void main()
{

texcoord = Vtexcoord;

vertexClip.x = dp43(MVP1, Vpos);
vertexClip.y = dp43(MVP2, Vpos);
vertexClip.z = dp43(MVP3, Vpos);
vertexClip.w = dp43(MVP4, Vpos);
v_normal = Vnormal;


vertex_position = Vpos;

gl_Position = vertexClip;






}


[/spoiler]
 
 
FS
[spoiler]

const float Eta = 0.15; 

varying highp vec3 vertex_position;
varying highp vec3 v_normal;
varying highp vec2 texcoord;

uniform highp vec3 u_cameraPosition;
uniform highp vec3 LPOS;

uniform sampler2D normal_tex;
uniform sampler2D height_tex;


uniform mediump float min_height;
uniform mediump float height_length;
uniform mediump float time;

uniform mediump vec3 sky_color;
uniform mediump vec3 sun_color;

vec3 vectorAB(vec3 A, vec3 B)
{
return vec3(B.x-A.x, B.y-A.y, B.z-A.z);
}



vec3 vectorcross(vec3 v1, vec3 v2)
{
	vec3 crossproduct;
	crossproduct.x  = (v1.y*v2.z)-(v1.z*v2.y);
	crossproduct.y  = (v1.z*v2.x)-(v1.x*v2.z);
	crossproduct.z  = (v1.x*v2.y)-(v1.y*v2.x);
	
	return crossproduct;
}


vec3 Normal(vec3 p1, vec3 p2, vec3 p3)
{
	vec3 v1 = vectorAB(p2, p1);
	vec3 v2 = vectorAB(p3, p1);
	return normalize( cross(v1, v2) );	
}

float absnf(float x)
{
	if (x < 0.0) return -x;
	
	return x;
}


float PhongReflection(vec3 DirFromVertToLight, vec3 surfnormal, vec3 PerfectlyReflectedDir, vec3 DirVertexCam)
{
float eep = dot(PerfectlyReflectedDir, DirVertexCam);
float sign = 1.0;
//if (eep < 0.0) sign = -1.0;

return 0.5*dot(DirFromVertToLight,surfnormal) + 0.8*sign*pow(absnf(eep), 7.0);
}




vec3 return_color(vec3 worldNormal)
{
vec3 lightDir = normalize(vectorAB(LPOS, vertex_position));

vec3 refraction 	= refract(lightDir, worldNormal, Eta);
vec3 reflection_vec = reflect(lightDir, worldNormal);
        
vec3 cam_vert = normalize( vectorAB( u_cameraPosition, vertex_position) );

float REFLECTIONi = max(0.0, -dot(cam_vert, reflection_vec));
float REFRACTIONi = max(0.0, 1.0-dot(cam_vert, refraction));


vec3 refractionColor =  sky_color * REFRACTIONi;
vec3 reflectionColor =  sun_color * REFLECTIONi;

float fresnel = Eta + (1.0 - Eta) * pow(max(0.6, 1.0 - dot(-lightDir, worldNormal)), 5.0);

vec3 ambient_light = vec3(0.0, 0.0, 0.3);
vec3 vert_light = normalize( vectorAB(vertex_position, LPOS) );
vec3 vert_cam = normalize( vectorAB( vertex_position, u_cameraPosition) );

float phong_r = PhongReflection(vert_light, worldNormal, reflection_vec, vert_cam);
 // if (isnan(phong_r)) phong_r = 0.0;

phong_r = max( phong_r, 0.0 );

phong_r = clamp( phong_r, 0.0, 256.0 );
phong_r = clamp( phong_r, 0.0, 1.0 );
    float intensity = min(1.0, max(0.0, fresnel*phong_r));


vec3 res_color = mix(refractionColor, reflectionColor, intensity);

               vec3 sth = (sky_color + res_color);
               sth = clamp(sth, 0.0, 1.0);

        
return sth;
}

float n3ddistance(vec3 first_point, vec3 second_point)
{
float x = first_point.x-second_point.x;
float y = first_point.y-second_point.y;
float z = first_point.z-second_point.z;
float val = x*x + y*y + z*z;
return sqrt(val);
}

void main()
{     
	vec3 finalColor;	
	vec3 adot = vectorAB(u_cameraPosition, vertex_position);
	float dst = sqrt(dot(adot,adot));
	float mipmapintens = dst/320.0;	
	
if ( n3ddistance(u_cameraPosition, vertex_position) >= 660.0 )
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
else
{
//	float4 relfectColor = cubeTexture.Sample(SampleType, reflect(nnormal, nviewdirection));
	
vec3 nnormal = texture2D(normal_tex, texcoord ).rgb*2.0 - 1.0;
vec3 vertex_color = return_color(v_normal);
vec3 texture_color= return_color(nnormal);




    finalColor = (vertex_color + texture_color) / 2.0; //tutaj mozna by bylo zrobic jeszcze rozjasnianie



	
	float intens = 1.0 - dst/500.0;
	finalColor = finalColor * intens + (1.0-intens)*sky_color;	
gl_FragColor = vec4(finalColor, 1.0);
}
}
[/spoiler]
 
 
 

Share this post


Link to post
Share on other sites

editor here sucks so to not screw the code i had to write new reply:

 

vec3 lightDir = normalize(vectorAB(LPOS, vertex_position));
 
vec3 refraction = refract(lightDir, worldNormal, Eta); 
vec3 reflection_vec = reflect(lightDir, worldNormal);
 
return refraction; or  return reflection_vec;
 
 
provide me with different results on bothd devices... now i dont know whats possible wrong is that not sending LPOS, or worldNormal, or maybe wrongly calculated lightDir.
 
because worldNormal (normal i pass to the return_color function  are identical on both devices
 
 
even set light position in shader same for camera position (so now it may be that vertex_position is wrongly passed or somethings else) but when i make a distance function to check cam_pos (passed to shader not fixed to vertex_position seems to be the same on both devices 99.99% sure its the same)
 
here are results with fixed campos and light pos
 
2.0
es_20_3.jpg
 
 
 
 
 
3.0
 
es_30_3.jpg
Edited by ?W ?I ?R ?E ?D ? ?C ?A ?T

Share this post


Link to post
Share on other sites

Sound like the problem was when passing sun position: 

sun_pos = vec3(0.0, earth_radius+earth_radius*0.050, -earth_radius*2.0);

 

es 3.0 device (stupid samsung lets name crap with name) couldn't handle the number (i fixed that by forcing light direction to be computed at cpu) 

in shader i was doing normalize ( vectorAB(sun_pos, vertex_pos) ); // that couldnt be handled

but since sunlight is directional light i passed that direction as an uniform (calculated on cpu)

 

heres a retarded vid of final, warning sound!

 

 

https://www.youtube.com/watch?v=hCCpveIqeKo&feature=youtu.be

Share this post


Link to post
Share on other sites

This topic is 823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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