PageRenderTime 44ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/llcamera.h

https://bitbucket.org/lindenlab/viewer-beta/
C Header | 217 lines | 132 code | 39 blank | 46 comment | 0 complexity | aa5931b5ffaf5fb111e80958e5f203d4 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llcamera.h
  3. * @brief Header file for the LLCamera class.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_CAMERA_H
  27. #define LL_CAMERA_H
  28. #include "llmath.h"
  29. #include "llcoordframe.h"
  30. #include "llplane.h"
  31. #include "llvector4a.h"
  32. const F32 DEFAULT_FIELD_OF_VIEW = 60.f * DEG_TO_RAD;
  33. const F32 DEFAULT_ASPECT_RATIO = 640.f / 480.f;
  34. const F32 DEFAULT_NEAR_PLANE = 0.25f;
  35. const F32 DEFAULT_FAR_PLANE = 64.f; // far reaches across two horizontal, not diagonal, regions
  36. const F32 MAX_ASPECT_RATIO = 50.0f;
  37. const F32 MAX_NEAR_PLANE = 10.f;
  38. const F32 MAX_FAR_PLANE = 100000.0f; //1000000.0f; // Max allowed. Not good Z precision though.
  39. const F32 MAX_FAR_CLIP = 512.0f;
  40. const F32 MIN_ASPECT_RATIO = 0.02f;
  41. const F32 MIN_NEAR_PLANE = 0.1f;
  42. const F32 MIN_FAR_PLANE = 0.2f;
  43. // Min/Max FOV values for square views. Call getMin/MaxView to get extremes based on current aspect ratio.
  44. static const F32 MIN_FIELD_OF_VIEW = 5.0f * DEG_TO_RAD;
  45. static const F32 MAX_FIELD_OF_VIEW = 175.f * DEG_TO_RAD;
  46. // An LLCamera is an LLCoorFrame with a view frustum.
  47. // This means that it has several methods for moving it around
  48. // that are inherited from the LLCoordFrame() class :
  49. //
  50. // setOrigin(), setAxes()
  51. // translate(), rotate()
  52. // roll(), pitch(), yaw()
  53. // etc...
  54. class LLCamera
  55. : public LLCoordFrame
  56. {
  57. public:
  58. LLCamera(const LLCamera& rhs)
  59. {
  60. *this = rhs;
  61. }
  62. enum {
  63. PLANE_LEFT = 0,
  64. PLANE_RIGHT = 1,
  65. PLANE_BOTTOM = 2,
  66. PLANE_TOP = 3,
  67. PLANE_NUM = 4
  68. };
  69. enum {
  70. PLANE_LEFT_MASK = (1<<PLANE_LEFT),
  71. PLANE_RIGHT_MASK = (1<<PLANE_RIGHT),
  72. PLANE_BOTTOM_MASK = (1<<PLANE_BOTTOM),
  73. PLANE_TOP_MASK = (1<<PLANE_TOP),
  74. PLANE_ALL_MASK = 0xf
  75. };
  76. enum
  77. {
  78. AGENT_PLANE_LEFT = 0,
  79. AGENT_PLANE_RIGHT,
  80. AGENT_PLANE_NEAR,
  81. AGENT_PLANE_BOTTOM,
  82. AGENT_PLANE_TOP,
  83. AGENT_PLANE_FAR,
  84. };
  85. enum {
  86. HORIZ_PLANE_LEFT = 0,
  87. HORIZ_PLANE_RIGHT = 1,
  88. HORIZ_PLANE_NUM = 2
  89. };
  90. enum {
  91. HORIZ_PLANE_LEFT_MASK = (1<<HORIZ_PLANE_LEFT),
  92. HORIZ_PLANE_RIGHT_MASK = (1<<HORIZ_PLANE_RIGHT),
  93. HORIZ_PLANE_ALL_MASK = 0x3
  94. };
  95. private:
  96. LLPlane mAgentPlanes[7]; //frustum planes in agent space a la gluUnproject (I'm a bastard, I know) - DaveP
  97. U8 mPlaneMask[8]; // 8 for alignment
  98. F32 mView; // angle between top and bottom frustum planes in radians.
  99. F32 mAspect; // width/height
  100. S32 mViewHeightInPixels; // for ViewHeightInPixels() only
  101. F32 mNearPlane;
  102. F32 mFarPlane;
  103. LLPlane mLocalPlanes[4];
  104. F32 mFixedDistance; // Always return this distance, unless < 0
  105. LLVector3 mFrustCenter; // center of frustum and radius squared for ultra-quick exclusion test
  106. F32 mFrustRadiusSquared;
  107. LLPlane mWorldPlanes[PLANE_NUM];
  108. LLPlane mHorizPlanes[HORIZ_PLANE_NUM];
  109. U32 mPlaneCount; //defaults to 6, if setUserClipPlane is called, uses user supplied clip plane in
  110. LLVector3 mWorldPlanePos; // Position of World Planes (may be offset from camera)
  111. public:
  112. LLVector3 mAgentFrustum[8]; //8 corners of 6-plane frustum
  113. F32 mFrustumCornerDist; //distance to corner of frustum against far clip plane
  114. LLPlane& getAgentPlane(U32 idx) { return mAgentPlanes[idx]; }
  115. public:
  116. LLCamera();
  117. LLCamera(F32 vertical_fov_rads, F32 aspect_ratio, S32 view_height_in_pixels, F32 near_plane, F32 far_plane);
  118. virtual ~LLCamera();
  119. void setUserClipPlane(LLPlane& plane);
  120. void disableUserClipPlane();
  121. virtual void setView(F32 vertical_fov_rads);
  122. void setViewHeightInPixels(S32 height);
  123. void setAspect(F32 new_aspect);
  124. void setNear(F32 new_near);
  125. void setFar(F32 new_far);
  126. F32 getView() const { return mView; } // vertical FOV in radians
  127. S32 getViewHeightInPixels() const { return mViewHeightInPixels; }
  128. F32 getAspect() const { return mAspect; } // width / height
  129. F32 getNear() const { return mNearPlane; } // meters
  130. F32 getFar() const { return mFarPlane; } // meters
  131. // The values returned by the min/max view getters depend upon the aspect ratio
  132. // at the time they are called and therefore should not be cached.
  133. F32 getMinView() const;
  134. F32 getMaxView() const;
  135. F32 getYaw() const
  136. {
  137. return atan2f(mXAxis[VY], mXAxis[VX]);
  138. }
  139. F32 getPitch() const
  140. {
  141. F32 xylen = sqrtf(mXAxis[VX]*mXAxis[VX] + mXAxis[VY]*mXAxis[VY]);
  142. return atan2f(mXAxis[VZ], xylen);
  143. }
  144. const LLPlane& getWorldPlane(S32 index) const { return mWorldPlanes[index]; }
  145. const LLVector3& getWorldPlanePos() const { return mWorldPlanePos; }
  146. // Copy mView, mAspect, mNearPlane, and mFarPlane to buffer.
  147. // Return number of bytes copied.
  148. size_t writeFrustumToBuffer(char *buffer) const;
  149. // Copy mView, mAspect, mNearPlane, and mFarPlane from buffer.
  150. // Return number of bytes copied.
  151. size_t readFrustumFromBuffer(const char *buffer);
  152. void calcAgentFrustumPlanes(LLVector3* frust);
  153. void ignoreAgentFrustumPlane(S32 idx);
  154. // Returns 1 if partly in, 2 if fully in.
  155. // NOTE: 'center' is in absolute frame.
  156. S32 sphereInFrustumOld(const LLVector3 &center, const F32 radius) const;
  157. S32 sphereInFrustum(const LLVector3 &center, const F32 radius) const;
  158. S32 pointInFrustum(const LLVector3 &point) const { return sphereInFrustum(point, 0.0f); }
  159. S32 sphereInFrustumFull(const LLVector3 &center, const F32 radius) const { return sphereInFrustum(center, radius); }
  160. S32 AABBInFrustum(const LLVector4a& center, const LLVector4a& radius);
  161. S32 AABBInFrustumNoFarClip(const LLVector4a& center, const LLVector4a& radius);
  162. //does a quick 'n dirty sphere-sphere check
  163. S32 sphereInFrustumQuick(const LLVector3 &sphere_center, const F32 radius);
  164. // Returns height of object in pixels (must be height because field of view
  165. // is based on window height).
  166. F32 heightInPixels(const LLVector3 &center, F32 radius ) const;
  167. // return the distance from pos to camera if visible (-distance if not visible)
  168. F32 visibleDistance(const LLVector3 &pos, F32 rad, F32 fudgescale = 1.0f, U32 planemask = PLANE_ALL_MASK) const;
  169. F32 visibleHorizDistance(const LLVector3 &pos, F32 rad, F32 fudgescale = 1.0f, U32 planemask = HORIZ_PLANE_ALL_MASK) const;
  170. void setFixedDistance(F32 distance) { mFixedDistance = distance; }
  171. friend std::ostream& operator<<(std::ostream &s, const LLCamera &C);
  172. protected:
  173. void calculateFrustumPlanes();
  174. void calculateFrustumPlanes(F32 left, F32 right, F32 top, F32 bottom);
  175. void calculateFrustumPlanesFromWindow(F32 x1, F32 y1, F32 x2, F32 y2);
  176. void calculateWorldFrustumPlanes();
  177. };
  178. #endif