PageRenderTime 75ms CodeModel.GetById 31ms app.highlight 26ms RepoModel.GetById 14ms app.codeStats 0ms

/indra/newview/llpolymesh.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 438 lines | 235 code | 88 blank | 115 comment | 2 complexity | 511b513a626010c555b28426d09b5e71 MD5 | raw file
  1/** 
  2 * @file llpolymesh.h
  3 * @brief Implementation of LLPolyMesh class
  4 *
  5 * $LicenseInfo:firstyear=2001&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#ifndef LL_LLPOLYMESH_H
 28#define LL_LLPOLYMESH_H
 29
 30#include <string>
 31#include <map>
 32#include "llstl.h"
 33
 34#include "v3math.h"
 35#include "v2math.h"
 36#include "llquaternion.h"
 37#include "llpolymorph.h"
 38#include "lljoint.h"
 39//#include "lldarray.h"
 40
 41class LLSkinJoint;
 42class LLVOAvatar;
 43class LLWearable;
 44
 45//#define USE_STRIPS	// Use tri-strips for rendering.
 46
 47//-----------------------------------------------------------------------------
 48// LLPolyFace
 49// A set of 4 vertex indices.
 50// An LLPolyFace can represent either a triangle or quad.
 51// If the last index is -1, it's a triangle.
 52//-----------------------------------------------------------------------------
 53typedef S32 LLPolyFace[3];
 54
 55//struct PrimitiveGroup;
 56
 57//-----------------------------------------------------------------------------
 58// LLPolyMesh
 59// A polyhedra consisting of any number of triangles and quads.
 60// All instances contain a set of faces, and optionally may include
 61// faces grouped into named face sets.
 62//-----------------------------------------------------------------------------
 63class LLPolyMorphTarget;
 64
 65class LLPolyMeshSharedData
 66{
 67	friend class LLPolyMesh;
 68private:
 69	// transform data
 70	LLVector3				mPosition;
 71	LLQuaternion			mRotation;
 72	LLVector3				mScale;
 73							
 74	// vertex data			
 75	S32						mNumVertices;
 76	LLVector3				*mBaseCoords;
 77	LLVector3				*mBaseNormals;
 78	LLVector3				*mBaseBinormals;
 79	LLVector2				*mTexCoords;
 80	LLVector2				*mDetailTexCoords;
 81	F32						*mWeights;
 82	
 83	BOOL					mHasWeights;
 84	BOOL					mHasDetailTexCoords;
 85
 86	// face data			
 87	S32						mNumFaces;
 88	LLPolyFace				*mFaces;
 89							
 90	// face set data		
 91	U32						mNumJointNames;
 92	std::string*			mJointNames;
 93
 94	// morph targets
 95	typedef std::set<LLPolyMorphData*> morphdata_list_t;
 96	morphdata_list_t			mMorphData;
 97
 98	std::map<S32, S32> 			mSharedVerts;
 99	
100	LLPolyMeshSharedData*		mReferenceData;
101	S32							mLastIndexOffset;
102
103public:
104	// Temporarily...
105	// Triangle indices
106	U32				mNumTriangleIndices;
107	U32				*mTriangleIndices;
108
109public:
110	LLPolyMeshSharedData();
111	~LLPolyMeshSharedData();
112
113private:
114	void setupLOD(LLPolyMeshSharedData* reference_data);
115
116	// Frees all mesh memory resources 
117	void freeMeshData();
118
119	void setPosition( const LLVector3 &pos ) { 	mPosition = pos; }
120	void setRotation( const LLQuaternion &rot ) { mRotation = rot; }
121	void setScale( const LLVector3 &scale ) { mScale = scale; }
122
123	BOOL allocateVertexData( U32 numVertices );
124
125	BOOL allocateFaceData( U32 numFaces );
126
127	BOOL allocateJointNames( U32 numJointNames );
128
129	// Retrieve the number of KB of memory used by this instance
130	U32 getNumKB();
131
132	// Load mesh data from file
133	BOOL loadMesh( const std::string& fileName );
134
135public:
136	void genIndices(S32 offset);
137
138	const LLVector2 &getUVs(U32 index);
139
140	const S32	*getSharedVert(S32 vert);
141
142	BOOL isLOD() { return (mReferenceData != NULL); }
143};
144
145
146class LLJointRenderData
147{
148public:
149	LLJointRenderData(const LLMatrix4* world_matrix, LLSkinJoint* skin_joint) : mWorldMatrix(world_matrix), mSkinJoint(skin_joint) {}
150	~LLJointRenderData(){}
151
152	const LLMatrix4*		mWorldMatrix;
153	LLSkinJoint*			mSkinJoint;
154};
155
156
157class LLPolyMesh
158{
159public:
160	
161	// Constructor
162	LLPolyMesh(LLPolyMeshSharedData *shared_data, LLPolyMesh *reference_mesh);
163
164	// Destructor 
165	~LLPolyMesh();
166
167	// Requests a mesh by name.
168	// If the mesh already exists in the global mesh table, it is returned,
169	// otherwise it is loaded from file, added to the table, and returned.
170	static LLPolyMesh *getMesh( const std::string &name, LLPolyMesh* reference_mesh = NULL);
171
172	// Frees all loaded meshes.
173	// This should only be called once you know there are no outstanding
174	// references to these objects.  Generally, upon exit of the application.
175	static void freeAllMeshes();
176
177	//--------------------------------------------------------------------
178	// Transform Data Access
179	//--------------------------------------------------------------------
180	// Get position
181	const LLVector3 &getPosition() { 
182		llassert (mSharedData);
183		return mSharedData->mPosition; 
184	}
185
186	// Get rotation
187	const LLQuaternion &getRotation() { 
188		llassert (mSharedData);
189		return mSharedData->mRotation; 
190	}
191
192	// Get scale
193	const LLVector3 &getScale() { 
194		llassert (mSharedData);
195		return mSharedData->mScale; 
196	}
197
198	//--------------------------------------------------------------------
199	// Vertex Data Access
200	//--------------------------------------------------------------------
201	// Get number of vertices
202	U32 getNumVertices() { 
203		llassert (mSharedData);
204		return mSharedData->mNumVertices; 
205	}
206
207	// Returns whether or not the mesh has detail texture coords
208	BOOL hasDetailTexCoords() { 
209		llassert (mSharedData);
210		return mSharedData->mHasDetailTexCoords; 
211	}
212
213	// Returns whether or not the mesh has vertex weights
214	BOOL hasWeights() const{ 
215		llassert (mSharedData);
216		return mSharedData->mHasWeights; 
217	}
218
219	// Get coords
220	const LLVector4	*getCoords() const{
221		return mCoords;
222	}
223
224	// non const version
225	LLVector4 *getWritableCoords();
226
227	// Get normals
228	const LLVector4	*getNormals() const{ 
229		return mNormals; 
230	}
231
232	// Get normals
233	const LLVector3	*getBinormals() const{ 
234		return mBinormals; 
235	}
236
237	// Get base mesh normals
238	const LLVector3 *getBaseNormals() const{
239		llassert(mSharedData);
240		return mSharedData->mBaseNormals;
241	}
242
243	// Get base mesh normals
244	const LLVector3 *getBaseBinormals() const{
245		llassert(mSharedData);
246		return mSharedData->mBaseBinormals;
247	}
248
249	// intermediate morphed normals and output normals
250	LLVector4 *getWritableNormals();
251	LLVector3 *getScaledNormals();
252
253	LLVector3 *getWritableBinormals();
254	LLVector3 *getScaledBinormals();
255
256	// Get texCoords
257	const LLVector2	*getTexCoords() const { 
258		return mTexCoords; 
259	}
260
261	// non const version
262	LLVector2 *getWritableTexCoords();
263
264	// Get detailTexCoords
265	const LLVector2	*getDetailTexCoords() const { 
266		llassert (mSharedData);
267		return mSharedData->mDetailTexCoords; 
268	}
269
270	// Get weights
271	const F32 *getWeights() const {
272		llassert (mSharedData);
273		return mSharedData->mWeights;
274	}
275
276	F32			*getWritableWeights() const;
277
278	LLVector4	*getWritableClothingWeights();
279
280	const LLVector4		*getClothingWeights()
281	{
282		return mClothingWeights;	
283	}
284
285	//--------------------------------------------------------------------
286	// Face Data Access
287	//--------------------------------------------------------------------
288	// Get number of faces
289	S32 getNumFaces() { 
290		llassert (mSharedData);
291		return mSharedData->mNumFaces; 
292	}
293
294	// Get faces
295	LLPolyFace *getFaces() { 
296		llassert (mSharedData);
297		return mSharedData->mFaces;
298	}
299
300	U32 getNumJointNames() { 
301		llassert (mSharedData);
302		return mSharedData->mNumJointNames; 
303	}
304
305	std::string *getJointNames() { 
306		llassert (mSharedData);
307		return mSharedData->mJointNames;
308	}
309
310	LLPolyMorphData*	getMorphData(const std::string& morph_name);
311// 	void	removeMorphData(LLPolyMorphData *morph_target);
312// 	void	deleteAllMorphData();
313
314	LLPolyMeshSharedData *getSharedData() const;
315	LLPolyMesh *getReferenceMesh() { return mReferenceMesh ? mReferenceMesh : this; }
316
317	// Get indices
318	U32*	getIndices() { return mSharedData ? mSharedData->mTriangleIndices : NULL; }
319
320	BOOL	isLOD() { return mSharedData && mSharedData->isLOD(); }
321
322	void setAvatar(LLVOAvatar* avatarp) { mAvatarp = avatarp; }
323	LLVOAvatar* getAvatar() { return mAvatarp; }
324
325	LLDynamicArray<LLJointRenderData*>	mJointRenderData;
326
327	U32				mFaceVertexOffset;
328	U32				mFaceVertexCount;
329	U32				mFaceIndexOffset;
330	U32				mFaceIndexCount;
331	U32				mCurVertexCount;
332private:
333	void initializeForMorph();
334
335	// Dumps diagnostic information about the global mesh table
336	static void dumpDiagInfo();
337
338protected:
339	// mesh data shared across all instances of a given mesh
340	LLPolyMeshSharedData	*mSharedData;
341	// Single array of floats for allocation / deletion
342	F32						*mVertexData;
343	// deformed vertices (resulting from application of morph targets)
344	LLVector4				*mCoords;
345	// deformed normals (resulting from application of morph targets)
346	LLVector3				*mScaledNormals;
347	// output normals (after normalization)
348	LLVector4				*mNormals;
349	// deformed binormals (resulting from application of morph targets)
350	LLVector3				*mScaledBinormals;
351	// output binormals (after normalization)
352	LLVector3				*mBinormals;
353	// weight values that mark verts as clothing/skin
354	LLVector4				*mClothingWeights;
355	// output texture coordinates
356	LLVector2				*mTexCoords;
357	
358	LLPolyMesh				*mReferenceMesh;
359
360	// global mesh list
361	typedef std::map<std::string, LLPolyMeshSharedData*> LLPolyMeshSharedDataTable; 
362	static LLPolyMeshSharedDataTable sGlobalSharedMeshList;
363
364	// Backlink only; don't make this an LLPointer.
365	LLVOAvatar* mAvatarp;
366};
367
368//-----------------------------------------------------------------------------
369// LLPolySkeletalDeformationInfo
370// Shared information for LLPolySkeletalDeformations
371//-----------------------------------------------------------------------------
372struct LLPolySkeletalBoneInfo
373{
374	LLPolySkeletalBoneInfo(std::string &name, LLVector3 &scale, LLVector3 &pos, BOOL haspos)
375		: mBoneName(name),
376		  mScaleDeformation(scale),
377		  mPositionDeformation(pos),
378		  mHasPositionDeformation(haspos) {}
379	std::string mBoneName;
380	LLVector3 mScaleDeformation;
381	LLVector3 mPositionDeformation;
382	BOOL mHasPositionDeformation;
383};
384
385class LLPolySkeletalDistortionInfo : public LLViewerVisualParamInfo
386{
387	friend class LLPolySkeletalDistortion;
388public:
389	LLPolySkeletalDistortionInfo();
390	/*virtual*/ ~LLPolySkeletalDistortionInfo() {};
391	
392	/*virtual*/ BOOL parseXml(LLXmlTreeNode* node);
393
394protected:
395	typedef std::vector<LLPolySkeletalBoneInfo> bone_info_list_t;
396	bone_info_list_t mBoneInfoList;
397};
398
399//-----------------------------------------------------------------------------
400// LLPolySkeletalDeformation
401// A set of joint scale data for deforming the avatar mesh
402//-----------------------------------------------------------------------------
403class LLPolySkeletalDistortion : public LLViewerVisualParam
404{
405public:
406	LLPolySkeletalDistortion(LLVOAvatar *avatarp);
407	~LLPolySkeletalDistortion();
408
409	// Special: These functions are overridden by child classes
410	LLPolySkeletalDistortionInfo*	getInfo() const { return (LLPolySkeletalDistortionInfo*)mInfo; }
411	//   This sets mInfo and calls initialization functions
412	BOOL							setInfo(LLPolySkeletalDistortionInfo *info);
413
414	/*virtual*/ LLViewerVisualParam* cloneParam(LLWearable* wearable) const;
415
416	// LLVisualParam Virtual functions
417	///*virtual*/ BOOL				parseData(LLXmlTreeNode* node);
418	/*virtual*/ void				apply( ESex sex );
419	
420	// LLViewerVisualParam Virtual functions
421	/*virtual*/ F32					getTotalDistortion() { return 0.1f; }
422	/*virtual*/ const LLVector3&	getAvgDistortion()	{ return mDefaultVec; }
423	/*virtual*/ F32					getMaxDistortion() { return 0.1f; }
424	/*virtual*/ LLVector3			getVertexDistortion(S32 index, LLPolyMesh *poly_mesh){return LLVector3(0.001f, 0.001f, 0.001f);}
425	/*virtual*/ const LLVector3*	getFirstDistortion(U32 *index, LLPolyMesh **poly_mesh){index = 0; poly_mesh = NULL; return &mDefaultVec;};
426	/*virtual*/ const LLVector3*	getNextDistortion(U32 *index, LLPolyMesh **poly_mesh){index = 0; poly_mesh = NULL; return NULL;};
427
428protected:
429	typedef std::map<LLJoint*, LLVector3> joint_vec_map_t;
430	joint_vec_map_t mJointScales;
431	joint_vec_map_t mJointOffsets;
432	LLVector3	mDefaultVec;
433	// Backlink only; don't make this an LLPointer.
434	LLVOAvatar *mAvatar;
435};
436
437#endif // LL_LLPOLYMESH_H
438