1 /*
2     Copyright (c) 2008-2009 NetAllied Systems GmbH
3 
4 	This file is part of COLLADAMax.
5 
6     Portions of the code are:
7     Copyright (c) 2005-2007 Feeling Software Inc.
8     Copyright (c) 2005-2007 Sony Computer Entertainment America
9 
10     Based on the 3dsMax COLLADASW Tools:
11     Copyright (c) 2005-2006 Autodesk Media Entertainment
12 
13     Licensed under the MIT Open Source License,
14     for details please see LICENSE file or the website
15     http://www.opensource.org/licenses/mit-license.php
16 */
17 
18 
19 #include "COLLADAMaxStableHeaders.h"
20 #include "COLLADAMaxExtraDataHandler.h"
21 #include "generated14/COLLADASaxFWLColladaParserAutoGen14Attributes.h"
22 
23 #include "GeneratedSaxParserUtils.h"
24 
25 
26 namespace COLLADAMax
27 {
28 
29 	static const char* MAX_EXTRA_ATTRIBUTE_SKYLIGHT = "skylight";
30 	static const char* MAX_EXTRA_ATTRIBUTE_CAST_SHADOWS = "cast_shadows";
31 	static const char* MAX_EXTRA_ATTRIBUTE_COLOR_MAP_AMOUNT = "color_map_amount";
32 	static const char* MAX_EXTRA_ATTRIBUTE_COLOR_MAP_ON = "color_map_on";
33 	static const char* MAX_EXTRA_ATTRIBUTE_INTENSITY_ON = "intensity_on";
34 	static const char* MAX_EXTRA_ATTRIBUTE_MODE = "mode";
35 	static const char* MAX_EXTRA_ATTRIBUTE_MULTIPLIER = "multiplier";
36 	static const char* MAX_EXTRA_ATTRIBUTE_RAY_BIAS = "ray_bias";
37 	static const char* MAX_EXTRA_ATTRIBUTE_RAYS_PER_SAMPLE = "rays_per_sample";
38 
39 
40 	static const char* MAX_EXTRA_ATTRIBUTE_BUMP = "bump";
41 	static const char* MAX_EXTRA_ATTRIBUTE_TEXTURE = "texture";
42 
43 
44 	//------------------------------
ExtraDataHandler(DocumentImporter * colladaImporter)45 	ExtraDataHandler::ExtraDataHandler(DocumentImporter* colladaImporter)
46 		: ImporterBase(colladaImporter)
47 		, mTextBuffer()
48 		, mExtraTagType(EXTRA_TAG_TYPE_UNKNOWN)
49 		, mCurrentElementUniqueId()
50 	{
51 
52 	}
53 
54 	//------------------------------
~ExtraDataHandler()55 	ExtraDataHandler::~ExtraDataHandler()
56 	{
57 
58 	}
59 
60 
61 	//------------------------------
elementBegin(const COLLADASaxFWL::ParserChar * elementName,const GeneratedSaxParser::xmlChar ** attributes)62 	bool ExtraDataHandler::elementBegin( const COLLADASaxFWL::ParserChar* elementName, const GeneratedSaxParser::xmlChar** attributes )
63 	{
64 
65 		switch ( mExtraTagType )
66 		{
67 		case EXTRA_TAG_TYPE_UNKNOWN:
68 			{
69 				if ( strcmp(elementName, MAX_EXTRA_ATTRIBUTE_SKYLIGHT) == 0 )
70 				{
71 					mExtraTagType = EXTRA_TAG_TYPE_SKYLIGHT;
72 					mExtraParameters.skyLightParameters.castShadows = false;
73 					mExtraParameters.skyLightParameters.colorMapAmount = 100;
74 					mExtraParameters.skyLightParameters.colorMapOn = true;
75 					mExtraParameters.skyLightParameters.intensityOn = true;
76 					mExtraParameters.skyLightParameters.mode = 1.0f;
77 					mExtraParameters.skyLightParameters.multiplier = 1.0f;
78 					mExtraParameters.skyLightParameters.rayBias = 0.004999995f;
79 					mExtraParameters.skyLightParameters.raysPerSample = 20;
80 				}
81 
82 				else if( strcmp(elementName, MAX_EXTRA_ATTRIBUTE_BUMP) == 0 )
83 				{
84 					determineBumpType(attributes);
85 					if( mExtraParameters.bumpParameters.bumpType != BUMP_TYPE_INVALID )
86 					{
87 						mExtraTagType = EXTRA_TAG_TYPE_BUMP;
88 						mExtraParameters.bumpParameters.textureAttributes = 0;
89 					}
90 				}
91 			}
92 			break;
93 		case EXTRA_TAG_TYPE_BUMP:
94 			if( strcmp(elementName, MAX_EXTRA_ATTRIBUTE_TEXTURE) == 0 )
95 			{
96 				determineBumpTextureSamplerAndTexCoord(attributes);
97 			}
98 		}
99 		return true;
100 	}
101 
102 	//------------------------------
elementEnd(const COLLADASaxFWL::ParserChar * elementName)103 	bool ExtraDataHandler::elementEnd( const COLLADASaxFWL::ParserChar* elementName )
104 	{
105 		bool failed = false;
106 		switch ( mExtraTagType )
107 		{
108 		case EXTRA_TAG_TYPE_SKYLIGHT:
109 			{
110 				if ( strcmp(elementName, MAX_EXTRA_ATTRIBUTE_SKYLIGHT) == 0 )
111 				{
112 					mExtraTagType = EXTRA_TAG_TYPE_UNKNOWN;
113 					addUniqueIdSkyLightParametersPair(mCurrentElementUniqueId, mExtraParameters.skyLightParameters);
114 				}
115 				else if ( strcmp(elementName, MAX_EXTRA_ATTRIBUTE_CAST_SHADOWS) == 0 )
116 				{
117 					bool val = GeneratedSaxParser::Utils::toBool(mTextBuffer.c_str(), failed);
118 					if ( !failed )
119 					{
120 						mExtraParameters.skyLightParameters.castShadows = val;
121 					}
122 				}
123 				else if ( strcmp(elementName, MAX_EXTRA_ATTRIBUTE_COLOR_MAP_AMOUNT) == 0 )
124 				{
125 					float val = GeneratedSaxParser::Utils::toFloat(mTextBuffer.c_str(), failed);
126 					if ( !failed )
127 					{
128 						mExtraParameters.skyLightParameters.colorMapAmount = val;
129 					}
130 				}
131 				else if ( strcmp(elementName, MAX_EXTRA_ATTRIBUTE_COLOR_MAP_ON) == 0 )
132 				{
133 					bool val = GeneratedSaxParser::Utils::toBool(mTextBuffer.c_str(), failed);
134 					if ( !failed )
135 					{
136 						mExtraParameters.skyLightParameters.colorMapOn = val;
137 					}
138 				}
139 				else if ( strcmp(elementName, MAX_EXTRA_ATTRIBUTE_INTENSITY_ON) == 0 )
140 				{
141 					bool val = GeneratedSaxParser::Utils::toBool(mTextBuffer.c_str(), failed);
142 					if ( !failed )
143 					{
144 						mExtraParameters.skyLightParameters.castShadows = val;
145 					}
146 				}
147 				else if ( strcmp(elementName, MAX_EXTRA_ATTRIBUTE_MODE) == 0 )
148 				{
149 					float val = GeneratedSaxParser::Utils::toFloat(mTextBuffer.c_str(), failed);
150 					if ( !failed )
151 					{
152 						mExtraParameters.skyLightParameters.mode = val;
153 					}
154 				}
155 				else if ( strcmp(elementName, MAX_EXTRA_ATTRIBUTE_MULTIPLIER) == 0 )
156 				{
157 					float val = GeneratedSaxParser::Utils::toFloat(mTextBuffer.c_str(), failed);
158 					if ( !failed )
159 					{
160 						mExtraParameters.skyLightParameters.multiplier = val;
161 					}
162 				}
163 				else if ( strcmp(elementName, MAX_EXTRA_ATTRIBUTE_RAY_BIAS) == 0 )
164 				{
165 					float val = GeneratedSaxParser::Utils::toFloat(mTextBuffer.c_str(), failed);
166 					if ( !failed )
167 					{
168 						mExtraParameters.skyLightParameters.rayBias = val;
169 					}
170 				}
171 				else if ( strcmp(elementName, MAX_EXTRA_ATTRIBUTE_RAYS_PER_SAMPLE) == 0 )
172 				{
173 					int val = GeneratedSaxParser::Utils::toSint32(mTextBuffer.c_str(), failed);
174 					if ( !failed )
175 					{
176 						mExtraParameters.skyLightParameters.raysPerSample = val;
177 					}
178 				}
179 			}
180 			break;
181 		case EXTRA_TAG_TYPE_BUMP:
182 			{
183 				if ( strcmp(elementName, MAX_EXTRA_ATTRIBUTE_BUMP) == 0 )
184 				{
185 					mExtraTagType = EXTRA_TAG_TYPE_UNKNOWN;
186 					addUniqueIdEffectBumpMapParametersPair(mCurrentElementUniqueId, mExtraParameters.bumpParameters);
187 				}
188 				//else if ( strcmp(elementName, MAX_EXTRA_ATTRIBUTE_...) == 0 )
189 				//{
190 				//}
191 			}
192 		}
193 
194 		mTextBuffer.clear();
195 		return true;
196 	}
197 
198 	//------------------------------
textData(const COLLADASaxFWL::ParserChar * text,size_t textLength)199 	bool ExtraDataHandler::textData( const COLLADASaxFWL::ParserChar* text, size_t textLength )
200 	{
201 		mTextBuffer.append(text, textLength);
202 		return true;
203 	}
204 
205 	//------------------------------
parseElement(const COLLADASaxFWL::ParserChar * profileName,const COLLADASaxFWL::StringHash & elementHash,const COLLADAFW::UniqueId & uniqueId,COLLADAFW::Object * object)206 	bool ExtraDataHandler::parseElement( const COLLADASaxFWL::ParserChar* profileName, const COLLADASaxFWL::StringHash& elementHash
207 		, const COLLADAFW::UniqueId& uniqueId, COLLADAFW::Object* object )
208 	{
209 		mCurrentElementUniqueId = uniqueId;
210 		mCurrentObject = 0;
211 		if( object != 0 && object->getUniqueId() == mCurrentElementUniqueId )
212 			mCurrentObject = object;
213 
214 		switch ( elementHash )
215 		{
216 		case COLLADASaxFWL14::HASH_ELEMENT_LIGHT:
217 		case COLLADASaxFWL14::HASH_ELEMENT_TECHNIQUE:
218 			if ( strcmp(profileName, "OpenCOLLADA3dsMax") == 0)
219 			{
220 				return true;
221 			}
222 			else
223 			{
224 				return false;
225 			}
226 		default:
227 			return false;
228 		}
229 	}
230 
231 	//------------------------------
determineBumpType(const GeneratedSaxParser::xmlChar ** attributes)232 	void ExtraDataHandler::determineBumpType( const GeneratedSaxParser::xmlChar** attributes )
233 	{
234 		mExtraParameters.bumpParameters.bumpType = BUMP_TYPE_INVALID;
235 
236 		size_t index = 0;
237 
238 		const GeneratedSaxParser::xmlChar* attributeKey = attributes[index++];
239 		const GeneratedSaxParser::xmlChar* attributeValue = 0;
240 		while( attributeKey != 0 )
241 		{
242 			attributeValue = attributes[index++];
243 			if( strcmp(attributeKey, "bumptype") == 0 && attributeValue != 0 )
244 			{
245 				if( strcmp(attributeValue, "HEIGHTFIELD") == 0 )
246 					mExtraParameters.bumpParameters.bumpType = BUMP_TYPE_HEIGHTFIELD;
247 				break;
248 			}
249 
250 			attributeKey = attributes[index++];
251 		}
252 	}
253 
254 	//------------------------------
determineBumpTextureSamplerAndTexCoord(const GeneratedSaxParser::xmlChar ** attributes)255 	void ExtraDataHandler::determineBumpTextureSamplerAndTexCoord( const GeneratedSaxParser::xmlChar** attributes )
256 	{
257 		mExtraParameters.bumpParameters.textureAttributes = 0;
258 
259 		if( mCurrentObject )
260 		{
261 			if( COLLADAFW::COLLADA_TYPE::EFFECT == mCurrentObject->getClassId() )
262 			{
263 				COLLADAFW::Effect* effect = (COLLADAFW::Effect*)mCurrentObject;
264 				mExtraParameters.bumpParameters.textureAttributes = effect->createExtraTextureAttributes();
265 			}
266 		}
267 
268 		if( mExtraParameters.bumpParameters.textureAttributes == 0 )
269 			return;
270 
271 		size_t index = 0;
272 
273 		const GeneratedSaxParser::xmlChar* attributeKey = attributes[index++];
274 		const GeneratedSaxParser::xmlChar* attributeValue = 0;
275 		while( attributeKey != 0 )
276 		{
277 			attributeValue = attributes[index++];
278 			if( strcmp(attributeKey, "texture") == 0 && attributeValue != 0 )
279 			{
280 				if( mExtraParameters.bumpParameters.textureAttributes )
281 					mExtraParameters.bumpParameters.textureAttributes->textureSampler = attributeValue;
282 			}
283 			else if( strcmp(attributeKey, "texcoord") == 0 && attributeValue != 0 )
284 			{
285 				if( mExtraParameters.bumpParameters.textureAttributes )
286 					mExtraParameters.bumpParameters.textureAttributes->texCoord = attributeValue;
287 			}
288 
289 			attributeKey = attributes[index++];
290 		}
291 	}
292 
293 }
294