lldrawpoolwater.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. /**
  2. * @file lldrawpoolwater.cpp
  3. * @brief LLDrawPoolWater class implementation
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-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. #include "llviewerprecompiledheaders.h"
  33. #include <array>
  34. #include "lldrawpoolwater.h"
  35. #include "imageids.h"
  36. #include "llcubemap.h"
  37. #include "lldir.h"
  38. #include "llfasttimer.h"
  39. #include "llrender.h"
  40. #include "llagent.h"
  41. #include "llappviewer.h" // For gFrameTimeSeconds
  42. #include "lldrawable.h"
  43. #include "llenvironment.h"
  44. #include "llface.h"
  45. #include "llfeaturemanager.h"
  46. #include "llgridmanager.h" // For gIsInSecondLife
  47. #include "llpipeline.h"
  48. //MK
  49. #include "mkrlinterface.h"
  50. //mk
  51. #include "llsky.h"
  52. #include "llviewercontrol.h"
  53. #include "llviewerdisplay.h" // For gCubeSnapshot
  54. #include "llviewerregion.h"
  55. #include "llviewershadermgr.h"
  56. #include "llviewertexturelist.h"
  57. #include "llvowater.h"
  58. #include "llwlwaterparammgr.h"
  59. #include "llworld.h"
  60. static F32 sTime = 0.f;
  61. bool sDeferredRender = false;
  62. bool LLDrawPoolWater::sNeedsReflectionUpdate = true;
  63. bool LLDrawPoolWater::sNeedsTexturesReload = true;
  64. LLColor4 LLDrawPoolWater::sWaterFogColor = LLColor4(0.2f, 0.5f, 0.5f, 0.f);
  65. LLDrawPoolWater::LLDrawPoolWater()
  66. : LLFacePool(POOL_WATER)
  67. {
  68. }
  69. void LLDrawPoolWater::setOpaqueTexture(const LLUUID& tex_id)
  70. {
  71. if (mOpaqueWaterImagep && mOpaqueWaterImagep->getID() == tex_id)
  72. {
  73. // Nothing to do !
  74. return;
  75. }
  76. if (tex_id == DEFAULT_WATER_OPAQUE || tex_id.isNull())
  77. {
  78. mOpaqueWaterImagep = LLViewerFetchedTexture::sOpaqueWaterImagep;
  79. }
  80. else
  81. {
  82. mOpaqueWaterImagep = LLViewerTextureManager::getFetchedTexture(tex_id);
  83. mOpaqueWaterImagep->setNoDelete();
  84. }
  85. mOpaqueWaterImagep->addTextureStats(1024.f * 1024.f);
  86. }
  87. void LLDrawPoolWater::setTransparentTextures(const LLUUID& tex1_id,
  88. const LLUUID& tex2_id)
  89. {
  90. if (!mWaterImagep[0] || mWaterImagep[0]->getID() != tex1_id)
  91. {
  92. if (tex1_id == DEFAULT_WATER_TEXTURE || tex1_id.isNull())
  93. {
  94. mWaterImagep[0] = LLViewerFetchedTexture::sWaterImagep;
  95. }
  96. else
  97. {
  98. mWaterImagep[0] =
  99. LLViewerTextureManager::getFetchedTexture(tex1_id);
  100. }
  101. mWaterImagep[0]->setNoDelete();
  102. mWaterImagep[0]->addTextureStats(1024.f * 1024.f);
  103. }
  104. if (mWaterImagep[1] && mWaterImagep[1]->getID() == tex2_id)
  105. {
  106. // Nothing left to do
  107. return;
  108. }
  109. if (tex2_id.notNull())
  110. {
  111. mWaterImagep[1] = LLViewerTextureManager::getFetchedTexture(tex2_id);
  112. }
  113. else
  114. {
  115. // Use the same texture as the first one...
  116. mWaterImagep[1] = mWaterImagep[0];
  117. }
  118. mWaterImagep[1]->setNoDelete();
  119. mWaterImagep[1]->addTextureStats(1024.f * 1024.f);
  120. }
  121. void LLDrawPoolWater::setNormalMaps(const LLUUID& tex1_id,
  122. const LLUUID& tex2_id)
  123. {
  124. if (!mWaterNormp[0] || mWaterNormp[0]->getID() != tex1_id)
  125. {
  126. if (tex1_id == DEFAULT_WATER_NORMAL || tex1_id.isNull())
  127. {
  128. mWaterNormp[0] = LLViewerFetchedTexture::sWaterNormapMapImagep;
  129. }
  130. else
  131. {
  132. mWaterNormp[0] =
  133. LLViewerTextureManager::getFetchedTexture(tex1_id);
  134. }
  135. mWaterNormp[0]->setNoDelete();
  136. mWaterNormp[0]->addTextureStats(1024.f * 1024.f);
  137. }
  138. if (mWaterNormp[1] && mWaterNormp[1]->getID() == tex2_id)
  139. {
  140. // Nothing left to do
  141. return;
  142. }
  143. if (tex2_id.notNull())
  144. {
  145. mWaterNormp[1] = LLViewerTextureManager::getFetchedTexture(tex2_id);
  146. }
  147. else
  148. {
  149. // Use the same texture as the first one...
  150. mWaterNormp[1] = mWaterNormp[0];
  151. }
  152. mWaterNormp[1]->setNoDelete();
  153. mWaterNormp[1]->addTextureStats(1024.f * 1024.f);
  154. }
  155. //virtual
  156. void LLDrawPoolWater::prerender()
  157. {
  158. mShaderLevel =
  159. gViewerShaderMgrp->getShaderLevel(LLViewerShaderMgr::SHADER_WATER);
  160. const LLSettingsWater::ptr_t& waterp = gEnvironment.getCurrentWater();
  161. if (waterp)
  162. {
  163. sWaterFogColor = LLColor4(waterp->getWaterFogColor(), 0.f);
  164. }
  165. if (sNeedsTexturesReload)
  166. {
  167. sNeedsTexturesReload = false;
  168. if (waterp)
  169. {
  170. setTransparentTextures(waterp->getTransparentTextureID(),
  171. waterp->getNextTransparentTextureID());
  172. setNormalMaps(waterp->getNormalMapID(),
  173. waterp->getNextNormalMapID());
  174. if (!gUsePBRShaders)
  175. {
  176. setOpaqueTexture(waterp->getDefaultOpaqueTextureAssetId());
  177. }
  178. }
  179. }
  180. LLVOSky* voskyp = gSky.mVOSkyp;
  181. if (!voskyp) return;
  182. mLightDir = gEnvironment.getLightDirection();
  183. mLightDir.normalize();
  184. const LLSettingsSky::ptr_t& skyp = gEnvironment.getCurrentSky();
  185. if (!skyp) return; // Paranoia
  186. if (gPipeline.mIsSunUp)
  187. {
  188. mLightDiffuse = voskyp->getSun().getColorCached();
  189. mLightColor = gPipeline.mSunLightColor;
  190. if (!gUsePBRShaders)
  191. {
  192. // *HACK for SL-18707: fix colours of light reflections on water at
  193. // sunrise and sunset.
  194. mLightColor.normalize();
  195. mLightColor.mV[0] = 5.f;
  196. mLightColor.mV[1] = 2.f;
  197. }
  198. }
  199. else if (gPipeline.mIsMoonUp)
  200. {
  201. mLightDiffuse = skyp->getMoonDiffuse();
  202. mLightColor = gPipeline.mMoonLightColor;
  203. }
  204. if (mLightDiffuse.normalize() > 0.f)
  205. {
  206. F32 ground_proj_sq = mLightDir.mV[0] * mLightDir.mV[0] +
  207. mLightDir.mV[1] * mLightDir.mV[1];
  208. mLightDiffuse *= 1.5f + (6.f * ground_proj_sq);
  209. }
  210. }
  211. // Do not render water above a configurable altitude.
  212. S32 LLDrawPoolWater::getWaterPasses()
  213. {
  214. static LLCachedControl<U32> max_alt(gSavedSettings,
  215. "RenderWaterMaxAltitude");
  216. if (!max_alt) // Always render when set to 0
  217. {
  218. return 1;
  219. }
  220. static LLCachedControl<F32> far_clip(gSavedSettings, "RenderFarClip");
  221. F32 limit = llmax(F32(far_clip), F32(max_alt));
  222. return gPipeline.mEyeAboveWater <= limit ? 1 : 0;
  223. }
  224. // Only for use by the EE renderer
  225. //virtual
  226. S32 LLDrawPoolWater::getNumPasses()
  227. {
  228. return gUsePBRShaders ? 0 : getWaterPasses();
  229. }
  230. // Only for use by the EE renderer
  231. //virtual
  232. void LLDrawPoolWater::render(S32)
  233. {
  234. LL_FAST_TIMER(FTM_RENDER_WATER);
  235. if (mDrawFace.empty() || LLViewerOctreeEntryData::getCurrentFrame() <= 1)
  236. {
  237. return;
  238. }
  239. // Do a quick'n dirty depth sort
  240. for (std::vector<LLFace*>::iterator iter = mDrawFace.begin(),
  241. end = mDrawFace.end();
  242. iter != end; ++iter)
  243. {
  244. LLFace* facep = *iter;
  245. facep->mDistance = -facep->mCenterLocal.mV[2];
  246. }
  247. std::sort(mDrawFace.begin(), mDrawFace.end(),
  248. LLFace::CompareDistanceGreater());
  249. if (!LLPipeline::RenderWaterReflectionType
  250. //MK
  251. || (gRLenabled && gRLInterface.mContainsCamTextures))
  252. //mk
  253. {
  254. // Render water for low end hardware
  255. renderOpaqueLegacyWater();
  256. return;
  257. }
  258. LLGLEnable blend(GL_BLEND);
  259. if (mShaderLevel > 0)
  260. {
  261. renderWater();
  262. return;
  263. }
  264. LLVOSky* voskyp = gSky.mVOSkyp;
  265. if (!voskyp)
  266. {
  267. return;
  268. }
  269. LLFace* refl_facep = voskyp->getReflFace();
  270. gPipeline.disableLights();
  271. LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);
  272. LLGLDisable cull_face(GL_CULL_FACE);
  273. // Set up second pass first
  274. LLTexUnit* unit1 = gGL.getTexUnit(1);
  275. unit1->activate();
  276. unit1->enable(LLTexUnit::TT_TEXTURE);
  277. unit1->bind(mWaterImagep[0]);
  278. LLTexUnit* unit2 = gGL.getTexUnit(2);
  279. unit2->activate();
  280. unit2->enable(LLTexUnit::TT_TEXTURE);
  281. unit2->bind(mWaterImagep[1]);
  282. const LLVector3& camera_up = gViewerCamera.getUpAxis();
  283. F32 up_dot = camera_up * LLVector3::z_axis;
  284. LLColor4 water_color;
  285. if (gViewerCamera.cameraUnderWater())
  286. {
  287. water_color.set(1.f, 1.f, 1.f, 0.4f);
  288. }
  289. else
  290. {
  291. water_color.set(1.f, 1.f, 1.f, 0.5f + 0.5f * up_dot);
  292. }
  293. gGL.diffuseColor4fv(water_color.mV);
  294. // Automatically generate texture coords for detail map
  295. glEnable(GL_TEXTURE_GEN_S); // Texture unit 1
  296. glEnable(GL_TEXTURE_GEN_T); // Texture unit 1
  297. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  298. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  299. // Slowly move over time.
  300. static F32 frame_time = 0.f;
  301. if (!LLPipeline::sFreezeTime)
  302. {
  303. frame_time = gFrameTimeSeconds;
  304. }
  305. F32 offset = fmod(frame_time * 2.f, 100.f);
  306. F32 tp0[4] = { 16.f / 256.f, 0.f, 0.f, offset * 0.01f };
  307. F32 tp1[4] = { 0.f, 16.f / 256.f, 0.f, offset * 0.01f };
  308. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0);
  309. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1);
  310. LLTexUnit* unit0 = gGL.getTexUnit(0);
  311. unit0->activate();
  312. glClearStencil(1);
  313. glClear(GL_STENCIL_BUFFER_BIT);
  314. glClearStencil(0);
  315. LLGLEnable gls_stencil(GL_STENCIL_TEST);
  316. glStencilOp(GL_KEEP, GL_REPLACE, GL_KEEP);
  317. glStencilFunc(GL_ALWAYS, 0, 0xFFFFFFFF);
  318. for (std::vector<LLFace*>::iterator iter = mDrawFace.begin(),
  319. end = mDrawFace.end();
  320. iter != end; ++iter)
  321. {
  322. LLFace* facep = *iter;
  323. if (!voskyp->isReflFace(facep))
  324. {
  325. LLViewerTexture* texp = facep->getTexture();
  326. if (texp && texp->hasGLTexture())
  327. {
  328. unit0->bind(texp);
  329. facep->renderIndexed();
  330. }
  331. }
  332. }
  333. // Now, disable texture coord generation on texture state 1
  334. unit1->activate();
  335. unit1->unbind(LLTexUnit::TT_TEXTURE);
  336. unit1->disable();
  337. glDisable(GL_TEXTURE_GEN_S); // Texture unit 1
  338. glDisable(GL_TEXTURE_GEN_T); // Texture unit 1
  339. unit2->activate();
  340. unit2->unbind(LLTexUnit::TT_TEXTURE);
  341. unit2->disable();
  342. glDisable(GL_TEXTURE_GEN_S); // Texture unit 2
  343. glDisable(GL_TEXTURE_GEN_T); // Texture unit 2
  344. // Disable texture coordinate and color arrays
  345. unit0->activate();
  346. unit0->unbind(LLTexUnit::TT_TEXTURE);
  347. if (voskyp->getCubeMap())
  348. {
  349. voskyp->getCubeMap()->enableTexture(0);
  350. voskyp->getCubeMap()->bind();
  351. gGL.matrixMode(LLRender::MM_TEXTURE);
  352. gGL.loadIdentity();
  353. LLMatrix4 camera_mat = gViewerCamera.getModelview();
  354. LLMatrix4 camera_rot(camera_mat.getMat3());
  355. camera_rot.invert();
  356. gGL.loadMatrix(camera_rot.getF32ptr());
  357. gGL.matrixMode(LLRender::MM_MODELVIEW);
  358. LLOverrideFaceColor overrid(this, 1.f, 1.f, 1.f, 0.5f * up_dot);
  359. for (std::vector<LLFace*>::iterator iter = mDrawFace.begin(),
  360. end = mDrawFace.end();
  361. iter != end; ++iter)
  362. {
  363. LLFace* face = *iter;
  364. if (!voskyp->isReflFace(face) && face->getGeomCount() > 0)
  365. {
  366. face->renderIndexed();
  367. }
  368. }
  369. voskyp->getCubeMap()->disableTexture();
  370. unit0->unbind(LLTexUnit::TT_TEXTURE);
  371. unit0->enable(LLTexUnit::TT_TEXTURE);
  372. gGL.matrixMode(LLRender::MM_TEXTURE);
  373. gGL.loadIdentity();
  374. gGL.matrixMode(LLRender::MM_MODELVIEW);
  375. }
  376. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  377. if (refl_facep)
  378. {
  379. glStencilFunc(GL_NOTEQUAL, 0, 0xFFFFFFFF);
  380. renderReflection(refl_facep);
  381. }
  382. stop_glerror();
  383. }
  384. // Only for use by the EE renderer
  385. //virtual
  386. S32 LLDrawPoolWater::getNumDeferredPasses()
  387. {
  388. return gUsePBRShaders ? 0 : getWaterPasses();
  389. }
  390. // Only for use by the EE renderer
  391. //virtual
  392. void LLDrawPoolWater::renderDeferred(S32 pass)
  393. {
  394. LL_FAST_TIMER(FTM_RENDER_WATER);
  395. if (!LLPipeline::RenderWaterReflectionType)
  396. {
  397. // Render opaque water without use of ALM
  398. render(pass);
  399. return;
  400. }
  401. sDeferredRender = true;
  402. renderWater();
  403. sDeferredRender = false;
  404. }
  405. // Only for use by the EE renderer
  406. // For low end hardware
  407. void LLDrawPoolWater::renderOpaqueLegacyWater()
  408. {
  409. LLVOSky* voskyp = gSky.mVOSkyp;
  410. if (!voskyp)
  411. {
  412. return;
  413. }
  414. LLGLSLShader* shader = NULL;
  415. if (LLPipeline::sUnderWaterRender)
  416. {
  417. shader = &gObjectSimpleNonIndexedTexGenWaterProgram;
  418. }
  419. else
  420. {
  421. shader = &gObjectSimpleNonIndexedTexGenProgram;
  422. }
  423. shader->bind();
  424. // Depth sorting and write to depth buffer since this is opaque, we should
  425. // see nothing behind the water. No blending because of no transparency.
  426. // And no face culling so that the underside of the water is also opaque.
  427. LLGLDepthTest gls_depth(GL_TRUE, GL_TRUE);
  428. LLGLDisable no_cull(GL_CULL_FACE);
  429. LLGLDisable no_blend(GL_BLEND);
  430. gPipeline.disableLights();
  431. // Activate the texture binding and bind one texture since all images will
  432. // have the same texture
  433. LLTexUnit* unit0 = gGL.getTexUnit(0);
  434. unit0->activate();
  435. unit0->enable(LLTexUnit::TT_TEXTURE);
  436. //MK
  437. if (gRLenabled && gRLInterface.mContainsCamTextures &&
  438. gRLInterface.mCamTexturesCustom.notNull())
  439. {
  440. unit0->bind(gRLInterface.mCamTexturesCustom);
  441. }
  442. else
  443. //mk
  444. {
  445. unit0->bind(mOpaqueWaterImagep);
  446. }
  447. // Automatically generate texture coords for water texture
  448. if (!shader)
  449. {
  450. glEnable(GL_TEXTURE_GEN_S); // Texture unit 0
  451. glEnable(GL_TEXTURE_GEN_T); // Texture unit 0
  452. glTexGenf(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  453. glTexGenf(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  454. }
  455. // Use the fact that we know all water faces are the same size to save some
  456. // computation
  457. // Slowly move texture coordinates over time so the watter appears to be
  458. // moving.
  459. F32 movement_period_secs = 50.f;
  460. // Slowly move over time.
  461. static F32 frame_time = 0.f;
  462. if (!LLPipeline::sFreezeTime)
  463. {
  464. frame_time = gFrameTimeSeconds;
  465. }
  466. F32 offset = fmod(frame_time, movement_period_secs);
  467. if (movement_period_secs != 0)
  468. {
  469. offset /= movement_period_secs;
  470. }
  471. else
  472. {
  473. offset = 0;
  474. }
  475. F32 tp0[4] = { 16.f / 256.f, 0.f, 0.f, offset };
  476. F32 tp1[4] = { 0.f, 16.f / 256.f, 0.f, offset };
  477. if (!shader)
  478. {
  479. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0);
  480. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1);
  481. }
  482. else
  483. {
  484. shader->uniform4fv(LLShaderMgr::OBJECT_PLANE_S, 1, tp0);
  485. shader->uniform4fv(LLShaderMgr::OBJECT_PLANE_T, 1, tp1);
  486. }
  487. gGL.diffuseColor3f(1.f, 1.f, 1.f);
  488. for (std::vector<LLFace*>::iterator iter = mDrawFace.begin(),
  489. end = mDrawFace.end();
  490. iter != end; ++iter)
  491. {
  492. LLFace* face = *iter;
  493. if (!voskyp->isReflFace(face))
  494. {
  495. face->renderIndexed();
  496. }
  497. }
  498. if (!shader)
  499. {
  500. // Reset the settings back to expected values
  501. glDisable(GL_TEXTURE_GEN_S); // Texture unit 0
  502. glDisable(GL_TEXTURE_GEN_T); // Texture unit 0
  503. }
  504. unit0->unbind(LLTexUnit::TT_TEXTURE);
  505. stop_glerror();
  506. }
  507. // Only for use by the EE renderer
  508. void LLDrawPoolWater::renderReflection(LLFace* facep)
  509. {
  510. LLVOSky* voskyp = gSky.mVOSkyp;
  511. if (!voskyp || !facep->getGeomCount())
  512. {
  513. return;
  514. }
  515. S8 dr = voskyp->getDrawRefl();
  516. if (dr < 0)
  517. {
  518. return;
  519. }
  520. gGL.getTexUnit(0)->bind(dr == 0 ? voskyp->getSunTex()
  521. : voskyp->getMoonTex());
  522. LLOverrideFaceColor override_color(this, facep->getFaceColor().mV);
  523. facep->renderIndexed();
  524. }
  525. // Only for use by the EE renderer
  526. void LLDrawPoolWater::renderWater()
  527. {
  528. LLVOSky* voskyp = gSky.mVOSkyp;
  529. if (!voskyp)
  530. {
  531. return;
  532. }
  533. static LLCachedControl<bool> mip_normal(gSavedSettings,
  534. "RenderWaterMipNormal");
  535. LLTexUnit::eTextureFilterOptions mode =
  536. mip_normal ? LLTexUnit::TFO_ANISOTROPIC : LLTexUnit::TFO_POINT;
  537. if (mWaterNormp[0])
  538. {
  539. mWaterNormp[0]->setFilteringOption(mode);
  540. }
  541. if (mWaterNormp[1])
  542. {
  543. mWaterNormp[1]->setFilteringOption(mode);
  544. }
  545. if (!sDeferredRender)
  546. {
  547. gGL.setColorMask(true, true);
  548. }
  549. LLGLDisable blend(GL_BLEND);
  550. LLGLSLShader* shaderp;
  551. LLGLSLShader* edge_shaderp = NULL;
  552. if (gPipeline.mEyeAboveWater < 0.f)
  553. {
  554. if (sDeferredRender)
  555. {
  556. shaderp = &gDeferredUnderWaterProgram;
  557. }
  558. else
  559. {
  560. shaderp = &gUnderWaterProgram;
  561. }
  562. }
  563. else if (sDeferredRender)
  564. {
  565. shaderp = &gDeferredWaterProgram;
  566. }
  567. else
  568. {
  569. shaderp = &gWaterProgram;
  570. edge_shaderp = &gWaterEdgeProgram;
  571. }
  572. shadeWater(shaderp, false);
  573. shadeWater(edge_shaderp ? edge_shaderp : shaderp, true);
  574. LLTexUnit* unit0 = gGL.getTexUnit(0);
  575. unit0->activate();
  576. unit0->enable(LLTexUnit::TT_TEXTURE);
  577. if (!sDeferredRender)
  578. {
  579. gGL.setColorMask(true, false);
  580. }
  581. stop_glerror();
  582. }
  583. // Only for use by the EE renderer
  584. void LLDrawPoolWater::shadeWater(LLGLSLShader* shaderp, bool edge)
  585. {
  586. const LLSettingsWater::ptr_t& waterp = gEnvironment.getCurrentWater();
  587. shaderp->bind();
  588. if (sDeferredRender &&
  589. shaderp->getUniformLocation(LLShaderMgr::DEFERRED_NORM_MATRIX) >= 0)
  590. {
  591. LLMatrix4a norm_mat = gGLModelView;
  592. norm_mat.invert();
  593. norm_mat.transpose();
  594. shaderp->uniformMatrix4fv(LLShaderMgr::DEFERRED_NORM_MATRIX, 1,
  595. GL_FALSE, norm_mat.getF32ptr());
  596. }
  597. shaderp->uniform4fv(LLShaderMgr::SPECULAR_COLOR, 1, mLightColor.mV);
  598. if (!LLPipeline::sFreezeTime)
  599. {
  600. sTime = (F32)LLFrameTimer::getElapsedSeconds() * 0.5f;
  601. }
  602. S32 reftex = shaderp->enableTexture(LLShaderMgr::WATER_REFTEX);
  603. if (reftex > -1)
  604. {
  605. LLTexUnit* unit = gGL.getTexUnit(reftex);
  606. unit->activate();
  607. unit->bind(&gPipeline.mWaterRef);
  608. gGL.getTexUnit(0)->activate();
  609. }
  610. // Bind normal map
  611. S32 bump_tex = shaderp->enableTexture(LLShaderMgr::BUMP_MAP);
  612. LLTexUnit* unitbump = gGL.getTexUnit(bump_tex);
  613. unitbump->unbind(LLTexUnit::TT_TEXTURE);
  614. S32 bump_tex2 = shaderp->enableTexture(LLShaderMgr::BUMP_MAP2);
  615. LLTexUnit* unitbump2 = bump_tex2 > -1 ? gGL.getTexUnit(bump_tex2) : NULL;
  616. if (unitbump2)
  617. {
  618. unitbump2->unbind(LLTexUnit::TT_TEXTURE);
  619. }
  620. LLViewerTexture* tex_a = mWaterNormp[0];
  621. LLViewerTexture* tex_b = mWaterNormp[1];
  622. F32 blend_factor = waterp->getBlendFactor();
  623. if (tex_a && (!tex_b || tex_a == tex_b))
  624. {
  625. unitbump->bind(tex_a);
  626. blend_factor = 0.f; // Only one tex provided, no blending
  627. }
  628. else if (tex_b && !tex_a)
  629. {
  630. unitbump->bind(tex_b);
  631. blend_factor = 0.f; // Only one tex provided, no blending
  632. }
  633. else if (tex_a != tex_b)
  634. {
  635. unitbump->bind(tex_a);
  636. if (unitbump2)
  637. {
  638. unitbump2->bind(tex_b);
  639. }
  640. }
  641. // Bind reflection texture from render target
  642. S32 screentex = shaderp->enableTexture(LLShaderMgr::WATER_SCREENTEX);
  643. // NOTE: there is actually no such uniform in the current water shaders, so
  644. // diff_tex is set to -1...
  645. S32 diff_tex = shaderp->enableTexture(LLShaderMgr::DIFFUSE_MAP);
  646. // Set uniforms for water rendering
  647. F32 screen_res[] =
  648. {
  649. 1.f / gGLViewport[2],
  650. 1.f / gGLViewport[3]
  651. };
  652. shaderp->uniform2fv(LLShaderMgr::DEFERRED_SCREEN_RES, 1, screen_res);
  653. shaderp->uniform1f(LLShaderMgr::BLEND_FACTOR, blend_factor);
  654. LLColor4 fog_color = sWaterFogColor;
  655. F32 eye_level = gPipeline.mEyeAboveWater;
  656. F32 fog_density = waterp->getModifiedWaterFogDensity(eye_level < 0.f);
  657. if (screentex > -1)
  658. {
  659. shaderp->uniform1f(LLShaderMgr::WATER_FOGDENSITY, fog_density);
  660. gGL.getTexUnit(screentex)->bind(&gPipeline.mWaterDis);
  661. }
  662. if (mShaderLevel == 1)
  663. {
  664. fog_color.mV[VW] = logf(fog_density) / F_LN2;
  665. }
  666. shaderp->uniform4fv(LLShaderMgr::WATER_FOGCOLOR, 1, fog_color.mV);
  667. shaderp->uniform1f(LLShaderMgr::WATER_WATERHEIGHT, eye_level);
  668. shaderp->uniform1f(LLShaderMgr::WATER_TIME, sTime);
  669. const LLVector3& camera_origin = gViewerCamera.getOrigin();
  670. shaderp->uniform3fv(LLShaderMgr::WATER_EYEVEC, 1, camera_origin.mV);
  671. shaderp->uniform3fv(LLShaderMgr::WATER_SPECULAR, 1, mLightDiffuse.mV);
  672. shaderp->uniform2fv(LLShaderMgr::WATER_WAVE_DIR1, 1,
  673. waterp->getWave1Dir().mV);
  674. shaderp->uniform2fv(LLShaderMgr::WATER_WAVE_DIR2, 1,
  675. waterp->getWave2Dir().mV);
  676. shaderp->uniform3fv(LLShaderMgr::WATER_LIGHT_DIR, 1, mLightDir.mV);
  677. shaderp->uniform3fv(LLShaderMgr::WATER_NORM_SCALE, 1,
  678. waterp->getNormalScale().mV);
  679. shaderp->uniform1f(LLShaderMgr::WATER_FRESNEL_SCALE,
  680. waterp->getFresnelScale());
  681. shaderp->uniform1f(LLShaderMgr::WATER_FRESNEL_OFFSET,
  682. waterp->getFresnelOffset());
  683. shaderp->uniform1f(LLShaderMgr::WATER_BLUR_MULTIPLIER,
  684. waterp->getBlurMultiplier());
  685. F32 sun_angle = llmax(0.f, mLightDir.mV[1]);
  686. shaderp->uniform1f(LLShaderMgr::WATER_SUN_ANGLE, 0.1f + 0.2f * sun_angle);
  687. shaderp->uniform1i(LLShaderMgr::SUN_UP_FACTOR, gPipeline.mIsSunUp ? 1 : 0);
  688. shaderp->uniform4fv(LLShaderMgr::LIGHTNORM, 1,
  689. gEnvironment.getClampedLightNorm().mV);
  690. shaderp->uniform3fv(LLShaderMgr::WL_CAMPOSLOCAL, 1, camera_origin.mV);
  691. if (eye_level < 0.f)
  692. {
  693. shaderp->uniform1f(LLShaderMgr::WATER_REFSCALE,
  694. waterp->getScaleBelow());
  695. }
  696. else
  697. {
  698. shaderp->uniform1f(LLShaderMgr::WATER_REFSCALE,
  699. waterp->getScaleAbove());
  700. }
  701. LLGLDisable cullface(GL_CULL_FACE);
  702. LLTexUnit* unitp = diff_tex > -1 ? gGL.getTexUnit(diff_tex) : NULL;
  703. for (U32 i = 0, count = mDrawFace.size(); i < count; ++i)
  704. {
  705. LLFace* facep = mDrawFace[i];
  706. if (!facep) continue;
  707. LLVOWater* vowaterp = (LLVOWater*)facep->getViewerObject();
  708. if (!vowaterp) continue;
  709. if (unitp)
  710. {
  711. unitp->bind(facep->getTexture());
  712. }
  713. bool edge_patch = vowaterp->getIsEdgePatch();
  714. if (edge)
  715. {
  716. if (edge_patch)
  717. {
  718. facep->renderIndexed();
  719. }
  720. }
  721. else if (!edge_patch)
  722. {
  723. sNeedsReflectionUpdate = true;
  724. facep->renderIndexed();
  725. }
  726. }
  727. unitbump->unbind(LLTexUnit::TT_TEXTURE);
  728. if (unitbump2)
  729. {
  730. unitbump2->unbind(LLTexUnit::TT_TEXTURE);
  731. }
  732. shaderp->disableTexture(LLShaderMgr::ENVIRONMENT_MAP,
  733. LLTexUnit::TT_CUBE_MAP);
  734. shaderp->disableTexture(LLShaderMgr::WATER_SCREENTEX);
  735. shaderp->disableTexture(LLShaderMgr::BUMP_MAP);
  736. shaderp->disableTexture(LLShaderMgr::DIFFUSE_MAP);
  737. shaderp->disableTexture(LLShaderMgr::WATER_REFTEX);
  738. shaderp->unbind();
  739. stop_glerror();
  740. }
  741. //virtual
  742. S32 LLDrawPoolWater::getNumPostDeferredPasses()
  743. {
  744. return gUsePBRShaders ? getWaterPasses() : 0;
  745. }
  746. // Only for use by the PBR renderer
  747. //virtual
  748. void LLDrawPoolWater::renderPostDeferred(S32)
  749. {
  750. LL_FAST_TIMER(FTM_RENDER_WATER);
  751. static LLCachedControl<bool> mip_normal(gSavedSettings,
  752. "RenderWaterMipNormal");
  753. LLTexUnit::eTextureFilterOptions mode =
  754. mip_normal ? LLTexUnit::TFO_ANISOTROPIC : LLTexUnit::TFO_POINT;
  755. if (mWaterNormp[0])
  756. {
  757. mWaterNormp[0]->setFilteringOption(mode);
  758. }
  759. if (mWaterNormp[1])
  760. {
  761. mWaterNormp[1]->setFilteringOption(mode);
  762. }
  763. LLGLDisable blend(GL_BLEND);
  764. gGL.setColorMask(true, true);
  765. if (!LLPipeline::sFreezeTime)
  766. {
  767. sTime = (F32)LLFrameTimer::getElapsedSeconds() * 0.5f;
  768. }
  769. // Two passes, first with standard water shader bound, second with edge
  770. // water shader bound.
  771. for (U32 edge = 0; edge < 2; ++edge)
  772. {
  773. LLGLSLShader* shaderp;
  774. if (gPipeline.mEyeAboveWater < 0.f)
  775. {
  776. shaderp = &gUnderWaterProgram;
  777. }
  778. else if (edge)
  779. {
  780. shaderp = &gWaterEdgeProgram;
  781. }
  782. else
  783. {
  784. shaderp = &gWaterProgram;
  785. }
  786. shadeWaterPBR(shaderp, edge);
  787. }
  788. LLTexUnit* unit0 = gGL.getTexUnit(0);
  789. unit0->activate();
  790. unit0->enable(LLTexUnit::TT_TEXTURE);
  791. gGL.setColorMask(true, false);
  792. }
  793. // Only for use by the PBR renderer
  794. void LLDrawPoolWater::shadeWaterPBR(LLGLSLShader* shaderp, bool edge)
  795. {
  796. gPipeline.bindDeferredShader(*shaderp, NULL, &gPipeline.mWaterDis);
  797. const LLSettingsWater::ptr_t& waterp = gEnvironment.getCurrentWater();
  798. // Bind normal map
  799. S32 bump_tex = shaderp->enableTexture(LLShaderMgr::BUMP_MAP);
  800. LLTexUnit* unitbump = gGL.getTexUnit(bump_tex);
  801. unitbump->unbind(LLTexUnit::TT_TEXTURE);
  802. S32 bump_tex2 = shaderp->enableTexture(LLShaderMgr::BUMP_MAP2);
  803. LLTexUnit* unitbump2 = bump_tex2 > -1 ? gGL.getTexUnit(bump_tex2) : NULL;
  804. if (unitbump2)
  805. {
  806. unitbump2->unbind(LLTexUnit::TT_TEXTURE);
  807. }
  808. LLViewerTexture* tex_a = mWaterNormp[0];
  809. LLViewerTexture* tex_b = mWaterNormp[1];
  810. F32 blend_factor = waterp->getBlendFactor();
  811. if (tex_a && (!tex_b || tex_a == tex_b))
  812. {
  813. unitbump->bind(tex_a);
  814. blend_factor = 0.f; // Only one tex provided, no blending
  815. }
  816. else if (tex_b && !tex_a)
  817. {
  818. unitbump->bind(tex_b);
  819. blend_factor = 0.f; // Only one tex provided, no blending
  820. }
  821. else if (tex_a != tex_b)
  822. {
  823. unitbump->bind(tex_a);
  824. if (unitbump2)
  825. {
  826. unitbump2->bind(tex_b);
  827. }
  828. }
  829. // Bind reflection texture from render target
  830. S32 screentex = shaderp->enableTexture(LLShaderMgr::WATER_SCREENTEX);
  831. S32 diff_tex = shaderp->enableTexture(LLShaderMgr::DIFFUSE_MAP);
  832. // Set uniforms for water rendering
  833. F32 screen_res[] =
  834. {
  835. 1.f / gGLViewport[2],
  836. 1.f / gGLViewport[3]
  837. };
  838. shaderp->uniform2fv(LLShaderMgr::DEFERRED_SCREEN_RES, 1, screen_res);
  839. shaderp->uniform1f(LLShaderMgr::BLEND_FACTOR, blend_factor);
  840. LLColor4 fog_color = sWaterFogColor;
  841. F32 eye_level = gPipeline.mEyeAboveWater;
  842. F32 fog_density = waterp->getModifiedWaterFogDensity(eye_level < 0.f);
  843. if (screentex > -1)
  844. {
  845. shaderp->uniform1f(LLShaderMgr::WATER_FOGDENSITY, fog_density);
  846. gGL.getTexUnit(screentex)->bind(&gPipeline.mWaterDis);
  847. }
  848. if (mShaderLevel == 1)
  849. {
  850. fog_color.mV[VW] = logf(fog_density) / F_LN2;
  851. }
  852. shaderp->uniform1f(LLShaderMgr::WATER_WATERHEIGHT, eye_level);
  853. shaderp->uniform1f(LLShaderMgr::WATER_TIME, sTime);
  854. const LLVector3& camera_origin = gViewerCamera.getOrigin();
  855. shaderp->uniform3fv(LLShaderMgr::WATER_EYEVEC, 1, camera_origin.mV);
  856. shaderp->uniform4fv(LLShaderMgr::SPECULAR_COLOR, 1, mLightColor.mV);
  857. shaderp->uniform4fv(LLShaderMgr::WATER_FOGCOLOR, 1, fog_color.mV);
  858. shaderp->uniform3fv(LLShaderMgr::WATER_FOGCOLOR_LINEAR, 1,
  859. linearColor3(fog_color).mV);
  860. shaderp->uniform3fv(LLShaderMgr::WATER_SPECULAR, 1, mLightDiffuse.mV);
  861. shaderp->uniform2fv(LLShaderMgr::WATER_WAVE_DIR1, 1,
  862. waterp->getWave1Dir().mV);
  863. shaderp->uniform2fv(LLShaderMgr::WATER_WAVE_DIR2, 1,
  864. waterp->getWave2Dir().mV);
  865. shaderp->uniform3fv(LLShaderMgr::WATER_LIGHT_DIR, 1, mLightDir.mV);
  866. shaderp->uniform3fv(LLShaderMgr::WATER_NORM_SCALE, 1,
  867. waterp->getNormalScale().mV);
  868. shaderp->uniform1f(LLShaderMgr::WATER_FRESNEL_SCALE,
  869. waterp->getFresnelScale());
  870. shaderp->uniform1f(LLShaderMgr::WATER_FRESNEL_OFFSET,
  871. waterp->getFresnelOffset());
  872. shaderp->uniform1f(LLShaderMgr::WATER_BLUR_MULTIPLIER,
  873. waterp->getBlurMultiplier());
  874. shaderp->uniform1i(LLShaderMgr::SUN_UP_FACTOR, gPipeline.mIsSunUp ? 1 : 0);
  875. #if 0 // No more in actual use in current PBR shaders. HB
  876. F32 sun_angle = llmax(0.f, mLightDir.mV[1]);
  877. shaderp->uniform1f(LLShaderMgr::WATER_SUN_ANGLE, 0.1f + 0.2f * sun_angle);
  878. shaderp->uniform1f(LLShaderMgr::WATER_SCALED_ANGLE, 1.f - sun_angle);
  879. #endif
  880. shaderp->uniform3fv(LLShaderMgr::LIGHTNORM, 1,
  881. gEnvironment.getClampedLightNorm().mV);
  882. shaderp->uniform3fv(LLShaderMgr::WL_CAMPOSLOCAL, 1, camera_origin.mV);
  883. if (gPipeline.mEyeAboveWater < 0.f)
  884. {
  885. shaderp->uniform1f(LLShaderMgr::WATER_REFSCALE,
  886. waterp->getScaleBelow());
  887. }
  888. else
  889. {
  890. shaderp->uniform1f(LLShaderMgr::WATER_REFSCALE,
  891. waterp->getScaleAbove());
  892. }
  893. LLGLDisable cullface(GL_CULL_FACE);
  894. LLTexUnit* unitp = diff_tex > -1 ? gGL.getTexUnit(diff_tex) : NULL;
  895. for (U32 i = 0, count = mDrawFace.size(); i < count; ++i)
  896. {
  897. LLFace* facep = mDrawFace[i];
  898. if (!facep) continue;
  899. LLVOWater* vowaterp = (LLVOWater*)facep->getViewerObject();
  900. if (!vowaterp) continue;
  901. if (unitp)
  902. {
  903. unitp->bind(facep->getTexture());
  904. }
  905. bool edge_patch = vowaterp->getIsEdgePatch();
  906. if (edge)
  907. {
  908. if (edge_patch)
  909. {
  910. facep->renderIndexed();
  911. }
  912. }
  913. else if (!edge_patch)
  914. {
  915. sNeedsReflectionUpdate = true;
  916. facep->renderIndexed();
  917. }
  918. }
  919. shaderp->disableTexture(LLShaderMgr::ENVIRONMENT_MAP,
  920. LLTexUnit::TT_CUBE_MAP);
  921. shaderp->disableTexture(LLShaderMgr::WATER_SCREENTEX);
  922. shaderp->disableTexture(LLShaderMgr::BUMP_MAP);
  923. shaderp->disableTexture(LLShaderMgr::DIFFUSE_MAP);
  924. shaderp->disableTexture(LLShaderMgr::WATER_REFTEX);
  925. gPipeline.unbindDeferredShader(*shaderp);
  926. unitbump->unbind(LLTexUnit::TT_TEXTURE);
  927. if (unitbump2)
  928. {
  929. unitbump2->unbind(LLTexUnit::TT_TEXTURE);
  930. }
  931. stop_glerror();
  932. }