PageRenderTime 115ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/indra/newview/lldrawpoolterrain.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 940 lines | 609 code | 182 blank | 149 comment | 33 complexity | 14b2b08fbe50e335c7dce181b9578d58 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lldrawpoolterrain.cpp
  3. * @brief LLDrawPoolTerrain class implementation
  4. *
  5. * $LicenseInfo:firstyear=2002&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 "lldrawpoolterrain.h"
  28. #include "llfasttimer.h"
  29. #include "llagent.h"
  30. #include "llviewercontrol.h"
  31. #include "lldrawable.h"
  32. #include "llface.h"
  33. #include "llsky.h"
  34. #include "llsurface.h"
  35. #include "llsurfacepatch.h"
  36. #include "llviewerregion.h"
  37. #include "llvlcomposition.h"
  38. #include "llviewerparcelmgr.h" // for gRenderParcelOwnership
  39. #include "llviewerparceloverlay.h"
  40. #include "llvosurfacepatch.h"
  41. #include "llviewercamera.h"
  42. #include "llviewertexturelist.h" // To get alpha gradients
  43. #include "llworld.h"
  44. #include "pipeline.h"
  45. #include "llviewershadermgr.h"
  46. #include "llrender.h"
  47. const F32 DETAIL_SCALE = 1.f/16.f;
  48. int DebugDetailMap = 0;
  49. S32 LLDrawPoolTerrain::sDetailMode = 1;
  50. F32 LLDrawPoolTerrain::sDetailScale = DETAIL_SCALE;
  51. static LLGLSLShader* sShader = NULL;
  52. static LLFastTimer::DeclareTimer FTM_SHADOW_TERRAIN("Terrain Shadow");
  53. LLDrawPoolTerrain::LLDrawPoolTerrain(LLViewerTexture *texturep) :
  54. LLFacePool(POOL_TERRAIN),
  55. mTexturep(texturep)
  56. {
  57. U32 format = GL_ALPHA8;
  58. U32 int_format = GL_ALPHA;
  59. // Hack!
  60. sDetailScale = 1.f/gSavedSettings.getF32("RenderTerrainScale");
  61. sDetailMode = gSavedSettings.getS32("RenderTerrainDetail");
  62. mAlphaRampImagep = LLViewerTextureManager::getFetchedTextureFromFile("alpha_gradient.tga",
  63. TRUE, LLViewerTexture::BOOST_UI,
  64. LLViewerTexture::FETCHED_TEXTURE,
  65. format, int_format,
  66. LLUUID("e97cf410-8e61-7005-ec06-629eba4cd1fb"));
  67. //gGL.getTexUnit(0)->bind(mAlphaRampImagep.get());
  68. mAlphaRampImagep->setAddressMode(LLTexUnit::TAM_CLAMP);
  69. m2DAlphaRampImagep = LLViewerTextureManager::getFetchedTextureFromFile("alpha_gradient_2d.j2c",
  70. TRUE, LLViewerTexture::BOOST_UI,
  71. LLViewerTexture::FETCHED_TEXTURE,
  72. format, int_format,
  73. LLUUID("38b86f85-2575-52a9-a531-23108d8da837"));
  74. //gGL.getTexUnit(0)->bind(m2DAlphaRampImagep.get());
  75. m2DAlphaRampImagep->setAddressMode(LLTexUnit::TAM_CLAMP);
  76. mTexturep->setBoostLevel(LLViewerTexture::BOOST_TERRAIN);
  77. //gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  78. }
  79. LLDrawPoolTerrain::~LLDrawPoolTerrain()
  80. {
  81. llassert( gPipeline.findPool( getType(), getTexture() ) == NULL );
  82. }
  83. LLDrawPool *LLDrawPoolTerrain::instancePool()
  84. {
  85. return new LLDrawPoolTerrain(mTexturep);
  86. }
  87. U32 LLDrawPoolTerrain::getVertexDataMask()
  88. {
  89. if (LLPipeline::sShadowRender)
  90. {
  91. return LLVertexBuffer::MAP_VERTEX;
  92. }
  93. else if (LLGLSLShader::sCurBoundShaderPtr)
  94. {
  95. return VERTEX_DATA_MASK & ~(LLVertexBuffer::MAP_TEXCOORD2 | LLVertexBuffer::MAP_TEXCOORD3);
  96. }
  97. else
  98. {
  99. return VERTEX_DATA_MASK;
  100. }
  101. }
  102. void LLDrawPoolTerrain::prerender()
  103. {
  104. mVertexShaderLevel = LLViewerShaderMgr::instance()->getVertexShaderLevel(LLViewerShaderMgr::SHADER_ENVIRONMENT);
  105. sDetailMode = gSavedSettings.getS32("RenderTerrainDetail");
  106. }
  107. void LLDrawPoolTerrain::beginRenderPass( S32 pass )
  108. {
  109. LLFastTimer t(FTM_RENDER_TERRAIN);
  110. LLFacePool::beginRenderPass(pass);
  111. sShader = LLPipeline::sUnderWaterRender ?
  112. &gTerrainWaterProgram :
  113. &gTerrainProgram;
  114. if (mVertexShaderLevel > 1 && sShader->mShaderLevel > 0)
  115. {
  116. sShader->bind();
  117. }
  118. }
  119. void LLDrawPoolTerrain::endRenderPass( S32 pass )
  120. {
  121. LLFastTimer t(FTM_RENDER_TERRAIN);
  122. //LLFacePool::endRenderPass(pass);
  123. if (mVertexShaderLevel > 1 && sShader->mShaderLevel > 0) {
  124. sShader->unbind();
  125. }
  126. }
  127. //static
  128. S32 LLDrawPoolTerrain::getDetailMode()
  129. {
  130. return sDetailMode;
  131. }
  132. void LLDrawPoolTerrain::render(S32 pass)
  133. {
  134. LLFastTimer t(FTM_RENDER_TERRAIN);
  135. if (mDrawFace.empty())
  136. {
  137. return;
  138. }
  139. // Hack! Get the region that this draw pool is rendering from!
  140. LLViewerRegion *regionp = mDrawFace[0]->getDrawable()->getVObj()->getRegion();
  141. LLVLComposition *compp = regionp->getComposition();
  142. for (S32 i = 0; i < 4; i++)
  143. {
  144. compp->mDetailTextures[i]->setBoostLevel(LLViewerTexture::BOOST_TERRAIN);
  145. compp->mDetailTextures[i]->addTextureStats(1024.f*1024.f); // assume large pixel area
  146. }
  147. LLOverrideFaceColor override(this, 1.f, 1.f, 1.f, 1.f);
  148. if (!gGLManager.mHasMultitexture)
  149. {
  150. // No multitexture, render simple land.
  151. renderSimple(); // Render without multitexture
  152. return;
  153. }
  154. // Render simplified land if video card can't do sufficient multitexturing
  155. if (!gGLManager.mHasARBEnvCombine || (gGLManager.mNumTextureUnits < 2))
  156. {
  157. renderSimple(); // Render without multitexture
  158. return;
  159. }
  160. LLGLSPipeline gls;
  161. if (mVertexShaderLevel > 1 && sShader->mShaderLevel > 0)
  162. {
  163. gPipeline.enableLightsDynamic();
  164. renderFullShader();
  165. }
  166. else
  167. {
  168. gPipeline.enableLightsStatic();
  169. if (sDetailMode == 0)
  170. {
  171. renderSimple();
  172. }
  173. else if (gGLManager.mNumTextureUnits < 4)
  174. {
  175. renderFull2TU();
  176. }
  177. else
  178. {
  179. renderFull4TU();
  180. }
  181. }
  182. // Special-case for land ownership feedback
  183. if (gSavedSettings.getBOOL("ShowParcelOwners"))
  184. {
  185. if (mVertexShaderLevel > 1)
  186. { //use fullbright shader for highlighting
  187. LLGLSLShader* old_shader = sShader;
  188. sShader->unbind();
  189. sShader = &gHighlightProgram;
  190. sShader->bind();
  191. gGL.diffuseColor4f(1,1,1,1);
  192. LLGLEnable polyOffset(GL_POLYGON_OFFSET_FILL);
  193. glPolygonOffset(-1.0f, -1.0f);
  194. renderOwnership();
  195. sShader = old_shader;
  196. sShader->bind();
  197. }
  198. else
  199. {
  200. gPipeline.disableLights();
  201. renderOwnership();
  202. }
  203. }
  204. }
  205. void LLDrawPoolTerrain::beginDeferredPass(S32 pass)
  206. {
  207. LLFastTimer t(FTM_RENDER_TERRAIN);
  208. LLFacePool::beginRenderPass(pass);
  209. sShader = &gDeferredTerrainProgram;
  210. sShader->bind();
  211. }
  212. void LLDrawPoolTerrain::endDeferredPass(S32 pass)
  213. {
  214. LLFastTimer t(FTM_RENDER_TERRAIN);
  215. LLFacePool::endRenderPass(pass);
  216. sShader->unbind();
  217. }
  218. void LLDrawPoolTerrain::renderDeferred(S32 pass)
  219. {
  220. LLFastTimer t(FTM_RENDER_TERRAIN);
  221. if (mDrawFace.empty())
  222. {
  223. return;
  224. }
  225. renderFullShader();
  226. }
  227. void LLDrawPoolTerrain::beginShadowPass(S32 pass)
  228. {
  229. LLFastTimer t(FTM_SHADOW_TERRAIN);
  230. LLFacePool::beginRenderPass(pass);
  231. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  232. gDeferredShadowProgram.bind();
  233. }
  234. void LLDrawPoolTerrain::endShadowPass(S32 pass)
  235. {
  236. LLFastTimer t(FTM_SHADOW_TERRAIN);
  237. LLFacePool::endRenderPass(pass);
  238. gDeferredShadowProgram.unbind();
  239. }
  240. void LLDrawPoolTerrain::renderShadow(S32 pass)
  241. {
  242. LLFastTimer t(FTM_SHADOW_TERRAIN);
  243. if (mDrawFace.empty())
  244. {
  245. return;
  246. }
  247. //LLGLEnable offset(GL_POLYGON_OFFSET);
  248. //glCullFace(GL_FRONT);
  249. drawLoop();
  250. //glCullFace(GL_BACK);
  251. }
  252. void LLDrawPoolTerrain::renderFullShader()
  253. {
  254. // Hack! Get the region that this draw pool is rendering from!
  255. LLViewerRegion *regionp = mDrawFace[0]->getDrawable()->getVObj()->getRegion();
  256. LLVLComposition *compp = regionp->getComposition();
  257. LLViewerTexture *detail_texture0p = compp->mDetailTextures[0];
  258. LLViewerTexture *detail_texture1p = compp->mDetailTextures[1];
  259. LLViewerTexture *detail_texture2p = compp->mDetailTextures[2];
  260. LLViewerTexture *detail_texture3p = compp->mDetailTextures[3];
  261. LLVector3d region_origin_global = gAgent.getRegion()->getOriginGlobal();
  262. F32 offset_x = (F32)fmod(region_origin_global.mdV[VX], 1.0/(F64)sDetailScale)*sDetailScale;
  263. F32 offset_y = (F32)fmod(region_origin_global.mdV[VY], 1.0/(F64)sDetailScale)*sDetailScale;
  264. LLVector4 tp0, tp1;
  265. tp0.setVec(sDetailScale, 0.0f, 0.0f, offset_x);
  266. tp1.setVec(0.0f, sDetailScale, 0.0f, offset_y);
  267. //
  268. // detail texture 0
  269. //
  270. S32 detail0 = sShader->enableTexture(LLViewerShaderMgr::TERRAIN_DETAIL0);
  271. gGL.getTexUnit(detail0)->bind(detail_texture0p);
  272. gGL.getTexUnit(0)->activate();
  273. LLGLSLShader* shader = LLGLSLShader::sCurBoundShaderPtr;
  274. llassert(shader);
  275. shader->uniform4fv("object_plane_s", 1, tp0.mV);
  276. shader->uniform4fv("object_plane_t", 1, tp1.mV);
  277. gGL.matrixMode(LLRender::MM_TEXTURE);
  278. gGL.loadIdentity();
  279. gGL.matrixMode(LLRender::MM_MODELVIEW);
  280. //
  281. // detail texture 1
  282. //
  283. S32 detail1 = sShader->enableTexture(LLViewerShaderMgr::TERRAIN_DETAIL1);
  284. gGL.getTexUnit(detail1)->bind(detail_texture1p);
  285. /// ALPHA TEXTURE COORDS 0:
  286. gGL.getTexUnit(1)->activate();
  287. gGL.matrixMode(LLRender::MM_TEXTURE);
  288. gGL.loadIdentity();
  289. gGL.matrixMode(LLRender::MM_MODELVIEW);
  290. // detail texture 2
  291. //
  292. S32 detail2 = sShader->enableTexture(LLViewerShaderMgr::TERRAIN_DETAIL2);
  293. gGL.getTexUnit(detail2)->bind(detail_texture2p);
  294. gGL.getTexUnit(2)->activate();
  295. /// ALPHA TEXTURE COORDS 1:
  296. gGL.matrixMode(LLRender::MM_TEXTURE);
  297. gGL.loadIdentity();
  298. gGL.translatef(-2.f, 0.f, 0.f);
  299. gGL.matrixMode(LLRender::MM_MODELVIEW);
  300. //
  301. // detail texture 3
  302. //
  303. S32 detail3 = sShader->enableTexture(LLViewerShaderMgr::TERRAIN_DETAIL3);
  304. gGL.getTexUnit(detail3)->bind(detail_texture3p);
  305. /// ALPHA TEXTURE COORDS 2:
  306. gGL.getTexUnit(3)->activate();
  307. gGL.matrixMode(LLRender::MM_TEXTURE);
  308. gGL.loadIdentity();
  309. gGL.translatef(-1.f, 0.f, 0.f);
  310. gGL.matrixMode(LLRender::MM_MODELVIEW);
  311. //
  312. // Alpha Ramp
  313. //
  314. S32 alpha_ramp = sShader->enableTexture(LLViewerShaderMgr::TERRAIN_ALPHARAMP);
  315. gGL.getTexUnit(alpha_ramp)->bind(m2DAlphaRampImagep);
  316. // GL_BLEND disabled by default
  317. drawLoop();
  318. // Disable multitexture
  319. sShader->disableTexture(LLViewerShaderMgr::TERRAIN_ALPHARAMP);
  320. sShader->disableTexture(LLViewerShaderMgr::TERRAIN_DETAIL0);
  321. sShader->disableTexture(LLViewerShaderMgr::TERRAIN_DETAIL1);
  322. sShader->disableTexture(LLViewerShaderMgr::TERRAIN_DETAIL2);
  323. sShader->disableTexture(LLViewerShaderMgr::TERRAIN_DETAIL3);
  324. gGL.getTexUnit(alpha_ramp)->unbind(LLTexUnit::TT_TEXTURE);
  325. gGL.getTexUnit(4)->disable();
  326. gGL.getTexUnit(4)->activate();
  327. gGL.getTexUnit(detail3)->unbind(LLTexUnit::TT_TEXTURE);
  328. gGL.getTexUnit(3)->disable();
  329. gGL.getTexUnit(3)->activate();
  330. gGL.matrixMode(LLRender::MM_TEXTURE);
  331. gGL.loadIdentity();
  332. gGL.matrixMode(LLRender::MM_MODELVIEW);
  333. gGL.getTexUnit(detail2)->unbind(LLTexUnit::TT_TEXTURE);
  334. gGL.getTexUnit(2)->disable();
  335. gGL.getTexUnit(2)->activate();
  336. gGL.matrixMode(LLRender::MM_TEXTURE);
  337. gGL.loadIdentity();
  338. gGL.matrixMode(LLRender::MM_MODELVIEW);
  339. gGL.getTexUnit(detail1)->unbind(LLTexUnit::TT_TEXTURE);
  340. gGL.getTexUnit(1)->disable();
  341. gGL.getTexUnit(1)->activate();
  342. gGL.matrixMode(LLRender::MM_TEXTURE);
  343. gGL.loadIdentity();
  344. gGL.matrixMode(LLRender::MM_MODELVIEW);
  345. //----------------------------------------------------------------------------
  346. // Restore Texture Unit 0 defaults
  347. gGL.getTexUnit(detail0)->unbind(LLTexUnit::TT_TEXTURE);
  348. gGL.getTexUnit(0)->enable(LLTexUnit::TT_TEXTURE);
  349. gGL.getTexUnit(0)->activate();
  350. gGL.matrixMode(LLRender::MM_TEXTURE);
  351. gGL.loadIdentity();
  352. gGL.matrixMode(LLRender::MM_MODELVIEW);
  353. }
  354. void LLDrawPoolTerrain::renderFull4TU()
  355. {
  356. // Hack! Get the region that this draw pool is rendering from!
  357. LLViewerRegion *regionp = mDrawFace[0]->getDrawable()->getVObj()->getRegion();
  358. LLVLComposition *compp = regionp->getComposition();
  359. LLViewerTexture *detail_texture0p = compp->mDetailTextures[0];
  360. LLViewerTexture *detail_texture1p = compp->mDetailTextures[1];
  361. LLViewerTexture *detail_texture2p = compp->mDetailTextures[2];
  362. LLViewerTexture *detail_texture3p = compp->mDetailTextures[3];
  363. LLVector3d region_origin_global = gAgent.getRegion()->getOriginGlobal();
  364. F32 offset_x = (F32)fmod(region_origin_global.mdV[VX], 1.0/(F64)sDetailScale)*sDetailScale;
  365. F32 offset_y = (F32)fmod(region_origin_global.mdV[VY], 1.0/(F64)sDetailScale)*sDetailScale;
  366. LLVector4 tp0, tp1;
  367. tp0.setVec(sDetailScale, 0.0f, 0.0f, offset_x);
  368. tp1.setVec(0.0f, sDetailScale, 0.0f, offset_y);
  369. gGL.blendFunc(LLRender::BF_ONE_MINUS_SOURCE_ALPHA, LLRender::BF_SOURCE_ALPHA);
  370. //----------------------------------------------------------------------------
  371. // Pass 1/1
  372. //
  373. // Stage 0: detail texture 0
  374. //
  375. gGL.getTexUnit(0)->activate();
  376. gGL.getTexUnit(0)->bind(detail_texture0p);
  377. glEnable(GL_TEXTURE_GEN_S);
  378. glEnable(GL_TEXTURE_GEN_T);
  379. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  380. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  381. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  382. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  383. gGL.getTexUnit(0)->setTextureColorBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_TEX_COLOR);
  384. //
  385. // Stage 1: Generate alpha ramp for detail0/detail1 transition
  386. //
  387. gGL.getTexUnit(1)->bind(m2DAlphaRampImagep.get());
  388. gGL.getTexUnit(1)->enable(LLTexUnit::TT_TEXTURE);
  389. gGL.getTexUnit(1)->activate();
  390. // Care about alpha only
  391. gGL.getTexUnit(1)->setTextureColorBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_PREV_COLOR);
  392. gGL.getTexUnit(1)->setTextureAlphaBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_TEX_ALPHA);
  393. //
  394. // Stage 2: Interpolate detail1 with existing based on ramp
  395. //
  396. gGL.getTexUnit(2)->bind(detail_texture1p);
  397. gGL.getTexUnit(2)->enable(LLTexUnit::TT_TEXTURE);
  398. gGL.getTexUnit(2)->activate();
  399. glEnable(GL_TEXTURE_GEN_S);
  400. glEnable(GL_TEXTURE_GEN_T);
  401. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  402. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  403. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  404. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  405. gGL.getTexUnit(2)->setTextureColorBlend(LLTexUnit::TBO_LERP_PREV_ALPHA, LLTexUnit::TBS_PREV_COLOR, LLTexUnit::TBS_TEX_COLOR);
  406. //
  407. // Stage 3: Modulate with primary (vertex) color for lighting
  408. //
  409. gGL.getTexUnit(3)->bind(detail_texture1p);
  410. gGL.getTexUnit(3)->enable(LLTexUnit::TT_TEXTURE);
  411. gGL.getTexUnit(3)->activate();
  412. // Set alpha texture and do lighting modulation
  413. gGL.getTexUnit(3)->setTextureColorBlend(LLTexUnit::TBO_MULT, LLTexUnit::TBS_PREV_COLOR, LLTexUnit::TBS_VERT_COLOR);
  414. gGL.getTexUnit(0)->activate();
  415. // GL_BLEND disabled by default
  416. drawLoop();
  417. //----------------------------------------------------------------------------
  418. // Second pass
  419. // Stage 0: Write detail3 into base
  420. //
  421. gGL.getTexUnit(0)->activate();
  422. gGL.getTexUnit(0)->bind(detail_texture3p);
  423. glEnable(GL_TEXTURE_GEN_S);
  424. glEnable(GL_TEXTURE_GEN_T);
  425. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  426. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  427. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  428. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  429. gGL.getTexUnit(0)->setTextureColorBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_TEX_COLOR);
  430. //
  431. // Stage 1: Generate alpha ramp for detail2/detail3 transition
  432. //
  433. gGL.getTexUnit(1)->bind(m2DAlphaRampImagep);
  434. gGL.getTexUnit(1)->enable(LLTexUnit::TT_TEXTURE);
  435. gGL.getTexUnit(1)->activate();
  436. // Set the texture matrix
  437. gGL.matrixMode(LLRender::MM_TEXTURE);
  438. gGL.loadIdentity();
  439. gGL.translatef(-2.f, 0.f, 0.f);
  440. // Care about alpha only
  441. gGL.getTexUnit(1)->setTextureColorBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_PREV_COLOR);
  442. gGL.getTexUnit(1)->setTextureAlphaBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_TEX_ALPHA);
  443. //
  444. // Stage 2: Interpolate detail2 with existing based on ramp
  445. //
  446. gGL.getTexUnit(2)->bind(detail_texture2p);
  447. gGL.getTexUnit(2)->enable(LLTexUnit::TT_TEXTURE);
  448. gGL.getTexUnit(2)->activate();
  449. glEnable(GL_TEXTURE_GEN_S);
  450. glEnable(GL_TEXTURE_GEN_T);
  451. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  452. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  453. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  454. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  455. gGL.getTexUnit(2)->setTextureColorBlend(LLTexUnit::TBO_LERP_PREV_ALPHA, LLTexUnit::TBS_TEX_COLOR, LLTexUnit::TBS_PREV_COLOR);
  456. //
  457. // Stage 3: Generate alpha ramp for detail1/detail2 transition
  458. //
  459. gGL.getTexUnit(3)->bind(m2DAlphaRampImagep);
  460. gGL.getTexUnit(3)->enable(LLTexUnit::TT_TEXTURE);
  461. gGL.getTexUnit(3)->activate();
  462. // Set the texture matrix
  463. gGL.matrixMode(LLRender::MM_TEXTURE);
  464. gGL.loadIdentity();
  465. gGL.translatef(-1.f, 0.f, 0.f);
  466. // Set alpha texture and do lighting modulation
  467. gGL.getTexUnit(3)->setTextureColorBlend(LLTexUnit::TBO_MULT, LLTexUnit::TBS_PREV_COLOR, LLTexUnit::TBS_VERT_COLOR);
  468. gGL.getTexUnit(3)->setTextureAlphaBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_TEX_ALPHA);
  469. gGL.getTexUnit(0)->activate();
  470. {
  471. LLGLEnable blend(GL_BLEND);
  472. drawLoop();
  473. }
  474. LLVertexBuffer::unbind();
  475. // Disable multitexture
  476. gGL.getTexUnit(3)->unbind(LLTexUnit::TT_TEXTURE);
  477. gGL.getTexUnit(3)->disable();
  478. gGL.getTexUnit(3)->activate();
  479. gGL.matrixMode(LLRender::MM_TEXTURE);
  480. gGL.loadIdentity();
  481. gGL.matrixMode(LLRender::MM_MODELVIEW);
  482. gGL.getTexUnit(2)->unbind(LLTexUnit::TT_TEXTURE);
  483. gGL.getTexUnit(2)->disable();
  484. gGL.getTexUnit(2)->activate();
  485. glDisable(GL_TEXTURE_GEN_S);
  486. glDisable(GL_TEXTURE_GEN_T);
  487. gGL.matrixMode(LLRender::MM_TEXTURE);
  488. gGL.loadIdentity();
  489. gGL.matrixMode(LLRender::MM_MODELVIEW);
  490. gGL.getTexUnit(1)->unbind(LLTexUnit::TT_TEXTURE);
  491. gGL.getTexUnit(1)->disable();
  492. gGL.getTexUnit(1)->activate();
  493. gGL.matrixMode(LLRender::MM_TEXTURE);
  494. gGL.loadIdentity();
  495. gGL.matrixMode(LLRender::MM_MODELVIEW);
  496. // Restore blend state
  497. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  498. //----------------------------------------------------------------------------
  499. // Restore Texture Unit 0 defaults
  500. gGL.getTexUnit(0)->activate();
  501. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  502. glDisable(GL_TEXTURE_GEN_S);
  503. glDisable(GL_TEXTURE_GEN_T);
  504. gGL.matrixMode(LLRender::MM_TEXTURE);
  505. gGL.loadIdentity();
  506. gGL.matrixMode(LLRender::MM_MODELVIEW);
  507. gGL.getTexUnit(0)->setTextureBlendType(LLTexUnit::TB_MULT);
  508. }
  509. void LLDrawPoolTerrain::renderFull2TU()
  510. {
  511. // Hack! Get the region that this draw pool is rendering from!
  512. LLViewerRegion *regionp = mDrawFace[0]->getDrawable()->getVObj()->getRegion();
  513. LLVLComposition *compp = regionp->getComposition();
  514. LLViewerTexture *detail_texture0p = compp->mDetailTextures[0];
  515. LLViewerTexture *detail_texture1p = compp->mDetailTextures[1];
  516. LLViewerTexture *detail_texture2p = compp->mDetailTextures[2];
  517. LLViewerTexture *detail_texture3p = compp->mDetailTextures[3];
  518. LLVector3d region_origin_global = gAgent.getRegion()->getOriginGlobal();
  519. F32 offset_x = (F32)fmod(region_origin_global.mdV[VX], 1.0/(F64)sDetailScale)*sDetailScale;
  520. F32 offset_y = (F32)fmod(region_origin_global.mdV[VY], 1.0/(F64)sDetailScale)*sDetailScale;
  521. LLVector4 tp0, tp1;
  522. tp0.setVec(sDetailScale, 0.0f, 0.0f, offset_x);
  523. tp1.setVec(0.0f, sDetailScale, 0.0f, offset_y);
  524. gGL.blendFunc(LLRender::BF_ONE_MINUS_SOURCE_ALPHA, LLRender::BF_SOURCE_ALPHA);
  525. //----------------------------------------------------------------------------
  526. // Pass 1/4
  527. //
  528. // Stage 0: Render detail 0 into base
  529. //
  530. gGL.getTexUnit(0)->bind(detail_texture0p);
  531. glEnable(GL_TEXTURE_GEN_S);
  532. glEnable(GL_TEXTURE_GEN_T);
  533. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  534. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  535. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  536. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  537. gGL.getTexUnit(0)->setTextureColorBlend(LLTexUnit::TBO_MULT, LLTexUnit::TBS_TEX_COLOR, LLTexUnit::TBS_VERT_COLOR);
  538. drawLoop();
  539. //----------------------------------------------------------------------------
  540. // Pass 2/4
  541. //
  542. // Stage 0: Generate alpha ramp for detail0/detail1 transition
  543. //
  544. gGL.getTexUnit(0)->bind(m2DAlphaRampImagep);
  545. glDisable(GL_TEXTURE_GEN_S);
  546. glDisable(GL_TEXTURE_GEN_T);
  547. // Care about alpha only
  548. gGL.getTexUnit(0)->setTextureColorBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_PREV_COLOR);
  549. gGL.getTexUnit(0)->setTextureAlphaBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_TEX_ALPHA);
  550. //
  551. // Stage 1: Write detail1
  552. //
  553. gGL.getTexUnit(1)->bind(detail_texture1p);
  554. gGL.getTexUnit(1)->enable(LLTexUnit::TT_TEXTURE);
  555. gGL.getTexUnit(1)->activate();
  556. glEnable(GL_TEXTURE_GEN_S);
  557. glEnable(GL_TEXTURE_GEN_T);
  558. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  559. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  560. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  561. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  562. gGL.getTexUnit(1)->setTextureColorBlend(LLTexUnit::TBO_MULT, LLTexUnit::TBS_TEX_COLOR, LLTexUnit::TBS_VERT_COLOR);
  563. gGL.getTexUnit(1)->setTextureAlphaBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_PREV_ALPHA);
  564. gGL.getTexUnit(0)->activate();
  565. {
  566. LLGLEnable blend(GL_BLEND);
  567. drawLoop();
  568. }
  569. //----------------------------------------------------------------------------
  570. // Pass 3/4
  571. //
  572. // Stage 0: Generate alpha ramp for detail1/detail2 transition
  573. //
  574. gGL.getTexUnit(0)->bind(m2DAlphaRampImagep);
  575. // Set the texture matrix
  576. gGL.matrixMode(LLRender::MM_TEXTURE);
  577. gGL.loadIdentity();
  578. gGL.translatef(-1.f, 0.f, 0.f);
  579. // Care about alpha only
  580. gGL.getTexUnit(0)->setTextureColorBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_PREV_COLOR);
  581. gGL.getTexUnit(0)->setTextureAlphaBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_TEX_ALPHA);
  582. //
  583. // Stage 1: Write detail2
  584. //
  585. gGL.getTexUnit(1)->bind(detail_texture2p);
  586. gGL.getTexUnit(1)->enable(LLTexUnit::TT_TEXTURE);
  587. gGL.getTexUnit(1)->activate();
  588. glEnable(GL_TEXTURE_GEN_S);
  589. glEnable(GL_TEXTURE_GEN_T);
  590. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  591. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  592. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  593. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  594. gGL.getTexUnit(1)->setTextureColorBlend(LLTexUnit::TBO_MULT, LLTexUnit::TBS_TEX_COLOR, LLTexUnit::TBS_VERT_COLOR);
  595. gGL.getTexUnit(1)->setTextureAlphaBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_PREV_ALPHA);
  596. {
  597. LLGLEnable blend(GL_BLEND);
  598. drawLoop();
  599. }
  600. //----------------------------------------------------------------------------
  601. // Pass 4/4
  602. //
  603. // Stage 0: Generate alpha ramp for detail2/detail3 transition
  604. //
  605. gGL.getTexUnit(0)->activate();
  606. gGL.getTexUnit(0)->bind(m2DAlphaRampImagep);
  607. // Set the texture matrix
  608. gGL.matrixMode(LLRender::MM_TEXTURE);
  609. gGL.loadIdentity();
  610. gGL.translatef(-2.f, 0.f, 0.f);
  611. // Care about alpha only
  612. gGL.getTexUnit(0)->setTextureColorBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_PREV_COLOR);
  613. gGL.getTexUnit(0)->setTextureAlphaBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_TEX_ALPHA);
  614. // Stage 1: Write detail3
  615. gGL.getTexUnit(1)->bind(detail_texture3p);
  616. gGL.getTexUnit(1)->enable(LLTexUnit::TT_TEXTURE);
  617. gGL.getTexUnit(1)->activate();
  618. glEnable(GL_TEXTURE_GEN_S);
  619. glEnable(GL_TEXTURE_GEN_T);
  620. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  621. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  622. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  623. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  624. gGL.getTexUnit(1)->setTextureColorBlend(LLTexUnit::TBO_MULT, LLTexUnit::TBS_TEX_COLOR, LLTexUnit::TBS_VERT_COLOR);
  625. gGL.getTexUnit(1)->setTextureAlphaBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_PREV_ALPHA);
  626. gGL.getTexUnit(0)->activate();
  627. {
  628. LLGLEnable blend(GL_BLEND);
  629. drawLoop();
  630. }
  631. // Restore blend state
  632. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  633. // Disable multitexture
  634. gGL.getTexUnit(1)->unbind(LLTexUnit::TT_TEXTURE);
  635. gGL.getTexUnit(1)->disable();
  636. gGL.getTexUnit(1)->activate();
  637. glDisable(GL_TEXTURE_GEN_S);
  638. glDisable(GL_TEXTURE_GEN_T);
  639. gGL.matrixMode(LLRender::MM_TEXTURE);
  640. gGL.loadIdentity();
  641. gGL.matrixMode(LLRender::MM_MODELVIEW);
  642. //----------------------------------------------------------------------------
  643. // Restore Texture Unit 0 defaults
  644. gGL.getTexUnit(0)->activate();
  645. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  646. glDisable(GL_TEXTURE_GEN_S);
  647. glDisable(GL_TEXTURE_GEN_T);
  648. gGL.matrixMode(LLRender::MM_TEXTURE);
  649. gGL.loadIdentity();
  650. gGL.matrixMode(LLRender::MM_MODELVIEW);
  651. gGL.getTexUnit(0)->setTextureBlendType(LLTexUnit::TB_MULT);
  652. }
  653. void LLDrawPoolTerrain::renderSimple()
  654. {
  655. LLVector4 tp0, tp1;
  656. //----------------------------------------------------------------------------
  657. // Pass 1/1
  658. // Stage 0: Base terrain texture pass
  659. mTexturep->addTextureStats(1024.f*1024.f);
  660. gGL.getTexUnit(0)->activate();
  661. gGL.getTexUnit(0)->enable(LLTexUnit::TT_TEXTURE);
  662. gGL.getTexUnit(0)->bind(mTexturep);
  663. LLVector3 origin_agent = mDrawFace[0]->getDrawable()->getVObj()->getRegion()->getOriginAgent();
  664. F32 tscale = 1.f/256.f;
  665. tp0.setVec(tscale, 0.f, 0.0f, -1.f*(origin_agent.mV[0]/256.f));
  666. tp1.setVec(0.f, tscale, 0.0f, -1.f*(origin_agent.mV[1]/256.f));
  667. if (LLGLSLShader::sNoFixedFunction)
  668. {
  669. sShader->uniform4fv("object_plane_s", 1, tp0.mV);
  670. sShader->uniform4fv("object_plane_t", 1, tp1.mV);
  671. }
  672. else
  673. {
  674. glEnable(GL_TEXTURE_GEN_S);
  675. glEnable(GL_TEXTURE_GEN_T);
  676. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  677. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  678. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  679. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  680. }
  681. gGL.getTexUnit(0)->setTextureColorBlend(LLTexUnit::TBO_MULT, LLTexUnit::TBS_TEX_COLOR, LLTexUnit::TBS_VERT_COLOR);
  682. drawLoop();
  683. //----------------------------------------------------------------------------
  684. // Restore Texture Unit 0 defaults
  685. gGL.getTexUnit(0)->activate();
  686. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  687. if (!LLGLSLShader::sNoFixedFunction)
  688. {
  689. glDisable(GL_TEXTURE_GEN_S);
  690. glDisable(GL_TEXTURE_GEN_T);
  691. }
  692. gGL.matrixMode(LLRender::MM_TEXTURE);
  693. gGL.loadIdentity();
  694. gGL.matrixMode(LLRender::MM_MODELVIEW);
  695. gGL.getTexUnit(0)->setTextureBlendType(LLTexUnit::TB_MULT);
  696. }
  697. //============================================================================
  698. void LLDrawPoolTerrain::renderOwnership()
  699. {
  700. LLGLSPipelineAlpha gls_pipeline_alpha;
  701. llassert(!mDrawFace.empty());
  702. // Each terrain pool is associated with a single region.
  703. // We need to peek back into the viewer's data to find out
  704. // which ownership overlay texture to use.
  705. LLFace *facep = mDrawFace[0];
  706. LLDrawable *drawablep = facep->getDrawable();
  707. const LLViewerObject *objectp = drawablep->getVObj();
  708. const LLVOSurfacePatch *vo_surface_patchp = (LLVOSurfacePatch *)objectp;
  709. LLSurfacePatch *surface_patchp = vo_surface_patchp->getPatch();
  710. LLSurface *surfacep = surface_patchp->getSurface();
  711. LLViewerRegion *regionp = surfacep->getRegion();
  712. LLViewerParcelOverlay *overlayp = regionp->getParcelOverlay();
  713. LLViewerTexture *texturep = overlayp->getTexture();
  714. gGL.getTexUnit(0)->bind(texturep);
  715. // *NOTE: Because the region is 256 meters wide, but has 257 pixels, the
  716. // texture coordinates for pixel 256x256 is not 1,1. This makes the
  717. // ownership map not line up with the selection. We address this with
  718. // a texture matrix multiply.
  719. gGL.matrixMode(LLRender::MM_TEXTURE);
  720. gGL.pushMatrix();
  721. const F32 TEXTURE_FUDGE = 257.f / 256.f;
  722. gGL.scalef( TEXTURE_FUDGE, TEXTURE_FUDGE, 1.f );
  723. for (std::vector<LLFace*>::iterator iter = mDrawFace.begin();
  724. iter != mDrawFace.end(); iter++)
  725. {
  726. LLFace *facep = *iter;
  727. facep->renderIndexed(LLVertexBuffer::MAP_VERTEX |
  728. LLVertexBuffer::MAP_TEXCOORD0);
  729. }
  730. gGL.matrixMode(LLRender::MM_TEXTURE);
  731. gGL.popMatrix();
  732. gGL.matrixMode(LLRender::MM_MODELVIEW);
  733. }
  734. void LLDrawPoolTerrain::dirtyTextures(const std::set<LLViewerFetchedTexture*>& textures)
  735. {
  736. LLViewerFetchedTexture* tex = LLViewerTextureManager::staticCastToFetchedTexture(mTexturep) ;
  737. if (tex && textures.find(tex) != textures.end())
  738. {
  739. for (std::vector<LLFace*>::iterator iter = mReferences.begin();
  740. iter != mReferences.end(); iter++)
  741. {
  742. LLFace *facep = *iter;
  743. gPipeline.markTextured(facep->getDrawable());
  744. }
  745. }
  746. }
  747. LLViewerTexture *LLDrawPoolTerrain::getTexture()
  748. {
  749. return mTexturep;
  750. }
  751. LLViewerTexture *LLDrawPoolTerrain::getDebugTexture()
  752. {
  753. return mTexturep;
  754. }
  755. LLColor3 LLDrawPoolTerrain::getDebugColor() const
  756. {
  757. return LLColor3(0.f, 0.f, 1.f);
  758. }