1 /*
2 	Copyright (C) 2005-2007 Feeling Software Inc.
3 	Portions of the code are:
4 	Copyright (C) 2005-2007 Sony Computer Entertainment America
5 
6 	MIT License: http://www.opensource.org/licenses/mit-license.php
7 */
8 
9 /**
10 	@file FCDGeometrySource.h
11 	This file contains the FCDGeometrySource class.
12 */
13 
14 #ifndef _FCD_GEOMETRY_SOURCE_H_
15 #define _FCD_GEOMETRY_SOURCE_H_
16 
17 #ifndef __FCD_OBJECT_WITH_ID_H_
18 #include "FCDocument/FCDObjectWithId.h"
19 #endif // __FCD_OBJECT_WITH_ID_H_
20 #ifndef _FU_DAE_ENUM_H_
21 #include "FUtils/FUDaeEnum.h"
22 #endif // _FU_DAE_ENUM_H_
23 #ifndef _FCD_PARAMETER_ANIMATABLE_H_
24 #include "FCDocument/FCDParameterAnimatable.h"
25 #endif // _FCD_PARAMETER_ANIMATABLE_H_
26 
27 class FCDExtra;
28 
29 /**
30 	A COLLADA data source for geometric meshes.
31 
32 	A COLLADA data source for geometric meshes contains a list of floating-point values and the information
33 	to parse these floating-point values into meaningful content: the stride of the list and the type of data
34 	that the floating-point values represent. When the floating-point values are split according to the stride,
35 	you get the individual source values of the given type. A data source may also have a user-generated name to
36 	identify the data within. The name is optional and is used to keep
37 	around the user-friendly name for texture coordinate sets or color sets.
38 
39 	Each source values of the COLLADA data source may be animated individually, or together: as an element.
40 
41 	@ingroup FCDGeometry
42 */
43 class FCOLLADA_EXPORT FCDGeometrySource : public FCDObjectWithId
44 {
45 private:
46 	DeclareObjectType(FCDObjectWithId);
47 	DeclareParameter(fstring, FUParameterQualifiers::SIMPLE, name, FC("Name"));
48 	DeclareParameterListAnimatable(float, FUParameterQualifiers::SIMPLE, sourceData, FC("Data"))
49 	DeclareParameter(uint32, FUParameterQualifiers::SIMPLE, stride, FC("Stride"));
50 	DeclareParameter(uint32, FUParameterQualifiers::SIMPLE, sourceType, FC("Value Type")); // FUDaeGeometryInput::Semantic sourceType;
51 	DeclareParameterRef(FCDExtra, extra, FC("Extra Tree"));
52 
53 public:
54 	/** Constructor: do not use directly.
55 		Use FCDGeometryMesh::AddSource or FCDGeometryMesh::AddValueSource instead.
56 		@param document The COLLADA document which owns the data source. */
57 	FCDGeometrySource(FCDocument* document);
58 
59 	/** Destructor. */
60 	virtual ~FCDGeometrySource();
61 
62 	/** Copies the data source into a clone.
63 		The clone may reside in another document.
64 		@param clone The empty clone. If this pointer is NULL, a new data source
65 			will be created and you will need to release the returned pointer manually.
66 		@return The clone. */
67 	FCDGeometrySource* Clone(FCDGeometrySource* clone = NULL) const;
68 
69 	/** Retrieves the name of the data source. The name is optional and is used to
70 		keep around a user-friendly name for texture coordinate sets or color sets.
71 		@return The name of the data source. */
GetName()72 	inline const fstring& GetName() const { return name; }
73 
74 	/** Retrieves the pure data of the data source. This is a dynamically-sized array of
75 		floating-point values that contains all the data of the source.
76 		@return The pure data of the data source. */
GetData()77 	inline float* GetData() { return !sourceData.empty() ? &sourceData.front() : NULL; }
GetData()78 	inline const float* GetData() const { return !sourceData.empty() ? &sourceData.front() : NULL; } /**< See above. */
79 
80 	/** [INTERNAL] Retrieve the reference to the source data.
81 		@return The reference to the source data.
82 	*/
GetSourceData()83 	inline FCDParameterListAnimatableFloat& GetSourceData(){ return sourceData; }
GetSourceData()84 	inline const FCDParameterListAnimatableFloat& GetSourceData() const { return sourceData; }
85 
86 	/** Retrieves a ptr to the data of the data source. This allows external objects to
87 		store pointers to our data even when the data memory is reallocated
88 		@return The ptr to the pure data of the data source. */
GetDataPtr()89 	inline float** GetDataPtr() { return (float**) sourceData.GetDataPtr(); }
GetDataPtr()90 	inline const float** GetDataPtr() const { return (const float**) sourceData.GetDataPtr(); } /**< See above. */
91 
92 	/** Retrieves the amount of data inside the source.
93 		@return The number of data entries in the source. */
GetDataCount()94 	inline size_t GetDataCount() const { return sourceData.size(); }
95 
96 	/** Sets the amount of data contained inside the source.
97 		It is preferable to set the stride and to use SetValueCount.
98 		No initialization of new values is done.
99 		@param count The amount of data for the source to contain. */
100 	void SetDataCount(size_t count);
101 
102 	/** Retrieves the stride of the data within the source.
103 		There is no guarantee that the number of data values within the source is a multiple of the stride,
104 		yet you should always verify that the stride is at least the wanted dimension. For example, there is
105 		no guarantee that your vertex position data source has a stride of 3. 3dsMax is known to always
106 		export 3D texture coordinate positions.
107 		@return The stride of the data. */
GetStride()108 	inline uint32 GetStride() const { return stride; }
109 
110 	/** Retrieves the number of individual source values contained in the source.
111 		@return The number of source values. */
GetValueCount()112 	inline size_t GetValueCount() const { return sourceData.size() / stride; }
113 
114 	/** Retrieves the max number of values this input can handle before memory is reallocated.
115 		@return The number of source values. */
GetValueReserved()116 	inline size_t GetValueReserved() const { return sourceData.capacity() / stride; }
117 
118 	/** Sets the number of individual source values contained in the source.
119 		No initialization of new values is done.
120 		@param count The number of individual source values to contain in this source. */
SetValueCount(size_t count)121 	inline void SetValueCount(size_t count) { FUAssert(stride > 0, return); SetDataCount(count * stride); }
122 
123 	/** Retrieves one source value out of this source.
124 		@param index The index of the source value.
125 		@return The source value. */
GetValue(size_t index)126 	inline const float* GetValue(size_t index) const { FUAssert(index < GetValueCount(), return NULL); return &sourceData.at(index * stride); } /**< See above. */
127 
128 	/** Sets one source value out of this source.
129 		@param index The index of the source value.
130 		@param value The new value. */
SetValue(size_t index,const float * value)131 	inline void SetValue(size_t index, const float* value) { FUAssert(index < GetValueCount(), return); for (size_t i = 0; i < stride; ++i) sourceData.set(stride * index + i, value[i]); }
132 
133 	/** Retrieves the type of data contained within the source.
134 		Common values for the type of data are POSITION, NORMAL, COLOR and TEXCOORD.
135 		Please see FUDaeGeometryInput for more information.
136 		@see FUDaeGeometryInput.
137 		@return The type of data contained within the source. */
GetType()138 	inline FUDaeGeometryInput::Semantic GetType() const { return (FUDaeGeometryInput::Semantic) *sourceType; }
139 
140 	/** Sets the type of data contained within the source.
141 		Modifying the source type of an existing source is not recommended.
142 		Common values for the type of data are POSITION, NORMAL, COLOR and TEXCOORD.
143 		Please see FUDaeGeometryInput for more information.
144 		@see FUDaeGeometryInput.
145 		@param type The type of data to be contained within the source. */
SetType(FUDaeGeometryInput::Semantic type)146 	inline void SetType(FUDaeGeometryInput::Semantic type) { sourceType = type; }
147 
148 	/** Retrieves the list of animated values for the data of the source.
149 		@return The list of animated values. */
GetAnimatedValues()150 	inline FUObjectContainer<FCDAnimated>& GetAnimatedValues() { return sourceData.GetAnimatedValues(); }
GetAnimatedValues()151 	inline const FUObjectContainer<FCDAnimated>& GetAnimatedValues() const { return sourceData.GetAnimatedValues(); } /**< See above. */
152 
153 	/** Sets the user-friendly name of the data source. The name is optional and is used to
154 		keep around a user-friendly name for texture coordinate sets or color sets.
155 		@param _name The user-friendly name of the data source. */
SetName(const fstring & _name)156 	inline void SetName(const fstring& _name) { name = _name; SetDirtyFlag(); }
157 
158 	/** Overwrites the data contained within the data source.
159 		@param _sourceData The new data for this source.
160 		@param _sourceStride The stride for the new data.
161 		@param offset The offset at which to start retrieving the new data.
162 			This argument defaults at 0 to indicate that the data copy should start from the beginning.
163 		@param count The number of data entries to copy into the data source.
164 			This argument defaults at 0 to indicate that the data copy should include everything. */
165 	void SetData(const FloatList& _sourceData, uint32 _sourceStride, size_t count=0, size_t offset=0);
166 
167 	/** Sets the stride for the source data.
168 		@param _stride The stride for the source data. */
SetStride(uint32 _stride)169 	inline void SetStride(uint32 _stride) { stride = _stride; SetDirtyFlag(); }
170 
171 	/** [INTERNAL] Set the source type.
172 		@param type The source type. */
SetSourceType(FUDaeGeometryInput::Semantic type)173 	void SetSourceType(FUDaeGeometryInput::Semantic type) { sourceType = type; }
174 
175 	/** Retrieves the extra information contained by this data source.
176 		@return The extra tree. This pointer will be NULL,
177 			in the const-version of this function, if there is no extra information.
178 			In the modifiable-version of this function:
179 			you will always get a valid extra tree that you can fill in. */
180 	FCDExtra* GetExtra();
GetExtra()181 	inline const FCDExtra* GetExtra() const { return extra; } /**< See above. */
182 
183 	/** [INTERNAL] Clones this data source. You will need to release the returned pointer manually.
184 		@return An identical copy of the data source. */
185 	FCDGeometrySource* Clone() const;
186 };
187 
188 #endif // _FCD_GEOMETRY_SOURCE_H_
189