Import DAE.cpp 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************/
  5. #define CONVERT_BIND_POSE 1 // do not undefine
  6. /******************************************************************************
  7. TODO:
  8. animations Lerp -> Lerp4
  9. process "ScaleAxisR" and "ScaleAxis" keyframes for non-baked matrixes
  10. INFO:
  11. if when using CONVERT_BIND_POSE some vertexes go crazy,
  12. this means that they have incorrect skinning (for example face vertexes have partially set body bone),
  13. matrixes with bad sign on "x" direction can also affect that result
  14. OpenCollada doesn't contain any information about animation length,
  15. so it must be calculated from the max value of keyframe time positions.
  16. Node is imported as Bone only if at least one of following conditions is met:
  17. -node has type="JOINT" in its declaration
  18. -node has any skinning (vertexes attached to the node)
  19. -node has animation keyframes and there is a (bone or mesh) in children
  20. /******************************************************************************/
  21. // STRUCT
  22. /******************************************************************************/
  23. enum SEMANTIC : Byte
  24. {
  25. NONE ,
  26. INPUT ,
  27. OUTPUT ,
  28. INTERPOLATION,
  29. VERTEX ,
  30. POSITION,
  31. NORMAL ,
  32. TEXCOORD,
  33. JOINT ,
  34. WEIGHT ,
  35. INV_BIND_MATRIX,
  36. };
  37. /******************************************************************************/
  38. struct DAE
  39. {
  40. struct Effect ;
  41. struct Mesh ;
  42. struct Skin ;
  43. struct Node ;
  44. struct Source ;
  45. struct Input ;
  46. struct Vertices;
  47. struct Sampler
  48. {
  49. Str id;
  50. Source *input, *output;
  51. void import(XmlNode &node, Memb<Source> &source);
  52. Sampler() {input=output=null;}
  53. };
  54. struct Channel
  55. {
  56. Str target;
  57. Source *time, *transform, *move, *move_x, *move_y, *move_z, *rot_z, *rot_y, *rot_x, *scale_axis_r, *scale, *scale_axis, *alpha;
  58. Node *node;
  59. void import (XmlNode &node, Memb<Sampler> &sampler, DAE &dae);
  60. void linkNode(DAE &dae);
  61. Bool anyDifferent()C; // if any of the keyframe is different from each other
  62. Channel() {time=transform=move=move_x=move_y=move_z=rot_z=rot_y=rot_x=scale_axis_r=scale=scale_axis=alpha=null; node=null;}
  63. };
  64. struct Animation
  65. {
  66. Str id;
  67. Memb<Source > source;
  68. Memb<Sampler > sampler;
  69. Memb<Channel > channel;
  70. Memb<Animation> animation;
  71. void import (XmlNode &node, DAE &dae);
  72. void linkNode(DAE &dae);
  73. };
  74. struct Image
  75. {
  76. Str id, name, texture;
  77. void import(XmlNode &node);
  78. };
  79. struct Material
  80. {
  81. Str id, name, fx;
  82. Effect *effect;
  83. void import (XmlNode &node);
  84. void linkEffect(DAE &dae );
  85. Material() {effect=null;}
  86. };
  87. struct NewParam
  88. {
  89. Str sid, image_id;
  90. void import(XmlNode &node, Memb<NewParam> &param, DAE &dae);
  91. };
  92. struct Effect
  93. {
  94. Bool double_sided;
  95. Str id, name, color_map_image_id, alpha_map_image_id, specular_map_image_id, bump_map_image_id;
  96. Flt shininess, spec_level;
  97. Vec ambient, color, specular;
  98. Memb<NewParam> param;
  99. void import(XmlNode &node, DAE &dae);
  100. Effect() {double_sided=false; ambient=0; color=1; specular=0; shininess=0; spec_level=-1;}
  101. };
  102. struct Source
  103. {
  104. Str id;
  105. Int stride;
  106. Memc<Flt > float_array;
  107. Memc<Str8> name_array;
  108. CChar8* getName (Int i) {i*=stride; return InRange(i, name_array) ? name_array[i+0]() : null;}
  109. Flt getFloat (Int i) {i*=stride; return InRange(i, float_array) ? float_array[i+0] : 0;}
  110. Vec2 getVec2 (Int i) {i*=stride; return (InRange(i, float_array) && InRange(i+1, float_array)) ? Vec2(float_array[i+0], float_array[i+1] ) : 0;}
  111. Vec getVec (Int i) {i*=stride; return (InRange(i, float_array) && InRange(i+2, float_array)) ? Vec (float_array[i+0], float_array[i+1], float_array[i+2] ) : VecZero;}
  112. Vec4 getVec4 (Int i) {i*=stride; return (InRange(i, float_array) && InRange(i+3, float_array)) ? Vec4(float_array[i+0], float_array[i+1], float_array[i+2], float_array[i+3]) : 0;}
  113. Matrix getMatrix(Int i)
  114. {
  115. i*=stride;
  116. if(InRange(i , float_array)
  117. && InRange(i+11, float_array))
  118. {
  119. Matrix m;
  120. m.x .set(float_array[i+0], float_array[i+4], float_array[i+ 8]);
  121. m.y .set(float_array[i+1], float_array[i+5], float_array[i+ 9]);
  122. m.z .set(float_array[i+2], float_array[i+6], float_array[i+10]);
  123. m.pos.set(float_array[i+3], float_array[i+7], float_array[i+11]);
  124. return m;
  125. }
  126. return MatrixIdentity;
  127. }
  128. void import(XmlNode &node);
  129. Source() {stride=1;}
  130. };
  131. struct Input
  132. {
  133. SEMANTIC semantic;
  134. Source *source;
  135. Vertices *vertices;
  136. Int offset, set;
  137. void import(XmlNode &node, Memb<Source> &source, Mesh *mesh=null);
  138. Input() {semantic=NONE; source=null; vertices=null; offset=0; set=0;}
  139. };
  140. struct Vertices
  141. {
  142. Str id;
  143. Memb<Input> inputs;
  144. void import(XmlNode &node, Mesh &mesh);
  145. };
  146. struct Triangles
  147. {
  148. Str material_symbol;
  149. Memb<Input> inputs;
  150. Memc<Int > p;
  151. void import(XmlNode &node, Mesh &mesh, DAE &dae);
  152. };
  153. struct PolyList
  154. {
  155. Str material_symbol;
  156. Memb<Input> inputs;
  157. Memc<Int> vcount, p;
  158. void import(XmlNode &node, Mesh &mesh, DAE &dae);
  159. };
  160. struct Polygons
  161. {
  162. Str material_symbol;
  163. Memb< Input > inputs;
  164. Memb< Memc<Int> > p;
  165. void import(XmlNode &node, Mesh &mesh, DAE &dae);
  166. };
  167. struct Mesh
  168. {
  169. Memb<Source > source ;
  170. Memb<Vertices > vertices ;
  171. Memb<Triangles> triangles;
  172. Memb<PolyList > poly_list;
  173. Memb<Polygons > polygons ;
  174. void import(XmlNode &node, DAE &dae);
  175. };
  176. struct Geometry
  177. {
  178. Str id ,
  179. name;
  180. Memb<Mesh> mesh;
  181. Skin *skin;
  182. void import(XmlNode &node, DAE &dae);
  183. Geometry() {skin=null;}
  184. };
  185. struct Joint
  186. {
  187. Memb<Input> inputs;
  188. void import(XmlNode &node, Memb<Source> &source);
  189. };
  190. struct VertexWeights
  191. {
  192. Memb<Input> inputs;
  193. Memc<Int > vcount;
  194. Memc<Int > v;
  195. Input *joint, *weight;
  196. void import(XmlNode &node, Memb<Source> &source);
  197. VertexWeights() {joint=weight=null;}
  198. };
  199. struct Skin // exists as 1 element in 'Controller'
  200. {
  201. Geometry *geometry;
  202. Str geometry_name;
  203. Matrix bind_shape_matrix;
  204. Source *name, *matrix;
  205. Memb<Source> source;
  206. Memb<Joint > joint ;
  207. Memc<Byte > bone_remap; // remap from local 'joint' set to global index of bones in target skeleton
  208. Memb<VertexWeights> vtx_wgt;
  209. void import(XmlNode &node, DAE &dae);
  210. Skin() {geometry=null; bind_shape_matrix.identity(); name=matrix=null;}
  211. };
  212. struct Controller // exists as 1 element for each 'Geometry', contains set of bones (with matrixes) and vertex skinning for that bones (here are listed only bones which may have skinning, dummy/helpers/bones without skinning aren't listed)
  213. {
  214. Str id;
  215. Memb<Skin> skin;
  216. void import(XmlNode &node, DAE &dae);
  217. void linkSkin(DAE &dae);
  218. };
  219. struct InstanceMaterial
  220. {
  221. Str material_symbol, material_id;
  222. };
  223. struct InstanceGeometry
  224. {
  225. Str geometry_id;
  226. Memb<InstanceMaterial> instance_material;
  227. void import(XmlNode &node);
  228. };
  229. struct InstanceController
  230. {
  231. Str controller_id;
  232. Memb<InstanceMaterial> instance_material;
  233. void import(XmlNode &node);
  234. };
  235. struct Node
  236. {
  237. Str id, name, sid;
  238. Bool bone;
  239. Int bone_index; // index of this bone in 'DAE.bones' container
  240. Vec4 color;
  241. Matrix local_matrix, anim_matrix, world_matrix;
  242. Node *parent ;
  243. Memb<Node > nodes ; // children
  244. Memb<Channel *> channels; // animation channels
  245. Memb<InstanceGeometry > instance_geometry;
  246. Memb<InstanceController> instance_controller;
  247. Node() {bone=false; bone_index=-1; color=1; local_matrix.identity(); anim_matrix.identity(); parent=null;}
  248. // create
  249. void create(::Mesh &mesh, MemPtr<Int> part_material_index, ::Skeleton &skeleton, DAE &dae);
  250. // get
  251. Node* findNode(CChar *name)
  252. {
  253. if(Equal(id, name))return this;
  254. REPA(nodes)if(Node *node=nodes[i].findNode(name))return node;
  255. return null;
  256. }
  257. Bool hasAnim()
  258. {
  259. if(channels.elms())
  260. {
  261. REPA(channels)if(channels[i]->anyDifferent())return true; // check if any keyframes are different from each other
  262. animate(0); // animate to first frame
  263. if(!Equal(local_matrix, anim_matrix, EPS_ANIM_ANGLE, EPS_ANIM_POS))return true; // check if first frame is animated (not the same as local matrix)
  264. }
  265. return false;
  266. }
  267. // set
  268. void linkBone (DAE &dae);
  269. void linkSkin (DAE &dae);
  270. void setWorldMatrix( ) {world_matrix=local_matrix; if(parent)world_matrix*=parent->world_matrix; REPAO(nodes).setWorldMatrix();}
  271. void defaultPose ( ) { anim_matrix=local_matrix; REPAO(nodes).defaultPose ();}
  272. void setBoneNodesFromSkin(DAE &dae);
  273. Bool setBoneNodesFromAnim(DAE &dae);
  274. void adjustBoneIndex(Int bone_index)
  275. {
  276. if(bone_index>=0xFF)bone_index=-1;
  277. T.bone_index=bone_index;
  278. if(bone_index<0)bone=false; // if we're clearing bone index, then it means we don't want this as a bone
  279. }
  280. // io
  281. void import(XmlNode &node, Node *parent);
  282. // animate
  283. void animate(Flt t);
  284. #if DEBUG
  285. // draw
  286. void draw(C Matrix &parent, Bool anim, Flt scale)
  287. {
  288. Matrix m=(anim ? anim_matrix : local_matrix)*parent;
  289. if(!Kb.ctrl() && bone)Matrix(m).scaleOrn(scale).draw();
  290. if( Kb.ctrl() && !bone)Matrix(m).scaleOrn(scale).draw();
  291. REPAO(nodes).draw(m, anim, scale);
  292. }
  293. #endif
  294. };
  295. struct VisualScene
  296. {
  297. Str id, name;
  298. Memb<Node> nodes;
  299. void import(XmlNode &node, DAE &dae);
  300. #if DEBUG
  301. void draw (Bool anim, Flt scale) {REPAO(nodes).draw(MatrixIdentity, anim, scale);}
  302. #endif
  303. };
  304. struct Skinning
  305. {
  306. VecB4 bone, blend;
  307. };
  308. struct Vtx
  309. {
  310. Vec pos, nrm;
  311. Vec2 tex[4];
  312. VecB4 matrix, blend;
  313. };
  314. Str version;
  315. Int up_axis;
  316. Flt scale, duration, force_duration, fps;
  317. Memb<Animation > animations;
  318. Memb<Image > images;
  319. Memb<Material > materials;
  320. Memb<Effect > effects;
  321. Memb<Geometry > geometries;
  322. Memb<Controller > controllers;
  323. Memb<VisualScene> visual_scenes;
  324. Memc<Node* > bones; // manually created helper, lists bones (node of joint type) in their order of appearance
  325. DAE() {up_axis=-1; scale=1; duration=0; force_duration=-1; fps=0;}
  326. // get
  327. Int findMaterial (CChar *name);
  328. Int findMaterial (CChar *symbol, Memb<InstanceMaterial> &im);
  329. Image* findImage (CChar *name);
  330. Geometry* findGeometry (CChar *name);
  331. Controller* findController(CChar *name);
  332. Node* findNode (CChar *name);
  333. // import
  334. void loadAsset (XmlNode &node);
  335. void loadLibraryAnimations (XmlNode &node);
  336. void loadLibraryImages (XmlNode &node);
  337. void loadLibraryMaterials (XmlNode &node);
  338. void loadLibraryEffects (XmlNode &node);
  339. void loadLibraryGeometries (XmlNode &node);
  340. void loadLibraryControllers (XmlNode &node);
  341. void loadLibraryVisualScenes(XmlNode &node);
  342. void setBoneNodesFromSkin();
  343. void setBoneNodesFromAnim();
  344. // create
  345. void create( ::Mesh &mesh , MemPtr<Int> part_material_index, ::Skeleton &skeleton, Geometry &geometry, Memb<InstanceMaterial> &im, Node &node);
  346. void create( ::Mesh &mesh , MemPtr<Int> part_material_index, ::Skeleton &skeleton);
  347. void create( ::Skeleton &skeleton , XAnimation *animation);
  348. void create( XAnimation &animation, ::Skeleton &skeleton );
  349. void create(MemPtr<XMaterial> materials, Str path );
  350. #if DEBUG
  351. // animate and draw
  352. void draw(Bool anim, Flt scale) {REPAO(visual_scenes).draw(anim, scale);}
  353. #endif
  354. };
  355. /******************************************************************************/
  356. // HELPER FUNCTIONS
  357. /******************************************************************************/
  358. static Int Safe(Memc<Int> &m, Int i) {return InRange(i, m) ? m[i] : -1;}
  359. static void Import(Memc<Str8> &texts , XmlNode &node) {texts .setNum(node.data.elms()); REPAO(texts )= node.data[i] ;}
  360. static void Import(Memc<Int> &values, XmlNode &node) {values.setNum(node.data.elms()); REPAO(values)=TextInt(node.data[i]);}
  361. static void Import(Memc<Flt> &values, XmlNode &node) {values.setNum(node.data.elms()); REPAO(values)=TextFlt(node.data[i]);}
  362. static void Import( Bool &value , XmlNode &node) {value =(InRange(0, node.data) ? TextBool(node.data[0]) : false);}
  363. static void Import( Str &text , XmlNode &node) {text =(InRange(0, node.data) ? node.data[0] : S);}
  364. static void Import( Flt &value , XmlNode &node) {value =(InRange(0, node.data) ? TextFlt (node.data[0]) : 0);}
  365. static void Import( Vec &vec , XmlNode &node) {REPAO(vec.c)=(InRange(i, node.data) ? TextFlt (node.data[i]) : 0);}
  366. static void Import( Vec4 &vec , XmlNode &node) {REPAO(vec.c)=(InRange(i, node.data) ? TextFlt (node.data[i]) : 0);}
  367. static void Import( Matrix &matrix, XmlNode &node)
  368. {
  369. matrix.identity();
  370. REP(Min(node.data.elms(), 12))
  371. {
  372. Flt f=TextFlt(node.data[i]);
  373. switch(i)
  374. {
  375. case 0: matrix.x .x=f; break;
  376. case 4: matrix.x .y=f; break;
  377. case 8: matrix.x .z=f; break;
  378. case 1: matrix.y .x=f; break;
  379. case 5: matrix.y .y=f; break;
  380. case 9: matrix.y .z=f; break;
  381. case 2: matrix.z .x=f; break;
  382. case 6: matrix.z .y=f; break;
  383. case 10: matrix.z .z=f; break;
  384. case 3: matrix.pos.x=f; break;
  385. case 7: matrix.pos.y=f; break;
  386. case 11: matrix.pos.z=f; break;
  387. }
  388. }
  389. }
  390. /******************************************************************************/
  391. static DAE::Source* FindSource(Memb<DAE::Source> &source, CChar *name)
  392. {
  393. REPA(source)if(Equal(source[i].id, name))return &source[i];
  394. return null;
  395. }
  396. static DAE::Sampler* FindSampler(Memb<DAE::Sampler> &sampler, CChar *name)
  397. {
  398. REPA(sampler)if(Equal(sampler[i].id, name))return &sampler[i];
  399. return null;
  400. }
  401. /******************************************************************************/
  402. // GET
  403. /******************************************************************************/
  404. Int DAE::findMaterial(CChar *name)
  405. {
  406. REPA(materials)if(Equal(materials[i].id, name))return i;
  407. return -1;
  408. }
  409. Int DAE::findMaterial(CChar *symbol, Memb<InstanceMaterial> &im)
  410. {
  411. REPA(im)if(Equal(im[i].material_symbol, symbol))
  412. {
  413. Int m=findMaterial(im[i].material_id);
  414. if( m>=0)return m;
  415. }
  416. return -1;
  417. }
  418. DAE::Image* DAE::findImage(CChar *name)
  419. {
  420. REPA(images)if(Equal(images[i].id, name))return &images[i];
  421. return null;
  422. }
  423. DAE::Geometry* DAE::findGeometry(CChar *name)
  424. {
  425. REPA(geometries)if(Equal(geometries[i].id, name))return &geometries[i];
  426. return null;
  427. }
  428. DAE::Controller* DAE::findController(CChar *name)
  429. {
  430. REPA(controllers)if(Equal(controllers[i].id, name))return &controllers[i];
  431. return null;
  432. }
  433. DAE::Node* DAE::findNode(CChar *name)
  434. {
  435. REPA(visual_scenes)REPAD(j, visual_scenes[i].nodes)if(Node *node=visual_scenes[i].nodes[j].findNode(name))return node;
  436. return null;
  437. }
  438. /******************************************************************************/
  439. // IMPORT
  440. /******************************************************************************/
  441. void DAE::Sampler::import(XmlNode &node, Memb<Source> &source)
  442. {
  443. if(XmlParam *id=node.findParam("id"))T.id=id->asText();
  444. FREPA(node.nodes)if(node.nodes[i].name=="input")
  445. {
  446. Input input; input.import(node.nodes[i], source);
  447. if(input.source)switch(input.semantic)
  448. {
  449. case INPUT: T.input =input.source; break;
  450. case OUTPUT: T.output=input.source; break;
  451. }
  452. }
  453. }
  454. void DAE::Channel::import(XmlNode &node, Memb<Sampler> &sampler, DAE &dae)
  455. {
  456. Source *output=null;
  457. if(XmlParam *p=node.findParam("source"))if(Sampler *s=FindSampler(sampler, SkipStart(p->value, '#')))
  458. {
  459. T.time =s-> input;
  460. output=s->output;
  461. }
  462. if(XmlParam *p=node.findParam("target"))
  463. {
  464. target=GetPath(p->value);
  465. Str type=GetBase(p->value);
  466. if(Equal(type, "transform" ) || Equal(type, "matrix" ) )transform =output;else
  467. if(Equal(type, "translation" ) )move =output;else
  468. if(Equal(type, "translation.X") || Equal(type, "translate.X" ) || Equal(type, "transform(3)(0)"))move_x =output;else
  469. if(Equal(type, "translation.Y") || Equal(type, "translate.Y" ) || Equal(type, "transform(3)(1)"))move_y =output;else
  470. if(Equal(type, "translation.Z") || Equal(type, "translate.Z" ) || Equal(type, "transform(3)(2)"))move_z =output;else
  471. if(Equal(type, "RotZ.ANGLE" ) || Equal(type, "RotateZ.ANGLE") || Equal(type, "rotationZ.ANGLE"))rot_z =output;else
  472. if(Equal(type, "RotY.ANGLE" ) || Equal(type, "RotateY.ANGLE") || Equal(type, "rotationY.ANGLE"))rot_y =output;else
  473. if(Equal(type, "RotX.ANGLE" ) || Equal(type, "RotateX.ANGLE") || Equal(type, "rotationX.ANGLE"))rot_x =output;else
  474. if(Equal(type, "ScaleAxisR" ) )scale_axis_r=output;else
  475. if(Equal(type, "scale" ) )scale =output;else
  476. if(Equal(type, "ScaleAxis" ) )scale_axis =output;else
  477. if(Equal(type, "transparency" ) )alpha =output;
  478. }
  479. if(time && time->float_array.elms())MAX(dae.duration, time->float_array.last());
  480. #if 0 && DEBUG
  481. if(time)FREPA(time->float_array)Log(S+time->getFloat(i)+" "); LogN();
  482. #endif
  483. }
  484. void DAE::Animation::import(XmlNode &node, DAE &dae)
  485. {
  486. if(XmlParam *id=node.findParam("id"))T.id=id->asText();
  487. FREPA(node.nodes)
  488. {
  489. XmlNode &n=node.nodes[i];
  490. if(n.name=="source" )source .New().import(n );else
  491. if(n.name=="sampler" )sampler .New().import(n, source );else
  492. if(n.name=="channel" )channel .New().import(n, sampler, dae);else
  493. if(n.name=="animation")animation.New().import(n, dae);
  494. }
  495. }
  496. void DAE::Image::import(XmlNode &node)
  497. {
  498. if(XmlParam *id =node.findParam("id" ))T.id =id ->asText();
  499. if(XmlParam *name=node.findParam("name"))T.name=name->asText();
  500. if(XmlNode *n=node.findNode("init_from"))if(n->data.elms())texture=SkipStartPath(SkipStartPath(SkipStartPath(Replace(n->data[0], "%20", " "), "file:///"), "file://"), "./");
  501. }
  502. void DAE::Material::import(XmlNode &node)
  503. {
  504. if(XmlParam *id =node.findParam("id" ))T.id =id ->asText();
  505. if(XmlParam *name=node.findParam("name"))T.name=name->asText();
  506. if(XmlNode *n=node.findNode("instance_effect"))if(XmlParam *p=n->findParam("url"))fx=SkipStart(p->value, '#');
  507. }
  508. void DAE::NewParam::import(XmlNode &node, Memb<NewParam> &param, DAE &dae)
  509. {
  510. if(XmlParam *sid=node.findParam("sid"))T.sid=sid->asText();
  511. if(XmlNode *n=node.findNode("surface" ))if(XmlNode *i=n->findNode("init_from"))Import(image_id, *i);
  512. if(XmlNode *n=node.findNode("sampler2D"))if(XmlNode *s=n->findNode("source" ))if(s->data.elms())REPA(param)if(param[i].sid==s->data[0]){image_id=param[i].image_id; break;}
  513. }
  514. void DAE::Effect::import(XmlNode &node, DAE &dae)
  515. {
  516. if(XmlParam *id =node.findParam("id" ))T.id =id ->asText();
  517. if(XmlParam *name=node.findParam("name"))T.name=name->asText();
  518. if(XmlNode *n=node.findNode("profile_COMMON"))
  519. {
  520. FREPA(n->nodes)if(n->nodes[i].name=="newparam")param.New().import(n->nodes[i], param, dae);
  521. if(XmlNode *technique=n->findNode("technique"))
  522. {
  523. XmlNode *sub=technique->findNode("blinn"); if(!sub)sub=technique->findNode("phong");
  524. if(sub)
  525. {
  526. if(XmlNode *ambient =sub->findNode("ambient" ))if(XmlNode *color=ambient ->findNode("color"))Import(T.ambient , *color);
  527. if(XmlNode *specular =sub->findNode("specular" ))if(XmlNode *color=specular ->findNode("color"))Import(T.specular , *color);
  528. if(XmlNode *shininess=sub->findNode("shininess"))if(XmlNode *n =shininess->findNode("float"))Import(T.shininess, *n);
  529. if(XmlNode *diffuse =sub->findNode("diffuse" ))
  530. {
  531. if(XmlNode *color =diffuse->findNode("color" ))Import(T.color, *color);
  532. if(XmlNode *texture=diffuse->findNode("texture"))if(XmlParam *t=texture->findParam("texture"))
  533. {
  534. color_map_image_id=t->value;
  535. REPA(param)if(param[i].sid==t->value){color_map_image_id=param[i].image_id; break;}
  536. }
  537. }
  538. if(XmlNode *transparent=sub->findNode("transparent"))
  539. {
  540. if(XmlNode *texture=transparent->findNode("texture"))if(XmlParam *t=texture->findParam("texture"))
  541. {
  542. alpha_map_image_id=t->value;
  543. REPA(param)if(param[i].sid==t->value){alpha_map_image_id=param[i].image_id; break;}
  544. }
  545. }
  546. }
  547. if(XmlNode *extra=technique->findNode("extra"))
  548. if(XmlNode *technique=extra->findNode("technique"))
  549. {
  550. XmlNode *spec=technique->findNode("spec_level"); if(!spec)spec=technique->findNode("specularLevel");
  551. if(spec)
  552. {
  553. if(XmlNode *n=spec->findNode("float"))Import(spec_level, *n);
  554. if(XmlNode *texture=spec->findNode("texture"))if(XmlParam *t=texture->findParam("texture"))
  555. {
  556. specular_map_image_id=t->value;
  557. REPA(param)if(param[i].sid==t->value){specular_map_image_id=param[i].image_id; break;}
  558. }
  559. }
  560. if(XmlNode *bump=technique->findNode("bump"))
  561. {
  562. if(XmlNode *texture=bump->findNode("texture"))if(XmlParam *t=texture->findParam("texture"))
  563. {
  564. bump_map_image_id=t->value;
  565. REPA(param)if(param[i].sid==t->value){bump_map_image_id=param[i].image_id; break;}
  566. }
  567. }
  568. }
  569. }
  570. }
  571. if(XmlNode *extra=node.findNode("extra"))
  572. if(XmlNode *technique=extra->findNode("technique"))
  573. if(XmlNode *dbl=technique->findNode("double_sided"))Import(double_sided, *dbl);
  574. }
  575. void DAE::Source::import(XmlNode &node)
  576. {
  577. if(XmlParam *id=node.findParam("id"))T.id=id->asText();
  578. if(XmlNode *float_array=node.findNode("float_array"))Import(T.float_array, *float_array);
  579. if(XmlNode * name_array=node.findNode( "name_array"))Import(T. name_array, * name_array);
  580. if(XmlNode *technique=node.findNode("technique_common"))
  581. if(XmlNode *accessor=technique->findNode("accessor"))
  582. if(XmlParam *stride=accessor->findParam("stride"))T.stride=Max(1, stride->asInt());
  583. }
  584. void DAE::Input::import(XmlNode &node, Memb<Source> &source, Mesh *mesh)
  585. {
  586. if(XmlParam *semantic=node.findParam("semantic"))
  587. {
  588. if(semantic->value=="INPUT" )T.semantic=INPUT ;else
  589. if(semantic->value=="OUTPUT" )T.semantic=OUTPUT ;else
  590. if(semantic->value=="INTERPOLATION" )T.semantic=INTERPOLATION ;else
  591. if(semantic->value=="VERTEX" )T.semantic=VERTEX ;else
  592. if(semantic->value=="POSITION" )T.semantic=POSITION ;else
  593. if(semantic->value=="NORMAL" )T.semantic=NORMAL ;else
  594. if(semantic->value=="TEXCOORD" )T.semantic=TEXCOORD ;else
  595. if(semantic->value=="JOINT" )T.semantic=JOINT ;else
  596. if(semantic->value=="WEIGHT" )T.semantic=WEIGHT ;else
  597. if(semantic->value=="INV_BIND_MATRIX")T.semantic=INV_BIND_MATRIX;
  598. }
  599. if(XmlParam *src=node.findParam("source"))
  600. {
  601. Str s=SkipStart(src->value, '#');
  602. if(semantic==VERTEX && mesh)REPA(mesh->vertices)if(Equal(s, mesh->vertices[i].id)){T.vertices=&mesh->vertices[i]; break;}
  603. if(!T.source)T.source=FindSource(source, s);
  604. }
  605. if(XmlParam *offset=node.findParam("offset"))T.offset=offset->asInt();
  606. if(XmlParam * set=node.findParam( "set"))T. set= set->asInt();
  607. }
  608. void DAE::Vertices::import(XmlNode &node, Mesh &mesh)
  609. {
  610. if(XmlParam *id=node.findParam("id"))T.id=id->asText();
  611. FREPA(node.nodes)if(node.nodes[i].name=="input")inputs.New().import(node.nodes[i], mesh.source, &mesh);
  612. }
  613. void DAE::Triangles::import(XmlNode &node, Mesh &mesh, DAE &dae)
  614. {
  615. if(XmlParam *material=node.findParam("material"))material_symbol=material->asText();
  616. if(XmlNode *p=node.findNode("p"))Import(T.p, *p);
  617. FREPA(node.nodes)if(node.nodes[i].name=="input")inputs.New().import(node.nodes[i], mesh.source, &mesh);
  618. }
  619. void DAE::PolyList::import(XmlNode &node, Mesh &mesh, DAE &dae)
  620. {
  621. if(XmlParam *material=node.findParam("material"))material_symbol=material->asText();
  622. if(XmlNode *p =node.findNode("p" ))Import(T.p , *p );
  623. if(XmlNode *vcount=node.findNode("vcount"))Import(T.vcount, *vcount);
  624. FREPA(node.nodes)if(node.nodes[i].name=="input")inputs.New().import(node.nodes[i], mesh.source, &mesh);
  625. }
  626. void DAE::Polygons::import(XmlNode &node, Mesh &mesh, DAE &dae)
  627. {
  628. if(XmlParam *material=node.findParam("material"))material_symbol=material->asText();
  629. FREPA(node.nodes)
  630. {
  631. XmlNode &n=node.nodes[i];
  632. if(n.name=="input")inputs.New().import(n, mesh.source, &mesh);else
  633. if(n.name=="p" )Import(p.New(), n);
  634. }
  635. }
  636. void DAE::Mesh::import(XmlNode &node, DAE &dae)
  637. {
  638. FREPA(node.nodes)
  639. {
  640. XmlNode &n=node.nodes[i];
  641. if(n.name=="source" )source .New().import(n );else
  642. if(n.name=="vertices" )vertices .New().import(n, T );else
  643. if(n.name=="triangles")triangles.New().import(n, T, dae);else
  644. if(n.name=="polylist" )poly_list.New().import(n, T, dae);else
  645. if(n.name=="polygons" )polygons .New().import(n, T, dae);
  646. }
  647. }
  648. void DAE::Geometry::import(XmlNode &node, DAE &dae)
  649. {
  650. if(XmlParam *id =node.findParam("id" ))T.id =id ->asText();
  651. if(XmlParam *name=node.findParam("name"))T.name=name->asText();
  652. FREPA(node.nodes)if(node.nodes[i].name=="mesh")mesh.New().import(node.nodes[i], dae);
  653. }
  654. void DAE::Joint::import(XmlNode &node, Memb<Source> &source)
  655. {
  656. FREPA(node.nodes)if(node.nodes[i].name=="input")inputs.New().import(node.nodes[i], source);
  657. }
  658. void DAE::VertexWeights::import(XmlNode &node, Memb<Source> &source)
  659. {
  660. if(XmlNode *v =node.findNode("v" ))Import(T.v , *v );
  661. if(XmlNode *vcount=node.findNode("vcount"))Import(T.vcount, *vcount);
  662. FREPA(node.nodes)if(node.nodes[i].name=="input")inputs.New().import(node.nodes[i], source);
  663. REPA(inputs)if(inputs[i].source)switch(inputs[i].semantic)
  664. {
  665. case JOINT : joint =&inputs[i]; break;
  666. case WEIGHT: weight=&inputs[i]; break;
  667. }
  668. }
  669. void DAE::Skin::import(XmlNode &node, DAE &dae)
  670. {
  671. if(XmlParam *source=node.findParam("source"))geometry_name=SkipStart(source->asText(), '#');
  672. if(XmlNode *bind_shape_matrix=node.findNode("bind_shape_matrix"))Import(T.bind_shape_matrix, *bind_shape_matrix);
  673. FREPA(node.nodes)
  674. {
  675. XmlNode &n=node.nodes[i];
  676. if(n.name=="source" )source .New().import(n );else
  677. if(n.name=="joints" )joint .New().import(n, source);else
  678. if(n.name=="vertex_weights")vtx_wgt.New().import(n, source);
  679. }
  680. REPA(joint)
  681. {
  682. Joint &joint=T.joint[i];
  683. REPA(joint.inputs)if(joint.inputs[i].source)switch(joint.inputs[i].semantic)
  684. {
  685. case JOINT : name =joint.inputs[i].source; break;
  686. case INV_BIND_MATRIX: matrix=joint.inputs[i].source; break;
  687. }
  688. }
  689. }
  690. void DAE::Controller::import(XmlNode &node, DAE &dae)
  691. {
  692. if(XmlParam *id=node.findParam("id"))T.id=id->asText();
  693. FREPA(node.nodes)if(node.nodes[i].name=="skin")skin.New().import(node.nodes[i], dae);
  694. }
  695. void DAE::Controller::linkSkin(DAE &dae)
  696. {
  697. FREPA(skin)
  698. {
  699. Skin &skin=T.skin[i];
  700. if(skin.geometry=dae.findGeometry(skin.geometry_name))skin.geometry->skin=&skin;
  701. }
  702. }
  703. void DAE::InstanceGeometry::import(XmlNode &node)
  704. {
  705. if(XmlParam *url=node.findParam("url"))geometry_id=SkipStart(url->asText(), '#');
  706. FREPA(node.nodes)
  707. {
  708. XmlNode &bm=node.nodes[i]; if(bm.name=="bind_material")FREPA(bm.nodes)
  709. {
  710. XmlNode &tc=bm.nodes[i]; if(tc.name=="technique_common")FREPA(tc.nodes)
  711. {
  712. XmlNode &im=tc.nodes[i]; if(im.name=="instance_material")
  713. {
  714. InstanceMaterial &mtrl=instance_material.New();
  715. if(XmlParam *symbol=im.findParam("symbol"))mtrl.material_symbol= symbol->asText();
  716. if(XmlParam *target=im.findParam("target"))mtrl.material_id =SkipStart(target->asText(), '#');
  717. }
  718. }
  719. }
  720. }
  721. }
  722. void DAE::InstanceController::import(XmlNode &node)
  723. {
  724. if(XmlParam *url=node.findParam("url"))controller_id=SkipStart(url->asText(), '#');
  725. FREPA(node.nodes)
  726. {
  727. XmlNode &bm=node.nodes[i]; if(bm.name=="bind_material")FREPA(bm.nodes)
  728. {
  729. XmlNode &tc=bm.nodes[i]; if(tc.name=="technique_common")FREPA(tc.nodes)
  730. {
  731. XmlNode &im=tc.nodes[i]; if(im.name=="instance_material")
  732. {
  733. InstanceMaterial &mtrl=instance_material.New();
  734. if(XmlParam *symbol=im.findParam("symbol"))mtrl.material_symbol= symbol->asText();
  735. if(XmlParam *target=im.findParam("target"))mtrl.material_id =SkipStart(target->asText(), '#');
  736. }
  737. }
  738. }
  739. }
  740. }
  741. void DAE::Node::import(XmlNode &node, Node *parent)
  742. {
  743. T.parent=parent;
  744. Memc<Matrix> transform;
  745. if(XmlParam *id =node.findParam("id" ))T.id = id ->asText();
  746. if(XmlParam *name=node.findParam("name"))T.name= name->asText();
  747. if(XmlParam *sid =node.findParam("sid" ))T.sid = sid ->asText();
  748. if(XmlParam *type=node.findParam("type"))T.bone=(type->asText()=="JOINT");
  749. FREPA(node.nodes)
  750. {
  751. XmlNode &n=node.nodes[i];
  752. if(n.name=="node" )T.nodes.New().import(n, this);else
  753. if(n.name=="translate" ){Vec temp; Import(temp, n); transform.New().setPos (temp);}else
  754. if(n.name=="rotate" ){Vec4 temp; Import(temp, n); transform.New().setRotate(temp.xyz, DegToRad(temp.w));}else
  755. if(n.name=="scale" ){Vec temp; Import(temp, n); transform.New().setScale (temp);}else
  756. if(n.name=="matrix" ){Matrix temp; Import(temp, n); transform.New()= temp ;}else
  757. if(n.name=="instance_geometry" )instance_geometry .New().import(n);else
  758. if(n.name=="instance_controller")instance_controller.New().import(n);
  759. }
  760. REPA(transform)local_matrix*=transform[i]; // transformations need to be performed from the end
  761. }
  762. void DAE::VisualScene::import(XmlNode &node, DAE &dae)
  763. {
  764. if(XmlParam *id =node.findParam("id" ))T.id =id ->asText();
  765. if(XmlParam *name=node.findParam("name"))T.name=name->asText();
  766. if(XmlNode *extra=node.findNode("extra"))
  767. FREPA(extra->nodes)if(extra->nodes[i].name=="technique")
  768. {
  769. XmlNode &technique=extra->nodes[i];
  770. if(XmlNode * end_time=technique.findNode( "end_time")){Flt time; Import(time , * end_time); MAX(dae.force_duration, time);}
  771. if(XmlNode *frame_rate=technique.findNode("frame_rate")) Import(dae.fps, *frame_rate);
  772. }
  773. FREPA(node.nodes)if(node.nodes[i].name=="node")T.nodes.New().import(node.nodes[i], null);
  774. }
  775. /******************************************************************************/
  776. void DAE::loadAsset(XmlNode &node)
  777. {
  778. if(XmlNode *unit=node.findNode("unit"))
  779. if(XmlParam *meter=unit->findParam("meter"))scale=meter->asFlt();
  780. if(XmlNode *up_axis=node.findNode("up_axis"))
  781. if(up_axis->data.elms())
  782. {
  783. if(up_axis->data[0]=="X_UP")T.up_axis=0;else
  784. if(up_axis->data[0]=="Y_UP")T.up_axis=1;else
  785. if(up_axis->data[0]=="Z_UP")T.up_axis=2;
  786. }
  787. }
  788. void DAE::loadLibraryAnimations(XmlNode &node)
  789. {
  790. FREPA(node.nodes)if(node.nodes[i].name=="animation")animations.New().import(node.nodes[i], T);
  791. }
  792. void DAE::loadLibraryImages(XmlNode &node)
  793. {
  794. FREPA(node.nodes)if(node.nodes[i].name=="image")images.New().import(node.nodes[i]);
  795. }
  796. void DAE::loadLibraryMaterials(XmlNode &node)
  797. {
  798. FREPA(node.nodes)if(node.nodes[i].name=="material")materials.New().import(node.nodes[i]);
  799. }
  800. void DAE::loadLibraryEffects(XmlNode &node)
  801. {
  802. FREPA(node.nodes)if(node.nodes[i].name=="effect")effects.New().import(node.nodes[i], T);
  803. }
  804. void DAE::loadLibraryGeometries(XmlNode &node)
  805. {
  806. FREPA(node.nodes)if(node.nodes[i].name=="geometry")geometries.New().import(node.nodes[i], T);
  807. }
  808. void DAE::loadLibraryControllers(XmlNode &node)
  809. {
  810. FREPA(node.nodes)if(node.nodes[i].name=="controller")controllers.New().import(node.nodes[i], T);
  811. }
  812. void DAE::loadLibraryVisualScenes(XmlNode &node)
  813. {
  814. FREPA(node.nodes)if(node.nodes[i].name=="visual_scene")visual_scenes.New().import(node.nodes[i], T);
  815. }
  816. /******************************************************************************/
  817. void DAE::Node::setBoneNodesFromSkin(DAE &dae)
  818. {
  819. FREPAO(nodes).setBoneNodesFromSkin(dae);
  820. if(!bone)FREPA(dae.controllers)
  821. {
  822. Controller &ctrl=dae.controllers[i];
  823. FREPA(ctrl.skin)
  824. {
  825. Skin &skin=ctrl.skin[i];
  826. if(skin.name)REPA(skin.name->name_array)if(sid==skin.name->getName(i)){bone=true; return;}
  827. }
  828. }
  829. }
  830. Bool DAE::Node::setBoneNodesFromAnim(DAE &dae)
  831. {
  832. Bool child_bone_mesh=false; FREPA(nodes)child_bone_mesh|=nodes[i].setBoneNodesFromAnim(dae);
  833. Bool mesh=(instance_geometry.elms() || instance_controller.elms()); // check in case the node has mesh (this is needed in case mesh has no skinning and would not setup a bone yet)
  834. child_bone_mesh|=mesh; // include from self
  835. if(!bone && child_bone_mesh && hasAnim())bone=true; // is not a bone, children have bone or mesh
  836. return child_bone_mesh || bone;
  837. }
  838. void DAE::setBoneNodesFromSkin()
  839. {
  840. FREPA(visual_scenes)
  841. {
  842. VisualScene &scene=visual_scenes[i];
  843. FREPAO(scene.nodes).setBoneNodesFromSkin(T);
  844. }
  845. }
  846. void DAE::setBoneNodesFromAnim()
  847. {
  848. FREPA(visual_scenes)
  849. {
  850. VisualScene &scene=visual_scenes[i];
  851. FREPAO(scene.nodes).setBoneNodesFromAnim(T);
  852. }
  853. }
  854. /******************************************************************************/
  855. // LINK
  856. /******************************************************************************/
  857. void DAE::Material::linkEffect(DAE &dae)
  858. {
  859. if(fx.is())REPA(dae.effects)if(Equal(fx, dae.effects[i].id))
  860. {
  861. effect=&dae.effects[i];
  862. break;
  863. }
  864. }
  865. void DAE::Channel::linkNode(DAE &dae)
  866. {
  867. if(node=dae.findNode(target))node->channels.include(this);
  868. }
  869. Bool DAE::Channel::anyDifferent()C
  870. {
  871. if(time)
  872. {
  873. if(transform){Matrix matrix=transform->getMatrix(0); REPA(time->float_array)if(!Equal(matrix, transform->getMatrix(i), EPS_ANIM_ANGLE, EPS_ANIM_POS))return true;}
  874. if(move ){Vec vec =move ->getVec (0); REPA(time->float_array)if(!Equal(vec , move ->getVec (i), EPS_ANIM_POS))return true;}
  875. if(move_x ){Flt f =move_x ->getFloat (0); REPA(time->float_array)if(!Equal(f , move_x ->getFloat (i), EPS_ANIM_POS))return true;}
  876. if(move_y ){Flt f =move_y ->getFloat (0); REPA(time->float_array)if(!Equal(f , move_y ->getFloat (i), EPS_ANIM_POS))return true;}
  877. if(move_z ){Flt f =move_z ->getFloat (0); REPA(time->float_array)if(!Equal(f , move_z ->getFloat (i), EPS_ANIM_POS))return true;}
  878. if( rot_x ){Flt f = rot_x ->getFloat (0); REPA(time->float_array)if(!Equal(f , rot_x ->getFloat (i), EPS_ANIM_ANGLE ))return true;}
  879. if( rot_y ){Flt f = rot_y ->getFloat (0); REPA(time->float_array)if(!Equal(f , rot_y ->getFloat (i), EPS_ANIM_ANGLE ))return true;}
  880. if( rot_z ){Flt f = rot_z ->getFloat (0); REPA(time->float_array)if(!Equal(f , rot_z ->getFloat (i), EPS_ANIM_ANGLE ))return true;}
  881. if(scale ){Flt f =scale ->getFloat (0); REPA(time->float_array)if(!Equal(f , scale ->getFloat (i), EPS_ANIM_SCALE ))return true;}
  882. }
  883. return false;
  884. }
  885. void DAE::Animation::linkNode(DAE &dae)
  886. {
  887. FREPAO(channel ).linkNode(dae); // order is important
  888. FREPAO(animation).linkNode(dae); // order is important
  889. }
  890. void DAE::Node::linkBone(DAE &dae)
  891. {
  892. if(bone)
  893. {
  894. bone_index=dae.bones.elms();
  895. dae.bones.add(this);
  896. }
  897. FREPAO(nodes).linkBone(dae); // order is important
  898. }
  899. void DAE::Node::linkSkin(DAE &dae)
  900. {
  901. if(bone_index>=0 && sid.is())FREPA(dae.controllers)
  902. {
  903. Controller &ctrl=dae.controllers[i]; FREPA(ctrl.skin)
  904. {
  905. Skin &skin=ctrl.skin[i];
  906. if(skin.name)REPA(skin.name->name_array)if(sid==skin.name->getName(i))skin.bone_remap(i)=bone_index;
  907. }
  908. }
  909. REPAO(nodes).linkSkin(dae);
  910. }
  911. /******************************************************************************/
  912. // ANIMATE
  913. /******************************************************************************/
  914. void DAE::Node::animate(Flt t)
  915. {
  916. Bool custom_orn=false;
  917. Matrix3 orn; orn.identity();
  918. color=1;
  919. anim_matrix=local_matrix; // node final position in local space
  920. REPA(channels) // order is important
  921. {
  922. Channel &channel=*channels[i]; if(channel.time)
  923. {
  924. Memc<Flt> &time=channel.time->float_array; if(time.elms())
  925. {
  926. Int prev=-1,
  927. next=-1;
  928. Flt step= 0;
  929. if(t<=time.first())prev=next= 0;else
  930. if(t>=time.last ())prev=next=time.elms()-1;else
  931. {
  932. Int l=0, r=time.elms(); for(; l<r; ){Int mid=UInt(l+r)/2; if(t<time[mid])r=mid;else l=mid+1;}
  933. prev=Mid(l-1, 0, time.elms()-1);
  934. next=Mid(l , 0, time.elms()-1);
  935. step=LerpRS(time[prev], time[next], t);
  936. }
  937. if(channel.alpha ) color.w =Lerp(channel.alpha ->getFloat(prev), channel.alpha ->getFloat(next), step);
  938. if(channel.move ) anim_matrix.pos =Lerp(channel.move ->getVec (prev), channel.move ->getVec (next), step);
  939. if(channel.move_x ) anim_matrix.pos.x=Lerp(channel.move_x->getFloat(prev), channel.move_x->getFloat(next), step);
  940. if(channel.move_y ) anim_matrix.pos.y=Lerp(channel.move_y->getFloat(prev), channel.move_y->getFloat(next), step);
  941. if(channel.move_z ) anim_matrix.pos.z=Lerp(channel.move_z->getFloat(prev), channel.move_z->getFloat(next), step);
  942. if(channel.rot_x ){orn.rotateX(DegToRad(Lerp(channel.rot_x ->getFloat(prev), channel.rot_x ->getFloat(next), step))); custom_orn=true;}
  943. if(channel.rot_y ){orn.rotateY(DegToRad(Lerp(channel.rot_y ->getFloat(prev), channel.rot_y ->getFloat(next), step))); custom_orn=true;}
  944. if(channel.rot_z ){orn.rotateZ(DegToRad(Lerp(channel.rot_z ->getFloat(prev), channel.rot_z ->getFloat(next), step))); custom_orn=true;}
  945. //if(channel.scale_axis_r){}
  946. if(channel.scale ){orn.scale ( Lerp(channel.scale ->getFloat(prev), channel.scale ->getFloat(next), step) ); custom_orn=true;}
  947. //if(channel.scale_axis ){}
  948. if(channel.transform )
  949. {
  950. if(prev==next)
  951. {
  952. anim_matrix=channel.transform->getMatrix(prev);
  953. }else
  954. {
  955. Matrix prev_matrix=channel.transform->getMatrix(prev),
  956. next_matrix=channel.transform->getMatrix(next);
  957. #if 1
  958. anim_matrix.x =Lerp(prev_matrix.x , next_matrix.x , step);
  959. anim_matrix.y =Lerp(prev_matrix.y , next_matrix.y , step);
  960. anim_matrix.z =Lerp(prev_matrix.z , next_matrix.z , step);
  961. anim_matrix.pos=Lerp(prev_matrix.pos, next_matrix.pos, step);
  962. #else
  963. // forces orthogonal matrix, seems like it's not required
  964. OrientP o(Lerp(prev_matrix.pos, next_matrix.pos, step), Lerp(prev_matrix.z, next_matrix.z, step), Lerp(prev_matrix.y, next_matrix.y, step));
  965. o.fix();
  966. anim_matrix=o;
  967. anim_matrix.x.setLength(Lerp(prev_matrix.x.length(), next_matrix.x.length(), step));
  968. anim_matrix.y.setLength(Lerp(prev_matrix.y.length(), next_matrix.y.length(), step));
  969. anim_matrix.z.setLength(Lerp(prev_matrix.z.length(), next_matrix.z.length(), step));
  970. #endif
  971. }
  972. }
  973. }
  974. }
  975. }
  976. if(custom_orn)anim_matrix.orn()=orn;
  977. }
  978. /******************************************************************************/
  979. // CREATE MESH
  980. /******************************************************************************/
  981. void DAE::create(::Mesh &mesh, MemPtr<Int> part_material_index, ::Skeleton &skeleton, Geometry &geometry, Memb<InstanceMaterial> &im, Node &node)
  982. {
  983. FREPA(geometry.mesh)
  984. {
  985. Mesh &geom_mesh=geometry.mesh[i];
  986. // set transform matrixes
  987. MemtN<Matrix, 256> matrixes;
  988. Int normal_matrixes=0, mirrored_matrixes=0; // matrixes can be sometimes "flipped" (x direction points in the wrong way that it should) in those cases faces are reversed, that's why I count them and later fix them
  989. if(Skin *skin=geometry.skin)
  990. if(skin->name && skin->matrix)
  991. {
  992. matrixes.setNum(skeleton.bones.elms()+VIRTUAL_ROOT_BONE);
  993. if(VIRTUAL_ROOT_BONE)matrixes[0].identity();
  994. #if CONVERT_BIND_POSE
  995. Matrix bind_shape_matrix_inv; skin->bind_shape_matrix.inverseNonOrthogonal(bind_shape_matrix_inv);
  996. REPA(skin->name->name_array)
  997. {
  998. Int bone_index=skin->bone_remap(i); if(InRange(bone_index, bones))
  999. {
  1000. Node &node=*bones [bone_index];
  1001. Matrix &m = matrixes[bone_index+VIRTUAL_ROOT_BONE];
  1002. skin->matrix->getMatrix(i).inverseNonOrthogonal(m); m*=bind_shape_matrix_inv; // m=GetTransform(skin.matrix->getMatrix(i), bind_shape_matrix_inv);
  1003. m.inverseNonOrthogonal(m); m*=node.world_matrix ; // m=GetTransform( m, node.world_matrix );
  1004. if(m.mirrored())mirrored_matrixes++;else normal_matrixes++;
  1005. }
  1006. }
  1007. #else
  1008. REPA(skin->name->name_array)
  1009. {
  1010. Int bone_index=skin->bone_remap(i); if(InRange(bone_index, bones))
  1011. {
  1012. Matrix &m=matrixes[bone_index+VIRTUAL_ROOT_BONE];
  1013. m=skin->bind_shape_matrix; if(m.mirrored())mirrored_matrixes++;else normal_matrixes++;
  1014. }
  1015. }
  1016. #endif
  1017. }
  1018. Bool reverse=(mirrored_matrixes>normal_matrixes);
  1019. if(node.world_matrix.mirrored())reverse^=1; // required
  1020. // set skinning
  1021. Bool node_anim=false; // if node is animated
  1022. Skinning node_skin ; // node skinning
  1023. Memc <Skinning> skinning ; // vertex skinning
  1024. MemtN<IndexWeight, 256> skin ; // temporal weights
  1025. if(geometry.skin && geometry.skin->vtx_wgt.elms())
  1026. {
  1027. VertexWeights &vtx_wgt=geometry.skin->vtx_wgt.first();
  1028. if(vtx_wgt.joint && vtx_wgt.weight)
  1029. {
  1030. Memc<Byte> &bone_remap=geometry.skin->bone_remap;
  1031. Int v_offset=0;
  1032. skinning.setNum(vtx_wgt.vcount.elms()); // this is the number of vertexes
  1033. FREPA( vtx_wgt.vcount)
  1034. {
  1035. REPD(w, vtx_wgt.vcount[i]) // this is the number of weights for one vertex
  1036. {
  1037. Int o=v_offset+vtx_wgt.joint ->offset; if(!InRange(o, vtx_wgt.v))break; Int joint_index=vtx_wgt.v[o];
  1038. o=v_offset+vtx_wgt.weight->offset; if(!InRange(o, vtx_wgt.v))break; Int weight_index=vtx_wgt.v[o];
  1039. if(InRange(joint_index, bone_remap))skin.New().set(bone_remap[joint_index]+VIRTUAL_ROOT_BONE, vtx_wgt.weight->source->getFloat(weight_index));
  1040. v_offset+=vtx_wgt.inputs.elms();
  1041. }
  1042. #if 0 && DEBUG
  1043. FREPA(skin)Log(S+skin[i].bone+"("+skin[i].weight+") ");
  1044. LogN();
  1045. #endif
  1046. SetSkin(skin, skinning[i].bone, skinning[i].blend, &skeleton); skin.clear();
  1047. }
  1048. }
  1049. }else // doesn't have skinning but belongs to a bone
  1050. {
  1051. for(Node *cur=&node; cur; cur=cur->parent)if(cur->bone_index>=0) // find first node that's a bone
  1052. {
  1053. node_anim=true;
  1054. node_skin.bone .set(cur->bone_index+VIRTUAL_ROOT_BONE, 0, 0, 0);
  1055. node_skin.blend.set( 255, 0, 0, 0); // 'blend.sum' must be equal to 255 !!
  1056. break;
  1057. }
  1058. }
  1059. // create mesh
  1060. // triangles
  1061. FREPA(geom_mesh.triangles)
  1062. {
  1063. Triangles &triangles=geom_mesh.triangles[i];
  1064. Input *position =null,
  1065. *normal =null,
  1066. *tex[4] ={null, null, null, null};
  1067. FREPA(triangles.inputs)
  1068. {
  1069. Input &input=triangles.inputs[i];
  1070. switch(input.semantic)
  1071. {
  1072. case POSITION: if(input.source && !position)position=&input; break;
  1073. case NORMAL : if(input.source && !normal )normal =&input; break;
  1074. case TEXCOORD: if(input.source && InRange(input.set, tex))tex[input.set]=&input; break;
  1075. case VERTEX : if(Vertices *vertices=input.vertices)FREPA(vertices->inputs)
  1076. {
  1077. Input &input=vertices->inputs[i];
  1078. switch(input.semantic)
  1079. {
  1080. case POSITION: if(input.source && !position)position=&input; break;
  1081. case NORMAL : if(input.source && !normal )normal =&input; break;
  1082. case TEXCOORD: if(input.source && InRange(input.set, tex))tex[input.set]=&input; break;
  1083. }
  1084. }break;
  1085. }
  1086. }
  1087. FREPA(tex)if(i && tex[i] && !tex[i-1]){Swap(tex[i], tex[i-1]); i-=2;}
  1088. if(triangles.p.elms() && triangles.inputs.elms() && position)
  1089. {
  1090. MeshPart &part=mesh.parts.New(); if(part_material_index)part_material_index.add(findMaterial(triangles.material_symbol, im));
  1091. MeshBase &base=part.base;
  1092. // set part
  1093. Set(part.name, geometry.name);
  1094. // set base
  1095. Int tris=triangles.p.elms()/triangles.inputs.elms()/3;
  1096. base.create(tris*3, 0, tris, 0, (normal?VTX_NRM:0)|(tex[0]?VTX_TEX0:0)|(tex[1]?VTX_TEX1:0)|(tex[2]?VTX_TEX2:0)|((skinning.elms()||node_anim)?VTX_SKIN:0));
  1097. REPA(base.tri)
  1098. {
  1099. Int a=i*3, b=a+1, c=b+1,
  1100. ta=a*triangles.inputs.elms(),
  1101. tb=b*triangles.inputs.elms(),
  1102. tc=c*triangles.inputs.elms(),
  1103. p0=Safe(triangles.p, ta+position->offset),
  1104. p1=Safe(triangles.p, tb+position->offset),
  1105. p2=Safe(triangles.p, tc+position->offset);
  1106. base.vtx.pos(a)=position->source->getVec(p0);
  1107. base.vtx.pos(b)=position->source->getVec(p1);
  1108. base.vtx.pos(c)=position->source->getVec(p2);
  1109. if(skinning.elms())
  1110. {
  1111. // out of range can happen on buggy exporters
  1112. if(InRange(p0, skinning)){base.vtx.matrix(a)=skinning[p0].bone; base.vtx.blend(a)=skinning[p0].blend;}else{base.vtx.matrix(a)=node_skin.bone; base.vtx.blend(a)=node_skin.blend;}
  1113. if(InRange(p1, skinning)){base.vtx.matrix(b)=skinning[p1].bone; base.vtx.blend(b)=skinning[p1].blend;}else{base.vtx.matrix(b)=node_skin.bone; base.vtx.blend(b)=node_skin.blend;}
  1114. if(InRange(p2, skinning)){base.vtx.matrix(c)=skinning[p2].bone; base.vtx.blend(c)=skinning[p2].blend;}else{base.vtx.matrix(c)=node_skin.bone; base.vtx.blend(c)=node_skin.blend;}
  1115. }else
  1116. if(node_anim)
  1117. {
  1118. base.vtx.matrix(a)=node_skin.bone; base.vtx.blend(a)=node_skin.blend;
  1119. base.vtx.matrix(b)=node_skin.bone; base.vtx.blend(b)=node_skin.blend;
  1120. base.vtx.matrix(c)=node_skin.bone; base.vtx.blend(c)=node_skin.blend;
  1121. }
  1122. if(normal)
  1123. {
  1124. base.vtx.nrm(a)=normal->source->getVec(Safe(triangles.p, ta+normal->offset));
  1125. base.vtx.nrm(b)=normal->source->getVec(Safe(triangles.p, tb+normal->offset));
  1126. base.vtx.nrm(c)=normal->source->getVec(Safe(triangles.p, tc+normal->offset));
  1127. }
  1128. if(tex[0])
  1129. {
  1130. base.vtx.tex0(a)=tex[0]->source->getVec2(Safe(triangles.p, ta+tex[0]->offset));
  1131. base.vtx.tex0(b)=tex[0]->source->getVec2(Safe(triangles.p, tb+tex[0]->offset));
  1132. base.vtx.tex0(c)=tex[0]->source->getVec2(Safe(triangles.p, tc+tex[0]->offset));
  1133. }
  1134. if(tex[1])
  1135. {
  1136. base.vtx.tex1(a)=tex[1]->source->getVec2(Safe(triangles.p, ta+tex[1]->offset));
  1137. base.vtx.tex1(b)=tex[1]->source->getVec2(Safe(triangles.p, tb+tex[1]->offset));
  1138. base.vtx.tex1(c)=tex[1]->source->getVec2(Safe(triangles.p, tc+tex[1]->offset));
  1139. }
  1140. if(tex[2])
  1141. {
  1142. base.vtx.tex2(a)=tex[2]->source->getVec2(Safe(triangles.p, ta+tex[2]->offset));
  1143. base.vtx.tex2(b)=tex[2]->source->getVec2(Safe(triangles.p, tb+tex[2]->offset));
  1144. base.vtx.tex2(c)=tex[2]->source->getVec2(Safe(triangles.p, tc+tex[2]->offset));
  1145. }
  1146. base.tri.ind(i).set(a, b, c); if(reverse)base.tri.ind(i).reverse();
  1147. }
  1148. base.removeUnusedVtxs(); base.weldVtx(VTX_NRM|VTX_TEX_ALL|VTX_SKIN|VTX_COLOR, EPSD, EPS_COL_COS, -1); // use small epsilon in case mesh is scaled down
  1149. if( matrixes.elms())base.animate (matrixes);else base.transform(node.world_matrix);
  1150. if(!base.vtx.nrm ())base.setNormals();
  1151. }
  1152. }
  1153. // poly_list
  1154. FREPA(geom_mesh.poly_list)
  1155. {
  1156. PolyList &polys =geom_mesh.poly_list[i];
  1157. Input *position=null,
  1158. *normal =null,
  1159. *tex[4] ={null, null, null, null};
  1160. FREPA(polys.inputs)
  1161. {
  1162. Input &input=polys.inputs[i];
  1163. switch(input.semantic)
  1164. {
  1165. case POSITION: if(input.source && !position)position=&input; break;
  1166. case NORMAL : if(input.source && !normal )normal =&input; break;
  1167. case TEXCOORD: if(input.source && InRange(input.set, tex))tex[input.set]=&input; break;
  1168. case VERTEX : if(Vertices *vertices=input.vertices)FREPA(vertices->inputs)
  1169. {
  1170. Input &input=vertices->inputs[i];
  1171. switch(input.semantic)
  1172. {
  1173. case POSITION: if(input.source && !position)position=&input; break;
  1174. case NORMAL : if(input.source && !normal )normal =&input; break;
  1175. case TEXCOORD: if(input.source && InRange(input.set, tex))tex[input.set]=&input; break;
  1176. }
  1177. }break;
  1178. }
  1179. }
  1180. FREPA(tex)if(i && tex[i] && !tex[i-1]){Swap(tex[i], tex[i-1]); i-=2;}
  1181. if(polys.vcount.elms() && polys.p.elms() && polys.inputs.elms() && position)
  1182. {
  1183. MeshPart &part=mesh.parts.New(); if(part_material_index)part_material_index.add(findMaterial(polys.material_symbol, im));
  1184. MeshBase &base=part.base;
  1185. // set part
  1186. Set(part.name, geometry.name);
  1187. // set base
  1188. Int tris=0; REPA(polys.vcount)tris+=Max(0, polys.vcount[i]-2);
  1189. base.create(tris*3, 0, tris, 0, (normal?VTX_NRM:0)|(tex[0]?VTX_TEX0:0)|(tex[1]?VTX_TEX1:0)|(tex[2]?VTX_TEX2:0)|((skinning.elms()||node_anim)?VTX_SKIN:0));
  1190. Memb<Vtx> poly;
  1191. Int v=0; tris=0; FREPA(polys.vcount)
  1192. {
  1193. Int vrts=polys.vcount[i]; FREP(vrts)
  1194. {
  1195. Int p =Safe(polys.p, v+position->offset);
  1196. Vtx &vtx=poly.New();
  1197. if(skinning.elms())
  1198. {
  1199. if(InRange(p, skinning)) // out of range can happen on buggy exporters
  1200. {
  1201. vtx.matrix=skinning[p].bone ;
  1202. vtx.blend =skinning[p].blend;
  1203. }else
  1204. {
  1205. vtx.matrix=node_skin.bone ;
  1206. vtx.blend =node_skin.blend;
  1207. }
  1208. }else
  1209. if(node_anim)
  1210. {
  1211. vtx.matrix=node_skin.bone ;
  1212. vtx.blend =node_skin.blend;
  1213. }
  1214. vtx.pos =position->source->getVec (p);
  1215. if(normal)vtx.nrm =normal ->source->getVec (Safe(polys.p, v+normal->offset));
  1216. if(tex[0])vtx.tex[0]=tex[0] ->source->getVec2(Safe(polys.p, v+tex[0]->offset));
  1217. if(tex[1])vtx.tex[1]=tex[1] ->source->getVec2(Safe(polys.p, v+tex[1]->offset));
  1218. if(tex[2])vtx.tex[2]=tex[2] ->source->getVec2(Safe(polys.p, v+tex[2]->offset));
  1219. v+=polys.inputs.elms();
  1220. }
  1221. for(; poly.elms()>=3; )
  1222. {
  1223. Int p0=tris*3+0,
  1224. p1=tris*3+1,
  1225. p2=tris*3+2;
  1226. Int o =0,
  1227. o0=(o ) ,
  1228. o1=(o+1)%poly.elms(),
  1229. o2=(o+2)%poly.elms();
  1230. Vtx &v0=poly[o0],
  1231. &v1=poly[o1],
  1232. &v2=poly[o2];
  1233. base.vtx.pos(p0)=v0.pos;
  1234. base.vtx.pos(p1)=v1.pos;
  1235. base.vtx.pos(p2)=v2.pos;
  1236. if(skinning.elms() || node_anim)
  1237. {
  1238. base.vtx.matrix(p0)=v0.matrix; base.vtx.blend(p0)=v0.blend;
  1239. base.vtx.matrix(p1)=v1.matrix; base.vtx.blend(p1)=v1.blend;
  1240. base.vtx.matrix(p2)=v2.matrix; base.vtx.blend(p2)=v2.blend;
  1241. }
  1242. if(normal)
  1243. {
  1244. base.vtx.nrm(p0)=v0.nrm;
  1245. base.vtx.nrm(p1)=v1.nrm;
  1246. base.vtx.nrm(p2)=v2.nrm;
  1247. }
  1248. if(tex[0])
  1249. {
  1250. base.vtx.tex0(p0)=v0.tex[0];
  1251. base.vtx.tex0(p1)=v1.tex[0];
  1252. base.vtx.tex0(p2)=v2.tex[0];
  1253. }
  1254. if(tex[1])
  1255. {
  1256. base.vtx.tex1(p0)=v0.tex[1];
  1257. base.vtx.tex1(p1)=v1.tex[1];
  1258. base.vtx.tex1(p2)=v2.tex[1];
  1259. }
  1260. if(tex[2])
  1261. {
  1262. base.vtx.tex2(p0)=v0.tex[2];
  1263. base.vtx.tex2(p1)=v1.tex[2];
  1264. base.vtx.tex2(p2)=v2.tex[2];
  1265. }
  1266. base.tri.ind(tris).set(p0, p1, p2); if(reverse)base.tri.ind(tris).reverse(); tris++;
  1267. poly.remove(o1, true);
  1268. }
  1269. poly.clear();
  1270. }
  1271. base.removeUnusedVtxs(); base.weldVtx(VTX_NRM|VTX_TEX_ALL|VTX_SKIN|VTX_COLOR, EPSD, EPS_COL_COS, -1); // use small epsilon in case mesh is scaled down
  1272. if( matrixes.elms())base.animate (matrixes);else base.transform(node.world_matrix);
  1273. if(!base.vtx.nrm ())base.setNormals();
  1274. }
  1275. }
  1276. // polygons
  1277. FREPA(geom_mesh.polygons)
  1278. {
  1279. Polygons &polys =geom_mesh.polygons[i];
  1280. Input *position=null,
  1281. *normal =null,
  1282. *tex[4] ={null, null, null, null};
  1283. FREPA(polys.inputs)
  1284. {
  1285. Input &input=polys.inputs[i]; switch(input.semantic)
  1286. {
  1287. case POSITION: if(input.source && !position)position=&input; break;
  1288. case NORMAL : if(input.source && !normal )normal =&input; break;
  1289. case TEXCOORD: if(input.source && InRange(input.set, tex))tex[input.set]=&input; break;
  1290. case VERTEX : if(Vertices *vertices=input.vertices)FREPA(vertices->inputs)
  1291. {
  1292. Input &input=vertices->inputs[i];
  1293. switch(input.semantic)
  1294. {
  1295. case POSITION: if(input.source && !position)position=&input; break;
  1296. case NORMAL : if(input.source && !normal )normal =&input; break;
  1297. case TEXCOORD: if(input.source && InRange(input.set, tex))tex[input.set]=&input; break;
  1298. }
  1299. }break;
  1300. }
  1301. }
  1302. FREPA(tex)if(i && tex[i] && !tex[i-1]){Swap(tex[i], tex[i-1]); i-=2;}
  1303. if(polys.p.elms() && polys.inputs.elms() && position)
  1304. {
  1305. MeshPart &part=mesh.parts.New(); if(part_material_index)part_material_index.add(findMaterial(polys.material_symbol, im));
  1306. MeshBase &base=part.base;
  1307. // set part
  1308. Set(part.name, geometry.name);
  1309. // set base
  1310. Int tris=0; REPA(polys.p)tris+=Max(0, polys.p[i].elms()/polys.inputs.elms()-2);
  1311. base.create(tris*3, 0, tris, 0, (normal?VTX_NRM:0)|(tex[0]?VTX_TEX0:0)|(tex[1]?VTX_TEX1:0)|(tex[2]?VTX_TEX2:0)|((skinning.elms()||node_anim)?VTX_SKIN:0));
  1312. Memb<Vtx> poly;
  1313. tris=0; FREPA(polys.p)
  1314. {
  1315. Memc<Int> &polys_p=polys.p[i];
  1316. Int vrts =polys_p.elms()/polys.inputs.elms(), v=0;
  1317. FREP(vrts)
  1318. {
  1319. Int p =Safe(polys_p, v+position->offset);
  1320. Vtx &vtx=poly.New();
  1321. if(skinning.elms())
  1322. {
  1323. if(InRange(p, skinning)) // out of range can happen on buggy exporters
  1324. {
  1325. vtx.matrix=skinning[p].bone ;
  1326. vtx.blend =skinning[p].blend;
  1327. }else
  1328. {
  1329. vtx.matrix=node_skin.bone ;
  1330. vtx.blend =node_skin.blend;
  1331. }
  1332. }else
  1333. if(node_anim)
  1334. {
  1335. vtx.matrix=node_skin.bone ;
  1336. vtx.blend =node_skin.blend;
  1337. }
  1338. vtx.pos =position->source->getVec (p);
  1339. if(normal)vtx.nrm =normal ->source->getVec (Safe(polys_p, v+normal->offset));
  1340. if(tex[0])vtx.tex[0]=tex[0] ->source->getVec2(Safe(polys_p, v+tex[0]->offset));
  1341. if(tex[1])vtx.tex[1]=tex[1] ->source->getVec2(Safe(polys_p, v+tex[1]->offset));
  1342. if(tex[2])vtx.tex[2]=tex[2] ->source->getVec2(Safe(polys_p, v+tex[2]->offset));
  1343. v+=polys.inputs.elms();
  1344. }
  1345. for(; poly.elms()>=3; )
  1346. {
  1347. Int p0=tris*3+0,
  1348. p1=tris*3+1,
  1349. p2=tris*3+2;
  1350. Int o =0,
  1351. o0=(o ) ,
  1352. o1=(o+1)%poly.elms(),
  1353. o2=(o+2)%poly.elms();
  1354. Vtx &v0=poly[o0],
  1355. &v1=poly[o1],
  1356. &v2=poly[o2];
  1357. base.vtx.pos(p0)=v0.pos;
  1358. base.vtx.pos(p1)=v1.pos;
  1359. base.vtx.pos(p2)=v2.pos;
  1360. if(skinning.elms() || node_anim)
  1361. {
  1362. base.vtx.matrix(p0)=v0.matrix; base.vtx.blend(p0)=v0.blend;
  1363. base.vtx.matrix(p1)=v1.matrix; base.vtx.blend(p1)=v1.blend;
  1364. base.vtx.matrix(p2)=v2.matrix; base.vtx.blend(p2)=v2.blend;
  1365. }
  1366. if(normal)
  1367. {
  1368. base.vtx.nrm(p0)=v0.nrm;
  1369. base.vtx.nrm(p1)=v1.nrm;
  1370. base.vtx.nrm(p2)=v2.nrm;
  1371. }
  1372. if(tex[0])
  1373. {
  1374. base.vtx.tex0(p0)=v0.tex[0];
  1375. base.vtx.tex0(p1)=v1.tex[0];
  1376. base.vtx.tex0(p2)=v2.tex[0];
  1377. }
  1378. if(tex[1])
  1379. {
  1380. base.vtx.tex0(p0)=v0.tex[1];
  1381. base.vtx.tex0(p1)=v1.tex[1];
  1382. base.vtx.tex0(p2)=v2.tex[1];
  1383. }
  1384. if(tex[2])
  1385. {
  1386. base.vtx.tex0(p0)=v0.tex[2];
  1387. base.vtx.tex0(p1)=v1.tex[2];
  1388. base.vtx.tex0(p2)=v2.tex[2];
  1389. }
  1390. base.tri.ind(tris).set(p0, p1, p2); if(reverse)base.tri.ind(tris).reverse(); tris++;
  1391. poly.remove(o1, true);
  1392. }
  1393. poly.clear();
  1394. }
  1395. base.removeUnusedVtxs(); base.weldVtx(VTX_NRM|VTX_TEX_ALL|VTX_SKIN|VTX_COLOR, EPSD, EPS_COL_COS, -1); // use small epsilon in case mesh is scaled down
  1396. if( matrixes.elms())base.animate (matrixes);else base.transform(node.world_matrix);
  1397. if(!base.vtx.nrm ())base.setNormals();
  1398. }
  1399. }
  1400. }
  1401. }
  1402. void DAE::Node::create(::Mesh &mesh, MemPtr<Int> part_material_index, ::Skeleton &skeleton, DAE &dae)
  1403. {
  1404. FREPA(instance_geometry)
  1405. {
  1406. InstanceGeometry &ig=instance_geometry[i];
  1407. if(Geometry *geometry=dae.findGeometry(ig.geometry_id))
  1408. dae.create(mesh, part_material_index, skeleton, *geometry, ig.instance_material, T);
  1409. }
  1410. FREPA(instance_controller)
  1411. {
  1412. InstanceController &ic=instance_controller[i];
  1413. if(Controller *controller=dae.findController(ic.controller_id))if(controller->skin.elms())if(Geometry *geometry=controller->skin[0].geometry)
  1414. dae.create(mesh, part_material_index, skeleton, *geometry, ic.instance_material, T);
  1415. }
  1416. FREPA(nodes)nodes[i].create(mesh, part_material_index, skeleton, dae);
  1417. }
  1418. void DAE::create(::Mesh &mesh, MemPtr<Int> part_material_index, ::Skeleton &skeleton)
  1419. {
  1420. FREPA(visual_scenes)FREPAD(j, visual_scenes[i].nodes)visual_scenes[i].nodes[j].create(mesh, part_material_index, skeleton, T);
  1421. mesh.texScale(Vec2(1, -1), 0)
  1422. .texScale(Vec2(1, -1), 1)
  1423. .texScale(Vec2(1, -1), 2)
  1424. .texScale(Vec2(1, -1), 3);
  1425. }
  1426. /******************************************************************************/
  1427. // CREATE SKELETON
  1428. /******************************************************************************/
  1429. void DAE::create(::Skeleton &skeleton, XAnimation *animation)
  1430. {
  1431. skeleton.bones.setNum(bones.elms());
  1432. FREPA(bones)
  1433. {
  1434. Node &node= *bones[i];
  1435. SkelBone &sbon=skeleton.bones[i];
  1436. Set(sbon.name,node.name);
  1437. sbon.pos =node.world_matrix.pos;
  1438. sbon.dir =node.world_matrix.x ;
  1439. sbon.perp=node.world_matrix.y ;
  1440. sbon.fix();
  1441. Byte parent=0xFF; for(Node *cur=node.parent; cur; cur=cur->parent)if(cur->bone_index>=0){parent=cur->bone_index; break;} // find first parent which is a bone
  1442. sbon.parent=parent;
  1443. #if !CONVERT_BIND_POSE
  1444. Int bone_index=i;
  1445. FREPA(controllers)
  1446. {
  1447. Controller &controller=controllers[i];
  1448. FREPA(controller.skin)
  1449. {
  1450. Skin &skin=controller.skin[i];
  1451. if(skin.name && skin.matrix)
  1452. {
  1453. FREPA(skin.bone_remap)if(skin.bone_remap[i]==bone_index)
  1454. {
  1455. //Matrix bind_shape_matrix_inv; skin.bind_shape_matrix.inverseNonOrthogonal(bind_shape_matrix_inv);
  1456. Matrix m=skin.matrix->getMatrix(i); m.inverseNonOrthogonal();
  1457. sbon.pos = m.pos;
  1458. sbon.dir =!m.x ;
  1459. sbon.perp=!m.y ;
  1460. }
  1461. }
  1462. }
  1463. }
  1464. #endif
  1465. }
  1466. // sort and remap
  1467. MemtN<Byte, 256> old_to_new; skeleton.sortBones(old_to_new);
  1468. Memc<Node*> sorted_bones; sorted_bones.setNumZero(skeleton.bones.elms());
  1469. FREPA(bones)
  1470. {
  1471. Node &node=*bones[i];
  1472. node.adjustBoneIndex(InRange(node.bone_index, old_to_new) ? old_to_new[node.bone_index] : 0xFF);
  1473. if(InRange(node.bone_index, sorted_bones))sorted_bones[node.bone_index]=&node;else node.bone_index=-1;
  1474. }
  1475. Swap(bones, sorted_bones);
  1476. skeleton.setBoneLengths();
  1477. if(animation)create(*animation, skeleton);
  1478. }
  1479. /******************************************************************************/
  1480. // CREATE ANIMATION
  1481. /******************************************************************************/
  1482. void DAE::create(XAnimation &animation, ::Skeleton &skeleton)
  1483. {
  1484. Flt dt_avg=0;
  1485. Int dt_num=0;
  1486. FREPA(skeleton.bones)
  1487. {
  1488. Node &node =*bones(i);
  1489. Memb<Channel*> &channels= node.channels;
  1490. if(channels.elms())
  1491. {
  1492. SkelBone &sbon=skeleton . bones[i];
  1493. AnimBone &abon=animation.anim.bones.New(); abon.set(sbon.name);
  1494. Matrix3 parent_matrix_inv; if(sbon.parent!=0xFF)skeleton.bones[sbon.parent].inverse(parent_matrix_inv);
  1495. Matrix3 local_to_world; node.local_matrix.orn().inverseNonOrthogonal(local_to_world); local_to_world*=node.world_matrix.orn(); // GetTransform(node.local_matrix.orn(), node.world_matrix.orn());
  1496. // gather keyframe time values
  1497. Memc<Flt> times;
  1498. REPA(channels)
  1499. {
  1500. Channel &channel=*channels[i];
  1501. if(channel.time)FREPA(channel.time->float_array)times.include(channel.time->getFloat(i));
  1502. }
  1503. times.sort(Compare);
  1504. Flt dt=0;
  1505. abon.orns .setNumZero(times.elms());
  1506. abon.poss .setNumZero(times.elms());
  1507. abon.scales.setNumZero(times.elms());
  1508. #if HAS_ANIM_COLOR
  1509. abon.colors.setNumZero(times.elms());
  1510. #endif
  1511. FREPA(times)
  1512. {
  1513. Flt t=times[i]; // keyframe time position
  1514. if(i)dt+=t-times[i-1];
  1515. AnimKeys::Orn &orn =abon.orns [i]; orn .time=t;
  1516. AnimKeys::Pos &pos =abon.poss [i]; pos .time=t;
  1517. AnimKeys::Scale &scale=abon.scales[i]; scale.time=t;
  1518. #if HAS_ANIM_COLOR
  1519. AnimKeys::Color &color=abon.colors[i]; color.time=t;
  1520. #endif
  1521. node.animate(t);
  1522. // orientation
  1523. {
  1524. Matrix3 anim;
  1525. anim.x=node.anim_matrix.z;
  1526. anim.y=node.anim_matrix.y;
  1527. anim.z=node.anim_matrix.x;
  1528. anim*=local_to_world;
  1529. if(sbon.parent!=0xFF)anim*=parent_matrix_inv;
  1530. orn.orn=anim;
  1531. orn.orn.fix(); // orn.orn=Orient(!node.anim_matrix.x, !node.anim_matrix.y)*local_to_world; if(sbon.parent!=0xFF)orn.orn*=parent_matrix_inv; orn.orn.fix();
  1532. }
  1533. // position
  1534. {
  1535. pos.pos=node.anim_matrix.pos-node.local_matrix.pos;
  1536. if(node.parent )pos.pos*=node.parent->world_matrix.orn();
  1537. if(sbon.parent!=0xFF)pos.pos*=parent_matrix_inv;
  1538. }
  1539. // scale
  1540. {
  1541. scale.scale.x=ScaleFactorR(node.anim_matrix.z.length()/node.local_matrix.z.length());
  1542. scale.scale.y=ScaleFactorR(node.anim_matrix.y.length()/node.local_matrix.y.length());
  1543. scale.scale.z=ScaleFactorR(node.anim_matrix.x.length()/node.local_matrix.x.length());
  1544. }
  1545. #if HAS_ANIM_COLOR
  1546. // color
  1547. {
  1548. color.color=node.color;
  1549. }
  1550. #endif
  1551. }
  1552. if(times.elms()>2)dt/=times.elms()-1;
  1553. if(dt){dt_avg+=dt; dt_num++;}
  1554. }
  1555. }
  1556. if(dt_num)dt_avg/=dt_num;
  1557. animation.fps=(fps ? fps : dt_avg ? 1/dt_avg : 0);
  1558. animation.anim.length((force_duration>=0) ? force_duration : duration, false).setTangents().setRootMatrix();
  1559. }
  1560. /******************************************************************************/
  1561. // CREATE MATERIALS
  1562. /******************************************************************************/
  1563. void DAE::create(MemPtr<XMaterial> materials, Str path)
  1564. {
  1565. FREPA(T.materials)
  1566. {
  1567. Material &src =T.materials[i];
  1568. XMaterial &dest= materials.New();
  1569. if(src.effect)
  1570. {
  1571. Effect &fx=*src.effect;
  1572. dest.name = src.name;
  1573. dest.color.xyz= fx.color;
  1574. dest.specular =((fx.spec_level>=0) ? fx.spec_level : fx.specular);
  1575. dest.cull = !fx.double_sided;
  1576. if(Image * color_map=findImage(fx. color_map_image_id))dest. color_map= color_map->texture;
  1577. if(Image * alpha_map=findImage(fx. alpha_map_image_id))dest. alpha_map= alpha_map->texture;
  1578. if(Image *specular_map=findImage(fx.specular_map_image_id))dest.specular_map=specular_map->texture;
  1579. if(Image * bump_map=findImage(fx. bump_map_image_id))dest. normal_map= bump_map->texture;
  1580. dest.fixPath(path);
  1581. }
  1582. }
  1583. }
  1584. /******************************************************************************/
  1585. // MAIN
  1586. /******************************************************************************/
  1587. Bool ImportDAE(C Str &name, Mesh *mesh, Skeleton *skeleton, XAnimation *animation, MemPtr<XMaterial> materials, MemPtr<Int> part_material_index)
  1588. {
  1589. if(mesh )mesh ->del();
  1590. if(skeleton )skeleton ->del();
  1591. if(animation)animation->del();
  1592. materials .clear();
  1593. part_material_index.clear();
  1594. XmlData xml; xml.load(name); if(XmlNode *COLLADA=xml.findNode("COLLADA")) // try even if 'load' fails (partial data may be available)
  1595. {
  1596. DAE dae;
  1597. // import
  1598. if(XmlParam *version=COLLADA->findParam("version"))dae.version=version->asInt();
  1599. FREPA(COLLADA->nodes)
  1600. {
  1601. XmlNode &node=COLLADA->nodes[i];
  1602. if(node.name=="asset" )dae.loadAsset (node);else
  1603. if(node.name=="library_animations" )dae.loadLibraryAnimations (node);else
  1604. if(node.name=="library_images" )dae.loadLibraryImages (node);else
  1605. if(node.name=="library_materials" )dae.loadLibraryMaterials (node);else
  1606. if(node.name=="library_effects" )dae.loadLibraryEffects (node);else
  1607. if(node.name=="library_geometries" )dae.loadLibraryGeometries (node);else
  1608. if(node.name=="library_controllers" )dae.loadLibraryControllers (node);else
  1609. if(node.name=="library_visual_scenes")dae.loadLibraryVisualScenes(node);
  1610. }
  1611. // create
  1612. if(materials)
  1613. {
  1614. // link
  1615. FREPA(dae.materials)dae.materials[i].linkEffect(dae);
  1616. dae.create(materials, GetPath(name));
  1617. }
  1618. Skeleton temp, *skel=(skeleton ? skeleton : (mesh || animation) ? &temp : null); // if skel not specified, but we want mesh or animation, then we have to process it
  1619. if(skel)
  1620. {
  1621. // link
  1622. FREPA(dae.animations )dae.animations [i].linkNode(dae); // order is important, channels must be added in order (because there can be many channels for each node, for example rot,pos,..), call this before 'setBoneNodesFromAnim' because we're setting if node has animation or not
  1623. FREPA(dae.controllers)dae.controllers[i].linkSkin(dae); // call before 'setBoneNodesFromAnim' because we're setting controller skin links
  1624. dae.setBoneNodesFromSkin();
  1625. dae.setBoneNodesFromAnim(); // call after 'linkNode' and 'linkSkin' and before 'linkBone'
  1626. FREPA(dae.visual_scenes)FREPAD(j, dae.visual_scenes[i].nodes)dae.visual_scenes[i].nodes[j].linkBone (dae); // order is important, bones must be added in order in which they are in the hierarchy
  1627. FREPA(dae.visual_scenes)FREPAD(j, dae.visual_scenes[i].nodes)dae.visual_scenes[i].nodes[j].setWorldMatrix();
  1628. FREPA(dae.visual_scenes)FREPAD(j, dae.visual_scenes[i].nodes)dae.visual_scenes[i].nodes[j].defaultPose ();
  1629. dae.create(*skel, animation);
  1630. FREPA(dae.visual_scenes)FREPAD(j, dae.visual_scenes[i].nodes)dae.visual_scenes[i].nodes[j].linkSkin(dae); // 'linkSkin' after 'sortBones' inside "dae.create(*skel, .."
  1631. if(mesh)dae.create(*mesh, part_material_index, *skel); // create mesh after 'linkSkin'
  1632. //Animation *anim=(animation ? &animation->anim : null); RemoveNubBones(mesh, *skel, anim);
  1633. // rescale
  1634. if(dae.scale>=0.001f && dae.scale!=1)
  1635. {
  1636. skel -> scale(dae.scale);
  1637. if(animation)animation->anim.scale(dae.scale);
  1638. if(mesh )mesh -> scale(dae.scale);
  1639. }
  1640. // coordinate system
  1641. if(dae.up_axis<0 || dae.up_axis==2) // unknown or Z
  1642. {
  1643. skel -> rightToLeft();
  1644. if(animation)animation->anim.rightToLeft(*skel);
  1645. if(mesh )mesh -> rightToLeft();
  1646. }else
  1647. if(dae.up_axis==1) // Y
  1648. {
  1649. skel -> mirrorX();
  1650. if(animation)animation->anim.mirrorX();
  1651. if(mesh )mesh -> mirrorX();
  1652. }
  1653. skel->setBoneTypes(); // must be called after transforms
  1654. if(mesh ){mesh ->skeleton(skel).skeleton(null).setBox(); CleanMesh(*mesh);}
  1655. if(animation) animation->anim.setBoneTypeIndexesFromSkeleton(*skel);
  1656. if(skeleton ){skel ->setBoneShapes(); if(skeleton!=skel)Swap(*skeleton, *skel);}
  1657. }
  1658. return true;
  1659. }
  1660. return false;
  1661. }
  1662. /******************************************************************************/
  1663. }
  1664. /******************************************************************************/