Hello,
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 = -camPos.dot(v);
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 = vertex.xyz - psCenter;
//----- calculate sn -----//
float sn = dot(-camPos, v);
//----- calculate sd -----//
float sd = length(camPos);
sd = sd * sd;
//----- calculate isec -----//
camPos *= sn/sd;
vec3 isec = vertex.xyz + camPos;
float dist = distance(isec, vertex.xyz);
float distToFocalPlane = dist;