public class Camera extends java.lang.Object implements Savable, java.lang.Cloneable
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
Modifier and Type | Class and Description |
---|---|
static class |
Camera.FrustumIntersect
The result of a culling check operation.
|
Modifier and Type | Field and Description |
---|---|
protected float[] |
coeffBottom
Temporary values computed in onFrustumChange that are needed if a call is made to onFrameChange.
|
protected float[] |
coeffLeft
Temporary values computed in onFrustumChange that are needed if a call is made to onFrameChange.
|
protected float[] |
coeffRight
Temporary values computed in onFrustumChange that are needed if a call is made to onFrameChange.
|
protected float[] |
coeffTop
Temporary values computed in onFrustumChange that are needed if a call is made to onFrameChange.
|
protected float |
frustumBottom
Distance from camera to bottom frustum plane.
|
protected float |
frustumFar
Distance from camera to far frustum plane.
|
protected float |
frustumLeft
Distance from camera to left frustum plane.
|
protected float |
frustumNear
Distance from camera to near frustum plane.
|
protected float |
frustumRight
Distance from camera to right frustum plane.
|
protected float |
frustumTop
Distance from camera to top frustum plane.
|
protected int |
height
The height of the viewport in pixels.
|
protected Vector3f |
location
Camera's location.
|
protected java.lang.String |
name
The camera's name.
|
protected Matrix4f |
projectionMatrix
Transforms eye space into clip space, unless overridden by projectionMatrixOverride.
|
protected Matrix4f |
projectionMatrixOverride
Temporarily overrides the projection matrix.
|
protected Quaternion |
rotation
The orientation of the camera.
|
protected Matrix4f |
viewMatrix
Transforms world space into eye space.
|
protected float |
viewPortBottom
Percent value on display where vertical viewing begins for this camera.
|
protected boolean |
viewportChanged
True if the renderer needs to update its viewport boundaries.
|
protected float |
viewPortLeft
Percent value on display where horizontal viewing starts for this camera.
|
protected float |
viewPortRight
Percent value on display where horizontal viewing ends for this camera.
|
protected float |
viewPortTop
Percent value on display where vertical viewing ends for this camera.
|
protected Matrix4f |
viewProjectionMatrix
Transforms world space into clip space.
|
protected int |
width
The width of the viewport in pixels.
|
protected Plane[] |
worldPlane
Array holding the planes that this camera will check for culling.
|
Modifier | Constructor and Description |
---|---|
protected |
Camera()
Serialization only.
|
|
Camera(int width,
int height)
Instantiates a new
Camera object. |
Modifier and Type | Method and Description |
---|---|
void |
clearViewportChanged()
Clears the viewport changed flag once it has been updated inside
the renderer.
|
Camera |
clone() |
Camera.FrustumIntersect |
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 |
copyFrom(Camera cam)
Copies the settings of the given camera.
|
float |
distanceToNearPlane(Vector3f pos)
Returns the pseudo distance from the given position to the near
plane of the camera.
|
float |
getAspect()
Obtains the aspect ratio.
|
Vector3f |
getDirection()
Retrieves the direction vector the camera is
facing.
|
Vector3f |
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 |
getFrustumBottom()
Returns the value of the bottom frustum
plane.
|
float |
getFrustumFar()
Gets the value of the far frustum plane.
|
float |
getFrustumLeft()
Gets the value of the left frustum plane.
|
float |
getFrustumNear()
Gets the value of the near frustum plane.
|
float |
getFrustumRight()
Gets the value of the right frustum plane.
|
float |
getFrustumTop()
Gets the value of the top frustum plane.
|
int |
getHeight()
Returns the display height.
|
Vector3f |
getLeft()
Retrieves the left axis of the camera.
|
Vector3f |
getLeft(Vector3f store)
Retrieves the left axis of the camera.
|
Vector3f |
getLocation()
Retrieves the location vector of the camera.
|
java.lang.String |
getName()
Returns the camera's name.
|
int |
getPlaneState()
Returns the state of the frustum planes.
|
Matrix4f |
getProjectionMatrix()
Provides access to the projection matrix.
|
Quaternion |
getRotation()
Retrieves the rotation quaternion of the camera.
|
Vector3f |
getScreenCoordinates(Vector3f worldPos)
Converts the given position from world space to screen space.
|
Vector3f |
getScreenCoordinates(Vector3f worldPosition,
Vector3f store)
Converts the given position from world space to screen space.
|
Vector3f |
getUp()
Retrieves the up axis of the camera.
|
Vector3f |
getUp(Vector3f store)
Retrieves the up axis of the camera.
|
Matrix4f |
getViewMatrix()
Provides access to the view matrix.
|
float |
getViewPortBottom()
Gets the bottom boundary of the viewport.
|
float |
getViewPortLeft()
Gets the left boundary of the viewport.
|
float |
getViewPortRight()
Gets the right boundary of the viewport.
|
float |
getViewPortTop()
Gets the top boundary of the viewport.
|
Matrix4f |
getViewProjectionMatrix()
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.
|
Vector3f |
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).
|
Vector3f |
getWorldCoordinates(Vector2f screenPosition,
float projectionZPos,
Vector3f store)
Converts the given position from screen space to world space.
|
Plane |
getWorldPlane(int planeId)
Provides access to one of the planes used for culling.
|
boolean |
isParallelProjection()
Determines whether the projection is parallel or perspective.
|
boolean |
isViewportChanged()
Tests whether the viewport (width, height, left, right, bottom, up)
has been changed.
|
void |
lookAt(Vector3f pos,
Vector3f worldUpVector)
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 |
normalize()
Normalizes the camera vectors.
|
void |
onFrameChange()
Updates the view frame of the camera.
|
void |
onFrustumChange()
Updates the frustum to reflect any changes
made to the planes.
|
void |
onViewPortChange()
Called when the viewport has been changed.
|
void |
read(JmeImporter e) |
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 |
setAxes(Vector3f left,
Vector3f up,
Vector3f direction)
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 |
setFrame(Vector3f location,
Vector3f left,
Vector3f up,
Vector3f direction)
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 |
setName(java.lang.String name)
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.
|
java.lang.String |
toString() |
void |
update()
Updates the camera parameters by calling
onFrustumChange ,onViewPortChange and
onFrameChange . |
void |
updateViewProjection()
Updates the view projection matrix.
|
void |
write(JmeExporter e) |
protected Vector3f location
protected Quaternion rotation
protected float frustumNear
protected float frustumFar
protected float frustumLeft
protected float frustumRight
protected float frustumTop
protected float frustumBottom
protected float[] coeffLeft
protected float[] coeffRight
protected float[] coeffBottom
protected float[] coeffTop
protected float viewPortLeft
protected float viewPortRight
protected float viewPortTop
protected float viewPortBottom
protected Plane[] worldPlane
protected int width
protected int height
protected boolean viewportChanged
protected Matrix4f projectionMatrixOverride
protected Matrix4f viewMatrix
protected Matrix4f projectionMatrix
protected Matrix4f viewProjectionMatrix
protected java.lang.String name
protected Camera()
public Camera(int width, int height)
Camera
object. All
values of the camera are set to default.width
- the desired width (in pixels)height
- the desired height (in pixels)public Camera clone()
clone
in class java.lang.Object
public void copyFrom(Camera cam)
cam
- the camera we copy the settings frompublic void setName(java.lang.String name)
name
- the camera's namepublic java.lang.String getName()
public void setClipPlane(Plane clipPlane, Plane.Side side)
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
clipPlane
- the planeside
- the side the camera stands from the planepublic void setClipPlane(Plane clipPlane)
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
clipPlane
- the planepublic void resize(int width, int height, boolean fixAspect)
RenderManager
to notify the camera of changes to the
display dimensions.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 ratiopublic float getFrustumBottom()
public void setFrustumBottom(float frustumBottom)
frustumBottom
- the value of the bottom frustum plane.public float getFrustumFar()
public void setFrustumFar(float frustumFar)
frustumFar
- the value of the far frustum plane.public float getFrustumLeft()
public void setFrustumLeft(float frustumLeft)
frustumLeft
- the value of the left frustum plane.public float getFrustumNear()
public void setFrustumNear(float frustumNear)
frustumNear
- the value of the near frustum plane.public float getFrustumRight()
public void setFrustumRight(float frustumRight)
frustumRight
- the value of the right frustum plane.public float getFrustumTop()
public void setFrustumTop(float frustumTop)
frustumTop
- the value of the top frustum plane.public float getFov()
public void setFov(float fovY)
fovY
- Frame of view angle along the Y in degrees. This must be greater than 0.public float getAspect()
public Vector3f getLocation()
getLocation()
public Quaternion getRotation()
public Vector3f getDirection()
getDirection()
public Vector3f getLeft()
getLeft()
public Vector3f getUp()
getUp()
public Vector3f getDirection(Vector3f store)
store
- storage for the result (modified if not null)getDirection()
public Vector3f getLeft(Vector3f store)
store
- storage for the result (modified if not null)getLeft()
public Vector3f getUp(Vector3f store)
store
- storage for the result (modified if not null)getUp()
public void setLocation(Vector3f location)
location
- the position of the camera.public void setRotation(Quaternion rotation)
cam.setLeft(rotation.getRotationColumn(0));
cam.setUp(rotation.getRotationColumn(1));
cam.setDirection(rotation.getRotationColumn(2));
rotation
- the rotation of this camerapublic void lookAtDirection(Vector3f direction, Vector3f up)
direction
- the direction this camera is facing.up
- the desired "up" direction for the camera (not null,
unaffected, typically (0,1,0))public void setAxes(Vector3f left, Vector3f up, Vector3f direction)
left
- the left axis of the camera.up
- the up axis of the camera.direction
- the direction the camera is facing.setAxes(com.jme3.math.Quaternion)
public void setAxes(Quaternion axes)
axes
- the matrix that defines the orientation of the camera.public void normalize()
public void setFrustum(float near, float far, float left, float right, float top, float bottom)
near
- the near plane.far
- the far plane.left
- the left plane.right
- the right plane.top
- the top plane.bottom
- the bottom plane.setFrustum(float, float, float, float,
float, float)
public void setFrustumPerspective(float fovY, float aspect, float near, float far)
fovY
- Frame of view angle along the Y in degrees.aspect
- Width:Height rationear
- Near view plane distancefar
- Far view plane distancepublic void setFrame(Vector3f location, Vector3f left, Vector3f up, Vector3f direction)
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.setFrame(com.jme3.math.Vector3f,
com.jme3.math.Vector3f, com.jme3.math.Vector3f, com.jme3.math.Vector3f)
public void lookAt(Vector3f pos, Vector3f worldUpVector)
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.)public void setFrame(Vector3f location, Quaternion axes)
location
- the point position of the camera.axes
- the orientation of the camera.public void update()
onFrustumChange
,onViewPortChange
and
onFrameChange
.update()
public int getPlaneState()
public void setPlaneState(int planeState)
planeState
- the updated state.public float getViewPortLeft()
public void setViewPortLeft(float left)
left
- the left boundary of the viewportpublic float getViewPortRight()
public void setViewPortRight(float right)
right
- the right boundary of the viewportpublic float getViewPortTop()
public void setViewPortTop(float top)
top
- the top boundary of the viewportpublic float getViewPortBottom()
public void setViewPortBottom(float bottom)
bottom
- the bottom boundary of the viewportpublic void setViewPort(float left, float right, float bottom, float top)
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)public float distanceToNearPlane(Vector3f pos)
pos
- The position to compute a distance to.public Camera.FrustumIntersect contains(BoundingVolume bound)
BoundingVolume bv;
Camera c;
int planeState = c.getPlaneState();
c.setPlaneState(0);
c.contains(bv);
c.setPlaneState(plateState);
bound
- the bound to check for cullingFrustumIntersect
public Plane getWorldPlane(int planeId)
planeId
- the index of the Plane to access (0→left, 1→right, 2→bottom, 3→top,
4→far, 5→near)public boolean containsGui(BoundingVolume bound)
bound
- the bound to check for cullingpublic Matrix4f getViewMatrix()
The view matrix transforms world space into eye space. This matrix is usually defined by the position and orientation of the camera.
public void setProjectionMatrix(Matrix4f projMatrix)
projMatrix
- the desired projection matrix (unaffected) or null
to cease the overridepublic Matrix4f getProjectionMatrix()
The view projection matrix transforms eye space into clip space. This matrix is usually defined by the viewport and perspective settings of the camera.
public void updateViewProjection()
public Matrix4f getViewProjectionMatrix()
public boolean isViewportChanged()
public void clearViewportChanged()
public void onViewPortChange()
public void onFrustumChange()
public void onFrameChange()
public boolean isParallelProjection()
setParallelProjection(boolean)
public void setParallelProjection(boolean value)
value
- true to set up this camera for parallel projection,
false to enter perspective modepublic float getViewToProjectionZ(float viewZPos)
viewZPos
- the z value in view space.public Vector3f getWorldCoordinates(Vector2f screenPos, float projectionZPos)
To compute the projection space z from the view space z (distance from cam to object)
screenPos
- 2d coordinate in screen spaceprojectionZPos
- non linear z value in projection spacegetViewToProjectionZ(float)
public Vector3f getWorldCoordinates(Vector2f screenPosition, float projectionZPos, Vector3f store)
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)store
or a new vector)getWorldCoordinates(com.jme3.math.Vector2f, float)
public Vector3f getScreenCoordinates(Vector3f worldPos)
worldPos
- a location in world coordinates (not null, unaffected)getScreenCoordinates(com.jme3.math.Vector3f)
public Vector3f getScreenCoordinates(Vector3f worldPosition, Vector3f store)
worldPosition
- a location in world coordinates (not null, unaffected)store
- storage for the result (modified if not null)store
or a new vector)getScreenCoordinates(Vector3f, Vector3f)
public int getWidth()
public int getHeight()
public java.lang.String toString()
toString
in class java.lang.Object
public void write(JmeExporter e) throws java.io.IOException
public void read(JmeImporter e) throws java.io.IOException