Package raymarcher

Class Camera

java.lang.Object
raymarcher.Camera
All Implemented Interfaces:
java.awt.event.KeyListener, java.util.EventListener

public class Camera
extends java.lang.Object
implements java.awt.event.KeyListener
Represents the camera in 3D space from which the scene will be rendered
  • Field Summary

    Fields
    Modifier and Type Field Description
    private boolean back
    Indicates whether or not the camera is to move in that direction
    private Vector3 direction
    The vector of the direction the camera is looking.
    private boolean down
    Indicates whether or not the camera is to move in that direction
    private boolean forward
    Indicates whether or not the camera is to move in that direction
    private boolean left
    Indicates whether or not the camera is to move in that direction
    double movementSpeed
    The movement speed of the camera, in scene units per frame
    private double pixelDistance
    The distance, in scene units between the center of each pixel and the one adjacent to it.
    private Vector3 position
    The position of the camera in 3D space
    private Ray[] rays
    An array of the rays the camera shoots out, in the order of left to right for each row, from top to bottom
    private boolean right
    Indicates whether or not the camera is to move in that direction
    private Vector3 rightDirection
    The vector which represents the rightward direction of the camera
    private boolean rotateLeft
    Indicates whether or not the camera is to rotate or tilt in that direction
    private boolean rotateRight
    Indicates whether or not the camera is to rotate or tilt in that direction
    double rotationSpeed
    The rotation speed of the camera, in radians per frame
    private Scene scene
    The scene which the camera should render
    private double screenDistance
    The distance, in the direction the camera faces, from the camera to the screen where the image is projected upon (similar to focal length)
    private boolean tiltDown
    Indicates whether or not the camera is to rotate or tilt in that direction
    private boolean tiltUp
    Indicates whether or not the camera is to rotate or tilt in that direction
    private boolean up
    Indicates whether or not the camera is to move in that direction
    private Vector3 upDirection
    The vector which represents the upwards direction for the camera, perpendicualr to the camera's looking direction
  • Constructor Summary

    Constructors
    Constructor Description
    Camera()
    Creates a default Camera at (0,0,0), looking in the -z direction, with a screen distance of 1, the +y direction being up, and a screen distance of .1
    Camera​(Vector3 position, Vector3 direction, Vector3 upDirection, double screenDistance, Scene scene)
    Creates a camera in the given position and direction
  • Method Summary

    Modifier and Type Method Description
    void createRays​(int screenWidth, int screenHeight, double renderDistance)
    Creates the set or rays to be cast from the camera
    Vector3 getDirection()  
    double getMovementSpeed()  
    double getPixelDistance()
    Get the distance between each pixel in the camera.
    Vector3 getPosition()  
    double getRotationSpeed()  
    Scene getScene()  
    double getScreenDistance()  
    Vector3 getUpDirection()  
    void keyPressed​(java.awt.event.KeyEvent e)
    Detects when and how to move the camera
    void keyReleased​(java.awt.event.KeyEvent e)
    Detects when to stop moving the camera
    void keyTyped​(java.awt.event.KeyEvent e)  
    void move​(Vector3 shift)
    Shift's the camera's position by a given vector
    void render​(int[] pixels)
    Called on every frame refresh to recalculate the screen
    void rotateX​(double angle)
    Rotates the camera around the x-axis by a given amount
    void rotateY​(double angle)
    Rotates the camera around the y-axis by a given amount
    void setDirection​(Vector3 direction)  
    void setMovementSpeed​(double movementSpeed)  
    void setPixelDistance​(double pixelDistance)
    Set the distance between each pixel in the camera.
    void setPosition​(Vector3 position)  
    void setRotationSpeed​(double rotationSpeed)  
    void setScene​(Scene newScene)  
    void setScreenDistance​(double screenDistance)  
    void setUpDirection​(Vector3 upDirection)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • position

      private Vector3 position
      The position of the camera in 3D space
    • direction

      private Vector3 direction
      The vector of the direction the camera is looking. This vector is normal to the screen where the scene is projected onto.
    • upDirection

      private Vector3 upDirection
      The vector which represents the upwards direction for the camera, perpendicualr to the camera's looking direction
    • rightDirection

      private Vector3 rightDirection
      The vector which represents the rightward direction of the camera
    • screenDistance

      private double screenDistance
      The distance, in the direction the camera faces, from the camera to the screen where the image is projected upon (similar to focal length)
    • scene

      private Scene scene
      The scene which the camera should render
    • rays

      private Ray[] rays
      An array of the rays the camera shoots out, in the order of left to right for each row, from top to bottom
    • pixelDistance

      private double pixelDistance
      The distance, in scene units between the center of each pixel and the one adjacent to it. Decreasing this has a similar effect to increasing screenDistance
    • movementSpeed

      public double movementSpeed
      The movement speed of the camera, in scene units per frame
    • rotationSpeed

      public double rotationSpeed
      The rotation speed of the camera, in radians per frame
    • left

      private boolean left
      Indicates whether or not the camera is to move in that direction
    • forward

      private boolean forward
      Indicates whether or not the camera is to move in that direction
    • back

      private boolean back
      Indicates whether or not the camera is to move in that direction
    • up

      private boolean up
      Indicates whether or not the camera is to move in that direction
    • down

      private boolean down
      Indicates whether or not the camera is to move in that direction
    • tiltUp

      private boolean tiltUp
      Indicates whether or not the camera is to rotate or tilt in that direction
    • tiltDown

      private boolean tiltDown
      Indicates whether or not the camera is to rotate or tilt in that direction
    • rotateLeft

      private boolean rotateLeft
      Indicates whether or not the camera is to rotate or tilt in that direction
    • rotateRight

      private boolean rotateRight
      Indicates whether or not the camera is to rotate or tilt in that direction
  • Constructor Details

    • Camera

      public Camera()
      Creates a default Camera at (0,0,0), looking in the -z direction, with a screen distance of 1, the +y direction being up, and a screen distance of .1
    • Camera

      public Camera​(Vector3 position, Vector3 direction, Vector3 upDirection, double screenDistance, Scene scene)
      Creates a camera in the given position and direction
      Parameters:
      position - The position of the camera as a vector
      direction - The direction the camera is looking at. if a zero vector is passed, a camera will be created looking in the -z direction.
      upDirection - The direction that represents "up" for the camera. This direction must be perpendicular to the camera's look direction. If a non-perpendicular vector is passed, a perpendicular vector will be used.
      screenDistance - The distance, in the direction the camera faces, from the camera to the screen where the image is projected upon (similar to focal length). This distance must be > 0.
      scene - The scene which the camera should render
  • Method Details

    • getPosition

      public Vector3 getPosition()
      Returns:
      the position of the camera as a vector
    • setPosition

      public void setPosition​(Vector3 position)
      Parameters:
      position - the position to set the camera to as a vector
    • getDirection

      public Vector3 getDirection()
      Returns:
      the direction the camera is facing
    • setDirection

      public void setDirection​(Vector3 direction)
      Parameters:
      direction - the direction to set the camera facing to. Cannot be a zero vector
    • getUpDirection

      public Vector3 getUpDirection()
      Returns:
      the direction which is upwards for the camera
    • setUpDirection

      public void setUpDirection​(Vector3 upDirection)
      Parameters:
      upDirection - The direction that represents "up" for the camera. This direction must be perpendicular to the camera's look direction. If a non-perpendicular vector is passed, a perpendicular vector will be used.
    • getScreenDistance

      public double getScreenDistance()
      Returns:
      the distance from the camera to the screen it renders to
    • setScreenDistance

      public void setScreenDistance​(double screenDistance)
      Parameters:
      screenDistance - the distance from the screen to set the camera to, which must be > 0.
    • getScene

      public Scene getScene()
      Returns:
      the current scene which the camera will render
    • setScene

      public void setScene​(Scene newScene)
      Parameters:
      newScene - the new scene to replace the current one and for the camera to render
    • getPixelDistance

      public double getPixelDistance()
      Get the distance between each pixel in the camera. A greater value makes the screen's edges more stretched.
      Returns:
      The current distance, in scene units, between the center of each pixel and the one adjacent to it.
    • setPixelDistance

      public void setPixelDistance​(double pixelDistance)
      Set the distance between each pixel in the camera. A greater value makes the screen's edges more stretched.
      Parameters:
      pixelDistance - The new distance, in scene units, between the center of each pixel and the one adjacent to it. This value should be > 0.
    • getMovementSpeed

      public double getMovementSpeed()
      Returns:
      The current movement speed of the camera, in scene units per frame
    • setMovementSpeed

      public void setMovementSpeed​(double movementSpeed)
      Parameters:
      movementSpeed - The new movement speed to set the camera to, in scene units per frame
    • getRotationSpeed

      public double getRotationSpeed()
      Returns:
      The current rotation speed of the camera, in scene units per frame
    • setRotationSpeed

      public void setRotationSpeed​(double rotationSpeed)
      Parameters:
      rotationSpeed - The new rotation speed to set the camera to, in scene units per frame
    • move

      public void move​(Vector3 shift)
      Shift's the camera's position by a given vector
      Parameters:
      shift - The vector to add to the camera's current position
    • rotateY

      public void rotateY​(double angle)
      Rotates the camera around the y-axis by a given amount
      Parameters:
      angle - The amount to rotate, in radians
    • rotateX

      public void rotateX​(double angle)
      Rotates the camera around the x-axis by a given amount
      Parameters:
      angle - The amount to rotate, in radians
    • createRays

      public void createRays​(int screenWidth, int screenHeight, double renderDistance)
      Creates the set or rays to be cast from the camera
      Parameters:
      screenWidth - The amount of pixels wide the screen/window is
      screenHeight - The amount of pixels tall the screen/window is
      renderDistance - The maximum distance for a ray to travel before assuming it has hit nothing
    • render

      public void render​(int[] pixels)
      Called on every frame refresh to recalculate the screen
      Parameters:
      pixels - The array of pixel color values, linked to the pixels of the Screen's BufferedImage
    • keyPressed

      public void keyPressed​(java.awt.event.KeyEvent e)
      Detects when and how to move the camera
      Specified by:
      keyPressed in interface java.awt.event.KeyListener
    • keyReleased

      public void keyReleased​(java.awt.event.KeyEvent e)
      Detects when to stop moving the camera
      Specified by:
      keyReleased in interface java.awt.event.KeyListener
    • keyTyped

      public void keyTyped​(java.awt.event.KeyEvent e)
      Specified by:
      keyTyped in interface java.awt.event.KeyListener