← 输入框|github.com|刷新
Skip to content

Vertex attributes are passed in view space instead of model space to the vertex shader #2904

Closed

Description

@P0ulp

Relating to this topic : http://forum.processing.org/two/discussion/7484/vertex-attribute-bug-#Item_5

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 ?

PDE

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(); }

Vertex


uniform mat4 transform;

attribute vec4 position;

varying vec4 pos;

void main() {

pos = position;

gl_Position = transform * position;

}

Fragment


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); } }

Activity

  1. JakubValtar commented on Oct 21, 2014

    @JakubValtar
    Contributor

    Thanks for your report. What you observe is correct, right now vertex positions are premultiplied by modelview matrix before passing them to vertex shader. We will discuss if there is a way to make it easier for people to get model coordinates.

    Right now you can convert them back using inverted modelview matrix like this:

    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);
      cubemapShader.set("modelviewInv", ((PGraphicsOpenGL) g).modelviewInv);
    
      box(50);
      resetShader();
    }
    // vertex shader
    uniform mat4 transform;
    
    attribute vec4 position;
    
    varying vec4 pos;
    
    uniform mat4 modelviewInv;
    
    void main() {
      pos = position * modelviewInv;
    
      gl_Position = transform * position;
    }
    // fragment shader
    varying vec4 pos;
    
    void main() { 
      if (pos.x > 0.0) {
        gl_FragColor = vec4(1.0,0.0,0.0,1.0);
      } else {
        gl_FragColor = vec4(0.0,0.0,1.0,1.0);
      }
    }
    

    If you need more help, please post on processing forum.

  2. AmnonOwed commented on Oct 21, 2014

    @AmnonOwed
    Contributor

    We will discuss if there is a way to make it easier for people to get model coordinates.

    Please also discuss a way for easy world space coordinates. Is this even possible, currently?

  3. P0ulp commented on Oct 23, 2014

    @P0ulp
    Author

    Just for information, the matrix you get by the PGraphicsOpenGL object seams not really right.

    For example the cameraInv and the camera matrix are the same of the modelview / modelviewInv, according usual GLSL, the cameraInv and camera may be the same of the view / viewInv matrix.

    More strange, when i get the modelview from the PGraphicsOpenGL, and pass this matrix to the shader via Shader.set, this matrix is not the same passed automatically by the processing.

    So lot of strange things ...

  4. self-assigned this
    on Nov 3, 2014
  5. codeanticode commented on Nov 3, 2014

    @codeanticode
    Contributor

    Hi guys, by default the vertices are indeed sent to the vertex shader already multiplied by the modelview matrix (and so the modelview matrix in the vertex shader is set to the identity to avoid applying the transformation twice, so this might be one reason for the discrepancies you have observed).

    The reason for this seemingly puzzling approach is to optimize performance when drawing multiple shapes: since it is most efficient to minimize the number of buffer copies between CPU and GPU memory, the renderer batches as many shapes together as possible to draw them in a single call. However, if each shape had a different transformation, then it wouldn't be possible to group them because they would have different uniform matrices.

    You can disable the batching with the following hint: hint(DISABLE_OPTIMIZED_STROKE)

    @P0ulp regarding the relationship between the modelview and the camera matrices in Processing is the following:

    modelview = camera * [all other geometry transformations applied after camera()]

    The camera matrix is really not used for anything by the OpenGL renderer (by the exception of the modelXYZ functions), it more of a legacy from the renderers in 1.x

    I'm happy to discuss the matrix handling further if you thing it needs to be changed/improved/clarified.

  6. codeanticode commented on Jul 16, 2015

    @codeanticode
    Contributor

    We will add a section in the wiki explaining the workarounds discussed in this thread.

  7. technosalon commented on Sep 3, 2017

    @technosalon

    After reading this thread, I really need some help to understand the tutorial on Shaders in the official Processing page. https://processing.org/tutorials/pshader/
    If the modelview matrix in vertex shader is the identity, this means that transform * vertex = projection * vertex ?
    Even though, in some tutorials with the position in eye coordinates, expressions like "ecPosition = modelview * position" are used, does this equal to "ecPosition = position" ?
    I am not sure what the meaning of modelview matrix is in the vertex shader in Processing.

  8. benfry commented on Sep 4, 2017

    @benfry
    Contributor

    Please use the forum for coding questions.

  9. locked and limited conversation to collaborators on Sep 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions