PDA

View Full Version : TinyPlanet shader problem



sanchiski
02-20-2015, 07:23 AM
Hello, I'm using the next shader to create tiny planet images.
The problem I'm facing is that though it renders perfectly in Desktop machines, when it comes to Android mobile devices it renders also a small square-artifact right at the center of the final image. Can anyone spot the problem? Thank you



varying vec2 v_texcoord;
uniform sampler2D u_texture;

void main()
{
float PI = 3.14159265358979323846264;
float HALF_PI = PI * 0.5;
float TWO_PI = PI * 2.0;
float SCALE = 0.45;
float PLANET_FACTOR = 4.7;

float cosphi0 = cos(PLANET_FACTOR);
float sinphi0 = sin(PLANET_FACTOR);

float x = (v_texcoord.x - 0.5) / SCALE;
float y = (0.5 - v_texcoord.y) / SCALE;
float rho = sqrt(x * x + y * y);
float c = 2.0 * atan(rho);

float sinc = sin(c);
float cosc = cos(c);

float lambda = atan(x * sinc, rho * cosc);
float phi = asin(y * sinc / rho);
float cosphi = cos(phi);

float x1 = cos(lambda) * cosphi;
float y1 = sin(lambda) * cosphi;
float z1 = sin(phi);
lambda = atan(y1, x1 * cosphi0 + z1 * sinphi0);
phi = asin(z1 * cosphi0 - x1 * sinphi0);

vec2 coord = vec2((lambda + PI) / TWO_PI, (phi + HALF_PI) / PI);

gl_FragColor = texture2D(u_texture, coord);
}

GClements
02-20-2015, 09:13 AM
My first guess would be issues related to the precision of the trigonometric functions.

You could try reformulating your equations to avoid some of these. A brief look at your code shows that most of the trigonometric functions can be eliminated using trigonometric identities (http://en.wikipedia.org/wiki/List_of_trigonometric_identities). Primarily, the identity tan(x)=sin(x)/cos(x), the Pythagorean identity sinē(x)+cosē(x)=1, and the double-angle formulae (for sin(2x) and cos(2x) in terms of sin(x), cos(x) and/or tan(x)).

Calculating sin/cos/tan of an angle obtained from asin/acos/atan can be done using only the four arithmetic operators and square roots, without using any trigonometric functions. Likewise for half the angle or an integer multiple of it.