Vertex attribute bug ?

edited October 2014 in GLSL / Shaders

Hello,

After a lot of headach digging in shader, it seems that the vertex attribute received by the fragment shader is already in the view space.

For example, i have exactly the same result using :

gl_Position = projection * vertex;

or

gl_Position = transform * vertex;

or

gl_Position = modelview * projection * vertex;

Transform must be modelview * projection ?

Thanks in advance for your reply :)

Answers

  • Just a precision according to my poor knowledge in shader the vertex received by the vertex shader must be in model space no ?

  • Nobody have the same issue ?

    Thanks again for your help !

  • edited October 2014

    Vertex goes to vertex shader in object space (aka model space). So regarding your first post, no the vertex attribute does not go to the fragment shader and no it is not in view space.

    The uniform variable (set by Processing) transform is a 4x4 matrix holding the product of the projection and the modelview matrices. The multiplication of a vertex by the transform matrix gives the clip coordinates.

    If you were to do it separately, you should do projection * modelview * vertex. So the other way around than from what you write.

    You don't post any runnable code that shows this supposed 'bug', so your claim seems to be made with little basis. For debugging shaders you can use colors.

    I suggest you read this and for background information you can look at this.

    Good luck! ;-)

  • edited October 2014

    Thank you for your reply, maybe i just don't understand but here my test. I just set a color if the vertex X position is > 0 or not. So if the vertex is really in world space, not in view, if i move the camera the color would stay at the same position on the box. Here, the color move on the box when i move the camera not the world(so the view matrix ?)

    Is-it a normal behaviour ?

    import peasy.*;
    
    PShader cubemapShader;
    PeasyCam cam;
    
    void setup() {
      size(800, 640, P3D);
      frameRate(60);
    
      // Load cubemap shader.
      cubemapShader = loadShader("cubemapfrag.glsl", "cubemapvert.glsl");
      cam = new PeasyCam(this, 0, 0, 0, 180);
      cam.setYawRotationMode();
    }
    
    void draw() {
      background(0);
      shader(cubemapShader);
      box(50);
      resetShader();
    }
    

    uniform mat4 transform;
    attribute vec4 position;
    varying vec4 pos;
    
    void main() {
      pos = position;
      gl_Position = transform * position;
    }
    

    varying vec4 pos;
    
    void main() {
        if(pos.x > 0.0){
            gl_FragColor = vec4(1,0,0,1);
        }
        else{
            gl_FragColor = vec4(0,0,1,1);
        }
    }
    
  • Hmm, indeed this is strange. Perhaps with this code example you can open an issue here to shed some light on these matters: https://github.com/processing/processing/issues

  • Opened !

    Thank you again for your time on this subject :)

Sign In or Register to comment.