llsurface.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**
  2. * @file llsurface.h
  3. * @brief Description of LLSurface class
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLSURFACE_H
  33. #define LL_LLSURFACE_H
  34. #include <list>
  35. #include "llvector3.h"
  36. #include "llvector3d.h"
  37. #include "llvector4.h"
  38. #include "llmatrix3.h"
  39. #include "llmatrix4.h"
  40. #include "llquaternion.h"
  41. #include "llcolor4u.h"
  42. #include "llcolor4.h"
  43. #include "llvowater.h"
  44. #include "llpatchvertexarray.h"
  45. #include "llviewertexture.h"
  46. class LLTimer;
  47. class LLUUID;
  48. class LLStat;
  49. constexpr U8 NO_EDGE = 0x00;
  50. constexpr U8 EAST_EDGE = 0x01;
  51. constexpr U8 NORTH_EDGE = 0x02;
  52. constexpr U8 WEST_EDGE = 0x04;
  53. constexpr U8 SOUTH_EDGE = 0x08;
  54. constexpr S32 ONE_MORE_THAN_NEIGHBOR = 1;
  55. constexpr S32 EQUAL_TO_NEIGHBOR = 0;
  56. constexpr S32 ONE_LESS_THAN_NEIGHBOR = -1;
  57. // The alpha of water when the land elevation is above the waterline.
  58. constexpr S32 ABOVE_WATERLINE_ALPHA = 32;
  59. class LLViewerRegion;
  60. class LLSurfacePatch;
  61. class LLBitPack;
  62. class LLGroupHeader;
  63. class LLSurface
  64. {
  65. friend class LLSurfacePatch;
  66. protected:
  67. LOG_CLASS(LLSurface);
  68. public:
  69. LLSurface(U32 type, LLViewerRegion* regionp = NULL);
  70. ~LLSurface();
  71. // Allocates and initializes surface
  72. void create(S32 surface_grid_width, S32 surface_patch_width,
  73. const LLVector3d& origin_global, U32 width);
  74. void setRegion(LLViewerRegion* regionp);
  75. void setOriginGlobal(const LLVector3d& origin_global);
  76. void connectNeighbor(LLSurface* neighborp, U32 direction);
  77. void disconnectNeighbor(LLSurface* neighborp);
  78. void disconnectAllNeighbors();
  79. void decompressDCTPatch(LLBitPack& bitpack, LLGroupHeader* gopp,
  80. bool large_patch);
  81. void updatePatchVisibilities();
  82. LL_INLINE F32 getZ(U32 k) const { return mSurfaceZ[k]; }
  83. LL_INLINE F32 getZ(S32 i, S32 j) const { return mSurfaceZ[i + j * mGridsPerEdge]; }
  84. LLVector3 getOriginAgent() const;
  85. LL_INLINE const LLVector3d& getOriginGlobal() const { return mOriginGlobal; }
  86. LL_INLINE F32 getMetersPerGrid() const { return mMetersPerGrid; }
  87. LL_INLINE S32 getGridsPerEdge() const { return mGridsPerEdge; }
  88. LL_INLINE S32 getPatchesPerEdge() const { return mPatchesPerEdge; }
  89. LL_INLINE S32 getGridsPerPatchEdge() const { return mGridsPerPatchEdge; }
  90. U32 getRenderStride(U32 render_level) const;
  91. U32 getRenderLevel(U32 render_stride) const;
  92. // Returns the height of the surface immediately above (or below) location,
  93. // or if location is not above surface returns zero.
  94. F32 resolveHeightRegion(F32 x, F32 y) const;
  95. LL_INLINE F32 resolveHeightRegion(const LLVector3& location) const
  96. {
  97. return resolveHeightRegion(location.mV[VX], location.mV[VY]);
  98. }
  99. F32 resolveHeightGlobal(const LLVector3d& position_global) const;
  100. // Returns normal to surface
  101. LLVector3 resolveNormalGlobal(const LLVector3d& v) const;
  102. LLSurfacePatch* resolvePatchRegion(F32 x, F32 y) const;
  103. LLSurfacePatch* resolvePatchRegion(const LLVector3& pos_region) const;
  104. LLSurfacePatch* resolvePatchGlobal(const LLVector3d& pos_global) const;
  105. // Update methods (called during idle, normally)
  106. void idleUpdate(F32 max_update_time);
  107. // Returns true if "position" is within the bounds of surface. "position"
  108. // is region-local.
  109. LL_INLINE bool containsPosition(const LLVector3& position)
  110. {
  111. return position.mV[VX] >= 0.f && position.mV[VX] <= mMetersPerEdge &&
  112. position.mV[VY] >= 0.f && position.mV[VY] <= mMetersPerEdge;
  113. }
  114. void moveZ(S32 x, S32 y, F32 delta);
  115. LL_INLINE LLViewerRegion* getRegion() const { return mRegionp; }
  116. LL_INLINE F32 getMinZ() const { return mMinZ; }
  117. LL_INLINE F32 getMaxZ() const { return mMaxZ; }
  118. void setWaterHeight(F32 height);
  119. F32 getWaterHeight() const;
  120. LLViewerTexture* getSTexture();
  121. LLViewerTexture* getWaterTexture();
  122. LL_INLINE bool hasZData() const { return mHasZData; }
  123. // Use this to dirty all patches when changing terrain parameters
  124. void dirtyAllPatches();
  125. void dirtySurfacePatch(LLSurfacePatch* patchp);
  126. LL_INLINE LLVOWater* getWaterObj() { return mWaterObjp; }
  127. static void setTextureSize(U32 size);
  128. friend std::ostream& operator<<(std::ostream& s, const LLSurface& S);
  129. void getNeighboringRegions(std::vector<LLViewerRegion*>& regions);
  130. void getNeighboringRegionsStatus(std::vector<S32>& regions);
  131. public:
  132. // Number of grid points on one side of a region, including +1 buffer for
  133. // north and east edge.
  134. S32 mGridsPerEdge;
  135. F32 mOOGridsPerEdge; // Inverse of grids per edge
  136. S32 mPatchesPerEdge; // Number of patches on one side of a region
  137. S32 mNumberOfPatches; // Total number of patches
  138. // Each surface points at 8 neighbors (or NULL)
  139. // +---+---+---+
  140. // |NW | N | NE|
  141. // +---+---+---+
  142. // | W | 0 | E |
  143. // +---+---+---+
  144. // |SW | S | SE|
  145. // +---+---+---+
  146. LLSurface* mNeighbors[8]; // Adjacent patches
  147. U32 mType; // Useful for identifying derived classes
  148. // Number of times to repeat detail texture across this surface
  149. F32 mDetailTextureScale;
  150. static F32 sTextureUpdateTime;
  151. static S32 sTexelsUpdated;
  152. static LLStat sTexelsUpdatedPerSecStat;
  153. protected:
  154. void createSTexture();
  155. void createWaterTexture();
  156. void initTextures();
  157. void initWater();
  158. void createPatchData(); // Allocates memory for patches.
  159. void destroyPatchData(); // Deallocates memory for patches.
  160. // Generate texture from composition values.
  161. bool generateWaterTexture(F32 x, F32 y, F32 width, F32 height);
  162. LLSurfacePatch* getPatch(S32 x, S32 y) const;
  163. private:
  164. // Patch whose coordinate system this surface is using.
  165. LLViewerRegion* mRegionp;
  166. // Default size of the surface texture
  167. static U32 sTextureSize;
  168. protected:
  169. LLVector3d mOriginGlobal; // In absolute frame
  170. LLSurfacePatch* mPatchList; // Array of all patches
  171. // Array of grid data, mGridsPerEdge * mGridsPerEdge
  172. F32* mSurfaceZ;
  173. // Array of grid normals, mGridsPerEdge * mGridsPerEdge
  174. LLVector3* mNorm;
  175. typedef std::list<LLSurfacePatch*> patch_list_t;
  176. patch_list_t mDirtyPatchList;
  177. // The textures should never be directly initialized ! Use the setter
  178. // methods !
  179. LLPointer<LLViewerTexture> mSTexturep; // Texture for surface
  180. LLPointer<LLViewerTexture> mWaterTexturep; // Water texture
  181. LLPointer<LLVOWater> mWaterObjp;
  182. // When we want multiple cameras we'll need one of each these for each
  183. // camera
  184. S32 mVisiblePatchCount;
  185. // Number of grid points on a side of a patch
  186. U32 mGridsPerPatchEdge;
  187. // Converts (i, j) indexes to distance
  188. F32 mMetersPerGrid;
  189. // = mMetersPerGrid * (mGridsPerEdge - 1)
  190. F32 mMetersPerEdge;
  191. // Size for the surface texture.
  192. U32 mTextureSize;
  193. LLPatchVertexArray mPVArray;
  194. // Number of frames since last update.
  195. S32 mSurfacePatchUpdateCount;
  196. // Min and max Z for this region (during the session)
  197. F32 mMinZ;
  198. F32 mMaxZ;
  199. // Wether or not we have received any patch data for this surface:
  200. bool mHasZData;
  201. };
  202. // . __.
  203. // Z /|\ /| Y North
  204. // | /
  205. // | / |<----------------- mGridsPerSurfaceEdge --------------->|
  206. // | / __________________________________________________________
  207. // |/______\ X /_______________________________________________________ /
  208. // / / / / / / / /M*M-2 /M*M-1 / /
  209. // /______/______/______/______/______/______/______/______/ /
  210. // / / / / / / / / / /
  211. // /______/______/______/______/______/______/______/______/ /
  212. // / / / / / / / / / /
  213. // /______/______/______/______/______/______/______/______/ /
  214. // West / / / / / / / / / /
  215. // /______/______/______/______/______/______/______/______/ / East
  216. // /... / / / / / / / / /
  217. // /______/______/______/______/______/______/______/______/ /
  218. // _. / 2M / / / / / / / / /
  219. // /| /______/______/______/______/______/______/______/______/ /
  220. // / / M / M+1 / M+2 / ... / / / / 2M-1 / /
  221. // j /______/______/______/______/______/______/______/______/ /
  222. // / 0 / 1 / 2 / ... / / / / M-1 / /
  223. // /______/______/______/______/______/______/______/______/_/
  224. // South |<-L->|
  225. // i -->
  226. //
  227. // where M = mSurfPatchWidth
  228. // and L = mPatchGridWidth
  229. //
  230. // Notice that mGridsPerSurfaceEdge = a power of two + 1
  231. // This provides a buffer on the east and north edges that will allow us to
  232. // fill the cracks between adjacent surfaces when rendering.
  233. extern LLColor4U MAX_WATER_COLOR;
  234. #endif