pepis

benpis
 avatar
unknown
glsl
3 years ago
5.5 kB
3
Indexable
#version 150

#define MIDICUBE_USE_SAVED 0
#define SPUI_USE_SAVED 0

in VertexData
{
    vec4 v_position;
    vec3 v_normal;
    vec2 v_texcoord;
} inData;

out vec4 fragColor;

uniform float       time;
uniform vec2        resolution;
uniform vec2        mouse;
uniform vec3        spectrum;
uniform sampler2D   prevFrame;
uniform sampler2D   prevPass;

uniform sampler2D   midicube_tex_LIVE;
uniform sampler2D   midicube_tex_SAVED;
#if MIDICUBE_USE_SAVED
#define MIDICUBE_TEX midicube_tex_SAVED
#else
#define MIDICUBE_TEX midicube_tex_LIVE
#endif

uniform sampler2D   spui_tex_LIVE;
uniform sampler2D   spui_tex_SAVED;
#if SPUI_USE_SAVED
#define SPUI_TEX spui_tex_SAVED
#else
#define SPUI_TEX spui_tex_LIVE
#endif
#define SV_SPUI_YRES 128
#define SV_SPUI_BLOCKSIZE 16

uniform vec4        viewport2d;

//***************************************
// Thats it for common stuff.          //
// Here come shader-specific uniforms: //
//***************************************
uniform sampler2D   texture0;
uniform vec3        t_off;
uniform ivec3       depths;


//********************
// COMMON FUNCTIONS //
//********************
float myfractpart(float f)
{
    return f - int(f);
}
float lim_fold(float from, float to, float arg)
{
    bool closer2from = arg - from < arg - to;
    float outside = closer2from ? arg - from : arg - to;

    float arg_in_01space = outside / (to - from);
    bool even = int(arg_in_01space) % 2 == 0;
    float place01 = !even ? abs(myfractpart(arg_in_01space)) : 1.0 - abs(myfractpart(arg_in_01space));
    return from + (to - from) * place01;
}
float lim_tor(float from, float to, float arg)
{
    float arg_in_01space = arg / (to - from);
    float fract01 = myfractpart(arg_in_01space);
    if (fract01 < 0) fract01 += 1.0;
    return from + (to - from) * fract01;
}
float remap_ff(float A_from, float A_to, float B_from, float B_to, float A_arg)
{
    A_arg = clamp(A_arg, A_from, A_to);

    float arg01 = (A_arg - A_from) / float(A_to - A_from);
    return B_from + (B_to - B_from) * arg01;
}
float remap_if(int A_from, int A_to, float B_from, float B_to, int A_arg)
{
    A_arg = clamp(A_arg, A_from, A_to);

    float arg01 = (A_arg - A_from) / float(A_to - A_from);
    return B_from + (B_to - B_from) * arg01;
}
float remap_fi(float A_from, float A_to, int B_from, int B_to, float A_arg)
{
    A_arg = clamp(A_arg, A_from, A_to);

    float arg01 = (A_arg - A_from) / float(A_to - A_from);
    return B_from + (B_to - B_from) * arg01;
}
float n11_to_01(float num)
{
    return (num + 1.0) * 0.5;
}
float n01_to_11(float num)
{
    return num * 2.0 - 1.0;
}
float lim_fold01(float num)
{
    return lim_fold(0.0, 1.0, num);
}
float lim_fold11(float num)
{
    return lim_fold(0.0, 1.0, num);
}
//'viewport' = {scale, yaspect, xoffset, yoffset}
vec2 apply_viewport(vec2 basendc, vec4 viewport)
{
    basendc.x *= viewport.x;
    basendc.y *= (viewport.x * viewport.y);
    basendc.x += viewport.z;
    basendc.y += viewport.w;
    
    return basendc;
}
//assumes {0,0} is top left
vec4 getpoint(sampler2D tex, int xres, int yres, int x, int y)
{
    y = yres - 1 - y;

    float x_px_size = 1.0 / float(xres);
    float y_px_size = 1.0 / float(yres);
    
    float x_pos01   = (float(x) + 0.5) * x_px_size;
    float y_pos01   = (float(y) + 0.5) * y_px_size;
    
    return texture(tex, vec2(x_pos01, y_pos01));
}

//**********************
// SPOUT UI FUNCTIONS //
//**********************
vec4 sp_grad(int blockid, float pos01)
{
    const float blockysize = float(SV_SPUI_BLOCKSIZE) / float(SV_SPUI_YRES);
    float y01 = (float(blockid) + 0.5) * blockysize;
    
    float y01a = (float(blockid + 1) + 0.5) * blockysize;
    
    vec4 rgb = texture(SPUI_TEX, vec2(pos01, 1.0-y01));
    vec4 a = texture(SPUI_TEX, vec2(pos01, 1.0-y01a));
    
    return vec4(rgb.r, rgb.g, rgb.b, a.r);
}
float sp_graph(int blockid, float pos01)
{
    const float blockysize = float(SV_SPUI_BLOCKSIZE) / float(SV_SPUI_YRES);
    float y01 = (float(blockid) + 0.5) * blockysize;
    
    return texture(SPUI_TEX, vec2(pos01, 1.0-y01)).r;
}

//**********************
// MIDICUBE FUNCTIONS //
//**********************
float getcc(int ccid)
{
    int zero = 32 * 18 + 29;
    
    int pxid = zero + ccid;
    
    int y = pxid / 32;
    int x = pxid - y * 32;
    
    return getpoint(MIDICUBE_TEX, 32, 32, x, y).x;
}
float cubecc(int channelid)
{
    return getcc(channelid + 20);
}

//*****************************
// UNSTABLE COMMON FUNCTIONS //
//*****************************
vec4 samplefold_11(sampler2D tex, vec2 ndc11)
{
    ndc11.x = lim_fold(-1.0, 1.0, ndc11.x);
    ndc11.y = lim_fold(-1.0, 1.0, ndc11.y);
    
    return texture(tex, vec2(ndc11.x * 0.5 + 0.5, ndc11.y * 0.5 + 0.5));
}

//*************************************
//                                   //
//     HERE BEGINS ACTUAL SHADER     //
//                                   //
//*************************************


void main(void)
{
    vec2 uv01   = inData.v_texcoord;
    vec2 uv01i  = vec2(uv01.x, 1.0-uv01.y);
    vec2 ndc    = -1.0 + 2.0 * uv01;
    
    vec4 usedvp = viewport2d;
    
    vec2 ndc_vp = apply_viewport(ndc, usedvp);
    
    
    fragColor = vec4(
        lim_fold01(n11_to_01(ndc_vp.x)),
        lim_fold01(n11_to_01(ndc_vp.y)),
        lim_fold01(sin(time)),
        1.0);
}