PageRenderTime 99ms CodeModel.GetById 29ms app.highlight 64ms RepoModel.GetById 1ms app.codeStats 0ms

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