Loading...
Logo
Processing Forum
Hi All I'm trying to learn how to use fragment shaders in process, but I can't get them to work as expected. Here is an example that takes an image as a texture and uses a fragment shader as a filter to swap the red and green channels... only it doesn't. It seems to replace the image with a slightly off-red fill and I have no idea why. What am I doing wrong?

Copy code
  1. PShader myShader;
  2. PImage rtex;
  3. Boolean customShader = false;
  4. PGraphics pg;

  5. void setup() {
  6.   size(1000, 600, OPENGL);
  7.   noSmooth();
  8.   rtex = loadImage("someimage.png");
  9.   myShader = loadShader("test.fs");
  10.   pg = createGraphics(1000, 600, OPENGL);
  11. }

  12. void draw() {
  13.   
  14.   pg.beginDraw();
  15.   pg.image(rtex, 0, 0);
  16.   
  17.   if (customShader) {
  18.     pg.filter(myShader);
  19.   }
  20.   
  21.   pg.endDraw();
  22.   
  23.   image(pg,0,0,width,height);
  24. }

  25. void mousePressed() {
  26.   customShader = !customShader;
  27. }
And here is the shader code:

Copy code
  1. #define PROCESSING_TEXTURE_SHADER

  2. uniform sampler2D textureSampler;
  3. uniform vec2 pixel;
  4. uniform vec2 resolution;

  5. void main() {

  6.     vec2 position = gl_FragCoord.xy / resolution.xy;
  7.     vec4 current = texture2D(textureSampler, position);
  8.     
  9.     float r = current.r;
  10.     float g = current.g;
  11.     float b = current.b;
  12.     float a = current.a;
  13.     
  14.     gl_FragColor = vec4(g,r,b,a);
  15. }

Replies(1)

Ooops, I forgot to include the vertex shader...

Copy code
  1. #define PROCESSING_TEXTURE_SHADER

  2. uniform sampler2D textureSampler;
  3. varying vec2 vTexCoord;

  4. void main()

  5. {
  6.     vTexCoord = gl_MultiTexCoord0.xy;
  7.     gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  8. }