Why does the uniform not work while a fixed value does

If I change:

"gl_Position = transform * (vertex + vec4(offsetX, offsetY, 0, 0));",

to

    "gl_Position = transform * (vertex + vec4(300, offsetY, 0, 0));",

for example then it does shift to the right. But for some unclear reason, offsetX and offsetY never seem to work. Why is that?

PShader shader;

PImage img;

void settings() {
  size(512, 512, P3D);  
}

void setup() {
  frameRate(999);

  shader = new PShader(this, new String[] {
    "#version 410",
    "uniform mat4 transform;",
    "uniform float offsetX;",
    "uniform float offsetY;",

    "in vec4 vertex;",
    "in vec2 texCoord;",

    "out vec2 vertTexCoord;",

    "void main() {",

    "vertTexCoord = vec2(texCoord.x, 1.0 - texCoord.y);",

    // why does offsetX not work but 300 for example does work?
    "gl_Position = transform * (vertex + vec4(offsetX, offsetY, 0, 0));",
   "}"

  },
  new String[] {
    "#version 410",

    "uniform sampler2D texture;",

    "in vec2 vertTexCoord;",

    "out vec4 fragColor;",

    "void main() {",
      "fragColor = vec4(1,0,0,1);",
    "}"
  });



  img = loadImage("http://bellard.org/bpg/lena30.jpg");

}

void draw() {
  background(0);

  shader.set("offsetX", mouseX);
  shader.set("offsetY", mouseY);

  shader(shader);

  translate(width/2, height/2);
  sphere(100);

}

Answers

  • edited October 2015 Answer ✓

    Using shader.set("offsetX", mouseX); Processing will try to set an uniform int offsetX;. Use shader.set("offsetX", (float)mouseX); and Processing will set the correct uniform float offsetX;.

    Anyhow, why not using a vec2 instead of 2 floats:

    PShader shader;
    
    PImage img;
    
    void settings() {
        size(512, 512, P3D);
    }
    
    void setup() {
        frameRate(999);
        shader = new PShader(this, new String[] {
            "#version 410",
    
            "uniform mat4 transform;",
            "uniform vec2 offset;",
    
            "in vec4 vertex;", 
            "in vec2 texCoord;",
    
            "out vec2 vertTexCoord;",
    
            "void main() {", 
                "vertTexCoord = vec2(texCoord.x, 1.0 - texCoord.y);", 
                "gl_Position = transform * (vertex + vec4(offset, 0.0, 0.0));", 
            "}"
        }, new String[] {
            "#version 410",
    
            "uniform sampler2D texture;",
    
            "in vec2 vertTexCoord;",
    
            "out vec4 fragColor;",
    
            "void main() {", 
                "fragColor = vec4(1.0, 0.0, 0.0, 1.0);", 
            "}"
        });
    }
    
    void draw() {
        background(0);
    
        shader.set("offset", (float)mouseX, (float)mouseY);
        shader(shader);
    
        translate(width/2, height/2);
        sphere(100);
    }
    

    But always remember to cast. Using shader.set("offset", mouseX, mouseY); (without the casting to float) would set uniform ivec2 offset; for example.

  • Thanks! I would have never thought of that.

Sign In or Register to comment.