@@ -8,8 +8,8 @@ namespace sauce {
88struct CameraCreateInfo {
99 float scrWidth;
1010 float scrHeight;
11- glm::vec3 pos = { 0 , 0 , 1 };
12- glm::vec3 worldUp = { 0 , 1 , 0 };
11+ glm::vec3 pos = { 2 . 0f , 2 . 0f , 2 . 0f };
12+ glm::vec3 worldUp = { 0 . 0f , 0 . 0f , 1 . 0f };
1313 float yaw = YAW_DEFAULT ;
1414 float pitch = PITCH_DEFAULT ;
1515 float fov = FOV_DEFAULT ;
@@ -44,55 +44,59 @@ class Camera {
4444 movementSpeed(createInfo.movementSpeed), mouseSensitivity(createInfo.mouseSensitivity),
4545 scrWidth(createInfo.scrWidth), scrHeight(createInfo.scrHeight)
4646 {
47- updateView ();
47+ updateView ();
4848 }
4949
50- /* *
51- * Sets camera position
52- *
53- * @param pos - position to move the camera to
54- */
55- void setPos (glm::vec3 pos) {
56- this ->pos =pos;
57- updateView ();
58- }
59-
60- /* *
61- * Translates the camera position by offset
62- *
63- * @param offs - offset by which to translate the camera
64- */
65- void translate (glm::vec3 offs) {
66- translate (offs.x , offs.y , offs.z );
67- }
68-
69- /* *
70- * Translates the camera position by (x, y, z)
71- */
72- void translate (float x, float y, float z) {
50+ /* *
51+ * Sets camera position
52+ *
53+ * @param pos - position to move the camera to
54+ */
55+ void setPos (glm::vec3 pos) {
56+ this ->pos =pos;
57+ updateView ();
58+ }
59+
60+ glm::vec3 getPos () const {
61+ return this ->pos ;
62+ }
63+
64+ /* *
65+ * Translates the camera position by offset
66+ *
67+ * @param offs - offset by which to translate the camera
68+ */
69+ void translate (glm::vec3 offs) {
70+ translate (offs.x , offs.y , offs.z );
71+ }
72+
73+ /* *
74+ * Translates the camera position by (x, y, z)
75+ */
76+ void translate (float x, float y, float z) {
7377 this ->pos .x +=x;
7478 this ->pos .y +=y;
7579 this ->pos .z +=z;
7680 updateView ();
77- }
78-
79- /* *
80- * Get current FOV
81- */
82- float getFOV () const { return fov; }
83-
84- /* *
85- * Set camera FOV
86- */
87- void setFOV (float fov) { this ->fov =fov; }
88-
89- /* *
90- * Get view matrix from the current view vectors
91- *
92- * @return the view matrix for this camera
93- */
94- glm::mat4 getViewMatrix () const {
95- return glm::lookAt (this ->pos , this ->front , this ->up );
81+ }
82+
83+ /* *
84+ * Get current FOV
85+ */
86+ float getFOV () const { return fov; }
87+
88+ /* *
89+ * Set camera FOV
90+ */
91+ void setFOV (float fov) { this ->fov =fov; }
92+
93+ /* *
94+ * Get view matrix from the current view vectors
95+ *
96+ * @return the view matrix for this camera
97+ */
98+ glm::mat4 getViewMatrix () const {
99+ return glm::lookAt (this ->pos , this ->pos + this -> front , this ->worldUp );
96100 }
97101
98102 /* *
@@ -101,7 +105,7 @@ class Camera {
101105 * @return projection matrix for this camera
102106 */
103107 glm::mat4 getProjectionMatrix () const {
104- return glm::perspective (this ->fov , scrWidth/scrHeight, 0 .1f , 100 .f );
108+ return glm::perspective (glm::radians ( this ->fov ) , scrWidth/scrHeight, 0 .1f , 100 .f );
105109 }
106110
107111 /* *
@@ -140,44 +144,48 @@ class Camera {
140144 * @param constrainPitch - whether or not to clamp pitch when out of bounds
141145 */
142146 void processMouseMovement (float xoffset, float yoffset, bool constrainPitch = true ) {
143- xoffset=glm::radians (xoffset);
144- yoffset=glm::radians (yoffset);
147+ yaw += xoffset;
148+ pitch += yoffset;
149+
145150 if (constrainPitch) {
146- yoffset=(yoffset>CameraCreateInfo:: PITCH_MAX )? CameraCreateInfo:: PITCH_MAX : ((yoffset< CameraCreateInfo::PITCH_MIN )? CameraCreateInfo::PITCH_MIN : yoffset );
151+ pitch = std::max ( std::min (pitch, CameraCreateInfo::PITCH_MAX ), CameraCreateInfo::PITCH_MIN );
147152 }
148153
149- yaw+=glm::radians (xoffset);
150- pitch+=glm::radians (yoffset);
154+ updateView ();
151155 }
152156
153157private:
154- glm::vec3 pos;
158+ glm::vec3 pos;
155159
156- /* view orientation vectors */
157- glm::vec3 front, up, right;
160+ /* view orientation vectors */
161+ glm::vec3 front, up, right;
158162
159- /* Used to get the right vector from front vector */
160- glm::vec3 worldUp;
163+ /* Used to get the right vector from front vector */
164+ glm::vec3 worldUp;
161165
162- float movementSpeed, mouseSensitivity;
166+ float movementSpeed, mouseSensitivity;
163167
164- // euler Angles
165- float yaw, pitch;
168+ // euler Angles
169+ float yaw, pitch;
166170
167- float fov;
168- float scrWidth, scrHeight;
171+ float fov;
172+ float scrWidth, scrHeight;
169173
170- /* *
171- * Sets the front, right, and up vectors using the current values for yaw and pitch.
172- * This should be called any time the camera position or angle is changed.
173- */
174- void updateView () {
175- front.x =cos (pitch)*cos (yaw);
176- front.y =sin (pitch);
177- front.z =cos (pitch)*sin (yaw);
178- right=glm::normalize (glm::cross (worldUp, front));
179- up=glm::normalize (glm::cross (front, right));
180- }
174+ /* *
175+ * Sets the front, right, and up vectors using the current values for yaw and pitch.
176+ * This should be called any time the camera position or angle is changed.
177+ */
178+ void updateView () {
179+ float pitchRad = glm::radians (pitch);
180+ float yawRad = glm::radians (yaw);
181+
182+ front.x = cos (pitchRad) * sin (yawRad);
183+ front.y = cos (pitchRad) * cos (yawRad);
184+ front.z = sin (pitchRad);
185+
186+ right=glm::normalize (glm::cross (front, worldUp));
187+ up=glm::normalize (glm::cross (right, front));
188+ }
181189};
182190
183191}
0 commit comments