/*

   PSX OGL2 Noise v1.1

   Copyright (C) 2018 SimoneT - simone1tarditi@gmail.com

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    
   Gold Noise ©2017-2018 dcerisano@standard3d.com 

   https://www.shadertoy.com/view/ltB3zD

    - based on the Golden Ratio, PI and Square Root of Two
    - fastest noise generator function
    - works with all chipsets (including low precision)

*/

uniform sampler2D tex0;
uniform vec4 OGL2Param;
uniform vec4 OGL2Size;
uniform vec4 OGL2InvSize;

float PHI = 1.61803398874989484820459 * 00000.1; // Golden Ratio   
float PI  = 3.14159265358979323846264 * 00000.1; // PI
float SQ2 = 1.41421356237309504880169 * 10000.0; // Square Root of Two

float gold_noise( vec2 coordinate, float seed){

    return fract(sin(dot(coordinate*(seed+PHI), vec2(PHI, PI)))*SQ2);

}

void main ()
{

	vec3 c0 = texture2D(tex0,gl_TexCoord[0].xy).rgb;

	float noise1 = gold_noise(gl_TexCoord[0].xy+0.5,(gl_TexCoord[0].x+0.5)*OGL2Size.x);
	float noise2 = gold_noise(gl_TexCoord[0].xy-0.5,(gl_TexCoord[0].x-0.5)*OGL2Size.x);
	float noise3 = gold_noise(gl_TexCoord[0].xy+0.5,(gl_TexCoord[0].y+0.5)*OGL2Size.y);
	float noise4 = gold_noise(gl_TexCoord[0].xy-0.5,(gl_TexCoord[0].y-0.5)*OGL2Size.y);

	gl_FragColor.rgb = vec3(min(min(noise1,noise2),min(noise3,noise4)))*c0+c0;

}
