Simple game engine with complete export to scripting language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

146 lines
2.6 KiB

  1. #include "Camera.h"
  2. #include "Math/Matrix.h"
  3. #include "GL/glfw.h"
  4. namespace BlueCore
  5. {
  6. Camera::Camera()
  7. {
  8. }
  9. Camera::~Camera()
  10. {
  11. }
  12. void Camera::setFoV(Scalar fov)
  13. {
  14. _FoV = fov;
  15. }
  16. void Camera::setAspectRatio(Scalar aspect)
  17. {
  18. _AspectRatio = aspect;
  19. }
  20. void Camera::setNearPlane(Scalar near)
  21. {
  22. _NearPlane = near;
  23. }
  24. void Camera::setFarPlane(Scalar far)
  25. {
  26. _FarPlane = far;
  27. }
  28. void Camera::setPosition(const Vector3& position)
  29. {
  30. _Position = position;
  31. }
  32. const Vector3& Camera::getPosition()
  33. {
  34. return _Position;
  35. }
  36. void Camera::setRotation(const Quaternion& rotation)
  37. {
  38. _Rotation = rotation;
  39. }
  40. const Quaternion& Camera::getRotation()
  41. {
  42. return _Rotation;
  43. }
  44. #if 0
  45. void Camera::setupProjectionMatrix()
  46. {
  47. Scalar fW, fH;
  48. fH = tan ( (_FoV / 2) / 180* Pi ) * _NearPlane;
  49. fW = fH * _AspectRatio;
  50. // setup projectiom matrix
  51. glMatrixMode (GL_PROJECTION );
  52. glLoadIdentity();
  53. glFrustum ( -fW, fW, -fH, fH, _NearPlane, _FarPlane );
  54. // save variables for frustum culling
  55. /*
  56. _near = nearZ;
  57. _far = farZ;
  58. _hNear = tan ( ( fov / 2 ) / 180 * Pi ) * nearZ;
  59. _wNear = _hNear * aspect;
  60. _hFar = tan ( ( fov / 2 ) / 180 * Pi ) * farZ;
  61. _wFar = _hFar * aspect;
  62. */
  63. }
  64. void Camera::setupViewMatrix()
  65. {
  66. // set the view matrix
  67. glMatrixMode (GL_MODELVIEW );
  68. glLoadIdentity();
  69. Matrix4x4 m(_Rotation, _Rotation.applyInversed(_Position * -1.0) );
  70. glMultMatrixd ( ( GLdouble * ) &m.m );
  71. /*
  72. // calculate frustum planes
  73. Vector3 up = q.apply ( Vector3 ( 0.0, 1.0, 0.0 ) );
  74. Vector3 right = q.apply ( Vector3 ( 1.0, 0.0, 0.0 ) );
  75. Vector3 d = q.apply ( Vector3 ( 0.0, 0.0, -1.0 ) );
  76. Vector3 fc = p + d * _far;
  77. Vector3 ftl = fc + ( up * _hFar ) - ( right * _wFar );
  78. Vector3 ftr = fc + ( up * _hFar ) + ( right * _wFar );
  79. Vector3 fbl = fc - ( up * _hFar ) - ( right * _wFar );
  80. Vector3 fbr = fc - ( up * _hFar ) + ( right * _wFar );
  81. Vector3 nc = p + d * _near;
  82. Vector3 ntl = nc + ( up * _hNear ) - ( right * _wNear );
  83. Vector3 ntr = nc + ( up * _hNear ) + ( right * _wNear );
  84. Vector3 nbl = nc - ( up * _hNear ) - ( right * _wNear );
  85. Vector3 nbr = nc - ( up * _hNear ) + ( right * _wNear );
  86. _frustumPlanes[RightPlane] = Plane ( nbr, fbr, ntr );
  87. _frustumPlanes[LeftPlane] = Plane ( ntl, fbl, nbl );
  88. _frustumPlanes[BottomPlane] = Plane ( nbl, fbr, nbr );
  89. _frustumPlanes[TopPlane] = Plane ( ntr, ftl, ntl );
  90. _frustumPlanes[FarPlane] = Plane ( ftl, ftr, fbl );
  91. _frustumPlanes[NearPlane] = Plane ( ntl, nbl, ntr );
  92. */
  93. }
  94. #endif
  95. Scalar Camera::getNearPlane() const
  96. {
  97. return _NearPlane;
  98. }
  99. Scalar Camera::getFarPlane() const
  100. {
  101. return _FarPlane;
  102. }
  103. Scalar Camera::getFov() const
  104. {
  105. return _FoV;
  106. }
  107. } // namespace BlueCore