From d3f4d3285d1be8a6445bae43827961c44880033b Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Thu, 15 Feb 2018 18:33:39 +0100 Subject: note switching --- shader.frag | 38 ++++++++++---------------------- sv.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 72 insertions(+), 39 deletions(-) diff --git a/shader.frag b/shader.frag index 0141563..4bb6810 100644 --- a/shader.frag +++ b/shader.frag @@ -10,7 +10,9 @@ uniform int program; uniform sampler2D images[4]; layout (binding = 0,rgba32f) uniform image2D backbuffer; uniform float ratios[4]; -uniform float params[32]; +uniform int idx[2]; +uniform float vel[2]; +uniform float params[4]; out vec4 fragColor; @@ -18,34 +20,16 @@ float random (in float x) { return fract(sin(x)*43758.5453123)-0.5; } -vec4 img(int i) { +vec2 coordinates(int i) { vec2 st = gl_FragCoord.xy/resolution.xy; - st.x *= ratios[i]; - st.x += (1-ratios[i])*0.5; - st.x += 0.5; - return texture2D(images[i],st); + return st; } void main (void) { - vec4 image = img(0); - if (program == 0) { - image = img(2); - image.rg = image.gr; - image = 1.5*image; - } - /* - else { - ivec2 P = ivec2(gl_FragCoord.xy); - vec4 buf = imageLoad(backbuffer,P); - vec4 img1 = mix(img(0),img(1),sin(ticks)); - vec4 img2 = mix(img(2),img(3),cos(ticks)); - buf.bg = buf.gr; - //buf.b = 0.0; - vec4 i = mix(img1,img2,0.5); - //P = ivec2(gl_FragCoord.xy); - imageStore(backbuffer,P,i); - image = mix(i,buf,0.8); - } - */ - fragColor = image; + vec4 img; + vec2 st = coordinates(idx[0]); + img = texture2D(images[idx[0]],st); + st = coordinates(idx[1]); + img = pow(img, texture2D(images[idx[1]],st)); + fragColor = img; } diff --git a/sv.c b/sv.c index de792f0..338509d 100644 --- a/sv.c +++ b/sv.c @@ -48,7 +48,7 @@ typedef struct Param { int new; } Parameter; -Parameter parameters[32]; +Parameter parameters[4]; static snd_seq_t *seq_handle; static int in_port; @@ -180,10 +180,10 @@ void readImage(int i) { glBindTexture(GL_TEXTURE_2D,images[i].id); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); stbi_image_free(pixels); } @@ -200,7 +200,7 @@ void updateImages(int force) { void updateParams(int force) { glUniform1i(glGetUniformLocation(shader.id, "bars"), bars); - for (int i = 0; i<32; i++) { + for (int i = 0; i<4; i++) { if (parameters[i].new || force) { glUniform1f(glGetUniformLocation(shader.id, "params")+i, parameters[i].value); parameters[i].new = 0; @@ -210,8 +210,8 @@ void updateParams(int force) { int ticks =0; int running = 0; -int note = 0; -int vel = 0; +int idx[2] = {0,1}; +float vel[2] = {0,0}; void *readMidi() { midi_open(); @@ -219,11 +219,56 @@ void *readMidi() { snd_seq_event_t *ev = NULL; snd_seq_event_input(seq_handle, &ev); if (ev->type == SND_SEQ_EVENT_NOTEON) { - note = ev->data.note.note; - vel = ev->data.note.velocity; + int note = ev->data.note.note; + int v = ev->data.note.velocity; + int i,j; + if (note < 48) { + i = 0; + j = note-36; + } + else { + i = 1; + j = note-48; + } + // only white keys + if (j == 0) { + idx[i] = 0; vel[i] = (float)v/127.0; + } + else if (j == 2) { + idx[i] = 1; vel[i] = (float)v/127.0; + } + else if (j == 4) { + idx[i] = 2; vel[i] = (float)v/127.0; + } + else if (j == 5) { + idx[i] = 3; vel[i] = (float)v/127.0; + } + } else if (ev->type == SND_SEQ_EVENT_NOTEOFF) { - vel = 0; + int note = ev->data.note.note; + int i,j; + if (note < 48) { + i = 0; + j = note-36; + } + else { + i = 1; + j = note-48; + } + // only white keys + if (j == 0) { + idx[i] = 0; vel[i] = 0; + } + else if (j == 2) { + idx[i] = 1; vel[i] = 0; + } + else if (j == 4) { + idx[i] = 2; vel[i] = 0; + } + else if (j == 5) { + idx[i] = 3; vel[i] = 0; + } } else if(ev->type == SND_SEQ_EVENT_CONTROLLER) printf("[%d] Control: %2x val(%2x)\n", ev->time.tick, @@ -318,7 +363,11 @@ int main(int argc, char **argv) { //glUniform1f(glGetUniformLocation(shader.id, "time"),glfwGetTime()); glUniform1f(glGetUniformLocation(shader.id, "ticks"),(float)ticks); //printf("%i\n",note); - glUniform1i(glGetUniformLocation(shader.id, "program"),note-36); + //glUniform1i(glGetUniformLocation(shader.id, "program"),note-36); + for (int i = 0; i<2; i++) { + glUniform1i(glGetUniformLocation(shader.id, "idx")+i,idx[i]); + glUniform1f(glGetUniformLocation(shader.id, "vel")+i,vel[i]); + } if (shader.new) { createShader(); glUniform2f(glGetUniformLocation(shader.id, "resolution"),width,height); // important!! -- cgit v1.2.3