Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

GLSL shader help

$
0
0
With the release of the Processing 2.0 the integration of GLSL shaders through the PShader has sort of forced me into properly learning GLSL (not a bad thing - just a steep learning curve) but has thrown up a few questions which if anybody has a pointer would be much appreciated. Ok brief outline of what I'm trying to achieve, I took one of the the examples from the  http://thndl.com  GLSL tutorials and started hacking away to try and make an audio visualization rendered entirely in a fragment shader. 

To do this I pass the left and right audio buffer data as float arrays into a GLSL shader and then use this to visualization of the waveforms. The first question is that when trying to index into the array containing the audio data I keep getting runtime errors - I think this is to do with dynamic access and that I need to create a dynamic uniform expression to index the array. I tried to get around this by creating a new array of floats but kept getting a redefinition error saying that I cannot convert a uniform float[] to a float[].

I've posted the code below if anybody could point me in the right direction the last 5 hours of slowly removing my hair will have not been in vain  .

Rich

GLSL Fragment shader
  1. #ifdef GL_ES
  2. precision highp float;
  3. precision highp int;
  4. #endif

  5. #define PROCESSING_COLOR_SHADER

  6. uniform float time;
  7. uniform vec2 resolution;
  8. uniform vec2 mouse;
  9. uniform float audioLeft[1024];
  10. uniform float audioRight[1024];
  11. uniform float bufferSize;

  12. // Required by Processing's default vertex shader
  13. varying vec4 vertColor;

  14. // f will be used to store the color of the current fragment
  15. vec4 f = vec4(1.,1.,1.,1.);

  16. void main(void)
  17. {    
  18.   // c will contain the position information for the current fragment (from -1,-1 to 1,1)
  19.   vec2 c = vec2 (-1.0 + 2.0 * gl_FragCoord.x / resolution.x, 1.0 - 2.0 * gl_FragCoord.y / resolution.y) ;
  20.  
  21.           // mousepos will contain the current mouse position (from -1,-1 to 1,1)
  22. vec2 mousepos = -1.0 + 2.0 * mouse.xy / resolution.xy;
  23.                     
  24.   float t = time;
  25.         
  26.         // second problem line of code
  27.         float audioBufferLeft[1024] = audioLeft;
  28.  
  29.         // problem line of code 
  30.   int bufX = int(bufferSize * gl_FragCoord.x / resolution.x);

  31.   float yDiff = c.y - audioLeft[bufX];
  32. float yDiff1 = c.y - audioRight[bufX];
  33. float y = c.y + yDiff;
  34. float g = abs(0.3 / y);
  35. float h = abs(c.x);
  36. vec3 col = vec3(g * (1.0 - h), g * h, g * 1.5);
  37. f = vec4(col, 1.0);  
  38.     
  39.   // sin wave   
  40. //float y = c.y + 0.5 * sin(c.x * 3.14 - t);
  41.   //float g = abs(0.1 / y);
  42. //float h = abs(c.x);
  43. //vec3 col = vec3(g * (1.0 - h), g * h, g * 1.5);
  44. //f = vec4(col, 1.0);  

  45. gl_FragColor = f;
  46. }

Processing Sketch
  1. import ddf.minim.*;

  2. Minim minim;
  3. AudioInput in;

  4. PShader myShader;

  5. void setup() {
  6.   size(1024, 1080, P2D);
  7.   noSmooth();

  8.   minim = new Minim(this);

  9.   in = minim.getLineIn();

  10.   myShader = loadShader("shader.glsl");
  11.   myShader.set("resolution", float(width), float(height));
  12.   myShader.set("bufferSize", 1024.0);
  13.   shader(myShader); 
  14. }

  15. void draw() {
  16.   background(0);
  17.   float[] left = new float[in.bufferSize()];
  18.   float[] right = new float[in.bufferSize()];
  19.   for (int i = 0; i < in.bufferSize(); i++)
  20.   {
  21.     left[i] = in.left.get(i);
  22.     right[i] = in.right.get(i);
  23.   }

  24.   myShader.set("audioLeft", left);
  25.   myShader.set("audioRight", right);
  26.   myShader.set("time", millis() / 1000.0);
  27.   myShader.set("mouse", float(mouseX), float(mouseY));
  28.   shader(myShader);

  29.   noStroke();
  30.   fill(0);
  31.   rect(0, 0, width, height);  

  32.   resetShader();
  33. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles