RunicVTT Open Source Virtual Tabletop for TTRPG using P2P
Loading...
Searching...
No Matches
Camera Class Reference

#include <BoardManager.h>

Collaboration diagram for Camera:

Public Member Functions

 Camera ()
 
void pan (glm::vec2 delta)
 
void zoom (float factor, glm::vec2 mouse_world_pos_before_zoom)
 
void setPosition (glm::vec2 newPosition)
 
glm::vec2 getPosition () const
 
void setZoom (float newZoomLevel)
 
float getZoom () const
 
glm::mat4 getViewMatrix () const
 
glm::mat4 getProjectionMatrix () const
 
glm::vec2 worldToScreenPosition (glm::vec2 world_position) const
 
glm::vec2 fboToNdcPos (glm::vec2 fbo_pixel_top_left_origin) const
 
glm::vec2 screenToWorldPosition (glm::vec2 fbo_pixel_top_left_origin) const
 
void setFboDimensions (glm::vec2 dims)
 

Private Attributes

glm::vec2 position
 
float zoom_level
 
glm::vec2 fbo_dimensions
 

Detailed Description

Definition at line 32 of file BoardManager.h.

Constructor & Destructor Documentation

◆ Camera()

Camera::Camera ( )
inline

Definition at line 35 of file BoardManager.h.

35 :
36 position(0.0f, 0.0f), zoom_level(1.0f), fbo_dimensions(0, 0) {} // Set initial zoom to 1.0f for no scaling by default
float zoom_level
glm::vec2 position
glm::vec2 fbo_dimensions

Member Function Documentation

◆ fboToNdcPos()

glm::vec2 Camera::fboToNdcPos ( glm::vec2 fbo_pixel_top_left_origin) const
inline

Definition at line 115 of file BoardManager.h.

116 {
117 glm::vec2 ndc;
118 ndc.x = (2.0f * fbo_pixel_top_left_origin.x) / fbo_dimensions.x - 1.0f;
119 ndc.y = 1.0f - (2.0f * fbo_pixel_top_left_origin.y) / fbo_dimensions.y;
120 return ndc;
121 }
Here is the caller graph for this function:

◆ getPosition()

glm::vec2 Camera::getPosition ( ) const
inline

Definition at line 61 of file BoardManager.h.

62 {
63 return position;
64 }

◆ getProjectionMatrix()

glm::mat4 Camera::getProjectionMatrix ( ) const
inline

Definition at line 82 of file BoardManager.h.

83 {
84 float half_width = (fbo_dimensions.x / 2.0f) / zoom_level;
85 float half_height = (fbo_dimensions.y / 2.0f) / zoom_level;
86 return glm::ortho(-half_width, half_width, half_height, -half_height, -1.0f, 1.0f);
87 }
Here is the caller graph for this function:

◆ getViewMatrix()

glm::mat4 Camera::getViewMatrix ( ) const
inline

Definition at line 77 of file BoardManager.h.

78 {
79 return glm::translate(glm::mat4(1.0f), glm::vec3(-position.x, -position.y, 0.0f));
80 }
Here is the caller graph for this function:

◆ getZoom()

float Camera::getZoom ( ) const
inline

Definition at line 72 of file BoardManager.h.

73 {
74 return zoom_level;
75 }
Here is the caller graph for this function:

◆ pan()

void Camera::pan ( glm::vec2 delta)
inline

Definition at line 38 of file BoardManager.h.

39 {
40 position += delta;
41 }
Here is the caller graph for this function:

◆ screenToWorldPosition()

glm::vec2 Camera::screenToWorldPosition ( glm::vec2 fbo_pixel_top_left_origin) const
inline

Definition at line 123 of file BoardManager.h.

