Class Camera
Given input data such as location, orientation (direction, left, up), and viewport settings, it can compute data necessary to render objects with the graphics library. Two matrices are generated, the view matrix transforms objects from world space into eye space, while the projection matrix transforms objects from eye space into clip space.
Another purpose of the camera class is to do frustum culling operations, defined by six planes which define a 3D frustum shape, it is possible to test if an object bounded by a mathematically defined volume is inside the camera frustum, and thus to avoid rendering objects that are outside the frustum
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
The result of a culling check operation. -
Field Summary
Modifier and TypeFieldDescriptionprotected float[]
Temporary values computed in onFrustumChange that are needed if a call is made to onFrameChange.protected float[]
Temporary values computed in onFrustumChange that are needed if a call is made to onFrameChange.protected float[]
Temporary values computed in onFrustumChange that are needed if a call is made to onFrameChange.protected float[]
Temporary values computed in onFrustumChange that are needed if a call is made to onFrameChange.protected float
Distance from camera to bottom frustum plane.protected float
Distance from camera to far frustum plane.protected float
Distance from camera to left frustum plane.protected float
Distance from camera to near frustum plane.protected float
Distance from camera to right frustum plane.protected float
Distance from camera to top frustum plane.protected int
The height of the viewport in pixels.protected Vector3f
Camera's location.protected String
The camera's name.protected Matrix4f
Transforms eye space into clip space, unless overridden by projectionMatrixOverride.protected Matrix4f
Temporarily overrides the projection matrix.protected Quaternion
The orientation of the camera.protected Matrix4f
Transforms world space into eye space.protected float
Percent value on display where vertical viewing begins for this camera.protected boolean
True if the renderer needs to update its viewport boundaries.protected float
Percent value on display where horizontal viewing starts for this camera.protected float
Percent value on display where horizontal viewing ends for this camera.protected float
Percent value on display where vertical viewing ends for this camera.protected Matrix4f
Transforms world space into clip space.protected int
The width of the viewport in pixels.protected Plane[]
Array holding the planes that this camera will check for culling. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Clears the viewport changed flag once it has been updated inside the renderer.clone()
contains
(BoundingVolume bound) Tests a bounding volume against the planes of the camera's frustum.boolean
containsGui
(BoundingVolume bound) Tests a bounding volume against the ortho bounding box of the camera.void
Copies the settings of the given camera.float
Returns the pseudo distance from the given position to the near plane of the camera.float
Obtains the aspect ratio.Retrieves the direction vector the camera is facing.getDirection
(Vector3f store) Retrieves the direction vector the camera is facing.float
getFov()
Obtains field of view when the camera is in perspective mode.float
Returns the value of the bottom frustum plane.float
Gets the value of the far frustum plane.float
Gets the value of the left frustum plane.float
Gets the value of the near frustum plane.float
Gets the value of the right frustum plane.float
Gets the value of the top frustum plane.int
Returns the display height.getLeft()
Retrieves the left axis of the camera.Retrieves the left axis of the camera.Retrieves the location vector of the camera.getName()
Returns the camera's name.int
Returns the state of the frustum planes.Provides access to the projection matrix.Retrieves the rotation quaternion of the camera.getScreenCoordinates
(Vector3f worldPos) Converts the given position from world space to screen space.getScreenCoordinates
(Vector3f worldPosition, Vector3f store) Converts the given position from world space to screen space.getUp()
Retrieves the up axis of the camera.Retrieves the up axis of the camera.Provides access to the view matrix.float
Gets the bottom boundary of the viewport.float
Gets the left boundary of the viewport.float
Gets the right boundary of the viewport.float
Gets the top boundary of the viewport.Provides access to the view projection matrix.float
getViewToProjectionZ
(float viewZPos) Computes the z value in projection space from the z value in view space Note that the returned value goes non-linearly from 0 to 1.int
getWidth()
Returns the display width.getWorldCoordinates
(Vector2f screenPos, float projectionZPos) Computes a position in World space given a screen position in screen space (0,0 to width, height) and a z position in projection space ( 0 to 1 non-linear).getWorldCoordinates
(Vector2f screenPosition, float projectionZPos, Vector3f store) Converts the given position from screen space to world space.getWorldPlane
(int planeId) Provides access to one of the planes used for culling.boolean
Determines whether the projection is parallel or perspective.boolean
Tests whether the viewport (width, height, left, right, bottom, up) has been changed.void
A convenience method for auto-setting the frame based on a world position the user desires the camera to look at.void
lookAtDirection
(Vector3f direction, Vector3f up) Sets the direction the camera is facing given a direction and an up vector.void
Normalizes the camera vectors.void
Updates the view frame of the camera.void
Updates the frustum to reflect any changes made to the planes.void
Called when the viewport has been changed.void
read
(JmeImporter importer) void
resize
(int width, int height, boolean fixAspect) Resizes this camera's view for the specified display size.void
setAxes
(Quaternion axes) Uses a rotational matrix to set the axes of the camera.void
Sets the axes (left, up and direction) for this camera.void
setClipPlane
(Plane clipPlane) Sets a clipPlane for this camera.void
setClipPlane
(Plane clipPlane, Plane.Side side) Sets a clipPlane for this camera.void
setFov
(float fovY) Sets the field of view when the camera is in perspective mode.void
setFrame
(Vector3f location, Quaternion axes) Sets the orientation and location of the camera.void
Sets the orientation and location of the camera.void
setFrustum
(float near, float far, float left, float right, float top, float bottom) Sets the frustum of this camera object.void
setFrustumBottom
(float frustumBottom) Sets the value of the bottom frustum plane.void
setFrustumFar
(float frustumFar) Sets the value of the far frustum plane.void
setFrustumLeft
(float frustumLeft) Sets the value of the left frustum plane.void
setFrustumNear
(float frustumNear) Sets the value of the near frustum plane.void
setFrustumPerspective
(float fovY, float aspect, float near, float far) Defines the frustum for the camera.void
setFrustumRight
(float frustumRight) Sets the value of the right frustum plane.void
setFrustumTop
(float frustumTop) Sets the value of the top frustum plane.void
setLocation
(Vector3f location) Sets the position of the camera.void
Sets the camera's name.void
setParallelProjection
(boolean value) Enables/disables parallel projection.void
setPlaneState
(int planeState) Sets the state to keep track of tested planes for culling.void
setProjectionMatrix
(Matrix4f projMatrix) Overrides the projection matrix used by the camera.void
setRotation
(Quaternion rotation) Sets the orientation of this camera.void
setViewPort
(float left, float right, float bottom, float top) Sets the boundaries of the viewport.void
setViewPortBottom
(float bottom) Sets the bottom boundary of the viewport.void
setViewPortLeft
(float left) Sets the left boundary of the viewport.void
setViewPortRight
(float right) Sets the right boundary of the viewport.void
setViewPortTop
(float top) Sets the top boundary of the viewport.toString()
void
update()
Updates the camera parameters by callingonFrustumChange
,onViewPortChange
andonFrameChange
.void
Updates the view projection matrix.void
write
(JmeExporter e)
-
Field Details
-
location
Camera's location. -
rotation
The orientation of the camera. -
frustumNear
protected float frustumNearDistance from camera to near frustum plane. -
frustumFar
protected float frustumFarDistance from camera to far frustum plane. -
frustumLeft
protected float frustumLeftDistance from camera to left frustum plane. -
frustumRight
protected float frustumRightDistance from camera to right frustum plane. -
frustumTop
protected float frustumTopDistance from camera to top frustum plane. -
frustumBottom
protected float frustumBottomDistance from camera to bottom frustum plane. -
coeffLeft
protected float[] coeffLeftTemporary values computed in onFrustumChange that are needed if a call is made to onFrameChange. -
coeffRight
protected float[] coeffRightTemporary values computed in onFrustumChange that are needed if a call is made to onFrameChange. -
coeffBottom
protected float[] coeffBottomTemporary values computed in onFrustumChange that are needed if a call is made to onFrameChange. -
coeffTop
protected float[] coeffTopTemporary values computed in onFrustumChange that are needed if a call is made to onFrameChange. -
viewPortLeft
protected float viewPortLeftPercent value on display where horizontal viewing starts for this camera. Default is 0. -
viewPortRight
protected float viewPortRightPercent value on display where horizontal viewing ends for this camera. Default is 1. -
viewPortTop
protected float viewPortTopPercent value on display where vertical viewing ends for this camera. Default is 1. -
viewPortBottom
protected float viewPortBottomPercent value on display where vertical viewing begins for this camera. Default is 0. -
worldPlane
Array holding the planes that this camera will check for culling. -
width
protected int widthThe width of the viewport in pixels. -
height
protected int heightThe height of the viewport in pixels. -
viewportChanged
protected boolean viewportChangedTrue if the renderer needs to update its viewport boundaries. -
projectionMatrixOverride
Temporarily overrides the projection matrix. -
viewMatrix
Transforms world space into eye space. -
projectionMatrix
Transforms eye space into clip space, unless overridden by projectionMatrixOverride. -
viewProjectionMatrix
Transforms world space into clip space. -
name
The camera's name.
-
-
Constructor Details
-
Camera
protected Camera()Serialization only. Do not use. -
Camera
public Camera(int width, int height) Instantiates a newCamera
object. All values of the camera are set to default.- Parameters:
width
- the desired width (in pixels)height
- the desired height (in pixels)
-
-
Method Details
-
clone
-
copyFrom
Copies the settings of the given camera.- Parameters:
cam
- the camera we copy the settings from
-
setName
Sets the camera's name.- Parameters:
name
- the camera's name
-
getName
Returns the camera's name.- Returns:
- the camera's name
-
setClipPlane
Sets a clipPlane for this camera. The clipPlane is used to recompute the projectionMatrix using the plane as the near plane This technique is known as the oblique near-plane clipping method introduced by Eric Lengyel more info here- http://www.terathon.com/code/oblique.html
- http://aras-p.info/texts/obliqueortho.html
- http://hacksoflife.blogspot.com/2008/12/every-now-and-then-i-come-across.html
Note that this will work properly only if it's called on each update, and be aware that it won't work properly with the sky bucket. if you want to handle the sky bucket, look at how it's done in SimpleWaterProcessor.java
- Parameters:
clipPlane
- the planeside
- the side the camera stands from the plane
-
setClipPlane
Sets a clipPlane for this camera. The cliPlane is used to recompute the projectionMatrix using the plane as the near plane This technique is known as the oblique near-plane clipping method introduced by Eric Lengyel more info here- http://www.terathon.com/code/oblique.html
- http://aras-p.info/texts/obliqueortho.html
- http://hacksoflife.blogspot.com/2008/12/every-now-and-then-i-come-across.html
Note that this will work properly only if it's called on each update, and be aware that it won't work properly with the sky bucket. if you want to handle the sky bucket, look at how it's done in SimpleWaterProcessor.java
- Parameters:
clipPlane
- the plane
-
resize
public void resize(int width, int height, boolean fixAspect) Resizes this camera's view for the specified display size. Invoked by an associatedRenderManager
to notify the camera of changes to the display dimensions.- Parameters:
width
- the new width of the display, in pixelsheight
- the new height of the display, in pixelsfixAspect
- if true, recompute the camera's frustum to preserve its prior aspect ratio
-
getFrustumBottom
public float getFrustumBottom()Returns the value of the bottom frustum plane.- Returns:
- the value of the bottom frustum plane.
-
setFrustumBottom
public void setFrustumBottom(float frustumBottom) Sets the value of the bottom frustum plane.- Parameters:
frustumBottom
- the value of the bottom frustum plane.
-
getFrustumFar
public float getFrustumFar()Gets the value of the far frustum plane.- Returns:
- the value of the far frustum plane.
-
setFrustumFar
public void setFrustumFar(float frustumFar) Sets the value of the far frustum plane.- Parameters:
frustumFar
- the value of the far frustum plane.
-
getFrustumLeft
public float getFrustumLeft()Gets the value of the left frustum plane.- Returns:
- the value of the left frustum plane.
-
setFrustumLeft
public void setFrustumLeft(float frustumLeft) Sets the value of the left frustum plane.- Parameters:
frustumLeft
- the value of the left frustum plane.
-
getFrustumNear
public float getFrustumNear()Gets the value of the near frustum plane.- Returns:
- the value of the near frustum plane.
-
setFrustumNear
public void setFrustumNear(float frustumNear) Sets the value of the near frustum plane.- Parameters:
frustumNear
- the value of the near frustum plane.
-
getFrustumRight
public float getFrustumRight()Gets the value of the right frustum plane.- Returns:
- frustumRight the value of the right frustum plane.
-
setFrustumRight
public void setFrustumRight(float frustumRight) Sets the value of the right frustum plane.- Parameters:
frustumRight
- the value of the right frustum plane.
-
getFrustumTop
public float getFrustumTop()Gets the value of the top frustum plane.- Returns:
- the value of the top frustum plane.
-
setFrustumTop
public void setFrustumTop(float frustumTop) Sets the value of the top frustum plane.- Parameters:
frustumTop
- the value of the top frustum plane.
-
getFov
public float getFov()Obtains field of view when the camera is in perspective mode.- Returns:
- Frame of view angle along the Y in degrees, or 0 if the camera is in orthogonal mode.
-
setFov
public void setFov(float fovY) Sets the field of view when the camera is in perspective mode. Note that this method has no effect when the camera is in orthogonal mode.- Parameters:
fovY
- Frame of view angle along the Y in degrees. This must be greater than 0.
-
getAspect
public float getAspect()Obtains the aspect ratio.- Returns:
- Width:Height ratio.
-
getLocation
Retrieves the location vector of the camera.- Returns:
- the position of the camera.
- See Also:
-
getRotation
Retrieves the rotation quaternion of the camera.- Returns:
- the rotation of the camera.
-
getDirection
Retrieves the direction vector the camera is facing.- Returns:
- the direction the camera is facing.
- See Also:
-
getLeft
Retrieves the left axis of the camera.- Returns:
- the left axis of the camera.
- See Also:
-
getUp
Retrieves the up axis of the camera.- Returns:
- the up axis of the camera.
- See Also:
-
getDirection
Retrieves the direction vector the camera is facing.- Parameters:
store
- storage for the result (modified if not null)- Returns:
- the direction the camera is facing.
- See Also:
-
getLeft
Retrieves the left axis of the camera.- Parameters:
store
- storage for the result (modified if not null)- Returns:
- the left axis of the camera.
- See Also:
-
getUp
Retrieves the up axis of the camera.- Parameters:
store
- storage for the result (modified if not null)- Returns:
- the up axis of the camera.
- See Also:
-
setLocation
Sets the position of the camera.- Parameters:
location
- the position of the camera.
-
setRotation
Sets the orientation of this camera. This will be equivalent to setting each of the axes:
cam.setLeft(rotation.getRotationColumn(0));
cam.setUp(rotation.getRotationColumn(1));
cam.setDirection(rotation.getRotationColumn(2));
- Parameters:
rotation
- the rotation of this camera
-
lookAtDirection
Sets the direction the camera is facing given a direction and an up vector.- Parameters:
direction
- the direction this camera is facing.up
- the desired "up" direction for the camera (not null, unaffected, typically (0,1,0))
-
setAxes
Sets the axes (left, up and direction) for this camera.- Parameters:
left
- the left axis of the camera.up
- the up axis of the camera.direction
- the direction the camera is facing.- See Also:
-
setAxes
Uses a rotational matrix to set the axes of the camera.- Parameters:
axes
- the matrix that defines the orientation of the camera.
-
normalize
public void normalize()Normalizes the camera vectors. -
setFrustum
public void setFrustum(float near, float far, float left, float right, float top, float bottom) Sets the frustum of this camera object.- Parameters:
near
- the near plane.far
- the far plane.left
- the left plane.right
- the right plane.top
- the top plane.bottom
- the bottom plane.- See Also:
-
setFrustumPerspective
public void setFrustumPerspective(float fovY, float aspect, float near, float far) Defines the frustum for the camera. This frustum is defined by a viewing angle, aspect ratio, and near/far planes- Parameters:
fovY
- Frame of view angle along the Y in degrees.aspect
- Width:Height rationear
- Near view plane distancefar
- Far view plane distance
-
setFrame
Sets the orientation and location of the camera.- Parameters:
location
- the point position of the camera.left
- the left axis of the camera.up
- the up axis of the camera.direction
- the facing of the camera.- See Also:
-
lookAt
A convenience method for auto-setting the frame based on a world position the user desires the camera to look at. It repoints the camera towards the given position using the difference between the position and the current camera location as a direction vector and the worldUpVector to compute up and left camera vectors.- Parameters:
pos
- where to look at in terms of world coordinatesworldUpVector
- a normalized vector indicating the up direction of the world. (typically {0, 1, 0} in jME.)
-
setFrame
Sets the orientation and location of the camera.- Parameters:
location
- the point position of the camera.axes
- the orientation of the camera.
-
update
public void update()Updates the camera parameters by callingonFrustumChange
,onViewPortChange
andonFrameChange
.- See Also:
-
getPlaneState
public int getPlaneState()Returns the state of the frustum planes. So checks can be made as to which frustum plane has been examined for culling thus far.- Returns:
- the current plane state int.
-
setPlaneState
public void setPlaneState(int planeState) Sets the state to keep track of tested planes for culling.- Parameters:
planeState
- the updated state.
-
getViewPortLeft
public float getViewPortLeft()Gets the left boundary of the viewport.- Returns:
- the left boundary of the viewport
-
setViewPortLeft
public void setViewPortLeft(float left) Sets the left boundary of the viewport.- Parameters:
left
- the left boundary of the viewport
-
getViewPortRight
public float getViewPortRight()Gets the right boundary of the viewport.- Returns:
- the right boundary of the viewport
-
setViewPortRight
public void setViewPortRight(float right) Sets the right boundary of the viewport.- Parameters:
right
- the right boundary of the viewport
-
getViewPortTop
public float getViewPortTop()Gets the top boundary of the viewport.- Returns:
- the top boundary of the viewport
-
setViewPortTop
public void setViewPortTop(float top) Sets the top boundary of the viewport.- Parameters:
top
- the top boundary of the viewport
-
getViewPortBottom
public float getViewPortBottom()Gets the bottom boundary of the viewport.- Returns:
- the bottom boundary of the viewport
-
setViewPortBottom
public void setViewPortBottom(float bottom) Sets the bottom boundary of the viewport.- Parameters:
bottom
- the bottom boundary of the viewport
-
setViewPort
public void setViewPort(float left, float right, float bottom, float top) Sets the boundaries of the viewport.- Parameters:
left
- the left boundary of the viewport (default: 0)right
- the right boundary of the viewport (default: 1)bottom
- the bottom boundary of the viewport (default: 0)top
- the top boundary of the viewport (default: 1)
-
distanceToNearPlane
Returns the pseudo distance from the given position to the near plane of the camera. This is used for render queue sorting.- Parameters:
pos
- The position to compute a distance to.- Returns:
- Distance from the near plane to the point.
-
contains
Tests a bounding volume against the planes of the camera's frustum. The frustum's planes are set such that the normals all face in towards the viewable scene. Therefore, if the bounding volume is on the negative side of the plane is can be culled out. NOTE: This method is used internally for culling, for public usage, the plane state of the camera must be saved and restored, e.g:BoundingVolume bv;
Camera c;
int planeState = c.getPlaneState();
c.setPlaneState(0);
c.contains(bv);
c.setPlaneState(plateState);
- Parameters:
bound
- the bound to check for culling- Returns:
- See enums in
FrustumIntersect
-
getWorldPlane
Provides access to one of the planes used for culling.- Parameters:
planeId
- the index of the Plane to access (0→left, 1→right, 2→bottom, 3→top, 4→far, 5→near)- Returns:
- the pre-existing instance
-
containsGui
Tests a bounding volume against the ortho bounding box of the camera. A bounding box spanning from 0, 0 to Width, Height. Constrained by the viewport settings on the camera.- Parameters:
bound
- the bound to check for culling- Returns:
- True if the camera contains the gui element bounding volume.
-
getViewMatrix
Provides access to the view matrix.- Returns:
- the view matrix of the camera.
The view matrix transforms world space into eye space. This matrix is usually defined by the position and orientation of the camera.
-
setProjectionMatrix
Overrides the projection matrix used by the camera. Will use the matrix for computing the view projection matrix as well. Use null argument to return to normal functionality.- Parameters:
projMatrix
- the desired projection matrix (unaffected) or null to cease the override
-
getProjectionMatrix
Provides access to the projection matrix.- Returns:
- the projection matrix of the camera.
The view projection matrix transforms eye space into clip space. This matrix is usually defined by the viewport and perspective settings of the camera.
-
updateViewProjection
public void updateViewProjection()Updates the view projection matrix. -
getViewProjectionMatrix
Provides access to the view projection matrix.- Returns:
- The result of multiplying the projection matrix by the view matrix. This matrix is required for rendering an object. It is precomputed to avoid computing it every time an object is rendered.
-
isViewportChanged
public boolean isViewportChanged()Tests whether the viewport (width, height, left, right, bottom, up) has been changed. This is needed in the renderer so that the proper viewport can be set-up.- Returns:
- true if changed, otherwise false
-
clearViewportChanged
public void clearViewportChanged()Clears the viewport changed flag once it has been updated inside the renderer. -
onViewPortChange
public void onViewPortChange()Called when the viewport has been changed. -
onFrustumChange
public void onFrustumChange()Updates the frustum to reflect any changes made to the planes. The new frustum values are kept in a temporary location for use when calculating the new frame. The projection matrix is updated to reflect the current values of the frustum. -
onFrameChange
public void onFrameChange()Updates the view frame of the camera. -
isParallelProjection
public boolean isParallelProjection()Determines whether the projection is parallel or perspective.- Returns:
- true if parallel projection is enabled, false if in normal perspective mode
- See Also:
-
setParallelProjection
public void setParallelProjection(boolean value) Enables/disables parallel projection.- Parameters:
value
- true to set up this camera for parallel projection, false to enter perspective mode
-
getViewToProjectionZ
public float getViewToProjectionZ(float viewZPos) Computes the z value in projection space from the z value in view space Note that the returned value goes non-linearly from 0 to 1. For more explanation of non-linear z buffer see http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html- Parameters:
viewZPos
- the z value in view space.- Returns:
- the z value in projection space.
-
getWorldCoordinates
Computes a position in World space given a screen position in screen space (0,0 to width, height) and a z position in projection space ( 0 to 1 non-linear). This former value is also known as the Z buffer value or non-linear depth buffer. For more explanation of non-linear Z buffer see http://www.sjbaker.org/steve/omniv/love_your_z_buffer.htmlTo compute the projection space z from the view space z (distance from cam to object)
- Parameters:
screenPos
- 2d coordinate in screen spaceprojectionZPos
- non linear z value in projection space- Returns:
- the position in world space.
- See Also:
-
getWorldCoordinates
Converts the given position from screen space to world space.- Parameters:
screenPosition
- a (2-D) location in screen space (not null)projectionZPos
- a (non-linear) Z value in projection spacestore
- storage for the result (modified if not null)- Returns:
- a location vector (in world coordinates, either
store
or a new vector) - See Also:
-
getScreenCoordinates
Converts the given position from world space to screen space.- Parameters:
worldPos
- a location in world coordinates (not null, unaffected)- Returns:
- a new (3-D) location vector (in screen coordinates)
- See Also:
-
getScreenCoordinates
Converts the given position from world space to screen space.- Parameters:
worldPosition
- a location in world coordinates (not null, unaffected)store
- storage for the result (modified if not null)- Returns:
- a (3-D) location vector (in screen coordinates, either
store
or a new vector) - See Also:
-
getWidth
public int getWidth()Returns the display width.- Returns:
- the width/resolution of the display.
-
getHeight
public int getHeight()Returns the display height.- Returns:
- the height/resolution of the display.
-
toString
-
write
- Specified by:
write
in interfaceSavable
- Throws:
IOException
-
read
- Specified by:
read
in interfaceSavable
- Throws:
IOException
-