llviewertextureanim.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**
  2. * @file llviewertextureanim.cpp
  3. * @brief LLViewerTextureAnim class implementation
  4. *
  5. * $LicenseInfo:firstyear=2003&license=viewergpl$
  6. *
  7. * Copyright (c) 2003-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 "llviewertextureanim.h"
  34. #include "llvovolume.h"
  35. std::vector<LLViewerTextureAnim*> LLViewerTextureAnim::sInstanceList;
  36. LLViewerTextureAnim::LLViewerTextureAnim(LLVOVolume* vobj) : LLTextureAnim()
  37. {
  38. mVObj = vobj;
  39. mLastFrame = -1.f; // Force an update initially
  40. mLastTime = 0.f;
  41. mOffS = mOffT = 0;
  42. mScaleS = mScaleT = 1;
  43. mRot = 0;
  44. mInstanceIndex = sInstanceList.size();
  45. sInstanceList.push_back(this);
  46. }
  47. LLViewerTextureAnim::~LLViewerTextureAnim()
  48. {
  49. S32 end_idx = sInstanceList.size() - 1;
  50. if (end_idx != mInstanceIndex)
  51. {
  52. sInstanceList[mInstanceIndex] = sInstanceList[end_idx];
  53. sInstanceList[mInstanceIndex]->mInstanceIndex = mInstanceIndex;
  54. }
  55. sInstanceList.pop_back();
  56. }
  57. void LLViewerTextureAnim::reset()
  58. {
  59. LLTextureAnim::reset();
  60. mTimer.reset();
  61. }
  62. //static
  63. void LLViewerTextureAnim::initClass()
  64. {
  65. // Let's avoid memory fragmentation over time...
  66. sInstanceList.reserve(2048);
  67. }
  68. //static
  69. void LLViewerTextureAnim::updateClass()
  70. {
  71. for (std::vector<LLViewerTextureAnim*>::iterator
  72. iter = sInstanceList.begin(), end = sInstanceList.end();
  73. iter != end; ++iter)
  74. {
  75. (*iter)->mVObj->animateTextures();
  76. }
  77. }
  78. //static
  79. void LLViewerTextureAnim::dumpStats()
  80. {
  81. llinfos << "sInstanceList capacity reached: " << sInstanceList.capacity()
  82. << llendl;
  83. }
  84. S32 LLViewerTextureAnim::animateTextures(F32& off_s, F32& off_t,
  85. F32& scale_s, F32& scale_t,
  86. F32& rot)
  87. {
  88. S32 result = 0;
  89. if (!(mMode & ON))
  90. {
  91. mLastTime = 0.f;
  92. mLastFrame = -1.f;
  93. return result;
  94. }
  95. F32 num_frames = 1.f;
  96. F32 full_length = 1.f;
  97. if (mLength)
  98. {
  99. num_frames = mLength;
  100. }
  101. else
  102. {
  103. num_frames = llmax(1.f, (F32)(mSizeX * mSizeY));
  104. }
  105. if (mMode & PING_PONG)
  106. {
  107. if (mMode & SMOOTH)
  108. {
  109. full_length = 2.f * num_frames;
  110. }
  111. else if (mMode & LOOP)
  112. {
  113. full_length = 2.f * num_frames - 2.f;
  114. full_length = llmax(1.f, full_length);
  115. }
  116. else
  117. {
  118. full_length = 2.f * num_frames - 1.f;
  119. full_length = llmax(1.f, full_length);
  120. }
  121. }
  122. else
  123. {
  124. full_length = num_frames;
  125. }
  126. F32 frame_counter;
  127. if (mMode & SMOOTH)
  128. {
  129. frame_counter = mTimer.getElapsedTimeAndResetF32() * mRate +
  130. (F32)mLastTime;
  131. }
  132. else
  133. {
  134. frame_counter = mTimer.getElapsedTimeF32() * mRate;
  135. }
  136. mLastTime = frame_counter;
  137. if (mMode & LOOP)
  138. {
  139. frame_counter = fmod(frame_counter, full_length);
  140. }
  141. else
  142. {
  143. frame_counter = llmin(full_length - 1.f, frame_counter);
  144. }
  145. if (!(mMode & SMOOTH))
  146. {
  147. frame_counter = (F32)llfloor(frame_counter + 0.01f);
  148. #if 1 // account for 0.01, we should not step over full length
  149. frame_counter = llmin(full_length - 1.f, frame_counter);
  150. #endif
  151. }
  152. if (mMode & PING_PONG)
  153. {
  154. if (frame_counter >= num_frames)
  155. {
  156. if (mMode & SMOOTH)
  157. {
  158. frame_counter = 2.f * num_frames - frame_counter;
  159. }
  160. else
  161. {
  162. frame_counter = 2.f * num_frames - 1.99f - frame_counter;
  163. }
  164. }
  165. }
  166. if (mMode & REVERSE)
  167. {
  168. if (mMode & SMOOTH)
  169. {
  170. frame_counter = num_frames - frame_counter;
  171. }
  172. else
  173. {
  174. frame_counter = num_frames - 0.99f - frame_counter;
  175. }
  176. }
  177. frame_counter += mStart;
  178. if (!(mMode & SMOOTH))
  179. {
  180. frame_counter = ll_roundp(frame_counter);
  181. }
  182. // Now that we have calculated the frame time, do an update. Will we
  183. // correctly update stuff if the texture anim has changed, but not the
  184. // frame counter ?
  185. if (mLastFrame != frame_counter)
  186. {
  187. mLastFrame = frame_counter;
  188. if (mMode & ROTATE)
  189. {
  190. result |= ROTATE;
  191. mRot = rot = frame_counter;
  192. }
  193. else if (mMode & SCALE)
  194. {
  195. result |= SCALE;
  196. mScaleS = scale_s = frame_counter;
  197. mScaleT = scale_t = frame_counter;
  198. }
  199. else
  200. {
  201. result |= TRANSLATE;
  202. F32 x_frame;
  203. S32 y_frame;
  204. F32 x_pos;
  205. F32 y_pos;
  206. if (mSizeX && mSizeY)
  207. {
  208. result |= SCALE;
  209. mScaleS = scale_s = 1.f / mSizeX;
  210. mScaleT = scale_t = 1.f / mSizeY;
  211. x_frame = fmod(frame_counter, mSizeX);
  212. y_frame = (S32)(frame_counter / mSizeX);
  213. x_pos = x_frame * scale_s;
  214. y_pos = y_frame * scale_t;
  215. mOffS = off_s = -0.5f + 0.5f * scale_s + x_pos;
  216. mOffT = off_t = 0.5f - 0.5f * scale_t - y_pos;
  217. }
  218. else
  219. {
  220. mScaleS = scale_s = mScaleT = scale_t = 1.f;
  221. x_pos = frame_counter * scale_s;
  222. mOffS = off_s = -0.5f + 0.5f * scale_s + x_pos;
  223. mOffT = off_t = 0.f;
  224. }
  225. }
  226. }
  227. return result;
  228. }