124 {
125
126 glm::vec2 ndc = fboToNdcPos(fbo_pixel_top_left_origin);
127 glm::vec4 ndc_homogeneous = glm::vec4(ndc.x, ndc.y, 0.0f, 1.0f);
128 glm::mat4 inverse_pv_matrix = glm::inverse(getProjectionMatrix() * getViewMatrix());
129 glm::vec4 world_homogeneous = inverse_pv_matrix * ndc_homogeneous;
130 glm::vec2 world_position;
131 if (world_homogeneous.w != 0.0f)
132 {
133 world_position.x = world_homogeneous.x / world_homogeneous.w;
134 world_position.y = world_homogeneous.y / world_homogeneous.w;
135 }
136 else
137 {
138 return glm::vec2(NAN, NAN); // Indicate invalid result
139 }
140
141 return world_position;
142 }
glm::mat4 getViewMatrix() const
glm::mat4 getProjectionMatrix() const
glm::vec2 fboToNdcPos(glm::vec2 fbo_pixel_top_left_origin) const
Here is the call graph for this function:

◆ setFboDimensions()

void Camera::setFboDimensions ( glm::vec2 dims)
inline

Definition at line 144 of file BoardManager.h.

145 {
146 fbo_dimensions = dims;
147 }

◆ setPosition()

void Camera::setPosition ( glm::vec2 newPosition)
inline

Definition at line 56 of file BoardManager.h.

57 {
58 position = newPosition;
59 }
Here is the caller graph for this function:

◆ setZoom()

void Camera::setZoom ( float newZoomLevel)
inline

Definition at line 66 of file BoardManager.h.

67 {
68 zoom_level = newZoomLevel;
69 zoom_level = glm::clamp(zoom_level, 0.1f, 10.0f);
70 }
Here is the caller graph for this function:

◆ worldToScreenPosition()

glm::vec2 Camera::worldToScreenPosition ( glm::vec2 world_position) const
inline

Definition at line 89 of file BoardManager.h.

90 {
91 glm::vec4 world_homogeneous = glm::vec4(world_position.x, world_position.y, 0.0f, 1.0f);
92 /*glm::vec4 camera_coords = getViewMatrix() * world_homogeneous;
93 glm::vec4 clip_coords = getProjectionMatrix() * camera_coords;*/
94
95 glm::mat4 pv_matrix = getProjectionMatrix() * getViewMatrix(); // Combine PV matrix
96 glm::vec4 clip_coords = pv_matrix * world_homogeneous;
97
98 glm::vec2 ndc;
99 if (clip_coords.w != 0.0f)
100 { // Avoid division by zero
101 ndc.x = clip_coords.x / clip_coords.w;
102 ndc.y = clip_coords.y / clip_coords.w;
103 }
104 else
105 {
106 return glm::vec2(NAN, NAN); // Indicate invalid position
107 }
108 glm::vec2 fbo_pixel_top_left_origin;
109 fbo_pixel_top_left_origin.x = (ndc.x + 1.0f) * 0.5f * fbo_dimensions.x;
110 fbo_pixel_top_left_origin.y = (1.0f - ndc.y) * 0.5f * fbo_dimensions.y;
111
112 return fbo_pixel_top_left_origin;
113 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ zoom()

void Camera::zoom ( float factor,
glm::vec2 mouse_world_pos_before_zoom )
inline

Definition at line 43 of file BoardManager.h.

44 {
45 float old_zoom_level = zoom_level;
46 zoom_level *= factor;
47 zoom_level = glm::clamp(zoom_level, 0.05f, 20.0f); // Example limits
48 if (zoom_level == old_zoom_level)
49 {
50 return;
51 }
52 float zoom_ratio = zoom_level / old_zoom_level;
53 position = mouse_world_pos_before_zoom - (mouse_world_pos_before_zoom - position) / zoom_ratio;
54 }

Member Data Documentation

◆ fbo_dimensions

glm::vec2 Camera::fbo_dimensions
private

Definition at line 152 of file BoardManager.h.

◆ position

glm::vec2 Camera::position
private

Definition at line 150 of file BoardManager.h.

◆ zoom_level

float Camera::zoom_level
private

Definition at line 151 of file BoardManager.h.


The documentation for this class was generated from the following file: