Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added `tdogl::Camera::lookAt` method

  • Loading branch information...
commit 490be8dc8f98789045e97cbeb217fe9e162d1fa3 1 parent 73364b6
@tomdalling authored
Showing with 576 additions and 96 deletions.
  1. +27 −6 ios/04_camera/source/tdogl/Camera.cpp
  2. +9 −0 ios/04_camera/source/tdogl/Camera.h
  3. +27 −6 ios/05_asset_instance/source/tdogl/Camera.cpp
  4. +9 −0 ios/05_asset_instance/source/tdogl/Camera.h
  5. +27 −6 ios/06_diffuse_lighting/source/tdogl/Camera.cpp
  6. +9 −0 ios/06_diffuse_lighting/source/tdogl/Camera.h
  7. +27 −6 linux/03_matrices/source/tdogl/Camera.cpp
  8. +9 −0 linux/03_matrices/source/tdogl/Camera.h
  9. +27 −6 linux/04_camera/source/tdogl/Camera.cpp
  10. +9 −0 linux/04_camera/source/tdogl/Camera.h
  11. +27 −6 linux/05_asset_instance/source/tdogl/Camera.cpp
  12. +9 −0 linux/05_asset_instance/source/tdogl/Camera.h
  13. +27 −6 linux/06_diffuse_lighting/source/tdogl/Camera.cpp
  14. +9 −0 linux/06_diffuse_lighting/source/tdogl/Camera.h
  15. +27 −6 linux/07_more_lighting/source/tdogl/Camera.cpp
  16. +9 −0 linux/07_more_lighting/source/tdogl/Camera.h
  17. +27 −6 osx/04_camera/source/tdogl/Camera.cpp
  18. +9 −0 osx/04_camera/source/tdogl/Camera.h
  19. +27 −6 osx/05_asset_instance/source/tdogl/Camera.cpp
  20. +9 −0 osx/05_asset_instance/source/tdogl/Camera.h
  21. +27 −6 osx/06_diffuse_lighting/source/tdogl/Camera.cpp
  22. +9 −0 osx/06_diffuse_lighting/source/tdogl/Camera.h
  23. +27 −6 osx/07_more_lighting/source/tdogl/Camera.cpp
  24. +9 −0 osx/07_more_lighting/source/tdogl/Camera.h
  25. +27 −6 windows/04_camera/source/tdogl/Camera.cpp
  26. +9 −0 windows/04_camera/source/tdogl/Camera.h
  27. +27 −6 windows/05_asset_instance/source/tdogl/Camera.cpp
  28. +9 −0 windows/05_asset_instance/source/tdogl/Camera.h
  29. +27 −6 windows/06_diffuse_lighting/source/tdogl/Camera.cpp
  30. +9 −0 windows/06_diffuse_lighting/source/tdogl/Camera.h
  31. +27 −6 windows/07_more_lighting/source/tdogl/Camera.cpp
  32. +9 −0 windows/07_more_lighting/source/tdogl/Camera.h
View
33 ios/04_camera/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 ios/04_camera/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
View
33 ios/05_asset_instance/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 ios/05_asset_instance/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
View
33 ios/06_diffuse_lighting/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 ios/06_diffuse_lighting/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
View
33 linux/03_matrices/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 linux/03_matrices/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
View
33 linux/04_camera/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 linux/04_camera/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
View
33 linux/05_asset_instance/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 linux/05_asset_instance/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
View
33 linux/06_diffuse_lighting/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 linux/06_diffuse_lighting/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
View
33 linux/07_more_lighting/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 linux/07_more_lighting/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
View
33 osx/04_camera/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 osx/04_camera/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
View
33 osx/05_asset_instance/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 osx/05_asset_instance/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
View
33 osx/06_diffuse_lighting/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 osx/06_diffuse_lighting/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
View
33 osx/07_more_lighting/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 osx/07_more_lighting/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
View
33 windows/04_camera/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 windows/04_camera/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
View
33 windows/05_asset_instance/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 windows/05_asset_instance/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
View
33 windows/06_diffuse_lighting/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 windows/06_diffuse_lighting/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
View
33 windows/07_more_lighting/source/tdogl/Camera.cpp
@@ -15,7 +15,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-
+#define _USE_MATH_DEFINES
+#include <cmath>
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
@@ -23,6 +24,10 @@ using namespace tdogl;
static const float MaxVerticalAngle = 85.0f; //must be less than 90 to avoid gimbal lock
+static inline float RadiansToDegrees(float radians) {
+ return radians * 180.0f / (float)M_PI;
+}
+
Camera::Camera() :
_position(0.0f, 0.0f, 1.0f),
@@ -80,12 +85,16 @@ glm::mat4 Camera::orientation() const {
void Camera::offsetOrientation(float upAngle, float rightAngle) {
_horizontalAngle += rightAngle;
- while(_horizontalAngle > 360.0f) _horizontalAngle -= 360.0;
- while(_horizontalAngle < 0.0f) _horizontalAngle += 360.0;
-
_verticalAngle += upAngle;
- if(_verticalAngle > MaxVerticalAngle) _verticalAngle = MaxVerticalAngle;
- if(_verticalAngle < -MaxVerticalAngle) _verticalAngle = -MaxVerticalAngle;
+ normalizeAngles();
+}
+
+void Camera::lookAt(glm::vec3 position) {
+ assert(position != _position);
+ glm::vec3 direction = glm::normalize(position - _position);
+ _verticalAngle = RadiansToDegrees(asinf(-direction.y));
+ _horizontalAngle = -RadiansToDegrees(atan2f(-direction.x, -direction.z));
+ normalizeAngles();
}
float Camera::viewportAspectRatio() const {
@@ -123,3 +132,15 @@ glm::mat4 Camera::projection() const {
glm::mat4 Camera::view() const {
return orientation() * glm::translate(glm::mat4(), -_position);
}
+
+void Camera::normalizeAngles() {
+ _horizontalAngle = fmodf(_horizontalAngle, 360.0f);
+ //fmodf can return negative values, but this will make them all positive
+ if(_horizontalAngle < 0.0f)
+ _horizontalAngle += 360.0f;
+
+ if(_verticalAngle > MaxVerticalAngle)
+ _verticalAngle = MaxVerticalAngle;
+ else if(_verticalAngle < -MaxVerticalAngle)
+ _verticalAngle = -MaxVerticalAngle;
+}
View
9 windows/07_more_lighting/source/tdogl/Camera.h
@@ -101,6 +101,13 @@ namespace tdogl {
void offsetOrientation(float upAngle, float rightAngle);
/**
+ Orients the camera so that is it directly facing `position`
+
+ @param position the position to look at
+ */
+ void lookAt(glm::vec3 position);
+
+ /**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
@@ -145,6 +152,8 @@ namespace tdogl {
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
+
+ void normalizeAngles();
};
}
Please sign in to comment.
Something went wrong with that request. Please try again.