Class Camera
Represents a camera in a graphics context for rendering.
Inherited Members
Namespace: Genesis.Graphics
Assembly: Genesis.dll
Syntax
public class Camera
Constructors
Camera(Viewport, float, float)
Initializes a new instance of the Camera class with the specified location, size, near and far distances.
Declaration
public Camera(Viewport viewport, float near, float far)
Parameters
Type | Name | Description |
---|---|---|
Viewport | viewport | The viewport for the location and resoulution. |
float | near | The distance to the near clipping plane of the camera. |
float | far | The distance to the far clipping plane of the camera. |
Camera(Vec3, Vec3, float, float)
Initializes a new instance of the Camera class with the specified location, size, near and far distances.
Declaration
public Camera(Vec3 location, Vec3 size, float near, float far)
Parameters
Type | Name | Description |
---|---|---|
Vec3 | location | The initial location of the camera in 3D space. |
Vec3 | size | The size of the camera. |
float | near | The distance to the near clipping plane of the camera. |
float | far | The distance to the far clipping plane of the camera. |
Properties
FOV
Gets or sets the field of view (FOV) for the camera in degrees.
Declaration
public float FOV { get; set; }
Property Value
Type | Description |
---|---|
float |
Far
Gets or sets the distance to the far clipping plane of the camera.
Declaration
public float Far { get; set; }
Property Value
Type | Description |
---|---|
float |
Location
Gets or sets the location of the camera in 3D space.
Declaration
public Vec3 Location { get; set; }
Property Value
Type | Description |
---|---|
Vec3 |
Near
Gets or sets the distance to the near clipping plane of the camera.
Declaration
public float Near { get; set; }
Property Value
Type | Description |
---|---|
float |
Rotation
Gets or sets the rotation of the camera in 3D space.
Declaration
public Vec3 Rotation { get; set; }
Property Value
Type | Description |
---|---|
Vec3 |
Size
Gets or sets the size of the camera.
Declaration
public Vec3 Size { get; set; }
Property Value
Type | Description |
---|---|
Vec3 |
Type
Gets or sets the type of the camera, either orthographic or perspective.
Declaration
public CameraType Type { get; set; }
Property Value
Type | Description |
---|---|
CameraType |
Methods
CalculateScreenCorrection(Viewport)
Calculates the screen correction factor using the dimensions of a viewport.
Declaration
public float CalculateScreenCorrection(Viewport viewport)
Parameters
Type | Name | Description |
---|---|---|
Viewport | viewport | The viewport containing the screen dimensions. |
Returns
Type | Description |
---|---|
float | The correction factor to be applied to the screen size, based on the smaller ratio of viewport width to desired width (Size.X) and viewport height to desired height (Size.Y). |
CalculateScreenCorrection(float, float)
Calculates the screen correction factor.
Declaration
public float CalculateScreenCorrection(float screenWidth, float screenHeight)
Parameters
Type | Name | Description |
---|---|---|
float | screenWidth | The width of the screen. |
float | screenHeight | The height of the screen. |
Returns
Type | Description |
---|---|
float | The correction factor to be applied to the screen size, based on the smaller ratio of screen width to desired width (Size.X) and screen height to desired height (Size.Y). |
ConvertScreenToWorldOrtho(Viewport, float, float)
Converts screen coordinates to world coordinates using an orthographic projection.
Declaration
public Vec3 ConvertScreenToWorldOrtho(Viewport viewport, float x, float y)
Parameters
Type | Name | Description |
---|---|---|
Viewport | viewport | The viewport object that defines the dimensions of the screen space. |
float | x | The x-coordinate of the screen position to convert. |
float | y | The y-coordinate of the screen position to convert. |
Returns
Type | Description |
---|---|
Vec3 | A Vec3 object representing the corresponding world coordinates. |
Remarks
This method assumes an orthographic projection and calculates the world coordinates based on the screen position, the camera's position, and the size of the viewport. The screen coordinates are first normalized to a range of [-1, 1], then adjusted according to the camera's position and size.
GetProjectionMatrix(Camera, Viewport)
Calculates the projection matrix for the specified camera based on the viewport and field of view.
Declaration
public static mat4 GetProjectionMatrix(Camera camera, Viewport viewport)
Parameters
Type | Name | Description |
---|---|---|
Camera | camera | The camera for which to calculate the projection matrix. |
Viewport | viewport | The viewport used to calculate the projection matrix. |
Returns
Type | Description |
---|---|
mat4 | A GlmSharp.mat4 representing the projection matrix. |
GetProjectionMatrix(Viewport)
Calculates the projection matrix for this camera based on the specified viewport.
Declaration
public mat4 GetProjectionMatrix(Viewport viewport)
Parameters
Type | Name | Description |
---|---|---|
Viewport | viewport | The viewport used to calculate the projection matrix. |
Returns
Type | Description |
---|---|
mat4 | A GlmSharp.mat4 representing the projection matrix. |
GetRect()
Gets the rectangular region represented by the camera's location and size.
Declaration
public Rect GetRect()
Returns
Type | Description |
---|---|
Rect | A Rect object representing the camera's view region. |
GetViewMatrix()
Calculates the view matrix for this camera.
Declaration
public mat4 GetViewMatrix()
Returns
Type | Description |
---|---|
mat4 | A GlmSharp.mat4 representing the view matrix. |
GetViewMatrix(Camera)
Calculates the view matrix for the specified camera.
Declaration
public static mat4 GetViewMatrix(Camera camera)
Parameters
Type | Name | Description |
---|---|---|
Camera | camera | The camera for which to calculate the view matrix. |
Returns
Type | Description |
---|---|
mat4 | A GlmSharp.mat4 representing the view matrix. |
LookAt(GameElement)
Adjusts the camera to look at the specified game element (only 2D).
Declaration
public void LookAt(GameElement element)
Parameters
Type | Name | Description |
---|---|---|
GameElement | element | The game element to look at. |
LookAt(GameElement, bool)
Adjusts the camera to look at the specified game element, with an option to center the view on the element.
Declaration
public void LookAt(GameElement element, bool centerOffset)
Parameters
Type | Name | Description |
---|---|---|
GameElement | element | The game element to look at. |
bool | centerOffset | If true, centers the view on the element; otherwise, positions the camera at the element's location. |
ProjectMouse2D(Camera, Viewport, int, int)
Projects the mouse coords into screen coords
Declaration
public static Vec3 ProjectMouse2D(Camera camera, Viewport viewport, int mouseX, int mouseY)
Parameters
Type | Name | Description |
---|---|---|
Camera | camera | The camera |
Viewport | viewport | The viewport |
int | mouseX | the screen x coordinate |
int | mouseY | the screen y coordinate |
Returns
Type | Description |
---|---|
Vec3 |
ScreenToWorldDirection3D(PerspectiveCamera, Viewport, float, float)
Projects the mouse coordinates into 3D world space, returning the direction vector.
Declaration
public static Vec3 ScreenToWorldDirection3D(PerspectiveCamera camera, Viewport viewport, float sX, float sY)
Parameters
Type | Name | Description |
---|---|---|
PerspectiveCamera | camera | The camera used for the projection. |
Viewport | viewport | The viewport for the camera's dimensions. |
float | sX | The x-coordinate of the mouse in screen space. |
float | sY | The y-coordinate of the mouse in screen space. |
Returns
Type | Description |
---|---|
Vec3 | A Vec3 representing the direction of the ray in 3D space. |
ScreenToWorldPosition3D(PerspectiveCamera, Viewport, float, float)
Converts screen space coordinates (2D) into world space coordinates (3D).
Declaration
public static Vec3 ScreenToWorldPosition3D(PerspectiveCamera camera, Viewport viewport, float sX, float sY)
Parameters
Type | Name | Description |
---|---|---|
PerspectiveCamera | camera | The perspective camera used for projection and view matrix calculations. |
Viewport | viewport | The viewport that defines the screen's width and height. |
float | sX | The x-coordinate of the mouse position in screen space (pixels). |
float | sY | The y-coordinate of the mouse position in screen space (pixels). |
Returns
Type | Description |
---|---|
Vec3 | A Vec3 representing the corresponding world space coordinates. This is the position in the 3D world corresponding to the input screen coordinates, taking into account the perspective projection and camera view matrix. |
Remarks
The method first normalizes the screen coordinates to the range [-1, 1], then applies the inverse of the combined projection and view matrix to map the 2D screen coordinates back into 3D world coordinates. The result is a position in the world space, representing the direction of a ray starting from the camera's near plane and passing through the specified screen coordinates.