PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/llvowater.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 311 lines | 208 code | 63 blank | 40 comment | 7 complexity | 2ef3910561e6804a97669959657ad10f MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llvowater.cpp
  3. * @brief LLVOWater class implementation
  4. *
  5. * $LicenseInfo:firstyear=2005&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. #include "llviewerprecompiledheaders.h"
  27. #include "llvowater.h"
  28. #include "imageids.h"
  29. #include "llviewercontrol.h"
  30. #include "lldrawable.h"
  31. #include "lldrawpoolwater.h"
  32. #include "llface.h"
  33. #include "llsky.h"
  34. #include "llsurface.h"
  35. #include "llvosky.h"
  36. #include "llviewercamera.h"
  37. #include "llviewertexturelist.h"
  38. #include "llviewerregion.h"
  39. #include "llworld.h"
  40. #include "pipeline.h"
  41. #include "llspatialpartition.h"
  42. const BOOL gUseRoam = FALSE;
  43. ///////////////////////////////////
  44. template<class T> inline T LERP(T a, T b, F32 factor)
  45. {
  46. return a + (b - a) * factor;
  47. }
  48. const U32 N_RES_HALF = (N_RES >> 1);
  49. const U32 WIDTH = (N_RES * WAVE_STEP); //128.f //64 // width of wave tile, in meters
  50. const F32 WAVE_STEP_INV = (1. / WAVE_STEP);
  51. LLVOWater::LLVOWater(const LLUUID &id,
  52. const LLPCode pcode,
  53. LLViewerRegion *regionp) :
  54. LLStaticViewerObject(id, pcode, regionp),
  55. mRenderType(LLPipeline::RENDER_TYPE_WATER)
  56. {
  57. // Terrain must draw during selection passes so it can block objects behind it.
  58. mbCanSelect = FALSE;
  59. setScale(LLVector3(256.f, 256.f, 0.f)); // Hack for setting scale for bounding boxes/visibility.
  60. mUseTexture = TRUE;
  61. mIsEdgePatch = FALSE;
  62. }
  63. void LLVOWater::markDead()
  64. {
  65. LLViewerObject::markDead();
  66. }
  67. BOOL LLVOWater::isActive() const
  68. {
  69. return FALSE;
  70. }
  71. void LLVOWater::setPixelAreaAndAngle(LLAgent &agent)
  72. {
  73. mAppAngle = 50;
  74. mPixelArea = 500*500;
  75. }
  76. // virtual
  77. void LLVOWater::updateTextures()
  78. {
  79. }
  80. // Never gets called
  81. BOOL LLVOWater::idleUpdate(LLAgent &agent, LLWorld &world, const F64 &time)
  82. {
  83. /*if (mDead || !(gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_WATER)))
  84. {
  85. return TRUE;
  86. }
  87. if (mDrawable)
  88. {
  89. gPipeline.markRebuild(mDrawable, LLDrawable::REBUILD_VOLUME, TRUE);
  90. }*/
  91. return TRUE;
  92. }
  93. LLDrawable *LLVOWater::createDrawable(LLPipeline *pipeline)
  94. {
  95. pipeline->allocDrawable(this);
  96. mDrawable->setLit(FALSE);
  97. mDrawable->setRenderType(mRenderType);
  98. LLDrawPoolWater *pool = (LLDrawPoolWater*) gPipeline.getPool(LLDrawPool::POOL_WATER);
  99. if (mUseTexture)
  100. {
  101. mDrawable->setNumFaces(1, pool, mRegionp->getLand().getWaterTexture());
  102. }
  103. else
  104. {
  105. mDrawable->setNumFaces(1, pool, LLWorld::getInstance()->getDefaultWaterTexture());
  106. }
  107. return mDrawable;
  108. }
  109. static LLFastTimer::DeclareTimer FTM_UPDATE_WATER("Update Water");
  110. BOOL LLVOWater::updateGeometry(LLDrawable *drawable)
  111. {
  112. LLFastTimer ftm(FTM_UPDATE_WATER);
  113. LLFace *face;
  114. if (drawable->getNumFaces() < 1)
  115. {
  116. LLDrawPoolWater *poolp = (LLDrawPoolWater*) gPipeline.getPool(LLDrawPool::POOL_WATER);
  117. drawable->addFace(poolp, NULL);
  118. }
  119. face = drawable->getFace(0);
  120. // LLVector2 uvs[4];
  121. // LLVector3 vtx[4];
  122. LLStrider<LLVector3> verticesp, normalsp;
  123. LLStrider<LLVector2> texCoordsp;
  124. LLStrider<U16> indicesp;
  125. U16 index_offset;
  126. // A quad is 4 vertices and 6 indices (making 2 triangles)
  127. static const unsigned int vertices_per_quad = 4;
  128. static const unsigned int indices_per_quad = 6;
  129. const S32 size = gSavedSettings.getBOOL("RenderTransparentWater") && !LLGLSLShader::sNoFixedFunction ? 16 : 1;
  130. const S32 num_quads = size * size;
  131. face->setSize(vertices_per_quad * num_quads,
  132. indices_per_quad * num_quads);
  133. LLVertexBuffer* buff = face->getVertexBuffer();
  134. if (!buff)
  135. {
  136. buff = new LLVertexBuffer(LLDrawPoolWater::VERTEX_DATA_MASK, GL_DYNAMIC_DRAW_ARB);
  137. buff->allocateBuffer(face->getGeomCount(), face->getIndicesCount(), TRUE);
  138. face->setIndicesIndex(0);
  139. face->setGeomIndex(0);
  140. face->setVertexBuffer(buff);
  141. }
  142. else
  143. {
  144. buff->resizeBuffer(face->getGeomCount(), face->getIndicesCount());
  145. }
  146. index_offset = face->getGeometry(verticesp,normalsp,texCoordsp, indicesp);
  147. LLVector3 position_agent;
  148. position_agent = getPositionAgent();
  149. face->mCenterAgent = position_agent;
  150. face->mCenterLocal = position_agent;
  151. S32 x, y;
  152. F32 step_x = getScale().mV[0] / size;
  153. F32 step_y = getScale().mV[1] / size;
  154. const LLVector3 up(0.f, step_y * 0.5f, 0.f);
  155. const LLVector3 right(step_x * 0.5f, 0.f, 0.f);
  156. const LLVector3 normal(0.f, 0.f, 1.f);
  157. F32 size_inv = 1.f / size;
  158. for (y = 0; y < size; y++)
  159. {
  160. for (x = 0; x < size; x++)
  161. {
  162. S32 toffset = index_offset + 4*(y*size + x);
  163. position_agent = getPositionAgent() - getScale() * 0.5f;
  164. position_agent.mV[VX] += (x + 0.5f) * step_x;
  165. position_agent.mV[VY] += (y + 0.5f) * step_y;
  166. *verticesp++ = position_agent - right + up;
  167. *verticesp++ = position_agent - right - up;
  168. *verticesp++ = position_agent + right + up;
  169. *verticesp++ = position_agent + right - up;
  170. *texCoordsp++ = LLVector2(x*size_inv, (y+1)*size_inv);
  171. *texCoordsp++ = LLVector2(x*size_inv, y*size_inv);
  172. *texCoordsp++ = LLVector2((x+1)*size_inv, (y+1)*size_inv);
  173. *texCoordsp++ = LLVector2((x+1)*size_inv, y*size_inv);
  174. *normalsp++ = normal;
  175. *normalsp++ = normal;
  176. *normalsp++ = normal;
  177. *normalsp++ = normal;
  178. *indicesp++ = toffset + 0;
  179. *indicesp++ = toffset + 1;
  180. *indicesp++ = toffset + 2;
  181. *indicesp++ = toffset + 1;
  182. *indicesp++ = toffset + 3;
  183. *indicesp++ = toffset + 2;
  184. }
  185. }
  186. buff->flush();
  187. mDrawable->movePartition();
  188. LLPipeline::sCompiles++;
  189. return TRUE;
  190. }
  191. void LLVOWater::initClass()
  192. {
  193. }
  194. void LLVOWater::cleanupClass()
  195. {
  196. }
  197. void setVecZ(LLVector3& v)
  198. {
  199. v.mV[VX] = 0;
  200. v.mV[VY] = 0;
  201. v.mV[VZ] = 1;
  202. }
  203. void LLVOWater::setUseTexture(const BOOL use_texture)
  204. {
  205. mUseTexture = use_texture;
  206. }
  207. void LLVOWater::setIsEdgePatch(const BOOL edge_patch)
  208. {
  209. mIsEdgePatch = edge_patch;
  210. }
  211. void LLVOWater::updateSpatialExtents(LLVector4a &newMin, LLVector4a& newMax)
  212. {
  213. LLVector4a pos;
  214. pos.load3(getPositionAgent().mV);
  215. LLVector4a scale;
  216. scale.load3(getScale().mV);
  217. scale.mul(0.5f);
  218. newMin.setSub(pos, scale);
  219. newMax.setAdd(pos, scale);
  220. pos.setAdd(newMin,newMax);
  221. pos.mul(0.5f);
  222. mDrawable->setPositionGroup(pos);
  223. }
  224. U32 LLVOWater::getPartitionType() const
  225. {
  226. if (mIsEdgePatch)
  227. {
  228. return LLViewerRegion::PARTITION_VOIDWATER;
  229. }
  230. return LLViewerRegion::PARTITION_WATER;
  231. }
  232. U32 LLVOVoidWater::getPartitionType() const
  233. {
  234. return LLViewerRegion::PARTITION_VOIDWATER;
  235. }
  236. LLWaterPartition::LLWaterPartition()
  237. : LLSpatialPartition(0, FALSE, GL_DYNAMIC_DRAW_ARB)
  238. {
  239. mInfiniteFarClip = TRUE;
  240. mDrawableType = LLPipeline::RENDER_TYPE_WATER;
  241. mPartitionType = LLViewerRegion::PARTITION_WATER;
  242. }
  243. LLVoidWaterPartition::LLVoidWaterPartition()
  244. {
  245. mOcclusionEnabled = FALSE;
  246. mDrawableType = LLPipeline::RENDER_TYPE_VOIDWATER;
  247. mPartitionType = LLViewerRegion::PARTITION_VOIDWATER;
  248. }