# Unigine::Camera Class

Header: | #include <UnigineCamera.h> |

The **Camera** class is used to create a new camera, set it up (set all the required matrices, field of view, masks and so on) and then pass it to an instance of the Viewport class to render an image from this camera.

**not a node**.

A camera instance can have the following masks:

- Sound Source mask
- Sound Reverberation mask
- Viewport mask
- Reflection Viewport mask

Camera settings can be set up in the Camera Settings in UnigineEditor.

### Usage Example*#*

In this example we create a Camera instance and PlayerSpectator player, specify camera parameters, set camera to the PlayerSpectator and set it as current game player.

In the AppWorldLogic.h header file include the UniginePlayers.h header and declare a PlayerSpectator and Camera smart pointers.

```
#include <UnigineLogic.h>
#include <UnigineStreams.h>
#include <UniginePlayers.h>
class AppWorldLogic : public Unigine::WorldLogic {
public:
/* public methods */
private:
Unigine::PlayerSpectatorPtr playerSpectator;
Unigine::CameraPtr camera;
};
```

In the AppWorldLogic.cpp implementation file do the following:

- Include the UnigineGame.h header to set a new player by using
*setPlayer()*method. - Use
**using namespace Unigine**directive: names of the Unigine namespace will be injected into global namespace. - Specify the necessary parameters of the created
*Camera*instance and set it to*PlayerSpectator*. - Set PlayerSpectator as a current game player.
- Clear the pointer to the PlayerSpectator to avoid memory leaks.

Here are the necessary parts of code:

```
#include "AppWorldLogic.h"
#include "UnigineGame.h"
// inject Unigine namespace names to global namespace
using namespace Unigine;
/* ... */
int AppWorldLogic::init() {
// create a new PlayerSpectator and camera instance
playerSpectator = PlayerSpectator::create();
camera = Camera::create();
// specify the necessary camera parameters: FOV, ZNear, ZFar.
// add the post_sensor_red post-effect to camera
camera->setFov(110.0f);
camera->setZNear(0.1f);
camera->setZFar(10000.0f);
camera->setPostMaterials("post_sensor_red");
// set camera to the player
playerSpectator->setCamera(camera);
// specify the position and view direction of playerSpectator
playerSpectator->setViewDirection(Math::vec3(0.0f, 1.0f, 0.0f));
playerSpectator->setWorldPosition(Math::dvec3(-1.6f, -1.7f, 1.7f));
// set the Player to the Game singleton instance
Game::setPlayer(playerSpectator);
return 1;
}
/* ... */
int AppWorldLogic::shutdown() {
// clear the pointer to Player
playerSpectator.clear();
return 1;
}
```

## Camera Class

### Members

## static CameraPtr
create
(
)
*#*

Constructor. Creates a new camera with default settings:
- Modelview, inverse modelview and offset matrices are 4×4 identity matrices.
- FOV is 60 degrees.
- Distance to the near clipping plane is 0.1 unit.
- Distance to the far clipping plane is 10000 units.
- Up direction vector is (0,0,1).
- Viewport, reflection, source and reverb masks are set to 00000001.

## void getDirectionFromScreen
(
Math::Vec3 & p0, Math::Vec3 & p1, float screen_x, float screen_y, float aspect
)
*#* const

Casts the ray from a certain position on the screen.### Arguments

*Math::Vec3 &***p0**- Start coordinate of the ray.*Math::Vec3 &***p1**- End coordinate of the ray.*float***screen_x**- X-coordinate of screen, in the [0;1] range, where 0 is the left upper point, 1 is the right lower point.*float***screen_y**- Y-coordinate of screen, in the [0;1] range, where 0 is the left upper point, 1 is the right lower point.*float***aspect**- Screen's aspect ratio (height to width).

## Math::vec3 getDirectionFromScreen
(
float screen_x, float screen_y, float aspect
)
*#* const

Casts the ray from a certain position on the screen.### Arguments

*float***screen_x**- X-coordinate of screen, in the [0;1] range, where 0 is the left upper point, 1 is the right lower point.*float***screen_y**- Y-coordinate of screen, in the [0;1] range, where 0 is the left upper point, 1 is the right lower point.*float***aspect**- Screen's aspect ratio (height to width).

