public class Camera extends java.lang.Object implements Savable, java.lang.Cloneable
Camera
is a standalone, purely mathematical class for doing
camera-related computations.
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
FrustumIntersect enum is returned as a result
of a culling check operation,
see contains(com.jme3.bounding.BoundingVolume) |
Modifier and Type | Field and Description |
---|---|
protected float[] |
coeffBottom |
protected float[] |
coeffLeft |
protected float[] |
coeffRight |
protected float[] |
coeffTop |
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 |
protected Vector3f |
location
Camera's location
|
protected java.lang.String |
name
The camera's name.
|
protected Matrix4f |
projectionMatrix |
protected Matrix4f |
projectionMatrixOverride |
protected Quaternion |
rotation
The orientation of the camera.
|
protected Matrix4f |
viewMatrix |
protected float |
viewPortBottom
Percent value on display where vertical viewing begins for this camera.
|
protected boolean |
viewportChanged |
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 |
protected int |
width |
protected Plane[] |
worldPlane
Array holding the planes that this camera will check for culling.
|
Constructor and Description |
---|
Camera()
Serialization only.
|
Camera(int width,
int height)
Constructor 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)
contains tests a bounding volume against the planes of the
camera's frustum. |
boolean |
containsGui(BoundingVolume bound)
containsGui tests a bounding volume against the ortho
bounding box of the camera. |
void |
copyFrom(Camera cam)
This method 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.
|
Vector3f |
getDirection()
getDirection retrieves the direction vector the camera is
facing. |
Vector3f |
getDirection(Vector3f store)
getDirection retrieves the direction vector the camera is
facing. |
float |
getFrustumBottom()
getFrustumBottom returns the value of the bottom frustum
plane. |
float |
getFrustumFar()
getFrustumFar gets the value of the far frustum plane. |
float |
getFrustumLeft()
getFrustumLeft gets the value of the left frustum plane. |
float |
getFrustumNear()
getFrustumNear gets the value of the near frustum plane. |
float |
getFrustumRight()
getFrustumRight gets the value of the right frustum plane. |
float |
getFrustumTop()
getFrustumTop gets the value of the top frustum plane. |
int |
getHeight() |
Vector3f |
getLeft()
getLeft retrieves the left axis of the camera. |
Vector3f |
getLeft(Vector3f store)
getLeft retrieves the left axis of the camera. |
Vector3f |
getLocation()
getLocation retrieves the location vector of the camera. |
java.lang.String |
getName()
This method returns the cameras name.
|
int |
getPlaneState()
getPlaneState returns the state of the frustum planes. |
Matrix4f |
getProjectionMatrix() |
Quaternion |
getRotation()
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()
getUp retrieves the up axis of the camera. |
Vector3f |
getUp(Vector3f store)
getUp retrieves the up axis of the camera. |
Matrix4f |
getViewMatrix() |
float |
getViewPortBottom()
getViewPortBottom gets the bottom boundary of the viewport |
float |
getViewPortLeft()
getViewPortLeft gets the left boundary of the viewport |
float |
getViewPortRight()
getViewPortRight gets the right boundary of the viewport |
float |
getViewPortTop()
getViewPortTop gets the top boundary of the viewport |
Matrix4f |
getViewProjectionMatrix() |
float |
getViewToProjectionZ(float viewZPos)
Computes the z value in projection space from the z value in view space
Note that the returned value is going non linearly from 0 to 1.
|
int |
getWidth() |
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) |
Plane |
getWorldPlane(int planeId) |
boolean |
isParallelProjection() |
boolean |
isViewportChanged() |
void |
lookAt(Vector3f pos,
Vector3f worldUpVector)
lookAt is 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)
lookAtDirection sets the direction the camera is facing
given a direction and an up vector. |
void |
normalize()
normalize normalizes the camera vectors.
|
void |
onFrameChange()
onFrameChange updates the view frame of the camera. |
void |
onFrustumChange()
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)
Resize this camera's view for the specified display size.
|
void |
setAxes(Quaternion axes)
setAxes uses a rotational matrix to set the axes of the
camera. |
void |
setAxes(Vector3f left,
Vector3f up,
Vector3f direction)
setAxes 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 |
setFrame(Vector3f location,
Quaternion axes)
setFrame sets the orientation and location of the camera. |
void |
setFrame(Vector3f location,
Vector3f left,
Vector3f up,
Vector3f direction)
setFrame sets the orientation and location of the camera. |
void |
setFrustum(float near,
float far,
float left,
float right,
float top,
float bottom)
setFrustum sets the frustum of this camera object. |
void |
setFrustumBottom(float frustumBottom)
setFrustumBottom sets the value of the bottom frustum
plane. |
void |
setFrustumFar(float frustumFar)
setFrustumFar sets the value of the far frustum plane. |
void |
setFrustumLeft(float frustumLeft)
setFrustumLeft sets the value of the left frustum plane. |
void |
setFrustumNear(float frustumNear)
setFrustumNear sets the value of the near frustum plane. |
void |
setFrustumPerspective(float fovY,
float aspect,
float near,
float far)
setFrustumPerspective defines the frustum for the camera. |
void |
setFrustumRight(float frustumRight)
setFrustumRight sets the value of the right frustum plane. |
void |
setFrustumTop(float frustumTop)
setFrustumTop sets the value of the top frustum plane. |
void |
setLocation(Vector3f location)
setLocation sets the position of the camera. |
void |
setName(java.lang.String name)
This method sets the cameras name.
|
void |
setParallelProjection(boolean value)
Enable/disable parallel projection.
|
void |
setPlaneState(int planeState)
setPlaneState 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)
setRotation sets the orientation of this camera. |
void |
setViewPort(float left,
float right,
float bottom,
float top)
setViewPort sets the boundaries of the viewport |
void |
setViewPortBottom(float bottom)
setViewPortBottom sets the bottom boundary of the viewport |
void |
setViewPortLeft(float left)
setViewPortLeft sets the left boundary of the viewport |
void |
setViewPortRight(float right)
setViewPortRight sets the right boundary of the viewport |
void |
setViewPortTop(float top)
setViewPortTop sets the top boundary of the viewport |
java.lang.String |
toString() |
void |
update()
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
public Camera()
public Camera(int width, int height)
Camera
object. All
values of the camera are set to default.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 cameras namepublic java.lang.String getName()
public void setClipPlane(Plane clipPlane, Plane.Side side)
clipPlane
- the planeside
- the side the camera stands from the planepublic void setClipPlane(Plane clipPlane)
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()
getFrustumBottom
returns the value of the bottom frustum
plane.public void setFrustumBottom(float frustumBottom)
setFrustumBottom
sets the value of the bottom frustum
plane.frustumBottom
- the value of the bottom frustum plane.public float getFrustumFar()
getFrustumFar
gets the value of the far frustum plane.public void setFrustumFar(float frustumFar)
setFrustumFar
sets the value of the far frustum plane.frustumFar
- the value of the far frustum plane.public float getFrustumLeft()
getFrustumLeft
gets the value of the left frustum plane.public void setFrustumLeft(float frustumLeft)
setFrustumLeft
sets the value of the left frustum plane.frustumLeft
- the value of the left frustum plane.public float getFrustumNear()
getFrustumNear
gets the value of the near frustum plane.public void setFrustumNear(float frustumNear)
setFrustumNear
sets the value of the near frustum plane.frustumNear
- the value of the near frustum plane.public float getFrustumRight()
getFrustumRight
gets the value of the right frustum plane.public void setFrustumRight(float frustumRight)
setFrustumRight
sets the value of the right frustum plane.frustumRight
- the value of the right frustum plane.public float getFrustumTop()
getFrustumTop
gets the value of the top frustum plane.public void setFrustumTop(float frustumTop)
setFrustumTop
sets the value of the top frustum plane.frustumTop
- the value of the top frustum plane.public Vector3f getLocation()
getLocation
retrieves the location vector of the camera.getLocation()
public Quaternion getRotation()
getRotation
retrieves the rotation quaternion of the camera.public Vector3f getDirection()
getDirection
retrieves the direction vector the camera is
facing.getDirection()
public Vector3f getLeft()
getLeft
retrieves the left axis of the camera.getLeft()
public Vector3f getUp()
getUp
retrieves the up axis of the camera.getUp()
public Vector3f getDirection(Vector3f store)
getDirection
retrieves the direction vector the camera is
facing.getDirection()
public Vector3f getLeft(Vector3f store)
getLeft
retrieves the left axis of the camera.getLeft()
public Vector3f getUp(Vector3f store)
getUp
retrieves the up axis of the camera.getUp()
public void setLocation(Vector3f location)
setLocation
sets the position of the camera.location
- the position of the camera.public void setRotation(Quaternion rotation)
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));
rotation
- the rotation of this camerapublic void lookAtDirection(Vector3f direction, Vector3f up)
lookAtDirection
sets the direction the camera is facing
given a direction and an up vector.direction
- the direction this camera is facing.public void setAxes(Vector3f left, Vector3f up, Vector3f direction)
setAxes
sets the axes (left, up and direction) for this
camera.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)
setAxes
uses a rotational matrix to set the axes of the
camera.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)
setFrustum
sets the frustum of this camera object.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)
setFrustumPerspective
defines the frustum for the camera. This
frustum is defined by a viewing angle, aspect ratio, and near/far planesfovY
- 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)
setFrame
sets the orientation and location of the camera.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)
lookAt
is 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.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)
setFrame
sets the orientation and location of the camera.location
- the point position of the camera.axes
- the orientation of the camera.public void update()
update
updates the camera parameters by calling
onFrustumChange
,onViewPortChange
and
onFrameChange
.update()
public int getPlaneState()
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.public void setPlaneState(int planeState)
setPlaneState
sets the state to keep track of tested
planes for culling.planeState
- the updated state.public float getViewPortLeft()
getViewPortLeft
gets the left boundary of the viewportpublic void setViewPortLeft(float left)
setViewPortLeft
sets the left boundary of the viewportleft
- the left boundary of the viewportpublic float getViewPortRight()
getViewPortRight
gets the right boundary of the viewportpublic void setViewPortRight(float right)
setViewPortRight
sets the right boundary of the viewportright
- the right boundary of the viewportpublic float getViewPortTop()
getViewPortTop
gets the top boundary of the viewportpublic void setViewPortTop(float top)
setViewPortTop
sets the top boundary of the viewporttop
- the top boundary of the viewportpublic float getViewPortBottom()
getViewPortBottom
gets the bottom boundary of the viewportpublic void setViewPortBottom(float bottom)
setViewPortBottom
sets the bottom boundary of the viewportbottom
- the bottom boundary of the viewportpublic void setViewPort(float left, float right, float bottom, float top)
setViewPort
sets the boundaries of the viewportleft
- 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)
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 bounding volume must be saved and restored, e.g:
BoundingVolume bv;
Camera c;
int planeState = bv.getPlaneState();
bv.setPlaneState(0);
c.contains(bv);
bv.setPlaneState(plateState);
bound
- the bound to check for cullingFrustumIntersect
public Plane getWorldPlane(int planeId)
public boolean containsGui(BoundingVolume bound)
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.bound
- the bound to check for cullingpublic Matrix4f getViewMatrix()
public void setProjectionMatrix(Matrix4f projMatrix)
projMatrix
- public Matrix4f getProjectionMatrix()
public void updateViewProjection()
public Matrix4f getViewProjectionMatrix()
public boolean isViewportChanged()
public void clearViewportChanged()
public void onViewPortChange()
public void onFrustumChange()
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.public void onFrameChange()
onFrameChange
updates the view frame of the camera.public boolean isParallelProjection()
setParallelProjection(boolean)
public void setParallelProjection(boolean value)
value
- true to set up this camera for parallel projection is enable, false to enter normal perspective modepublic float getViewToProjectionZ(float viewZPos)
viewZPos
- the z value in view space.public Vector3f getWorldCoordinates(Vector2f screenPos, float projectionZPos)
screenPos
- 2d coordinate in screen spaceprojectionZPos
- non linear z value in projection spacepublic Vector3f getWorldCoordinates(Vector2f screenPosition, float projectionZPos, Vector3f store)
public Vector3f getScreenCoordinates(Vector3f worldPos)
public Vector3f getScreenCoordinates(Vector3f worldPosition, Vector3f store)
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