PageRenderTime 226ms CodeModel.GetById 80ms app.highlight 17ms RepoModel.GetById 124ms app.codeStats 0ms

/indra/newview/llviewertextureanim.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 212 lines | 160 code | 22 blank | 30 comment | 21 complexity | 672f5aa4a84bac623b4a1be6b027908d MD5 | raw file
  1/** 
  2 * @file llviewertextureanim.cpp
  3 * @brief LLViewerTextureAnim class implementation
  4 *
  5 * $LicenseInfo:firstyear=2003&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 "llviewertextureanim.h"
 30
 31#include "llmath.h"
 32#include "llerror.h"
 33
 34LLViewerTextureAnim::LLViewerTextureAnim() : LLTextureAnim()
 35{
 36	mLastFrame = -1.f;	// Force an update initially
 37	mLastTime = 0.f;
 38	mOffS = mOffT = 0;
 39	mScaleS = mScaleT = 1;
 40	mRot = 0;
 41}
 42
 43LLViewerTextureAnim::~LLViewerTextureAnim()
 44{
 45}
 46
 47void LLViewerTextureAnim::reset()
 48{
 49	LLTextureAnim::reset();
 50	mTimer.reset();
 51}
 52
 53
 54S32 LLViewerTextureAnim::animateTextures(F32 &off_s, F32 &off_t,
 55										F32 &scale_s, F32 &scale_t,
 56										F32 &rot)
 57{
 58	S32 result = 0;
 59	if (!(mMode & ON))
 60	{
 61		mLastTime = 0.f;
 62		mLastFrame = -1.f;
 63		return result;
 64	}
 65
 66
 67	F32 num_frames = 1.0;
 68	F32 full_length = 1.0;
 69
 70	if (mLength)
 71	{
 72		num_frames = mLength;
 73	}
 74	else
 75	{
 76		num_frames = llmax(1.f, (F32)(mSizeX * mSizeY));
 77	}
 78
 79	if (mMode & PING_PONG)
 80	{
 81		if (mMode & SMOOTH)
 82		{
 83			full_length = 2.f*num_frames;
 84		}
 85		else if (mMode & LOOP)
 86		{
 87			full_length = 2.f*num_frames - 2.f;
 88			full_length = llmax(1.f, full_length);
 89		}
 90		else
 91		{
 92			full_length = 2.f*num_frames - 1.f;
 93			full_length = llmax(1.f, full_length);
 94		}
 95	}
 96	else
 97	{
 98		full_length = num_frames;
 99	}
100
101
102	F32 frame_counter;
103	if (mMode & SMOOTH)
104	{
105		frame_counter = mTimer.getElapsedTimeAndResetF32() * mRate + (F32)mLastTime;
106	}
107	else
108	{
109		frame_counter = mTimer.getElapsedTimeF32() * mRate;
110	}
111	mLastTime = frame_counter;
112
113	if (mMode & LOOP)
114	{
115		frame_counter  = fmod(frame_counter, full_length);
116	}
117	else
118	{
119		frame_counter = llmin(full_length - 1.f, frame_counter);
120	}
121
122	if (!(mMode & SMOOTH))
123	{
124		frame_counter = (F32)llfloor(frame_counter + 0.01f);
125	}
126
127	if (mMode & PING_PONG)
128	{
129		if (frame_counter >= num_frames)
130		{
131			if (mMode & SMOOTH)
132			{
133				frame_counter = num_frames - (frame_counter - num_frames);
134			}
135			else
136			{
137				frame_counter = (num_frames - 1.99f) - (frame_counter - num_frames);
138			}
139		}
140	}
141
142	if (mMode & REVERSE)
143	{
144		if (mMode & SMOOTH)
145		{
146			frame_counter = num_frames - frame_counter;
147		}
148		else
149		{
150			frame_counter = (num_frames - 0.99f) - frame_counter;
151		}
152	}
153
154	frame_counter += mStart;
155
156	if (!(mMode & SMOOTH))
157	{
158		frame_counter = (F32)llround(frame_counter);
159	}
160
161	//
162	// Now that we've calculated the frame time, do an update.
163	// Will we correctly update stuff if the texture anim has
164	// changed, but not the frame counter?
165	//
166	if (mLastFrame != frame_counter)
167	{
168		mLastFrame = frame_counter;
169		if (mMode & ROTATE)
170		{
171			result |= ROTATE;
172			mRot = rot = frame_counter;
173		}
174		else if (mMode & SCALE)
175		{
176			result |= SCALE;
177			mScaleS = scale_s = frame_counter;
178			mScaleT = scale_t = frame_counter;
179		}
180		else
181		{
182			result |= TRANSLATE;
183			F32 x_frame;
184			S32 y_frame;
185			F32 x_pos;
186			F32 y_pos;
187
188			if (  (mSizeX)
189				&&(mSizeY))
190			{
191				result |= SCALE;
192				mScaleS = scale_s = 1.f/mSizeX;
193				mScaleT = scale_t = 1.f/mSizeY;
194				x_frame = fmod(frame_counter, mSizeX);
195				y_frame = (S32)(frame_counter / mSizeX);
196				x_pos = x_frame * scale_s;
197				y_pos = y_frame * scale_t;
198				mOffS = off_s = (-0.5f + 0.5f*scale_s)+ x_pos;
199				mOffT = off_t = (0.5f - 0.5f*scale_t) - y_pos;
200			}
201			else
202			{
203				mScaleS = scale_s = 1.f;
204				mScaleT = scale_t = 1.f;
205				x_pos = frame_counter * scale_s;
206				mOffS = off_s = (-0.5f + 0.5f*scale_s)+ x_pos;
207				mOffT = off_t = 0.f;
208			}
209		}
210	}
211	return result;
212}