PageRenderTime 72ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/lldrawpoolwlsky.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 418 lines | 267 code | 97 blank | 54 comment | 23 complexity | 047beb1f094d4015f5140ae8d76287c0 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lldrawpoolwlsky.cpp
  3. * @brief LLDrawPoolWLSky class implementation
  4. *
  5. * $LicenseInfo:firstyear=2007&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 "lldrawpoolwlsky.h"
  28. #include "llerror.h"
  29. #include "llgl.h"
  30. #include "pipeline.h"
  31. #include "llviewercamera.h"
  32. #include "llimage.h"
  33. #include "llwlparammanager.h"
  34. #include "llviewershadermgr.h"
  35. #include "llglslshader.h"
  36. #include "llsky.h"
  37. #include "llvowlsky.h"
  38. #include "llviewerregion.h"
  39. #include "llface.h"
  40. #include "llrender.h"
  41. LLPointer<LLViewerTexture> LLDrawPoolWLSky::sCloudNoiseTexture = NULL;
  42. LLPointer<LLImageRaw> LLDrawPoolWLSky::sCloudNoiseRawImage = NULL;
  43. static LLGLSLShader* cloud_shader = NULL;
  44. static LLGLSLShader* sky_shader = NULL;
  45. LLDrawPoolWLSky::LLDrawPoolWLSky(void) :
  46. LLDrawPool(POOL_WL_SKY)
  47. {
  48. const std::string cloudNoiseFilename(gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS, "windlight", "clouds2.tga"));
  49. llinfos << "loading WindLight cloud noise from " << cloudNoiseFilename << llendl;
  50. LLPointer<LLImageFormatted> cloudNoiseFile(LLImageFormatted::createFromExtension(cloudNoiseFilename));
  51. if(cloudNoiseFile.isNull()) {
  52. llerrs << "Error: Failed to load cloud noise image " << cloudNoiseFilename << llendl;
  53. }
  54. if(cloudNoiseFile->load(cloudNoiseFilename))
  55. {
  56. sCloudNoiseRawImage = new LLImageRaw();
  57. if(cloudNoiseFile->decode(sCloudNoiseRawImage, 0.0f))
  58. {
  59. //debug use
  60. lldebugs << "cloud noise raw image width: " << sCloudNoiseRawImage->getWidth() << " : height: " << sCloudNoiseRawImage->getHeight() << " : components: " <<
  61. (S32)sCloudNoiseRawImage->getComponents() << " : data size: " << sCloudNoiseRawImage->getDataSize() << llendl ;
  62. llassert_always(sCloudNoiseRawImage->getData()) ;
  63. sCloudNoiseTexture = LLViewerTextureManager::getLocalTexture(sCloudNoiseRawImage.get(), TRUE);
  64. }
  65. else
  66. {
  67. sCloudNoiseRawImage = NULL ;
  68. }
  69. }
  70. LLWLParamManager::getInstance()->propagateParameters();
  71. }
  72. LLDrawPoolWLSky::~LLDrawPoolWLSky()
  73. {
  74. //llinfos << "destructing wlsky draw pool." << llendl;
  75. sCloudNoiseTexture = NULL;
  76. sCloudNoiseRawImage = NULL;
  77. }
  78. LLViewerTexture *LLDrawPoolWLSky::getDebugTexture()
  79. {
  80. return NULL;
  81. }
  82. void LLDrawPoolWLSky::beginRenderPass( S32 pass )
  83. {
  84. sky_shader =
  85. LLPipeline::sUnderWaterRender ?
  86. &gObjectFullbrightNoColorWaterProgram :
  87. &gWLSkyProgram;
  88. cloud_shader =
  89. LLPipeline::sUnderWaterRender ?
  90. &gObjectFullbrightNoColorWaterProgram :
  91. &gWLCloudProgram;
  92. }
  93. void LLDrawPoolWLSky::endRenderPass( S32 pass )
  94. {
  95. }
  96. void LLDrawPoolWLSky::beginDeferredPass(S32 pass)
  97. {
  98. sky_shader = &gDeferredWLSkyProgram;
  99. cloud_shader = &gDeferredWLCloudProgram;
  100. }
  101. void LLDrawPoolWLSky::endDeferredPass(S32 pass)
  102. {
  103. }
  104. void LLDrawPoolWLSky::renderDome(F32 camHeightLocal, LLGLSLShader * shader) const
  105. {
  106. LLVector3 const & origin = LLViewerCamera::getInstance()->getOrigin();
  107. llassert_always(NULL != shader);
  108. gGL.pushMatrix();
  109. //chop off translation
  110. if (LLPipeline::sReflectionRender && origin.mV[2] > 256.f)
  111. {
  112. gGL.translatef(origin.mV[0], origin.mV[1], 256.f-origin.mV[2]*0.5f);
  113. }
  114. else
  115. {
  116. gGL.translatef(origin.mV[0], origin.mV[1], origin.mV[2]);
  117. }
  118. // the windlight sky dome works most conveniently in a coordinate system
  119. // where Y is up, so permute our basis vectors accordingly.
  120. gGL.rotatef(120.f, 1.f / F_SQRT3, 1.f / F_SQRT3, 1.f / F_SQRT3);
  121. gGL.scalef(0.333f, 0.333f, 0.333f);
  122. gGL.translatef(0.f,-camHeightLocal, 0.f);
  123. // Draw WL Sky
  124. shader->uniform3f("camPosLocal", 0.f, camHeightLocal, 0.f);
  125. gSky.mVOWLSkyp->drawDome();
  126. gGL.popMatrix();
  127. }
  128. void LLDrawPoolWLSky::renderSkyHaze(F32 camHeightLocal) const
  129. {
  130. if (gPipeline.canUseWindLightShaders() && gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_SKY))
  131. {
  132. LLGLDisable blend(GL_BLEND);
  133. sky_shader->bind();
  134. /// Render the skydome
  135. renderDome(camHeightLocal, sky_shader);
  136. sky_shader->unbind();
  137. }
  138. }
  139. void LLDrawPoolWLSky::renderStars(void) const
  140. {
  141. LLGLSPipelineSkyBox gls_sky;
  142. LLGLEnable blend(GL_BLEND);
  143. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  144. // *NOTE: have to have bound the cloud noise texture already since register
  145. // combiners blending below requires something to be bound
  146. // and we might as well only bind once.
  147. gGL.getTexUnit(0)->enable(LLTexUnit::TT_TEXTURE);
  148. gPipeline.disableLights();
  149. // *NOTE: we divide by two here and GL_ALPHA_SCALE by two below to avoid
  150. // clamping and allow the star_alpha param to brighten the stars.
  151. bool error;
  152. LLColor4 star_alpha(LLColor4::black);
  153. star_alpha.mV[3] = LLWLParamManager::getInstance()->mCurParams.getFloat("star_brightness", error) / 2.f;
  154. llassert_always(!error);
  155. gGL.getTexUnit(0)->bind(gSky.mVOSkyp->getBloomTex());
  156. gGL.pushMatrix();
  157. gGL.rotatef(gFrameTimeSeconds*0.01f, 0.f, 0.f, 1.f);
  158. // gl_FragColor.rgb = gl_Color.rgb;
  159. // gl_FragColor.a = gl_Color.a * star_alpha.a;
  160. if (LLGLSLShader::sNoFixedFunction)
  161. {
  162. gCustomAlphaProgram.bind();
  163. gCustomAlphaProgram.uniform1f("custom_alpha", star_alpha.mV[3]);
  164. }
  165. else
  166. {
  167. gGL.getTexUnit(0)->setTextureColorBlend(LLTexUnit::TBO_MULT, LLTexUnit::TBS_TEX_COLOR, LLTexUnit::TBS_VERT_COLOR);
  168. gGL.getTexUnit(0)->setTextureAlphaBlend(LLTexUnit::TBO_MULT_X2, LLTexUnit::TBS_CONST_ALPHA, LLTexUnit::TBS_TEX_ALPHA);
  169. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, star_alpha.mV);
  170. }
  171. gSky.mVOWLSkyp->drawStars();
  172. gGL.popMatrix();
  173. if (LLGLSLShader::sNoFixedFunction)
  174. {
  175. gCustomAlphaProgram.unbind();
  176. }
  177. else
  178. {
  179. // and disable the combiner states
  180. gGL.getTexUnit(0)->setTextureBlendType(LLTexUnit::TB_MULT);
  181. }
  182. }
  183. void LLDrawPoolWLSky::renderSkyClouds(F32 camHeightLocal) const
  184. {
  185. if (gPipeline.canUseWindLightShaders() && gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_CLOUDS) && sCloudNoiseTexture.notNull())
  186. {
  187. LLGLEnable blend(GL_BLEND);
  188. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  189. gGL.getTexUnit(0)->bind(sCloudNoiseTexture);
  190. cloud_shader->bind();
  191. /// Render the skydome
  192. renderDome(camHeightLocal, cloud_shader);
  193. cloud_shader->unbind();
  194. }
  195. }
  196. void LLDrawPoolWLSky::renderHeavenlyBodies()
  197. {
  198. LLGLSPipelineSkyBox gls_skybox;
  199. LLGLEnable blend_on(GL_BLEND);
  200. gPipeline.disableLights();
  201. #if 0 // when we want to re-add a texture sun disc, here's where to do it.
  202. LLFace * face = gSky.mVOSkyp->mFace[LLVOSky::FACE_SUN];
  203. if (gSky.mVOSkyp->getSun().getDraw() && face->getGeomCount())
  204. {
  205. LLViewerTexture * tex = face->getTexture();
  206. gGL.getTexUnit(0)->bind(tex);
  207. LLColor4 color(gSky.mVOSkyp->getSun().getInterpColor());
  208. LLFacePool::LLOverrideFaceColor color_override(this, color);
  209. face->renderIndexed();
  210. }
  211. #endif
  212. LLFace * face = gSky.mVOSkyp->mFace[LLVOSky::FACE_MOON];
  213. if (gSky.mVOSkyp->getMoon().getDraw() && face->getGeomCount())
  214. {
  215. // *NOTE: even though we already bound this texture above for the
  216. // stars register combiners, we bind again here for defensive reasons,
  217. // since LLImageGL::bind detects that it's a noop, and optimizes it out.
  218. gGL.getTexUnit(0)->bind(face->getTexture());
  219. LLColor4 color(gSky.mVOSkyp->getMoon().getInterpColor());
  220. F32 a = gSky.mVOSkyp->getMoon().getDirection().mV[2];
  221. if (a > 0.f)
  222. {
  223. a = a*a*4.f;
  224. }
  225. color.mV[3] = llclamp(a, 0.f, 1.f);
  226. if (gPipeline.canUseVertexShaders())
  227. {
  228. gHighlightProgram.bind();
  229. }
  230. LLFacePool::LLOverrideFaceColor color_override(this, color);
  231. face->renderIndexed();
  232. if (gPipeline.canUseVertexShaders())
  233. {
  234. gHighlightProgram.unbind();
  235. }
  236. }
  237. }
  238. void LLDrawPoolWLSky::renderDeferred(S32 pass)
  239. {
  240. if (!gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_SKY))
  241. {
  242. return;
  243. }
  244. LLFastTimer ftm(FTM_RENDER_WL_SKY);
  245. const F32 camHeightLocal = LLWLParamManager::getInstance()->getDomeOffset() * LLWLParamManager::getInstance()->getDomeRadius();
  246. LLGLSNoFog disableFog;
  247. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  248. LLGLDisable clip(GL_CLIP_PLANE0);
  249. gGL.setColorMask(true, false);
  250. LLGLSquashToFarClip far_clip(glh_get_current_projection());
  251. renderSkyHaze(camHeightLocal);
  252. LLVector3 const & origin = LLViewerCamera::getInstance()->getOrigin();
  253. gGL.pushMatrix();
  254. gGL.translatef(origin.mV[0], origin.mV[1], origin.mV[2]);
  255. gDeferredStarProgram.bind();
  256. // *NOTE: have to bind a texture here since register combiners blending in
  257. // renderStars() requires something to be bound and we might as well only
  258. // bind the moon's texture once.
  259. gGL.getTexUnit(0)->bind(gSky.mVOSkyp->mFace[LLVOSky::FACE_MOON]->getTexture());
  260. renderHeavenlyBodies();
  261. renderStars();
  262. gDeferredStarProgram.unbind();
  263. gGL.popMatrix();
  264. renderSkyClouds(camHeightLocal);
  265. gGL.setColorMask(true, true);
  266. //gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  267. }
  268. void LLDrawPoolWLSky::render(S32 pass)
  269. {
  270. if (!gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_SKY))
  271. {
  272. return;
  273. }
  274. LLFastTimer ftm(FTM_RENDER_WL_SKY);
  275. const F32 camHeightLocal = LLWLParamManager::getInstance()->getDomeOffset() * LLWLParamManager::getInstance()->getDomeRadius();
  276. LLGLSNoFog disableFog;
  277. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  278. LLGLDisable clip(GL_CLIP_PLANE0);
  279. LLGLSquashToFarClip far_clip(glh_get_current_projection());
  280. renderSkyHaze(camHeightLocal);
  281. LLVector3 const & origin = LLViewerCamera::getInstance()->getOrigin();
  282. gGL.pushMatrix();
  283. gGL.translatef(origin.mV[0], origin.mV[1], origin.mV[2]);
  284. // *NOTE: have to bind a texture here since register combiners blending in
  285. // renderStars() requires something to be bound and we might as well only
  286. // bind the moon's texture once.
  287. gGL.getTexUnit(0)->bind(gSky.mVOSkyp->mFace[LLVOSky::FACE_MOON]->getTexture());
  288. renderHeavenlyBodies();
  289. renderStars();
  290. gGL.popMatrix();
  291. renderSkyClouds(camHeightLocal);
  292. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  293. }
  294. void LLDrawPoolWLSky::prerender()
  295. {
  296. //llinfos << "wlsky prerendering pass." << llendl;
  297. }
  298. LLDrawPoolWLSky *LLDrawPoolWLSky::instancePool()
  299. {
  300. return new LLDrawPoolWLSky();
  301. }
  302. LLViewerTexture* LLDrawPoolWLSky::getTexture()
  303. {
  304. return NULL;
  305. }
  306. void LLDrawPoolWLSky::resetDrawOrders()
  307. {
  308. }
  309. //static
  310. void LLDrawPoolWLSky::cleanupGL()
  311. {
  312. sCloudNoiseTexture = NULL;
  313. }
  314. //static
  315. void LLDrawPoolWLSky::restoreGL()
  316. {
  317. if(sCloudNoiseRawImage.notNull())
  318. {
  319. sCloudNoiseTexture = LLViewerTextureManager::getLocalTexture(sCloudNoiseRawImage.get(), TRUE);
  320. }
  321. }