### Return value

Point coordinate.## void setFov
(
float fov
)
*#*

Sets a vertical field of view of the camera and updates the projection matrix.

You can use the following formula to calculate horizontal FOV from the vertical one for the given aspect ratio (width/height): **FOV_h = 2 × atan ( (width / height) × tan(FOV_v / 2))**.

### Arguments

*float***fov**- New vertical field of view in degrees. The provided value will be saturated in the range [0;180].

## float getFov
(
)
*#* const

Returns the current vertical field of view of the camera.

You can use the following formula to calculate horizontal FOV from the vertical one for the given aspect ratio (width/height): **FOV_h = 2 × atan ( (width / height) × tan(FOV_v / 2))**.

### Return value

Vertical field of view in degrees.## Math::Mat4 getIModelview
(
)
*#* const

Returns the inverted view matrix of the camera.### Return value

Inverse view matrix.## void setModelview
(
const Math::Mat4 & modelview
)
*#*

Updates a modelview and inverse modelview matrices for the camera.### Arguments

*const Math::Mat4 &***modelview**- New view matrix.

## Math::Mat4 getModelview
(
)
*#* const

Returns the current view matrix of the camera.### Return value

Current view matrix.## void setObliqueFrustum
(
bool frustum
)
*#*

Enables or disables obliqueness of the viewing frustum.
### Arguments

*bool***frustum**-**1**to enable oblique viewing frustum;**0**to disable it.

## bool isObliqueFrustum
(
)
*#* const

Returns a value indicating if the viewing frustum is oblique.### Return value

**1**if the viewing frustum is oblique; otherwise,

**0**.

## void setObliqueFrustumPlane
(
const Math::Vec4 & plane
)
*#*

Sets the oblique near clipping plane of the viewing frustum.
```
// AppWorldLogic.cpp
#include <UnigineGame.h>
using namespace Unigine;
/* .. */
int AppWorldLogic::update() {
// Write here code to be called before updating each render frame: specify all graphics-related functions you want to be called every frame while your application executes.
float time = Game::getTime();
// initializing a plane to be set as a near clipping plane
Math::Vec4 plane = Math::Vec4(1.0f, 1.0f, 1.0f, 1.0f + Math::sin(time) * 4.0f);
// getting a camera
CameraPtr camera = Game::getPlayer()->getCamera();
if (camera)
{
// enabling oblique frustum
camera->setObliqueFrustum(1);
// setting our plane as an oblique near clipping plane
camera->setObliqueFrustumPlane(plane);
}
return 1;
}
/* .. */
```

### Arguments

*const Math::Vec4 &***plane**- World coordinates of the oblique near clipping plane to set (Nx, Ny, Nz, D), where Nx, Ny, Nz - coordinates of the plane normal, D - distance from the origin to the plane.

## Math::Vec4 getObliqueFrustumPlane
(
)
*#* const

Returns the oblique near clipping plane of the viewing frustum.### Return value

World coordinates of the oblique near clipping plane to set (Nx, Ny, Nz, D), where Nx, Ny, Nz - coordinates of the plane normal, D - distance from the origin to the plane.## void setOffset
(
const Math::mat4 & offset
)
*#*

Sets an additional transformation (an offset matrix) for the camera. This transformation is applied after the modelview transformation. The offset matrix does not affect the view matrix or the position of the camera. For example, the offset matrix can be used to simulate camera shaking from an explosion.### Arguments

*const Math::mat4 &***offset**- Offset matrix.

## Math::mat4 getOffset
(
)
*#* const

Returns an additional transformation (an offset matrix) set for the camera. This transformation is applied after modelview transformation. The offset matrix does not affect the view matrix or the position of the camera. For example, it can be used to simulate camera shake from an explosion.### Return value

Offset matrix.## void setPosition
(
const Math::Vec3 & position
)
*#*

Sets a new position of the camera and updates the modelview and inverse modelview (its 3rd column) matrices.### Arguments

*const Math::Vec3 &***position**- Camera position in the world space.

