llviewertextureanim.cpp

Go to the documentation of this file.
00001 
00032 #include "llviewerprecompiledheaders.h"
00033 
00034 #include "llviewertextureanim.h"
00035 
00036 #include "llmath.h"
00037 #include "llerror.h"
00038 
00039 LLViewerTextureAnim::LLViewerTextureAnim() : LLTextureAnim()
00040 {
00041         mLastFrame = -1.f;      // Force an update initially
00042         mLastTime = 0.f;
00043         mOffS = mOffT = 0;
00044         mScaleS = mScaleT = 1;
00045         mRot = 0;
00046 }
00047 
00048 LLViewerTextureAnim::~LLViewerTextureAnim()
00049 {
00050 }
00051 
00052 void LLViewerTextureAnim::reset()
00053 {
00054         LLTextureAnim::reset();
00055         mTimer.reset();
00056 }
00057 
00058 
00059 S32 LLViewerTextureAnim::animateTextures(F32 &off_s, F32 &off_t,
00060                                                                                 F32 &scale_s, F32 &scale_t,
00061                                                                                 F32 &rot)
00062 {
00063         S32 result = 0;
00064         if (!(mMode & ON))
00065         {
00066                 mLastTime = 0.f;
00067                 mLastFrame = -1.f;
00068                 return result;
00069         }
00070 
00071 
00072         F32 num_frames = 1.0;
00073         F32 full_length = 1.0;
00074 
00075         if (mLength)
00076         {
00077                 num_frames = mLength;
00078         }
00079         else
00080         {
00081                 num_frames = llmax(1.f, (F32)(mSizeX * mSizeY));
00082         }
00083 
00084         if (mMode & PING_PONG)
00085         {
00086                 if (mMode & SMOOTH)
00087                 {
00088                         full_length = 2.f*num_frames;
00089                 }
00090                 else if (mMode & LOOP)
00091                 {
00092                         full_length = 2.f*num_frames - 2.f;
00093                         full_length = llmax(1.f, full_length);
00094                 }
00095                 else
00096                 {
00097                         full_length = 2.f*num_frames - 1.f;
00098                         full_length = llmax(1.f, full_length);
00099                 }
00100         }
00101         else
00102         {
00103                 full_length = num_frames;
00104         }
00105 
00106 
00107         F32 frame_counter;
00108         if (mMode & SMOOTH)
00109         {
00110                 frame_counter = mTimer.getElapsedTimeAndResetF32() * mRate + (F32)mLastTime;
00111         }
00112         else
00113         {
00114                 frame_counter = mTimer.getElapsedTimeF32() * mRate;
00115         }
00116         mLastTime = frame_counter;
00117 
00118         if (mMode & LOOP)
00119         {
00120                 frame_counter  = fmod(frame_counter, full_length);
00121         }
00122         else
00123         {
00124                 frame_counter = llmin(full_length - 1.f, frame_counter);
00125         }
00126 
00127         if (!(mMode & SMOOTH))
00128         {
00129                 frame_counter = (F32)llfloor(frame_counter + 0.01f);
00130         }
00131 
00132         if (mMode & PING_PONG)
00133         {
00134                 if (frame_counter >= num_frames)
00135                 {
00136                         if (mMode & SMOOTH)
00137                         {
00138                                 frame_counter = num_frames - (frame_counter - num_frames);
00139                         }
00140                         else
00141                         {
00142                                 frame_counter = (num_frames - 1.99f) - (frame_counter - num_frames);
00143                         }
00144                 }
00145         }
00146 
00147         if (mMode & REVERSE)
00148         {
00149                 if (mMode & SMOOTH)
00150                 {
00151                         frame_counter = num_frames - frame_counter;
00152                 }
00153                 else
00154                 {
00155                         frame_counter = (num_frames - 0.99f) - frame_counter;
00156                 }
00157         }
00158 
00159         frame_counter += mStart;
00160 
00161         if (!(mMode & SMOOTH))
00162         {
00163                 frame_counter = (F32)llround(frame_counter);
00164         }
00165 
00166         //
00167         // Now that we've calculated the frame time, do an update.
00168         // Will we correctly update stuff if the texture anim has
00169         // changed, but not the frame counter?
00170         //
00171         if (mLastFrame != frame_counter)
00172         {
00173                 mLastFrame = frame_counter;
00174                 if (mMode & ROTATE)
00175                 {
00176                         result |= ROTATE;
00177                         mRot = rot = frame_counter;
00178                 }
00179                 else if (mMode & SCALE)
00180                 {
00181                         result |= SCALE;
00182                         mScaleS = scale_s = frame_counter;
00183                         mScaleT = scale_t = frame_counter;
00184                 }
00185                 else
00186                 {
00187                         result |= TRANSLATE;
00188                         F32 x_frame;
00189                         S32 y_frame;
00190                         F32 x_pos;
00191                         F32 y_pos;
00192 
00193                         if (  (mSizeX)
00194                                 &&(mSizeY))
00195                         {
00196                                 result |= SCALE;
00197                                 mScaleS = scale_s = 1.f/mSizeX;
00198                                 mScaleT = scale_t = 1.f/mSizeY;
00199                                 x_frame = fmod(frame_counter, mSizeX);
00200                                 y_frame = (S32)(frame_counter / mSizeX);
00201                                 x_pos = x_frame * scale_s;
00202                                 y_pos = y_frame * scale_t;
00203                                 mOffS = off_s = (-0.5f + 0.5f*scale_s)+ x_pos;
00204                                 mOffT = off_t = (0.5f - 0.5f*scale_t) - y_pos;
00205                         }
00206                         else
00207                         {
00208                                 mScaleS = scale_s = 1.f;
00209                                 mScaleT = scale_t = 1.f;
00210                                 x_pos = frame_counter * scale_s;
00211                                 mOffS = off_s = (-0.5f + 0.5f*scale_s)+ x_pos;
00212                                 mOffT = off_t = 0.f;
00213                         }
00214                 }
00215         }
00216         return result;
00217 }

Generated on Thu Jul 1 06:09:34 2010 for Second Life Viewer by  doxygen 1.4.7