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 booleanbackIndicates whether or not the camera is to move in that directionprivate Vector3directionThe vector of the direction the camera is looking.private booleandownIndicates whether or not the camera is to move in that directionprivate booleanforwardIndicates whether or not the camera is to move in that directionprivate booleanleftIndicates whether or not the camera is to move in that directiondoublemovementSpeedThe movement speed of the camera, in scene units per frameprivate doublepixelDistanceThe distance, in scene units between the center of each pixel and the one adjacent to it.private Vector3positionThe position of the camera in 3D spaceprivate Ray[]raysAn array of the rays the camera shoots out, in the order of left to right for each row, from top to bottomprivate booleanrightIndicates whether or not the camera is to move in that directionprivate Vector3rightDirectionThe vector which represents the rightward direction of the cameraprivate booleanrotateLeftIndicates whether or not the camera is to rotate or tilt in that directionprivate booleanrotateRightIndicates whether or not the camera is to rotate or tilt in that directiondoublerotationSpeedThe rotation speed of the camera, in radians per frameprivate ScenesceneThe scene which the camera should renderprivate doublescreenDistanceThe distance, in the direction the camera faces, from the camera to the screen where the image is projected upon (similar to focal length)private booleantiltDownIndicates whether or not the camera is to rotate or tilt in that directionprivate booleantiltUpIndicates whether or not the camera is to rotate or tilt in that directionprivate booleanupIndicates whether or not the camera is to move in that directionprivate Vector3upDirectionThe 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 voidcreateRays(int screenWidth, int screenHeight, double renderDistance)Creates the set or rays to be cast from the cameraVector3getDirection()doublegetMovementSpeed()doublegetPixelDistance()Get the distance between each pixel in the camera.Vector3getPosition()doublegetRotationSpeed()ScenegetScene()doublegetScreenDistance()Vector3getUpDirection()voidkeyPressed(java.awt.event.KeyEvent e)Detects when and how to move the cameravoidkeyReleased(java.awt.event.KeyEvent e)Detects when to stop moving the cameravoidkeyTyped(java.awt.event.KeyEvent e)voidmove(Vector3 shift)Shift's the camera's position by a given vectorvoidrender(int[] pixels)Called on every frame refresh to recalculate the screenvoidrotateX(double angle)Rotates the camera around the x-axis by a given amountvoidrotateY(double angle)Rotates the camera around the y-axis by a given amountvoidsetDirection(Vector3 direction)voidsetMovementSpeed(double movementSpeed)voidsetPixelDistance(double pixelDistance)Set the distance between each pixel in the camera.voidsetPosition(Vector3 position)voidsetRotationSpeed(double rotationSpeed)voidsetScene(Scene newScene)voidsetScreenDistance(double screenDistance)voidsetUpDirection(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:
keyPressedin interfacejava.awt.event.KeyListener
-
keyReleased
public void keyReleased(java.awt.event.KeyEvent e)Detects when to stop moving the camera- Specified by:
keyReleasedin interfacejava.awt.event.KeyListener
-
keyTyped
public void keyTyped(java.awt.event.KeyEvent e)- Specified by:
keyTypedin interfacejava.awt.event.KeyListener
-