translate processing code into glsl

edited August 2015 in GLSL / Shaders


I have written this algorithm with Processing and I'm trying to rewrite it in glsl. I think I'm close to the solution but I'm missing something because I don't get the same result:

code to translate :

PVector camPos = cameraPos.get();    
PVector v  = PVector.sub(pos, psCenter);

float sn =;

float sd = camPos.mag();

sd *= sd;

camPos.mult(sn / sd);

PVector isec = PVector.add(pos, camPos);

float dist = isec.dist(pos);

Can you find a mistake below ?

  vec3 camPos = cameraPosition;

  vec3 v = - psCenter;

  //----- calculate sn -----//

  float sn = dot(-camPos, v);

  //----- calculate sd -----//

  float sd = length(camPos);
  sd = sd * sd;

  //----- calculate isec -----//

  camPos *= sn/sd;

  vec3 isec = + camPos;

  float dist = distance(isec,;

  float distToFocalPlane = dist;


  • a runnable example would help

  • edited August 2015


    You will find at this adress a runnable example:

    The image shows what I'm trying to achieve using a shader. You can active the shader by setting USESHADER to true. As you will see the blur effect is not at the same place.


  • edited September 2015 Answer ✓

    It seems like you didn't take into account that Processing is applying the modelview matrix to each vertex before sending it to the shader. Try to replace lines 79 and 80 with this:

    pointShader.set("psCenter", ((PGraphicsOpenGL)g).modelview.mult(psCenter, null));
    pointShader.set("cameraPosition", ((PGraphicsOpenGL)g).modelview.mult(cameraPosition, null));
  • edited September 2015

    Thank you so much! I think it was the solution I was looking for. I don't get exactly the same visual result (with and without shader) but I think it's because of the way I use offset (and m0ffset) in the vertex shader.

    Do you know where I can learn a lot more about PGraphicsOpenGL ?

  • Well, when I want to learn about a library or class I look at it's source. But there's also the javadoc.

  • I was speaking more about a book or a collection of examples but I can start with this. Thanks again.

  • As far as I know there is no book about Processing's PGraphicsOpenGL, especially as it is constantly changing from version to version.

  • looks really great. It's extreme well written. If I could only get it to work with Xcode...

  • Thank you. I will take a look.

Sign In or Register to comment.