#version 330 core
layout (location = 0) in vec3 vPos;
//layout (location = 1) in vec2 vNormal;
layout (location = 1) in vec2 vTexCoord;
//varying vec3 color;
uniform mat4 MVP;
uniform vec3 data; // time offset scale

out vec3 FragPos;
out vec3 ourNormal;
out vec2 TexCoord;
out float time;
out float height;

// Simple 2D noise function
float noise3(vec2 p) {
    // A basic pseudo-random function (not true Perlin noise, but sufficient for perturbation)
    return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}

// Smooth noise by interpolation (optional enhancement)
float smoothNoise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f); // Smoothstep
    float a = noise3(i);
    float b = noise3(i + vec2(1.0, 0.0));
    float c = noise3(i + vec2(0.0, 1.0));
    float d = noise3(i + vec2(1.0, 1.0));
    return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}


void main() {
    //vec3 pos = position;
    // Calculate relative position to origin
    float wlev=data.y-1.1;

    time=data.x;
    float scale=22.8;
    vec3 sPos = vPos; // scale
    TexCoord = vec2(vTexCoord.x, vTexCoord.y) * scale;
    TexCoord -= scale/2;
    vec2 vertexPosition = TexCoord * (scale + sin(TexCoord.x * 1.0) * 0.5);
    sPos = vec3(0.0, wlev, 0.0);
    sPos.x = vertexPosition.x;
    sPos.z = vertexPosition.y; // Assuming z is your second axis
    //sPos.y += sin(TexCoord.x * 50.0 + time) * 0.2; // Example height with animation



    // Wave parameters: directions, frequencies, speeds, amplitudes
    vec2 dirs[4] = vec2[](
        vec2(1.0, 0.0),              // Horizontal waves
        vec2(0.0, 1.0),              // Vertical waves
        normalize(vec2(1.0, 1.0)),   // Diagonal waves (45°)
        normalize(vec2(-1.0, 1.0))   // Diagonal waves (135°)
    );
    float freqs[4] = float[](0.5, 0.75, 1.0, 1.0);    // Frequencies (waves per unit)
    float speeds[4] = float[](0.1, 0.2, 0.3, 0.2);   // Speeds (phase shift per second)
    float amps[4] = float[](0.1, 0.1, 0.05, 0.05);     // Amplitudes (wave height)

    // Compute base wave height with cross waves
    height = 0.0;
    for (int i = 0; i < 4; i++) {
        float phase = dot(dirs[i], TexCoord) * freqs[i] + time * speeds[i];
        height += amps[i] * sin(phase);
    }

   // Add noise to perturb the UV coordinates for irregularity
    float noiseValue = smoothNoise(TexCoord + vec2(time * 0.1, time * 0.05));
    vec2 uvPerturbed = TexCoord + vec2(noiseValue * 0.1, noiseValue * 0.1);

    // Recompute height with perturbed UVs for more natural waves
    height = 0.0;
    for (int i = 0; i < 4; i++) {
        float phase = dot(dirs[i], uvPerturbed) * freqs[i] + time * speeds[i];
        height += amps[i] * sin(phase);
    }

    // Optional: Add small-scale noise directly to height
    height += 0.05 * smoothNoise(TexCoord * 5.0 + vec2(time * 0.2));

    // Displace the vertex along the y-axis (assuming y-up)
    sPos.y += height;

    // Compute approximate normal using finite differences
    float eps = 0.01;
    float h1 = 0.0, h2 = 0.0;
    for (int i = 0; i < 4; i++) {
        h1 += amps[i] * sin(dot(dirs[i], uvPerturbed + vec2(eps, 0.0)) * freqs[i] + time * speeds[i]);
        h2 += amps[i] * sin(dot(dirs[i], uvPerturbed + vec2(0.0, eps)) * freqs[i] + time * speeds[i]);
    }
    vec3 dx = vec3(eps, h1 - height, 0.0);
    vec3 dz = vec3(0.0, h2 - height, eps);
    ourNormal = normalize(cross(dz, dx));





    //gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, pos.z);
    gl_Position = MVP * vec4(sPos.x, sPos.y, sPos.z, 1.0);
    FragPos = vec3(MVP * vec4(sPos.x, sPos.y, sPos.z, 1.0));
    //gl_Position = MVP * vec4(vertexPosition.x, wlev, vertexPosition.y, 1.0);
    //FragPos = vec3(MVP * vec4(vertexPosition.x, wlev, vertexPosition.y, 1.0));
    //TexCoord = vec2(vNormal.x, vNormal.y);
    //--ourNormal = vec3(vNormal.x, vNormal.y, 0.0);
    //ourNormal = vec3(vNormal.x, 0.0, vNormal.y);
}
