/indra/llui/lllocalcliprect.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 110 lines · 64 code · 14 blank · 32 comment · 5 complexity · d12ea4052be4c664586e7ab98175275a MD5 · raw file

  1. /**
  2. * @file lllocalcliprect.cpp
  3. *
  4. * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  5. * Second Life Viewer Source Code
  6. * Copyright (C) 2010, Linden Research, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License only.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  23. * $/LicenseInfo$
  24. */
  25. #include "linden_common.h"
  26. #include "lllocalcliprect.h"
  27. #include "llfontgl.h"
  28. #include "llui.h"
  29. /*static*/ std::stack<LLRect> LLScreenClipRect::sClipRectStack;
  30. LLScreenClipRect::LLScreenClipRect(const LLRect& rect, BOOL enabled)
  31. : mScissorState(GL_SCISSOR_TEST),
  32. mEnabled(enabled)
  33. {
  34. if (mEnabled)
  35. {
  36. pushClipRect(rect);
  37. mScissorState.setEnabled(!sClipRectStack.empty());
  38. updateScissorRegion();
  39. }
  40. }
  41. LLScreenClipRect::~LLScreenClipRect()
  42. {
  43. if (mEnabled)
  44. {
  45. popClipRect();
  46. updateScissorRegion();
  47. }
  48. }
  49. //static
  50. void LLScreenClipRect::pushClipRect(const LLRect& rect)
  51. {
  52. LLRect combined_clip_rect = rect;
  53. if (!sClipRectStack.empty())
  54. {
  55. LLRect top = sClipRectStack.top();
  56. combined_clip_rect.intersectWith(top);
  57. if(combined_clip_rect.isEmpty())
  58. {
  59. // avoid artifacts where zero area rects show up as lines
  60. combined_clip_rect = LLRect::null;
  61. }
  62. }
  63. sClipRectStack.push(combined_clip_rect);
  64. }
  65. //static
  66. void LLScreenClipRect::popClipRect()
  67. {
  68. sClipRectStack.pop();
  69. }
  70. //static
  71. void LLScreenClipRect::updateScissorRegion()
  72. {
  73. if (sClipRectStack.empty()) return;
  74. // finish any deferred calls in the old clipping region
  75. gGL.flush();
  76. LLRect rect = sClipRectStack.top();
  77. stop_glerror();
  78. S32 x,y,w,h;
  79. x = llfloor(rect.mLeft * LLUI::sGLScaleFactor.mV[VX]);
  80. y = llfloor(rect.mBottom * LLUI::sGLScaleFactor.mV[VY]);
  81. w = llmax(0, llceil(rect.getWidth() * LLUI::sGLScaleFactor.mV[VX])) + 1;
  82. h = llmax(0, llceil(rect.getHeight() * LLUI::sGLScaleFactor.mV[VY])) + 1;
  83. glScissor( x,y,w,h );
  84. stop_glerror();
  85. }
  86. //---------------------------------------------------------------------------
  87. // LLLocalClipRect
  88. //---------------------------------------------------------------------------
  89. LLLocalClipRect::LLLocalClipRect(const LLRect& rect, BOOL enabled /* = TRUE */)
  90. : LLScreenClipRect(LLRect(rect.mLeft + LLFontGL::sCurOrigin.mX,
  91. rect.mTop + LLFontGL::sCurOrigin.mY,
  92. rect.mRight + LLFontGL::sCurOrigin.mX,
  93. rect.mBottom + LLFontGL::sCurOrigin.mY), enabled)
  94. {}
  95. LLLocalClipRect::~LLLocalClipRect()
  96. {}