## Math::Vec3 getPosition
(
)
*#* const

Returns the current position of the camera. The position vector is stored in the 3rd column of the inverse view matrix.### Return value

Camera position in the world space.## void setPostMaterials
(
const char * materials
)
*#*

Sets post postprocess materials that are applied after all other postprocess are rendered. They are used after *engine.render.setPostMaterials()*, if any.

### Arguments

*const char ****materials**- Comma-separated names of postprocess materials.

## const char * getPostMaterials
(
)
*#* const

Returns names of the current post postprocess materials that are applied after all other postprocess are rendered. They are used after *engine.render.getPostMaterials()*, if any.

### Return value

Name of the current post postprocess materials.## void setProjection
(
const Math::mat4 & projection
)
*#*

Updates the current projection matrix.
This method allows you to set your camera to use perspective or orthographic projection, depending on your project's requirements.

For example, you can use the following code to set up **orthographic projection** or **perspective projection** for your camera depending on a flag value:

```
// AppWorldLogic.cpp
/* ... */
#include <UnigineCamera.h>
#include <UnigineGame.h>
// inject Unigine namespace names to global namespace
using namespace Unigine;
/* ... */
// ortho flag - change this value to switch projection type
int ortho = 0;
int AppWorldLogic::init() {
// getting the camera of the current player
CameraPtr camera = Game::getPlayer()->getCamera();
// setting up near and far clipping planes and aspect ratio
float znear = 0.001f;
float zfar = 10000.0f;
float aspect = 16.0f / 9.0f;
if (ortho)
{
// setting up orthographic projection
camera->setProjection(Math::ortho(-1.0f, 1.0f, -1.0f, 1.0f, znear, zfar));
}
else
{
// setting up perspective projection
camera->setProjection(Math::perspective(60.0f, aspect, znear, zfar));
}
// setting player's camera
Game::getPlayer()->setCamera(camera);
return 1;
}
/* ... */
```

### Arguments

*const Math::mat4 &***projection**- Projection matrix.

## Math::mat4 getProjection
(
)
*#* const

Returns the current projection matrix with unit (1.0) aspect ratio.### Return value

Projection matrix.## Math::mat4 getProjectionFromScreen
(
float screen_x0, float screen_y0, float screen_x1, float screen_y1, float aspect
)
*#* const

Creates a projection matrix out of 2 screen positions. This is required for the frame selection.### Arguments

*float***screen_x0**- X-coordinate of the first screen position, in the [0;1] range, where 0 is the left upper point, 1 is the right lower point.*float***screen_y0**- Y-coordinate of the first screen position, in the [0;1] range, where 0 is the left upper point, 1 is the right lower point.*float***screen_x1**- X-coordinate of the second screen position, in the [0;1] range, where 0 is the left upper point, 1 is the right lower point.*float***screen_y1**- Y-coordinate of the second screen position, in the [0;1] range, where 0 is the left upper point, 1 is the right lower point.*float***aspect**- Screen's aspect ratio (height to width).

### Return value

Projection matrix.## void setReflectionViewportMask
(
int mask
)
*#*

Sets a bit mask for rendering reflections into the viewport. Reflections are rendered in the viewport if masks of reflective materials match this one (one bit at least).### Arguments

*int***mask**- Integer, each bit of which is used to set a mask.

## int getReflectionViewportMask
(
)
*#* const

Returns the current bit mask for rendering reflections into the camera viewport. Reflections are rendered in the camera viewport if masks of reflective materials match this one (one bit at least).### Return value

Integer, each bit of which is used to set a mask.## void setReverbMask
(
int mask
)
*#*

Updates a bit mask that determines what reverberation zones can be heard. For sound to reverberate, at least one bit of this mask should match a reverb mask of the sound source and a reverb mask of the reverberation zone. Masks of a sound source and reverberation zone can match the camera's one in several bits, not necessarily one.### Arguments

*int***mask**- Integer, each bit of which is a mask for reverberating sound sources and reverberation zones.

## int getReverbMask
(
)
*#* const

