[SOLVED] PGraphics or PImage as Uniform not working when using GLSL in Java OpenGL

This example works for me:

PShader fx;
PGraphics tex;
void setup() { 
  size(300, 300, P3D);
  fx = loadShader("tex.frag", "tex.vert"); 
  tex = createGraphics(200, 200, P2D);
  tex.beginDraw();
  for(int i=0; i<20; i++) {
    tex.fill(random(255), 0, random(255));
    tex.rect(random(200), random(200), 20, 200);
  }
  tex.endDraw();
   
  textureWrap(REPEAT); 
  noStroke();
  shader(fx);
  fx.set("tex1", tex);
}
void draw() {
  background(80, 100, 200);
  ellipse(mouseX, mouseY, 300, 300);
}
// frag
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

void main() {
  gl_FragColor = vec4(1.0);
}
// vert
uniform mat4 transformMatrix;
attribute vec4 position;

uniform sampler2D tex1;

void main() {
  vec4 c = texture2D(tex1, position.xy / 300.0);
  vec4 pos = position;
  pos.x += c.r * 20.0;
  gl_Position = transformMatrix * pos;
}

Screenshot%20from%202019-08-22%2010-48-19

1 Like