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 directionprivate 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 directionprivate boolean
forward
Indicates whether or not the camera is to move in that directionprivate boolean
left
Indicates whether or not the camera is to move in that directiondouble
movementSpeed
The movement speed of the camera, in scene units per frameprivate 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 spaceprivate Ray[]
rays
An array of the rays the camera shoots out, in the order of left to right for each row, from top to bottomprivate boolean
right
Indicates whether or not the camera is to move in that directionprivate Vector3
rightDirection
The vector which represents the rightward direction of the cameraprivate boolean
rotateLeft
Indicates whether or not the camera is to rotate or tilt in that directionprivate boolean
rotateRight
Indicates whether or not the camera is to rotate or tilt in that directiondouble
rotationSpeed
The rotation speed of the camera, in radians per frameprivate Scene
scene
The scene which the camera should renderprivate 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 directionprivate boolean
tiltUp
Indicates whether or not the camera is to rotate or tilt in that directionprivate boolean
up
Indicates whether or not the camera is to move in that directionprivate 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 .1Camera(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 cameraVector3
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 cameravoid
keyReleased(java.awt.event.KeyEvent e)
Detects when to stop moving the cameravoid
keyTyped(java.awt.event.KeyEvent e)
void
move(Vector3 shift)
Shift's the camera's position by a given vectorvoid
render(int[] pixels)
Called on every frame refresh to recalculate the screenvoid
rotateX(double angle)
Rotates the camera around the x-axis by a given amountvoid
rotateY(double angle)
Rotates the camera around the y-axis by a given amountvoid
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
The position of the camera in 3D space -
direction
The vector of the direction the camera is looking. This vector is normal to the screen where the scene is projected onto. -
upDirection
The vector which represents the upwards direction for the camera, perpendicualr to the camera's looking direction -
rightDirection
The vector which represents the rightward direction of the camera -
screenDistance
private double screenDistanceThe distance, in the direction the camera faces, from the camera to the screen where the image is projected upon (similar to focal length) -
scene
The scene which the camera should render -
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 pixelDistanceThe 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 movementSpeedThe movement speed of the camera, in scene units per frame -
rotationSpeed
public double rotationSpeedThe rotation speed of the camera, in radians per frame -
left
private boolean leftIndicates whether or not the camera is to move in that direction -
right
private boolean rightIndicates whether or not the camera is to move in that direction -
forward
private boolean forwardIndicates whether or not the camera is to move in that direction -
back
private boolean backIndicates whether or not the camera is to move in that direction -
up
private boolean upIndicates whether or not the camera is to move in that direction -
down
private boolean downIndicates whether or not the camera is to move in that direction -
tiltUp
private boolean tiltUpIndicates whether or not the camera is to rotate or tilt in that direction -
tiltDown
private boolean tiltDownIndicates whether or not the camera is to rotate or tilt in that direction -
rotateLeft
private boolean rotateLeftIndicates whether or not the camera is to rotate or tilt in that direction -
rotateRight
private boolean rotateRightIndicates 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 vectordirection
- 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
- Returns:
- the position of the camera as a vector
-
setPosition
- Parameters:
position
- the position to set the camera to as a vector
-
getDirection
- Returns:
- the direction the camera is facing
-
setDirection
- Parameters:
direction
- the direction to set the camera facing to. Cannot be a zero vector
-
getUpDirection
- Returns:
- the direction which is upwards for the camera
-
setUpDirection
- 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
- Returns:
- the current scene which the camera will render
-
setScene
- 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
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 isscreenHeight
- The amount of pixels tall the screen/window isrenderDistance
- 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 interfacejava.awt.event.KeyListener
-
keyReleased
public void keyReleased(java.awt.event.KeyEvent e)Detects when to stop moving the camera- Specified by:
keyReleased
in interfacejava.awt.event.KeyListener
-
keyTyped
public void keyTyped(java.awt.event.KeyEvent e)- Specified by:
keyTyped
in interfacejava.awt.event.KeyListener
-