Returns the current bit mask that determines what reverberation zones can be heard. For sound to reverberate, at least one bit of this mask should match a reverb mask of the sound source and a reverb mask of the reverberation zone. Masks of a sound source and reverberation zone can match with the camera's one in several bits, not necessarily one.### Return value

Integer, each bit of which is used to set a mask for reverberating sound sources and reverberation zones.## int getScreenPosition
(
float & screen_x, float & screen_y, const Math::Vec3 & point, float aspect
)
*#* const

Projects the point in world coordinates to the screen. Screen coordinates are written into the first 2 variables passed to the method.### Arguments

*float &***screen_x**- X-coordinate of the screen position.*float &***screen_y**- Y-coordinate of the screen position.*const Math::Vec3 &***point**- Point coordinates.*float***aspect**- Aspect ratio (screen height to width).

### Return value

**1**if the point has been projected successfully; otherwise,

**0**.

## void setSourceMask
(
int mask
)
*#*

Updates a bit mask that determines what sound sources can be heard. For a sound source to be heard, its mask should match this one in at least one bit. Plus, the volume of sound channel in which the sound plays (its number also depends on this mask) should not be equal to 0.### Arguments

*int***mask**- Integer, each bit of which specifies a sound channel.

## int getSourceMask
(
)
*#* const

Returns a bit mask that determines what sound channels can be heard. For a sound source to be heard, its mask should match this one in at least one bit. Plus, the volume of sound channel in which the sound plays (its number also depends on this mask) should not be equal to **0**.

### Return value

Integer, each bit of which specifies a sound channel.## void setUp
(
const Math::vec3 & up
)
*#*

Sets an up direction of the camera's viewport (i.e. tilt of the camera's viewport).### Arguments

*const Math::vec3 &***up**- New upward direction vector. The vector is normalized to 1.

## Math::vec3 getUp
(
)
*#* const

Returns the current up direction of the camera's viewport (i.e. tilt of the camera's viewport).### Return value

Upward direction vector.## void setViewportMask
(
int mask
)
*#*

Sets a bit mask for rendering into the viewport. Object surfaces, materials, decals, lights and GUI objects will be rendered into the viewport only if their viewport masks match the camera's one (one matching bit is enough).### Arguments

*int***mask**- Integer, each bit of which is used to set a mask.

## int getViewportMask
(
)
*#* const

Returns the current bit mask for rendering into the viewport. Object surfaces, materials, decals, lights and GUI objects will be rendered into the viewport only if their viewport mask matches the camera's one (one matching bit is enough).### Return value

Integer, each bit of which is used to set a mask.## void setZFar
(
float zfar
)
*#*

Sets a distance to the far clipping plane of the camera's viewing frustum and updates the projection matrix.### Arguments

*float***zfar**- New distance in units. If a negative value is provided, 0 will be used instead.

## float getZFar
(
)
*#* const

Returns the current distance to the far clipping plane of the camera's viewing frustum.### Return value

Distance in units.## void setZNear
(
float znear
)
*#*

Sets a distance to the near clipping plane of the camera's viewing frustum and updates the projection matrix.### Arguments

*float***znear**- New distance in units. If a negative value is provided, 0 will be used instead.

## float getZNear
(
)
*#* const

Returns the current distance to the near clipping plane of the camera's viewing frustum.### Return value

Distance in units.## Ptr<Camera> clone
(
)
*#* const

Clones the current camera and saves it to the given camera instance.### Return value

Copy of the camera.## void setFovMode
(
int mode
)
*#*

Sets the value indicating the type of FOV that is used for the camera:
- For the classic camera, the vertical FOV should be set. In this case, FOV is directly set in degrees.
- For the physically-based camera, the horizontal FOV should be set. In this case, FOV is calculated depending on the film gate and focal length of the camera.

### Arguments

*int***mode**-*FOV_MODE_VERTICAL*for the camera with the vertical FOV;*FOV_MODE_PHYSICALLY_BASED_CAMERA*for the physically-based camera with the horizontal FOV.

## int getFovMode
(
)
*#* const

Sets the value indicating the type of FOV that is used for the camera.### Return value

0 if the camera with the vertical FOV is used; 1 if the physically-based camera with the horizontal FOV is used.## void setFilmGate
(
float gate
)
*#*

