Class Camera


public class Camera extends AbstractViewportManager
The Camera allows projecting a 3d scene to a 2d screen based on an orthogonal or perspective projection. The 3d world part displayed on the 2d screen is defined by the following parameters which are illustrated on the image below
  • target indicates the position of the point that the camera is currently centering on.
  • eye indicates the position of the lens of the camera.
  • up indicates the direction of the top of the camera.
  • setRenderingSphereRadius(float) allows defining the volume to capture with the camera. Alternatively, a 2D view will use setRenderingSquare(BoundingBox2d) to define the volume to capture with the camera. They are used to define the width of the field of view.
  • near defines the distance from which a 3d item is visible by camera.
  • far defines the distance up to which a 3d item is visible by camera.

Schema source
All camera settings are in cartesian coordinates.
Author:
Martin Pernollet
  • Field Details

    • DEFAULT_VIEW

      public static final Coord3d DEFAULT_VIEW
      The polar default view point, i.e. Coord3d(Math.PI/3,Math.PI/5,500).
    • DEFAULT_CAMERA_DISTANCE_MODE

      public static final boolean DEFAULT_CAMERA_DISTANCE_MODE
      Defines if camera distance is real, or only squared (squared distance avoid computing Math.sqrt() and is thus faster). Second mode requires value true
      See Also:
    • useSquaredDistance

      protected boolean useSquaredDistance
      Defines if camera distance is real, or only squared (squared distance avoid computing Math.sqrt() and is thus faster).
    • eye

      protected Coord3d eye
      The camera viewpoint
    • target

      protected Coord3d target
      The camera target
    • up

      protected Coord3d up
      The camera up vector
    • scale

      protected Coord3d scale
      The scale used to display elements
    • projectionMode

      protected ProjectionMode projectionMode
      Indicates if we are processing visible volume for 3D or 2D charts. 3D chart will lead to processing a setRenderingSphereRadius(float), while 2D chart will lead to processing a
      invalid @link
      #setRenderingSquare(float, float, float, float, float, float)
    • renderingSphereRadius

      protected float renderingSphereRadius
      The rendering radius, used to automatically define with/height of scene and distance of clipping planes.
    • near

      protected float near
      The distance between the camera eye and the near clipping plane.
    • far

      protected float far
      The distance between the camera eye and the far clipping plane.
    • renderingSquare

      protected BoundingBox2d renderingSquare
    • ortho

      protected Camera.Ortho ortho
      The configuration used to make orthogonal rendering.
      See Also:
      • invalid @see
        glOrtho
    • perspectiveProjectionUseFrustrum

      protected boolean perspectiveProjectionUseFrustrum
  • Constructor Details

    • Camera

      public Camera(Coord3d target)
      Set up a Camera looking at target, with a viewpoint standing at target+(0,0,100). The top of the camera is set up toward the positive Z direction.
    • Camera

      public Camera()
  • Method Details

    • initWithTarget

      public void initWithTarget(Coord3d initialTarget)
    • setEye

      public void setEye(Coord3d eye)
      Set the position of this camera's lens.

      All other parameters of this camera remain unchanged. To change multiple parameters atomically use setPosition(Coord3d, Coord3d, Coord3d, Coord3d).

      Parameters:
      eye - the new scaled position of this camera's lens.
    • getEye

      public Coord3d getEye()
      Returns the eye's position.
    • setTarget

      public void setTarget(Coord3d target)
      Set the position of the target at which this camera is centering.

      All other parameters of this camera remain unchanged. To change multiple parameters atomically use setPosition(Coord3d, Coord3d, Coord3d, Coord3d).

      Parameters:
      target - the new scaled position of this camera's target
    • getTarget

      public Coord3d getTarget()
      Returns the target's position that was set at the last call to lookAt().
    • setUp

      public void setUp(Coord3d up)
      Set the up-direction of this camera.

      All other parameters of this camera remain unchanged. To change multiple parameters atomically use setPosition(Coord3d, Coord3d, Coord3d, Coord3d).

      Parameters:
      up - the new up-direction of this camera
    • getUp

      public Coord3d getUp()
      Returns the top of the camera.
    • getScale

      public Coord3d getScale()
      Returns the scale used by this camera to display elements.
    • setPosition

      public void setPosition(Coord3d eye, Coord3d target)
      Set the camera's eye- and target-location.

      All other parameters of this camera remain unchanged. To change more parameters atomically use setPosition(Coord3d, Coord3d, Coord3d, Coord3d).

      Parameters:
      eye -
      target -
    • setPosition

      public void setPosition(Coord3d eye, Coord3d target, Coord3d up, Coord3d scale)
      Atomically sets this camera's eye- and target-position, its up-direction and the scale with which data are displayed.
      Parameters:
      eye - the scaled location of the camera eye (previously multiplied with this camera's scale)
      target - the scaled location at which this camera will look (previously multiplied with this camera's scale)
      up - the direction of the up-side of this camera (not scaled)
      scale - the scale used by this camera to display elements
    • isTiltUp

      public boolean isTiltUp()
      Returns true if the camera is 'looking up', in other word if the eye's Z value is inferior to the target's Z value.
    • getProjectionMode

      public ProjectionMode getProjectionMode()
      Return the projection mode (for 3D or 2D charts), which was defined while calling setRenderingSphereRadius(float) for 3D charts, or setRenderingSquare(BoundingBox2d, float, float) for 2D charts.
    • setRenderingSphereRadius

      public void setRenderingSphereRadius(float radius)
      Set the radius of the sphere that will be visible by the camera (i.e. contained into the rendered view), for a 3D chart. The "far" and "near" clipping planes are modified according to the eye-target distance. After calling this method, getProjectionMode() returns
      invalid @link
      ProjectionMode.Projection3D
      .
    • setRenderingSquare

      public void setRenderingSquare(BoundingBox2d renderingSquare, float zNear, float zFar)
      Set the boundaries of the model space that should be visible by the camera, for a 2D chart having only X and Y boundaries. The values describe an area relative to the camera settings (eye, target, up), the actual visible region of space is then made of the rendering square centered at the eye/target axis.
    • setRenderingSquare

      public void setRenderingSquare(BoundingBox2d renderingSquare)
      Set the boundaries of the model space that should be visible by the camera, for a 2D chart having only X and Y boundaries. The values describe an area relative to the camera settings (eye, target, up), the actual visible region of space is then made of the rendering square centered at the eye/target axis.
    • setNearFarClippingPlanesWithRadius

      protected void setNearFarClippingPlanesWithRadius(float radius)
    • getRenderingSphereRadius

      public float getRenderingSphereRadius()
      Return the radius of the sphere that will be contained into the rendered 3D view.
    • getRenderingSquare

      public BoundingBox2d getRenderingSquare()
      Return the rendering (X,Y) square that will be contained into the rendered 2D view.
    • setRenderingDepth

      public void setRenderingDepth(float near, float far)
      Manually set the rendering depth (near and far clipping planes). Note that
      invalid @link
      Camera.setRenderingSphereRadius
      modified the "far" clipping plane.
    • getNear

      public float getNear()
      Return the position of the "near" clipping plane
    • getFar

      public float getFar()
      Return the position of the "far" clipping plane
    • side

      public boolean side(Coord3d point)
      Return true if the given (not scaled) point is on the left of the plane build by the eye->target and up-direction vector, else false.
      Parameters:
      point - not scaled point (its values correspond to a point in the scene)
      Returns:
      true if the given point is 'on the left side' of this camera
    • getOrtho

      public Camera.Ortho getOrtho()
      Return last values used to make orthogonal scene rendering. Do not edit.
    • screenToModel

      public Coord3d screenToModel(IPainter painter, Coord3d screen)
      Transform a 2d screen coordinate into a 3d coordinate. The z component of the screen coordinate indicates a depth value between the near and far clipping plane of the Camera. A null coordinate can be returned if the projection could not be performed for some reasons. This may occur if projection or modelview matrices are not invertible or if these matrices where unavailable (hence resulting to zero matrices) while invoking this method. Zero matrices can be avoided by ensuring the GL context is current using IPainter.acquireGL()
    • modelToScreen

      public Coord3d modelToScreen(IPainter painter, Coord3d point)
      Transform a 3d point coordinate into its screen position. This method requires the GL context to be current. If not called inside a rendering loop, that method may not apply correctly and output {0,0,0}. In that case and if the chart is based on JOGL (native), one may force the context to be current using
       
       NativeDesktopPainter p = (NativeDesktopPainter)chart.getPainter();
       p.getCurrentContext(chart.getCanvas()).makeCurrent(); // make context current
       
       Coord3d screen2dCoord = camera.modelToScreen(chart.getPainter(), world3dCoord);
       
       p.getCurrentContext(chart.getCanvas()).release(); // release context to let other use it
       
       
      A null coordinate can be returned if the projection could not be performed for some reasons.
    • show

      public void show(IPainter painter, Transform transform, Coord3d scaling)
    • shoot

      public void shoot(IPainter painter, CameraMode projection)
      Sets the projection and the mapping of the 3d model to 2d screen. The projection must be either Camera.PERSPECTIVE or Camera.ORTHOGONAL.
      shoot() finally calls the GL function glLookAt, according to the stored eye, target, up and scale values.
      Note that the Camera set by itselft the MatrixMode to model view at the end of a shoot().
      Parameters:
      painter - TODO
      projection - the projection mode.
      Throws:
      a - Runtime Exception if the projection mode is neither Camera.PERSPECTIVE nor Camera.ORTHOGONAL.
    • shoot

      public void shoot(IPainter painter, CameraMode projection, boolean doPushMatrixBeforeShooting)
    • doShoot

      public void doShoot(IPainter painter, CameraMode projection)
      Apply camera position and orientation and performs projection of the visible volume either in perspective or orthogonal mode. The orthogonal mode support 2D/3D.
    • projectionPerspective

      public void projectionPerspective(IPainter painter, ViewportConfiguration viewport)
      Perform a perspective projection by processing the field of view based on the renderingSphereRadius, target and eye.
      Parameters:
      painter -
      viewport -
    • doLookAt

      public void doLookAt(IPainter painter)
    • projectionOrtho

      public void projectionOrtho(IPainter painter, ViewportConfiguration viewport)
      Perform a orthogonal projection. The viewable part of the 3d scene is defined by parameters {left, right, bottom, top, near, far} which are processed according to the ViewportMode and the values of the camera settings (renderingSphereRadius, target and eye, near and far clipping planes).
      Parameters:
      painter -
      viewport -
    • projectionOrtho2D

      protected void projectionOrtho2D()
    • projectionOrtho3D

      protected void projectionOrtho3D(ViewportConfiguration viewport)
    • computeFieldOfView

      protected double computeFieldOfView(double size, double distance)
      Compute the field of View, in order to occupy the entire screen in PERSPECTIVE mode.
    • getDistance

      public double getDistance(Drawable drawable)
      Return the distance between the camera eye and the given drawable's barycenter.
    • getDistance

      public double getDistance(Drawable drawable, Coord3d viewScale)
      Apply scaling before computing distance between the camera eye and the given drawable's barycenter.
    • getDistance

      public double getDistance(Coord3d coord)
      Return the distance between the camera eye and the given coordinate.
    • getDistance

      public double getDistance(Coord3d coord, Coord3d viewScale)
      Apply scaling before computing distance between the camera eye and the given coordinate.
    • isUseSquaredDistance

      public boolean isUseSquaredDistance()
    • setUseSquaredDistance

      public void setUseSquaredDistance(boolean useSquaredDistance)
      Defines what getDistance(...) will return, either:
      • Squared distance (faster to compute since no Math.sqrt(...) at the end)
      • Real distance
      Default value is set to true, meaning it use the faster squared distance.
    • toString

      public String toString()
      Print out in console information concerning the camera.
      Overrides:
      toString in class Object
    • toString

      protected String toString(Coord3d eye, Coord3d target, Coord3d up)