PageRenderTime 63ms CodeModel.GetById 26ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llsprite.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 308 lines | 200 code | 59 blank | 49 comment | 9 complexity | 824de7350336dfe4db8e28b403015d16 MD5 | raw file
  1/** 
  2 * @file llsprite.cpp
  3 * @brief LLSprite class implementation
  4 *
  5 * $LicenseInfo:firstyear=2000&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/* -*- c++ -*-
 28 *	Notes:
 29 *		PR - Should add a creator that can take a pointer rather than handle for streaming 
 30 *		object textures.
 31 *		PR - Need to add support for lit/non-lit conditions, set normals?
 32 */
 33
 34#include "llviewerprecompiledheaders.h"
 35
 36#include <llglheaders.h>
 37
 38#include "llsprite.h"
 39
 40#include "math.h"
 41
 42#include "lldrawable.h"
 43#include "llface.h"
 44#include "llviewercamera.h"
 45#include "llviewertexturelist.h"
 46
 47LLVector3 LLSprite::sCameraUp(0.0f,0.0f,1.0f);
 48LLVector3 LLSprite::sCameraRight(1.0f,0.0f,0.0f);
 49LLVector3 LLSprite::sCameraPosition(0.f, 0.f, 0.f);
 50LLVector3 LLSprite::sNormal(0.0f,0.0f,0.0f);
 51
 52//////////////////////////////////////////////////////////////////////
 53// Construction/Destruction
 54//////////////////////////////////////////////////////////////////////
 55
 56// A simple initialization
 57LLSprite::LLSprite(const LLUUID &image_uuid) :
 58	mImageID(image_uuid),
 59	mImagep(NULL),
 60	mPitch(0.f),
 61	mYaw(0.f),
 62	mPosition(0.0f, 0.0f, 0.0f),
 63	mFollow(TRUE),
 64	mUseCameraUp(TRUE),
 65	mColor(0.5f, 0.5f, 0.5f, 1.0f),
 66	mTexMode(GL_REPLACE)
 67{
 68	setSize(1.0f, 1.0f);
 69}
 70
 71//////////////////////////////////////////////////////////////////////
 72LLSprite::~LLSprite()
 73{
 74}
 75
 76void LLSprite::updateFace(LLFace &face)
 77{
 78	LLViewerCamera &camera = *LLViewerCamera::getInstance();
 79
 80	// First, figure out how many vertices/indices we need.
 81	U32 num_vertices, num_indices;
 82	U32 vertex_count = 0;
 83	
 84	// Get the total number of vertices and indices
 85	if (mFollow)
 86	{
 87		num_vertices = 4;
 88		num_indices = 6;
 89	}
 90	else
 91	{
 92		num_vertices = 4;
 93		num_indices = 12;
 94	}
 95
 96	face.setSize(num_vertices, num_indices);
 97	
 98	if (mFollow) 
 99	{
100		sCameraUp = camera.getUpAxis();
101		sCameraRight = -camera.getLeftAxis();
102		sCameraPosition = camera.getOrigin();
103		sNormal = -camera.getAtAxis();
104		if (mUseCameraUp)
105		{
106			// these need to live here because the height/width may change between render calls
107			mScaledUp = sCameraUp;
108			mScaledRight = sCameraRight;
109
110			mScaledUp *= mHeightDiv2;
111			mScaledRight *= mWidthDiv2;
112
113			mA = mPosition + mScaledRight + mScaledUp;
114			mB = mPosition - mScaledRight + mScaledUp;
115			mC = mPosition - mScaledRight - mScaledUp;
116			mD = mPosition + mScaledRight - mScaledUp;
117		}
118		else
119		{
120			// The up vector is perpendicular to the camera vector...
121			LLVector3 camera_vec = mPosition - sCameraPosition;
122			mScaledRight = camera_vec % LLVector3(0.f, 0.f, 1.f);
123			mScaledUp = -(camera_vec % mScaledRight);
124			mScaledUp.normalize();
125			mScaledRight.normalize();
126			mScaledUp *= mHeightDiv2;
127			mScaledRight *= mWidthDiv2;
128
129			mA = mPosition + mScaledRight + mScaledUp;
130			mB = mPosition - mScaledRight + mScaledUp;
131			mC = mPosition - mScaledRight - mScaledUp;
132			mD = mPosition + mScaledRight - mScaledUp;
133		}
134	}
135	else
136	{
137		// this is equivalent to how it was done before. . . 
138		// we need to establish a way to 
139		// identify the orientation of a particular sprite rather than
140		// just banging it in on the x,z plane if it's not following the camera.
141
142		LLVector3 x_axis;
143		LLVector3 y_axis;
144
145		F32 dot = sNormal * LLVector3(0.f, 1.f, 0.f);
146		if (dot == 1.f || dot == -1.f)
147		{
148			x_axis.setVec(1.f, 0.f, 0.f);
149			y_axis.setVec(0.f, 1.f, 0.f);
150		}
151		else
152		{
153			x_axis = sNormal % LLVector3(0.f, -1.f, 0.f);
154			x_axis.normalize();
155
156			y_axis = sNormal % x_axis;
157		}
158
159		LLQuaternion yaw_rot(mYaw, sNormal);
160
161		// rotate axes by specified yaw
162		x_axis = x_axis * yaw_rot;
163		y_axis = y_axis * yaw_rot;
164
165		// rescale axes by width and height of sprite
166		x_axis = x_axis * mWidthDiv2;
167		y_axis = y_axis *  mHeightDiv2;
168
169		mA = -x_axis + y_axis;
170		mB = x_axis + y_axis;
171		mC = x_axis - y_axis;
172		mD = -x_axis - y_axis;
173
174		mA += mPosition;
175		mB += mPosition;
176		mC += mPosition;
177		mD += mPosition;
178	}
179
180	face.setFaceColor(mColor);
181
182	LLStrider<LLVector3> verticesp;
183	LLStrider<LLVector3> normalsp;
184	LLStrider<LLVector2> tex_coordsp;
185	LLStrider<U16> indicesp;
186	U16 index_offset;
187
188	// Setup face
189	if (!face.getVertexBuffer())
190	{	
191		LLVertexBuffer* buff = new LLVertexBuffer(LLVertexBuffer::MAP_VERTEX | 
192												LLVertexBuffer::MAP_TEXCOORD0,
193												GL_STREAM_DRAW_ARB);
194		buff->allocateBuffer(4, 12, TRUE);
195		face.setGeomIndex(0);
196		face.setIndicesIndex(0);
197		face.setVertexBuffer(buff);
198	}
199		
200	index_offset = face.getGeometry(verticesp,normalsp,tex_coordsp, indicesp);
201
202	*tex_coordsp = LLVector2(0.f, 0.f);
203	*verticesp = mC;
204	tex_coordsp++;
205	verticesp++;
206	vertex_count++;
207
208	*tex_coordsp = LLVector2(0.f, 1.f);
209	*verticesp = mB;
210	tex_coordsp++;
211	verticesp++;
212	vertex_count++;
213
214	*tex_coordsp = LLVector2(1.f, 1.f);
215	*verticesp = mA;
216	tex_coordsp++;
217	verticesp++;
218	vertex_count++;
219
220	*tex_coordsp = LLVector2(1.f, 0.0f);
221	*verticesp = mD;
222	tex_coordsp++;
223	verticesp++;
224	vertex_count++;
225
226	// Generate indices, since they're easy.
227	// Just a series of quads.
228	*indicesp++ = index_offset;
229	*indicesp++ = 2 + index_offset;
230	*indicesp++ = 1 + index_offset;
231
232	*indicesp++ = index_offset;
233	*indicesp++ = 3 + index_offset;
234	*indicesp++ = 2 + index_offset;
235
236	if (!mFollow)
237	{
238		*indicesp++ = 0 + index_offset;
239		*indicesp++ = 1 + index_offset;
240		*indicesp++ = 2 + index_offset;
241		*indicesp++ = 0 + index_offset;
242		*indicesp++ = 2 + index_offset;
243		*indicesp++ = 3 + index_offset;
244	}
245
246	face.getVertexBuffer()->flush();
247	face.mCenterAgent = mPosition;
248}
249
250void LLSprite::setPosition(const LLVector3 &position) 
251{
252	mPosition = position; 
253}
254
255
256void LLSprite::setPitch(const F32 pitch) 
257{
258	mPitch = pitch; 
259}
260
261
262void LLSprite::setSize(const F32 width, const F32 height) 
263{
264	mWidth = width; 
265	mHeight = height;
266	mWidthDiv2 = width/2.0f;
267	mHeightDiv2 = height/2.0f;
268}
269
270void LLSprite::setYaw(F32 yaw) 
271{
272	mYaw = yaw; 
273}
274
275void LLSprite::setFollow(const BOOL follow)
276{
277	mFollow = follow; 
278}
279
280void LLSprite::setUseCameraUp(const BOOL use_up)
281{
282	mUseCameraUp = use_up; 
283}
284
285void LLSprite::setTexMode(const LLGLenum mode) 
286{
287	mTexMode = mode; 
288}
289
290void LLSprite::setColor(const LLColor4 &color)
291{
292	mColor = color; 
293}
294
295void LLSprite::setColor(const F32 r, const F32 g, const F32 b, const F32 a)
296{
297	mColor.setVec(r, g, b, a);
298}
299
300
301
302
303
304
305
306
307
308