Sets a film gate for the physically-based camera with horizontal FOV.### Arguments

*float***gate**- Film gate.

## float getFilmGate
(
)
*#* const

Returns a film gate for the physically-based camera with horizontal FOV.### Return value

Film gate.## void setFocalLength
(
float length
)
*#*

Sets a focal length of the physically-based camera lens.### Arguments

*float***length**- Camera lens focal length.

## float getFocalLength
(
)
*#* const

Returns the focal length of the physically-based camera lens.### Return value

Camera lens focal length.## Math::mat4 getAspectCorrectedProjection
(
float aspect
)
*#* const

Returns projection matrix after correction for the specified aspect ratio. Currently fixed FOV component is taken into account.### Arguments

*float***aspect**- Aspect ratio.

### Return value

Projection matrix after correction for the specified aspect ratio.## int getFovFixed
(
)
*#* const

Returns a value indicating which FOV component (horizontal or vertical) is currently fixed.### Return value

Current fixed FOV component, one of the FOV_FIXED_* values.## void addScriptableMaterial
(
const Ptr<Material> & material
)
*#*

Attaches a new scriptable material to the camera. To apply a scriptable material globally, use the *addScriptableMaterial()*method of the Render class. The order of execution for scripts assigned to scriptable materials is defined by material's number in the list of the camera.

### Arguments

*const Ptr<Material> &***material**- Scriptable material to be attached to the camera.

## void removeScriptableMaterial
(
int num
)
*#*

Removes the scriptable material with the specified number from the camera.### Arguments

*int***num**- Scriptable material number in the range from 0 to the total number of scriptable materials.

## int getNumScriptableMaterials
(
)
*#* const

Returns the total number of scriptable materials attached to the camera.### Return value

Total number of scriptable materials attached to the camera.## int findScriptableMaterial
(
const Ptr<Material> & material
)
*#* const

Returns the number of the specified scriptable material for the camera. This number is camera-specific (valid for this camera only) and determines the order in which the assigned expressions are executed.
### Arguments

*const Ptr<Material> &***material**- Scriptable material for which a number is to be found.

### Return value

Scriptable material number in the range from 0 to the total number of scriptable materials, or -1 if the specified material was not found.## void setScriptableMaterial
(
int num, const Ptr<Material> & material
)
*#*

Replaces the scriptable material with the specified number with the new scriptable material specified. The number of material determines the order in which the expressions assigned to it are executed. This number is camera-specific (valid for this camera only).
### Arguments

*int***num**- Scriptable material number in the range from 0 to the total number of scriptable materials.*const Ptr<Material> &***material**- New scriptable material to replace the one with the specified number.

## Ptr<Material> getScriptableMaterial
(
int num
)
*#* const

Returns a scriptable material attached to the camera by its number.### Arguments

*int***num**- Scriptable material number in the range from 0 to the total number of scriptable materials.

### Return value

Scriptable material attached to the camera with the specified number.## void setScriptableMaterialEnabled
(
int num, bool enabled
)
*#*

Enables or disables the scriptable material with the specified number. When a material is disabled (inactive), the scripts attached to it are not executed.### Arguments

*int***num**- Scriptable material number in the range from 0 to the total number of scriptable materials.*bool***enabled**- 1 to enable the scriptable material with the specified number, 0 to disable it.

## bool getScriptableMaterialEnabled
(
int num
)
*#* const

Returns a value indicating if the scriptable material with the specified number attached to the camera is enabled (active). When a material is disabled (inactive), the scripts attached to it are not executed.### Arguments

*int***num**- Scriptable material number in the range from 0 to the total number of scriptable materials.

### Return value

1 if the scriptable material with the specified number is enabled; otherwise, 0.## void swapScriptableMaterials
(
int num_0, int num_1
)
*#*

Swaps two scriptable materials with specified numbers. The number of material determines the order in which the expressions assigned to it are executed.
### Arguments

*int***num_0**- Number of the first scriptable material in the range from 0 to the total number of scriptable materials.*int***num_1**- Number of the second scriptable material in the range from 0 to the total number of scriptable materials.