Search code examples
delphiopenglfrustumglscene

Determining intersection with frustum in GLScene


using GLScene in delphi I need to find the intersection between an object (a line or plane is enough) and the visible space, to determine what part of this object is currently showing.
I tried getting the view frustum but I couldn't find how. I was thinking of using camera's position, direction and field of view, but I suspect they are not updated when using methods like MoveAroundTarget or setting the target object.
Thanks,
Marco


Solution

  • To get the frustum you can use the ModelViewProjection matrix obtained multipying the ModelViewMatrix and the ProjectionMatrix from TGLScene's current buffer. To get the planes from the matrix use the ExtractFrustumFromModelViewProjection function. Here is a code snippet:

    var
      matMVP: TMatrix;
      frustum : TFrustum;
      intersectPoint : TVector;
    begin
      // get the ModelViewProjection matrix
      matMVP:=MatrixMultiply(GLScene1.CurrentBuffer.ModelViewMatrix, GLScene1.CurrentBuffer.ProjectionMatrix);
      // extract frustum
      frustum:=ExtractFrustumFromModelViewProjection(matMVP);
      // calculate intersection between left plane and line passing through GLArrowLineX object
      if (IntersectLinePlane(GLArrowLineX.Position.AsVector,GLArrowLineX.Direction.AsVector, frustum.pLeft, @intersectPoint)=1)
      then begin
        // do something with intersectPoint
      end else begin
        // no intersection point (parallel or inside plane)
      end;
    end;