How to send modelview matrix to vertex shader

edited September 2017 in GLSL / Shaders

Hello, I need some help to understand vertex shader in Processing.

What I want to do is just to change vertices coordinates in vertex shaders, such as in "" . In order to do that:

  1. restore the vertices coordinates into the world coordinates like worldPos = modlviewInv * vertex

  2. move vertices as we like

  3. re-restore the vertices coordinates into the eye coordinates like newPos = projection * modelview * worldPos

According to the official explanations (, the modelview matrix in the vertex shader in Processing is an "IDENTITY" matrix for some performance issues, which means our vertex shader does not know the original modelview matrix, we need to send the unmodified modelview matrix from the processing side to the vertex shader explicitly.

I thought the code below might work, but I failed, which I do not know why:

Processing side:

PGraphicsOpenGL pg = (PGraphicsOpenGL)g;
shader.set("modelviewOriginal", pg.modelview);//send the modelview matrix to the uniform mat4 variable "modelviewOriginal"

vertex shader side:

vec4 worldPos = modelviewInv * vertex;//restore world coordinates
worldPos = vec4(worldPos.x + 100., worldPos.yzw);//for example, translate x pos
vec4 newPos = projection * modelviewOriginal * worldPos;//re-restore eye coordinates

Probably, I have been missing something basic stuff, I could not have figured them out so far...

So...., please help me !!!!!


  • edited September 2017 Answer ✓

    I experimented with processing and matrices few months ago that is what i came up with:

    Sorry i can't help you any futher Good luck

  • edited September 2017

    Many thanks, nabr! Your code helps me a lot, and your project is also very inspiring!

    I guess your point is in reconstructing the modelview matrix at every frame and sending it at every frame by ourselves, where I just sended the modelview matrix in Processing side... I would try your suggestion this night!

    And, the code which you comment in your code ( seems to deal with exactly the same problem, which would also direct me to the clever way.

    After doing several experiments, I would report my results.

    Again, thank you very much for your swift and kind instructions !

Sign In or Register to comment.