Tuesday, July 26, 2022

Implementation: Perspective and Orthographic Camera

Overview
In the previous post, we understood how view matrix, perspective matrix and orthographic matrix. Two new cameras are used to provide perspective and orthographic views.

Details
This post discusses camera  and camera data that are used in projections and animations.
The Camera data holds transformation information such as pitch, yaw and roll angles, translation, scaleby as well as Model, View and Projection matrix information of the 3D object. 

Camera and CameraData
There are many specialized camera classes that are associated with different types of camera data.
The class  diagram below shows their association, The light blue classes are camera data and the others are camera classes. 
The details will be provided in the next posts.
ViewMatrixData
ViewMatrixData class contains Model to World transformation information such as camera position, object position in the world, and the World up vector. They are used by DualProjectionCamera class for calling lookAt function to generate view matrix.
Members
NameDescription
Position
Camera position in world Coordinates.
TargetPosition of the object in world Coordinates.
UpWorlds Up vector.
VContains view Matrix

Methods
NameDescription
getViewMatrixReturns latest View matrix.
setViewMatrixStores inputs such as positions, Target and up. Computes view matrix by internally calling lookAt function. Stores the result in V

PerspectiveProjectionMatrixData
PerspectiveProjectionMatrixData class contains perspective view frustum information such as near plane, far plane and fov angle. Also the aspect ratio. They are used by by DualProjectionCamera class to redraw.
Members
NameDescription
AspectRatioAn aspect ratio is the proportional relationship between the width and height of a shape, image, or screen, expressed as width:height (e.g., 16:9). It defines an object's proportions rather than its physical size, ensuring visuals scale cleanly without distortion.
FOVField of view (FOV) is the angular extent of the observable world that can be seen or captured at any given moment. It dictates how wide or narrow your view is.
NearPlaneA near plane (often called the clipping or projection plane) is the imaginary flat boundary closest to the camera in a 3D perspective view. It acts as a "window" that captures the scene. Everything closer to the camera than this plane is invisible to prevent visual clipping and rendering errors.
FarPlaneA far plane (or far clipping plane) is a boundary defining the maximum distance a virtual camera can "see". Any object or geometry beyond this plane is clipped, meaning it is excluded from the rendered image
PContains latest Perspective Projection Matrix.

Methods
NameDescription
getProjectionMatrixreturns latest Perspective Projection Matrix stored in P
setProjectionMatrixStores inputs such as near plane and far plane and computes Perspective Projection Matrix. Stores the result in P
setFOVStores FOV angle after clipping the input is lower or higher than tolerance.  Later computes Perspective Projection Matrix. Stores the result in P
setAspectRatioIf the input is a float, aspect ratio is stored from the input parameter.
If the input is an integer, the current FOV is doubled and sign is applied. typically used for zooming in or out.
Later computes Perspective Projection Matrix. Stores the result in P

OrthographicProjectionMatrixData
OrthographicProjectionMatrixData class contains orthographic view frustum information such as xmin, xmax, ymin, ymax, zmin, zmax. They are used by by DualProjectionCamera class to redraw.
Members
NameDescription
XMinMaxxmin and xmax define the left and right clipping planes (boundaries) of the 3D viewing volume in an orthographic projection.
YMinMaxymin and ymax define the left and right clipping planes (boundaries) of the 3D viewing volume in an orthographic projection.
ZMinMaxzmin and zmax define the nearest and farthest clipping planes (boundaries) of the 3D viewing volume in an orthographic projection.
PContains latest projection matrix.

Methods
NameDescription
getProjectionMatrixreturns latest Orthographic Projection Matrix stored in P
setProjectionMatrixStores inputs such as X,Y, Z min and max distance and computes Orthographic Projection Matrix. Stores the result in P

DualProjectionCamera
DualProjectionCamera class is derived from ThreeDCamera class. It process mouse and keyboard inputs and calculates zoom factor, view matrix, perspective and orthographic projection matrices. They are updated into ViewMatrixData, PerspectiveProjectionMatrixData, OrthographicProjectionMatrixData object.


Constructor
Initializes hwnd with the Window handle of the hosting window. Internally calls ThreeDCamera constructor.

Members
Name  Description
VMcontains 3D transformation information such as camera position, object position in the world, and the World up vector. . They are updated by based on mouse and keyboard inputs and are shared with 3D objects based on BaseGeometry class  to redraw.
PPM          contains perspective view frustum information such as near plane, far plane and fov angle. Also the aspect ratio. They are updated by based on mouse and keyboard inputs and are shared with 3D objects based on BaseGeometry class  to redraw Perspective projection.
OPMcontains orthographic view frustum information such as xmin, xmax, ymin, ymax, zmin, zmax.  They are updated by based on mouse and keyboard inputs and are shared with 3D objects based on BaseGeometry class  to redraw Orthographic projection.

Methods
NameDescription
CopyToClipboardCopies Unproject() information to clipboard.
OnKeyThis method handles keyboard input events as below:
P  => Captures x,y,z coordinates at current mouse position  in the world space  and copies to clipboard.
PageUp  => doubles current FOV resulting in Zooms Out effect or items on the screen becomes smaller.  This affects only Perspective projection.
PageUp  => current FOV is halved resulting in Zooms in effect or items on the screen becomes bigger.  This affects only Perspective projection.
OnMouseWheelThis simulates ZoomOut when scroll wheel is moved up and ZoomIn when scroll wheel is moved down.
setOrthographicProjectionMatrixApplies latest Orthographic Projection Matrix stored in OPM to the geometry object.
setPerspectiveProjectionMatrixApplies latest Perspective Projection Matrix stored in PPM to the geometry object.
setViewMatrixApplies latest View Matrix stored in VM to the geometry object.
UnprojectInternally called by keyboard handler to capture  x,y,z coordinates at the mouse cursor
UpdateWHCalls BaseCamera's method to reset window sizes and sets aspect ratio.





No comments:

Post a Comment