#version 330 core

layout(location = 0) in vec3 aPos;
layout(location = 1) in vec2 aUV;

out vec2 vUV;
out vec3 vWorldPos;

uniform mat4 uMVP;
uniform sampler2D uHeightTex;
uniform float uMinV;
uniform float uMaxV;
uniform float uHeightScale;
uniform float uBaseZ;

float value01(float v) {
    return clamp((v - uMinV) / max(uMaxV - uMinV, 1e-6), 0.0, 1.0);
}

void main() {
    vUV = aUV;
    float v = texture(uHeightTex, aUV).r;
    float h = value01(v) * uHeightScale;
    vec3 world = aPos + vec3(0.0, 0.0, uBaseZ + h);
    vWorldPos = world;
    gl_Position = uMVP * vec4(world, 1.0);
}
