-- Visualize IQ data as a texture from the HackRF
-- You want seizures? 'Cause this is how you get seizures.

VERTEX_SHADER = [[
#version 400
layout (location = 0) in vec3 vp;
layout (location = 1) in vec3 vn;
layout (location = 2) in vec2 vt;
out vec4 color;
out vec2 texCoord;
uniform mat4 uViewMatrix, uProjectionMatrix;
uniform float uTime;
uniform sampler2D uTexture;
void main() {
    float l = 1.0 - ((vp.x * vp.x + vp.z * vp.z) * 0.04);
    color = vec4(l, l, l, l);
    texCoord = vt;
    float r = texture(uTexture, vt).r * 1;

    vec3 pt = vec3(vp.x, r, vp.z);
    gl_Position = uProjectionMatrix * uViewMatrix * vec4(pt, 1.0);
}
]]

FRAGMENT_SHADER = [[
#version 400
in vec4 color;
in vec2 texCoord;
layout (location = 0) out vec4 fragColor;
void main() {
    fragColor = color;
}
]]

function setup()
    freq = 199.9
    device = nrf_device_new(freq, "../rfdata/rf-202.500-2.raw")
    camera = ngl_camera_new_look_at(0, 2, 4)
    shader = ngl_shader_new(GL_LINE_STRIP, VERTEX_SHADER, FRAGMENT_SHADER)
    texture = ngl_texture_new(shader, "uTexture")
    model = ngl_model_new_grid_triangles(100, 100, 0.1, 0.1)
end

function draw()
    ngl_clear(0.2, 0.2, 0.2, 1.0)
    buffer = nrf_device_get_samples_buffer(device)
    ngl_texture_update(texture, buffer, 512, 256)
    ngl_draw_model(camera, model, shader)
end

function on_key(key, mods)
    keys_frequency_handler(key, mods)
end

