Saturday, April 4, 2026

Implementation: BaseCamera and Camera Data

Overview
A scene consists of  a set of  3D objects. Transformations such as translation, scaling and rotation brings them into life. The source for these transformations are mouse and keyboard inputs, which are further computed  resulting in camera placements, 
For example  the following diagram shows a multi color cube with  50 degrees pitch and 20 degrees yaw. The cube looks elongated horizontally because aspect ratio is not applied. 



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. 

Coordinate System
OpenGL uses Right hand side coordinate system where +Z is pointing towards you.
Pitch, Yaw and Roll
A 3D object can rotate along all the three axes as shown below. 
Rotation along X Axis is called Pitch. Rotation along Y Axis is called Yaw. Rotation along Z Axis is called Roll

The following diagram shows how the positive rotation happens in Counter clockwise direction.
        Pitch(X Axis)                                    Yaw (Y Axis)                                  Roll (Z Axis)

Mouse and Keyboard input support
As discussed earlier, the BaseScene object captures all the Mouse and Keyboard activities such as key press, mouse button press, move etc. Later they are processed by BaseCamera class.
For example in BaseCamera class,  if a key is pressed, its scan code is stored. Similarly if mouse left button is pressed and the mouse is dragged, delta x and delta y are computed. 

The derived camera classes from BaseCamera use this information to further compute and  store additional information  in Camera Data structures.

For Example, ThreeDCamera class implements keyboard and mouse handling as below:
X key press  => Add pitch by 10 degrees.
Y key press  => Add yaw by  10 degrees.
Z key press  => Add roll by 10 degrees.
Mouse Drag using left button => Compute Pitch and Yaw angles from  Delta X, and Delta Y.

Later this is stored in ModelMatrixData class and shared with 3D objects in the scene to draw.


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.

BaseCamera
BaseCamera implements handlers for mouse and keyboard events. This is a base class that can be further specialized. It's located in VedaOGLLib\Scene\BaseCamera.h

Constructor
Initializes hwnd with the Window handle of the hosting window.

Members
NameDescription
windowrect
clientrect
Upon events like resize, windowrect and clientrect are updated.
windowrect stores window rectangle of the hosting window after calling GetWindowRect() method.
clientrect stores client rectangle of the hosting window after calling GetClientRect() method.
mouseX,mouseY
deltaX,deltaY
Upon mouse move event, the current X and Y coordinates are stored in mouseX and mouseY. Delta of X and Y coordinates are stored in deltaX and deltaY.
mouseLeftDown
mouseMiddleDown
mouseRightDown
Upon mouse button down event, one of these  is set to true based on the button pressed. Rest are set to false.

Methods
NameDescription
CenterCursor
This method places the cursor at the center of the screen.
OnKeyThis method handles keyboard input events.
OnMouseBtnDown
This method handle mouse button click events.
OnMouseMoveThis method handle mouse move events.
OnMouseWheelThis method handles Mouse scroll wheel inputs. 
UpdateWHThis method handles window resize events.

ModelMatrixData
As discussed earlier, ModelMatrixData class contains 3D information. It's updated by by ThreeDCamera class based on mouse and keyboard inputs and is shared with 3D objects based on BaseGeometry class  to redraw.


Members
NameDescription
pitch
yaw
roll
Computed values based on mouse and keyboard inputs. These are stored as degrees.
ScaleBy
TranslateBy
Affine transformation values for scaling and Translation, These are stored 3x3 matrices.

Methods
NameDescription
augment
Augments itself from another ModelMatrixData object.
getModelMatrixReturns self as 4x4 matrix. 
Reset
Resets itself to scratch.