Why is the result upside down

edited October 2015 in GLSL / Shaders

It might be due the time since it's getting a bit late... I know I could remove the 1.0 but it is somewhere else where it goes wrong I guess..

#version 410

uniform mat4 transform;

in vec4 vertex;
in vec2 texCoord;

out vec2 vertTexCoord;

void main() {

    vertTexCoord = vec2(texCoord.x, 1.0 - texCoord.y);
    gl_Position = transform * vertex;
}

// ----

#version 410


uniform sampler2D texture;

in vec2 vertTexCoord;

out vec4 fragColor;

void main() {
    fragColor = texture(texture, vertTexCoord);
}

Answers

  • Answer ✓

    That highly depends on how you are using the shader, as the inner workings of Processing's "coordinate system" are pretty srewed up. In short: It works for filter() not for shader():

    PShader myShader;
    PImage myImage;
    
    void setup() {
        size(600, 600, P2D);
        myShader = new PShader(this, new String[] {
            "#version 410",
            "uniform mat4 transform;",
            "in vec4 vertex;",
            "in vec2 texCoord;",
            "out vec2 vertTexCoord;",
            "void main() {",
                "vertTexCoord = vec2(texCoord.x, 1.0 - texCoord.y);",
                "gl_Position = transform * vertex;",
            "}"
        }, new String[] {
            "#version 410",
            "uniform sampler2D texture;",
            "in vec2 vertTexCoord;",
            "out vec4 fragColor;",
            "void main() {",
                "fragColor = texture2D(texture, vertTexCoord);",
            "}"
        });
        myImage = loadImage("http" + "://forum.processing.org/two/uploads/userpics/970/n4IUAUUTANWJL.png");
    }
    
    void draw() {
    
        // Needs flipped V coordinates
        //image(myImage, 0, 0, width, height);
        //filter(myShader);
    
        // Doesn't need flipped V coordinates
        shader(myShader);
        image(myImage, 0, 0, width, height);
    
    }
    
  • edited October 2015

    I got a reply from codeanticode. https://github.com/processing/processing/issues/4002

    The texture orientation is encoded in the texMatrix:

    PShader myShader;
    PImage myImage;
    
    void setup() {
        size(600, 600, P2D);
        myShader = new PShader(this, new String[] {
            "#version 410",
            "uniform mat4 transform;",
            "uniform mat4 texMatrix;",
            "in vec4 vertex;",
            "in vec2 texCoord;",
            "out vec4 vertTexCoord;",
            "void main() {",
                "vertTexCoord = texMatrix * vec4(texCoord, 1.0, 1.0);",
                "gl_Position = transform * vertex;",
            "}"
        }, new String[] {
            "#version 410",
            "uniform sampler2D texture;",
            "in vec4 vertTexCoord;",
            "out vec4 fragColor;",
            "void main() {",
                "fragColor = texture(texture, vertTexCoord.st);",
            "}"
        });
        myImage = loadImage("http://" + "www.gravatar.com/avatar.php?gravatar_id=15028aab6577e4121ddd4eca402e5f4d&size=250&default=http%3A%2F%2Fvanillicon.com%2F15028aab6577e4121ddd4eca402e5f4d_200.png");
    }
    
    void draw() {
    
        // Needs flipped V coordinates
        //image(myImage, 0, 0, width, height);
        //filter(myShader);
    
        // Doesn't need flipped V coordinates
        shader(myShader);
        image(myImage, 0, 0, width, height);
    }
    
  • edited October 2015

    Instead of just inverting the V value from the screen sized quad you'd have to do a mat4 multiplication for any rendered vertex... Processing. :(

  • Yeah kinda sucks...

  • edited October 2015

    Looked at PGraphicsOpenGL's filter() function, they would have to change the following code:

    beginShape(QUADS);
    texture(filterImage);
    vertex(0, 0, 0, 0);
    vertex(width, 0, 1, 0);
    vertex(width, height, 1, 1);
    vertex(0, height, 0, 1);
    endShape();
    

    to

    beginShape(QUADS);
    texture(filterImage);
    vertex(0, 0, 0, 1); // Just switched the V coordinate
    vertex(width, 0, 1, 1); // Just switched the V coordinate
    vertex(width, height, 1, 0); // Just switched the V coordinate
    vertex(0, height, 0, 0); // Just switched the V coordinate
    endShape();
    
  • Ok, I posted that on github. They like me less each day I guess :)

  • edited October 2015

    Not less than moi! They're very aggressive towards any requests about changing the way they did things!

    And the most insidious disease among many GitHub repositories:
    Replying & closing an issue at once rather than giving some days for the OP to close it on his/her own!

  • Yeah like you said before to someone "watch out they bite hard" :)

Sign In or Register to comment.