Fragment shader not working as expected...
in
Programming Questions
•
5 months ago
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?
- PShader myShader;
- PImage rtex;
- Boolean customShader = false;
- PGraphics pg;
- void setup() {
- size(1000, 600, OPENGL);
- noSmooth();
- rtex = loadImage("someimage.png");
- myShader = loadShader("test.fs");
- pg = createGraphics(1000, 600, OPENGL);
- }
- void draw() {
- pg.beginDraw();
- pg.image(rtex, 0, 0);
- if (customShader) {
- pg.filter(myShader);
- }
- pg.endDraw();
- image(pg,0,0,width,height);
- }
- void mousePressed() {
- customShader = !customShader;
- }
- #define PROCESSING_TEXTURE_SHADER
- uniform sampler2D textureSampler;
- uniform vec2 pixel;
- uniform vec2 resolution;
- void main() {
- vec2 position = gl_FragCoord.xy / resolution.xy;
- vec4 current = texture2D(textureSampler, position);
- float r = current.r;
- float g = current.g;
- float b = current.b;
- float a = current.a;
- gl_FragColor = vec4(g,r,b,a);
- }
1