1 //****************************************************************************//
2 // coremodel.cpp //
3 // Copyright (C) 2001, 2002 Bruno 'Beosil' Heidelberger //
4 //****************************************************************************//
5 // This library is free software; you can redistribute it and/or modify it //
6 // under the terms of the GNU Lesser General Public License as published by //
7 // the Free Software Foundation; either version 2.1 of the License, or (at //
8 // your option) any later version. //
9 //****************************************************************************//
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 //****************************************************************************//
16 // Includes //
17 //****************************************************************************//
18
19 #include "cal3d/coremodel.h"
20 #include "cal3d/error.h"
21 #include "cal3d/coreskeleton.h"
22 #include "cal3d/coreanimation.h"
23 #include "cal3d/coremorphanimation.h"
24 #include "cal3d/coremesh.h"
25 #include "cal3d/corematerial.h"
26 #include "cal3d/loader.h"
27 #include "cal3d/saver.h"
28
29 /*****************************************************************************/
30 /** Constructs the core model instance.
31 *
32 * This function is the default constructor of the core model instance.
33 *****************************************************************************/
34
CalCoreModel(const std::string & name)35 CalCoreModel::CalCoreModel(const std::string& name)
36 : m_strName(name)
37 , m_pCoreSkeleton(0)
38 , m_userData(0)
39 {
40 }
41
42 /*****************************************************************************/
43 /** Destructs the core model instance.
44 *
45 * This function is the destructor of the core model instance.
46 *****************************************************************************/
47
~CalCoreModel()48 CalCoreModel::~CalCoreModel()
49 {
50 // destroy all core morph animations
51 std::vector<CalCoreMorphAnimation *>::iterator iteratorCoreMorphAnimation;
52 for(iteratorCoreMorphAnimation = m_vectorCoreMorphAnimation.begin(); iteratorCoreMorphAnimation !=
53 m_vectorCoreMorphAnimation.end(); ++iteratorCoreMorphAnimation)
54 {
55 delete (*iteratorCoreMorphAnimation);
56 }
57 m_vectorCoreMorphAnimation.clear();
58 }
59
60 /*****************************************************************************/
61 /** Adds a core animation.
62 *
63 * This function adds a core animation to the core model instance.
64 *
65 * @param pCoreAnimation A pointer to the core animation that should be added.
66 *
67 * @return \li the assigned animation \b ID of the added core animation
68 *****************************************************************************/
69
addCoreAnimation(CalCoreAnimation * pCoreAnimation)70 int CalCoreModel::addCoreAnimation(CalCoreAnimation *pCoreAnimation)
71 {
72 int animationId = m_vectorCoreAnimation.size();
73 m_vectorCoreAnimation.push_back(pCoreAnimation);
74 return animationId;
75 }
76
77 /*****************************************************************************/
78 /** Adds a core morph animation.
79 *
80 * This function adds a core morph animation to the core model instance.
81 *
82 * @param pCoreMorphAnimation A pointer to the core morph animation that
83 * should be added.
84 *
85 * @return One of the following values:
86 * \li the assigned morph animation \b ID of the added core morph animation
87 * \li \b -1 if an error happend
88 *****************************************************************************/
89
addCoreMorphAnimation(CalCoreMorphAnimation * pCoreMorphAnimation)90 int CalCoreModel::addCoreMorphAnimation(CalCoreMorphAnimation *pCoreMorphAnimation)
91 {
92 // get the id of the core morph animation
93 int morphAnimationId;
94 morphAnimationId = m_vectorCoreMorphAnimation.size();
95
96 m_vectorCoreMorphAnimation.push_back(pCoreMorphAnimation);
97
98 return morphAnimationId;
99 }
100
101 /*****************************************************************************/
102 /** Adds a core material.
103 *
104 * This function adds a core material to the core model instance.
105 *
106 * @param pCoreMaterial A pointer to the core material that should be added.
107 *
108 * @return One of the following values:
109 * \li the assigned material \b ID of the added core material
110 * \li \b -1 if an error happend
111 *****************************************************************************/
112
addCoreMaterial(CalCoreMaterial * pCoreMaterial)113 int CalCoreModel::addCoreMaterial(CalCoreMaterial *pCoreMaterial)
114 {
115 // get the id of the core material
116 int materialId = m_vectorCoreMaterial.size();
117
118 m_vectorCoreMaterial.push_back(pCoreMaterial);
119
120 return materialId;
121 }
122
123 /*****************************************************************************/
124 /** Adds a core mesh.
125 *
126 * This function adds a core mesh to the core model instance.
127 *
128 * @param pCoreMesh A pointer to the core mesh that should be added.
129 *
130 * @return One of the following values:
131 * \li the assigned mesh \b ID of the added core material
132 * \li \b -1 if an error happend
133 *****************************************************************************/
134
addCoreMesh(CalCoreMesh * pCoreMesh)135 int CalCoreModel::addCoreMesh(CalCoreMesh *pCoreMesh)
136 {
137 // get the id of the core mesh
138 int meshId = m_vectorCoreMesh.size();
139 m_vectorCoreMesh.push_back(pCoreMesh);
140 return meshId;
141 }
142
143 /*****************************************************************************/
144 /** Creates a core material thread.
145 *
146 * This function creates a new core material thread with the given ID.
147 *
148 * @param coreMaterialThreadId The ID of the core material thread that should
149 * be created.
150 *
151 * @return One of the following values:
152 * \li \b true if successful
153 * \li \b false if an error happend
154 *****************************************************************************/
155
createCoreMaterialThread(int coreMaterialThreadId)156 bool CalCoreModel::createCoreMaterialThread(int coreMaterialThreadId)
157 {
158 // insert an empty core material thread with a given id
159 std::map<int, int> mapCoreMaterialThreadId;
160 m_mapmapCoreMaterialThread.insert(std::make_pair(coreMaterialThreadId, mapCoreMaterialThreadId));
161
162 return true;
163 }
164
165 /*****************************************************************************/
166 /** Provides access to a core animation.
167 *
168 * This function returns the core animation with the given ID.
169 *
170 * @param coreAnimationId The ID of the core animation that should be returned.
171 *
172 * @return One of the following values:
173 * \li a pointer to the core animation
174 * \li \b 0 if an error happend
175 *****************************************************************************/
176
getCoreAnimation(int coreAnimationId)177 CalCoreAnimation *CalCoreModel::getCoreAnimation(int coreAnimationId)
178 {
179 if((coreAnimationId < 0) || (coreAnimationId >= (int)m_vectorCoreAnimation.size()))
180 {
181 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
182 return 0;
183 }
184
185 return m_vectorCoreAnimation[coreAnimationId].get();
186 }
187
188 /*****************************************************************************/
189 /** Provides access to a core morph animation.
190 *
191 * This function returns the core morph animation with the given ID.
192 *
193 * @param coreMorphAnimationId The ID of the core morph animation that should be returned.
194 *
195 * @return One of the following values:
196 * \li a pointer to the core morph animation
197 * \li \b 0 if an error happend
198 *****************************************************************************/
199
getCoreMorphAnimation(int coreMorphAnimationId)200 CalCoreMorphAnimation *CalCoreModel::getCoreMorphAnimation(int coreMorphAnimationId)
201 {
202 if((coreMorphAnimationId < 0) || (coreMorphAnimationId >= (int)m_vectorCoreMorphAnimation.size()))
203 {
204 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
205 return 0;
206 }
207
208 return m_vectorCoreMorphAnimation[coreMorphAnimationId];
209 }
210
211 /*****************************************************************************/
212 /** Returns the number of core animations.
213 *
214 * This function returns the number of core animations in the core model
215 * instance.
216 *
217 * @return The number of core animations.
218 *****************************************************************************/
219
getCoreAnimationCount() const220 int CalCoreModel::getCoreAnimationCount() const
221 {
222 return m_vectorCoreAnimation.size();
223 }
224
225 /*****************************************************************************/
226 /** Returns the number of core morph animations.
227 *
228 * This function returns the number of core morph animations in the core model
229 * instance.
230 *
231 * @return The number of core morph animations.
232 *****************************************************************************/
233
getCoreMorphAnimationCount() const234 int CalCoreModel::getCoreMorphAnimationCount() const
235 {
236 return m_vectorCoreMorphAnimation.size();
237 }
238
239
240 /*****************************************************************************/
241 /** Provides access to a core material.
242 *
243 * This function returns the core material with the given ID.
244 *
245 * @param coreMaterialId The ID of the core material that should be returned.
246 *
247 * @return One of the following values:
248 * \li a pointer to the core material
249 * \li \b 0 if an error happend
250 *****************************************************************************/
251
getCoreMaterial(int coreMaterialId)252 CalCoreMaterial *CalCoreModel::getCoreMaterial(int coreMaterialId)
253 {
254 if((coreMaterialId < 0) || (coreMaterialId >= (int)m_vectorCoreMaterial.size()))
255 {
256 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
257 return 0;
258 }
259
260 return m_vectorCoreMaterial[coreMaterialId].get();
261 }
262
263 /*****************************************************************************/
264 /** Returns the number of core materials.
265 *
266 * This function returns the number of core materials in the core model
267 * instance.
268 *
269 * @return The number of core materials.
270 *****************************************************************************/
271
getCoreMaterialCount() const272 int CalCoreModel::getCoreMaterialCount() const
273 {
274 return m_vectorCoreMaterial.size();
275 }
276
277 /*****************************************************************************/
278 /** Returns a specified core material ID.
279 *
280 * This function returns the core material ID for a specified core material
281 * thread / core material set pair.
282 *
283 * @param coreMaterialThreadId The ID of the core material thread.
284 * @param coreMaterialSetId The ID of the core material set.
285 *
286 * @return One of the following values:
287 * \li the \b ID of the core material
288 * \li \b -1 if an error happend
289 *****************************************************************************/
290
getCoreMaterialId(int coreMaterialThreadId,int coreMaterialSetId)291 int CalCoreModel::getCoreMaterialId(int coreMaterialThreadId, int coreMaterialSetId)
292 {
293 // find the core material thread
294 std::map<int, std::map<int, int> >::iterator iteratorCoreMaterialThread;
295 iteratorCoreMaterialThread = m_mapmapCoreMaterialThread.find(coreMaterialThreadId);
296 if(iteratorCoreMaterialThread == m_mapmapCoreMaterialThread.end())
297 {
298 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
299 return -1;
300 }
301
302 // get the core material thread
303 std::map<int, int>& coreMaterialThread = (*iteratorCoreMaterialThread).second;
304
305 // find the material id for the given set
306 std::map<int, int>::iterator iteratorSet;
307 iteratorSet = coreMaterialThread.find(coreMaterialSetId);
308 if(iteratorSet == coreMaterialThread.end())
309 {
310 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
311 return -1;
312 }
313
314 return (*iteratorSet).second;
315 }
316
317 /*****************************************************************************/
318 /** Provides access to a core mesh.
319 *
320 * This function returns the core mesh with the given ID.
321 *
322 * @param coreMeshId The ID of the core mesh that should be returned.
323 *
324 * @return One of the following values:
325 * \li a pointer to the core mesh
326 * \li \b 0 if an error happend
327 *****************************************************************************/
328
getCoreMesh(int coreMeshId)329 CalCoreMesh *CalCoreModel::getCoreMesh(int coreMeshId)
330 {
331 if((coreMeshId < 0) || (coreMeshId >= (int)m_vectorCoreMesh.size()))
332 {
333 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
334 return 0;
335 }
336
337 return m_vectorCoreMesh[coreMeshId].get();
338 }
339
340 /*****************************************************************************/
341 /** Returns the number of core meshes.
342 *
343 * This function returns the number of core meshes in the core model instance.
344 *
345 * @return The number of core meshes.
346 *****************************************************************************/
347
getCoreMeshCount() const348 int CalCoreModel::getCoreMeshCount() const
349 {
350 return m_vectorCoreMesh.size();
351 }
352
353 /*****************************************************************************/
354 /** Provides access to the core skeleton.
355 *
356 * This function returns the core skeleton.
357 *
358 * @return One of the following values:
359 * \li a pointer to the core skeleton
360 * \li \b 0 if an error happend
361 *****************************************************************************/
362
getCoreSkeleton()363 CalCoreSkeleton *CalCoreModel::getCoreSkeleton()
364 {
365 return m_pCoreSkeleton.get();
366 }
367
368 /*****************************************************************************/
369 /** Provides access to the user data.
370 *
371 * This function returns the user data stored in the core model instance.
372 *
373 * @return The user data stored in the core model instance.
374 *****************************************************************************/
375
getUserData()376 Cal::UserData CalCoreModel::getUserData()
377 {
378 return m_userData;
379 }
380
381 /*****************************************************************************/
382 /** Loads a core animation.
383 *
384 * This function loads a core animation from a file.
385 *
386 * @param strFilename The file from which the core animation should be loaded
387 * from.
388 *
389 * @return One of the following values:
390 * \li the assigned \b ID of the loaded core animation
391 * \li \b -1 if an error happend
392 *****************************************************************************/
393
loadCoreAnimation(const std::string & strFilename)394 int CalCoreModel::loadCoreAnimation(const std::string& strFilename)
395 {
396 // the core skeleton has to be loaded already
397 if(!m_pCoreSkeleton)
398 {
399 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
400 return -1;
401 }
402
403 // load a new core animation
404 CalCoreAnimationPtr pCoreAnimation = CalLoader::loadCoreAnimation(strFilename, m_pCoreSkeleton.get());
405 if(!pCoreAnimation) return -1;
406
407
408 // add core animation to this core model
409 return addCoreAnimation(pCoreAnimation.get());
410 }
411
412 /*****************************************************************************/
413 /** Loads a core animation and bind it to a name.
414 *
415 * This function loads a core animation from a file. It is equivalent
416 * to calling addAnimName(strAnimationName, loadCoreAnimation(strFilename)).
417 * If strAnimationName is already associated to a coreAnimationId because
418 * of a previous call to addAnimName, the same coreAnimationId will
419 * be used.
420 *
421 * @param strFilename The file from which the core animation should be loaded
422 * from.
423 * @param strAnimationName A string that is associated with an anim ID number.
424 *
425 * @return One of the following values:
426 * \li the assigned \b ID of the loaded core animation
427 * \li \b -1 if an error happend
428 *****************************************************************************/
429
loadCoreAnimation(const std::string & strFilename,const std::string & strAnimationName)430 int CalCoreModel::loadCoreAnimation(const std::string& strFilename, const std::string& strAnimationName)
431 {
432 int id = -1;
433 std::map<std::string, int>::iterator it = m_animationName.find(strAnimationName);
434 if (it != m_animationName.end())
435 {
436 id=(*it).second;
437
438 // the core skeleton has to be loaded already
439 if(!m_pCoreSkeleton)
440 {
441 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
442 return -1;
443 }
444 if(m_vectorCoreAnimation[id])
445 {
446 CalError::setLastError(CalError::INDEX_BUILD_FAILED, __FILE__, __LINE__);
447 return -1;
448 }
449 CalCoreAnimationPtr pCoreAnimation = CalLoader::loadCoreAnimation(strFilename, m_pCoreSkeleton.get());
450 if(!pCoreAnimation) return -1;
451 pCoreAnimation->setName(strAnimationName);
452 m_vectorCoreAnimation[id] = pCoreAnimation;
453 }
454 else
455 {
456 id = loadCoreAnimation(strFilename);
457 if(id >= 0)
458 addAnimationName(strAnimationName, id);
459 }
460
461 return id;
462 }
463
464 /*****************************************************************************/
465 /** Delete the resources used by the named core animation. The name must
466 * be associated with a valid core animation Id with the function
467 * getAnimationId. The caller must ensure that the corresponding is not
468 * referenced anywhere otherwise unpredictable results will occur.
469 *
470 * @param name The symbolic name of the core animation to unload.
471 *
472 * @return One of the following values:
473 * \li the core \b ID of the unloaded core animation
474 * \li \b -1 if an error happend
475 *****************************************************************************/
476
unloadCoreAnimation(const std::string & name)477 int CalCoreModel::unloadCoreAnimation(const std::string& name)
478 {
479 int id = getCoreAnimationId(name);
480 if(id >= 0)
481 return unloadCoreAnimation(id);
482 else
483 return -1;
484 }
485
486 /*****************************************************************************/
487 /** Delete the resources used by a core animation. The caller must
488 * ensure that the corresponding is not referenced anywhere otherwise
489 * unpredictable results will occur.
490 *
491 * @param coreAnimationId The ID of the core animation that should be unloaded.
492 *
493 * @return One of the following values:
494 * \li the core \b ID of the unloaded core animation
495 * \li \b -1 if an error happend
496 *****************************************************************************/
497
498
unloadCoreAnimation(int coreAnimationId)499 int CalCoreModel::unloadCoreAnimation(int coreAnimationId)
500 {
501 if((coreAnimationId < 0) || (coreAnimationId >= (int)m_vectorCoreAnimation.size()))
502 {
503 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
504 return -1;
505 }
506
507 m_vectorCoreAnimation[coreAnimationId] = CalCoreAnimationPtr (0);
508
509 return coreAnimationId;
510 }
511
512 /*****************************************************************************/
513 /** Loads a core material.
514 *
515 * This function loads a core material from a file.
516 *
517 * @param strFilename The file from which the core material should be loaded
518 * from.
519 *
520 * @return One of the following values:
521 * \li the assigned \b ID of the loaded core material
522 * \li \b -1 if an error happend
523 *****************************************************************************/
524
loadCoreMaterial(const std::string & strFilename)525 int CalCoreModel::loadCoreMaterial(const std::string& strFilename)
526 {
527 // the core skeleton has to be loaded already
528 if(!m_pCoreSkeleton)
529 {
530 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
531 return -1;
532 }
533
534 // load a new core material
535 CalCoreMaterialPtr pCoreMaterial = CalLoader::loadCoreMaterial(strFilename);
536 if(!pCoreMaterial) return -1;
537
538 // add core material to this core model
539 return addCoreMaterial(pCoreMaterial.get());
540 }
541
542 /*****************************************************************************/
543 /** Loads a core material and bind it to a name.
544 *
545 * This function loads a core material from a file. It is equivalent
546 * to calling addMaterialName(strMaterialName, loadCoreMaterial(strFilename)).
547 * If strMaterialName is already associated to a coreMaterialId because
548 * of a previous call to addMaterialName, the same coreMaterialId will
549 * be used.
550 *
551 * @param strFilename The file from which the core material should be loaded
552 * from.
553 * @param strMaterialName A string that is associated with an anim ID number.
554 *
555 * @return One of the following values:
556 * \li the assigned \b ID of the loaded core material
557 * \li \b -1 if an error happend
558 *****************************************************************************/
559
loadCoreMaterial(const std::string & strFilename,const std::string & strMaterialName)560 int CalCoreModel::loadCoreMaterial(const std::string& strFilename, const std::string& strMaterialName)
561 {
562 int id = -1;
563 std::map<std::string, int>::iterator it = m_materialName.find(strMaterialName);
564 if (it != m_materialName.end())
565 {
566 id=(*it).second;
567
568 // the core skeleton has to be loaded already
569 if(!m_pCoreSkeleton)
570 {
571 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
572 return -1;
573 }
574 if(m_vectorCoreMaterial[id])
575 {
576 CalError::setLastError(CalError::INDEX_BUILD_FAILED, __FILE__, __LINE__);
577 return -1;
578 }
579 CalCoreMaterialPtr pCoreMaterial = CalLoader::loadCoreMaterial(strFilename);
580 if(!pCoreMaterial) return -1;
581 pCoreMaterial->setName(strMaterialName);
582 m_vectorCoreMaterial[id] = pCoreMaterial;
583 }
584 else
585 {
586 id = loadCoreMaterial(strFilename);
587 if(id >= 0)
588 addMaterialName(strMaterialName, id);
589 }
590
591 return id;
592 }
593
594 /*****************************************************************************/
595 /** Delete the resources used by the named core material. The name must
596 * be associated with a valid core material Id with the function
597 * getMaterialId. The caller must ensure that the corresponding is not
598 * referenced anywhere otherwise unpredictable results will occur.
599 *
600 * @param name The symbolic name of the core material to unload.
601 *
602 * @return One of the following values:
603 * \li the core \b ID of the unloaded core material
604 * \li \b -1 if an error happend
605 *****************************************************************************/
606
unloadCoreMaterial(const std::string & name)607 int CalCoreModel::unloadCoreMaterial(const std::string& name)
608 {
609 int id = getCoreMaterialId(name);
610 if(id >= 0)
611 return unloadCoreMaterial(id);
612 else
613 return -1;
614 }
615
616 /*****************************************************************************/
617 /** Delete the resources used by a core material. The caller must
618 * ensure that the corresponding is not referenced anywhere otherwise
619 * unpredictable results will occur.
620 *
621 * @param coreMaterialId The ID of the core material that should be unloaded.
622 *
623 * @return One of the following values:
624 * \li the core \b ID of the unloaded core material
625 * \li \b -1 if an error happend
626 *****************************************************************************/
627
628
unloadCoreMaterial(int coreMaterialId)629 int CalCoreModel::unloadCoreMaterial(int coreMaterialId)
630 {
631 if((coreMaterialId < 0) || (coreMaterialId >= (int)m_vectorCoreMaterial.size()))
632 {
633 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
634 return -1;
635 }
636
637 m_vectorCoreMaterial[coreMaterialId] = CalCoreMaterialPtr(0);
638
639 return coreMaterialId;
640 }
641
642 /*****************************************************************************/
643 /** Loads a core mesh.
644 *
645 * This function loads a core mesh from a file.
646 *
647 * @param strFilename The file from which the core mesh should be loaded from.
648 *
649 * @return One of the following values:
650 * \li the assigned \b ID of the loaded core mesh
651 * \li \b -1 if an error happend
652 *****************************************************************************/
653
loadCoreMesh(const std::string & strFilename)654 int CalCoreModel::loadCoreMesh(const std::string& strFilename)
655 {
656 // the core skeleton has to be loaded already
657 if(!m_pCoreSkeleton)
658 {
659 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
660 return -1;
661 }
662
663 // load a new core mesh
664 CalCoreMeshPtr pCoreMesh = CalLoader::loadCoreMesh(strFilename);
665 if(!pCoreMesh) return -1;
666
667 // add core mesh to this core model
668 return addCoreMesh(pCoreMesh.get());
669 }
670
671 /*****************************************************************************/
672 /** Loads a core mesh and bind it to a name.
673 *
674 * This function loads a core mesh from a file. It is equivalent
675 * to calling addMeshName(strMeshName, loadCoreMesh(strFilename)).
676 * If strMeshName is already associated to a coreMeshId because
677 * of a previous call to addMeshName, the same coreMeshId will
678 * be used.
679 *
680 * @param strFilename The file from which the core mesh should be loaded
681 * from.
682 * @param strMeshName A string that is associated with an anim ID number.
683 *
684 * @return One of the following values:
685 * \li the assigned \b ID of the loaded core mesh
686 * \li \b -1 if an error happend
687 *****************************************************************************/
688
loadCoreMesh(const std::string & strFilename,const std::string & strMeshName)689 int CalCoreModel::loadCoreMesh(const std::string& strFilename, const std::string& strMeshName)
690 {
691 int id = -1;
692 std::map<std::string, int>::iterator it = m_meshName.find(strMeshName);
693 if (it != m_meshName.end())
694 {
695 id=(*it).second;
696
697 // the core skeleton has to be loaded already
698 if(!m_pCoreSkeleton)
699 {
700 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
701 return -1;
702 }
703 if(m_vectorCoreMesh[id])
704 {
705 CalError::setLastError(CalError::INDEX_BUILD_FAILED, __FILE__, __LINE__);
706 return -1;
707 }
708 CalCoreMeshPtr pCoreMesh = CalLoader::loadCoreMesh(strFilename);
709 if(!pCoreMesh) return -1;
710 pCoreMesh->setName(strMeshName);
711 m_vectorCoreMesh[id] = pCoreMesh;
712 }
713 else
714 {
715 id = loadCoreMesh(strFilename);
716 if(id >= 0)
717 addMeshName(strMeshName, id);
718 }
719
720 return id;
721 }
722
723
724 /*****************************************************************************/
725 /** Delete the resources used by the named core mesh. The name must
726 * be associated with a valid core mesh Id with the function
727 * getMeshId. The caller must ensure that the corresponding is not
728 * referenced anywhere otherwise unpredictable results will occur.
729 *
730 * @param name The symbolic name of the core mesh to unload.
731 *
732 * @return One of the following values:
733 * \li the core \b ID of the unloaded core mesh
734 * \li \b -1 if an error happend
735 *****************************************************************************/
736
unloadCoreMesh(const std::string & name)737 int CalCoreModel::unloadCoreMesh(const std::string& name)
738 {
739 int id = getCoreMeshId(name);
740 if(id >= 0)
741 return unloadCoreMesh(id);
742 else
743 return -1;
744 }
745
746 /*****************************************************************************/
747 /** Delete the resources used by a core mesh. The caller must
748 * ensure that the corresponding is not referenced anywhere otherwise
749 * unpredictable results will occur.
750 *
751 * @param coreMeshId The ID of the core mesh that should be unloaded.
752 *
753 * @return One of the following values:
754 * \li the core \b ID of the unloaded core mesh
755 * \li \b -1 if an error happend
756 *****************************************************************************/
757
758
unloadCoreMesh(int coreMeshId)759 int CalCoreModel::unloadCoreMesh(int coreMeshId)
760 {
761 if((coreMeshId < 0) || (coreMeshId >= (int)m_vectorCoreMesh.size()))
762 {
763 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
764 return -1;
765 }
766
767 m_vectorCoreMesh[coreMeshId] = CalCoreMeshPtr(0);
768
769 return coreMeshId;
770 }
771
772
773 /*****************************************************************************/
774 /** Loads the core skeleton.
775 *
776 * This function loads the core skeleton from a file.
777 *
778 * @param strFilename The file from which the core skeleton should be loaded
779 * from.
780 *
781 * @return One of the following values:
782 * \li \b true if successful
783 * \li \b false if an error happend
784 *****************************************************************************/
785
loadCoreSkeleton(const std::string & strFilename)786 bool CalCoreModel::loadCoreSkeleton(const std::string& strFilename)
787 {
788 // load a new core skeleton
789 m_pCoreSkeleton = CalLoader::loadCoreSkeleton(strFilename);
790 return bool(m_pCoreSkeleton);
791 }
792
793 /*****************************************************************************/
794 /** Saves a core animation.
795 *
796 * This function saves a core animation to a file.
797 *
798 * @param strFilename The file to which the core animation should be saved to.
799 * @param coreAnimationId The ID of the core animation that should be saved.
800 *
801 * @return One of the following values:
802 * \li \b true if successful
803 * \li \b false if an error happend
804 *****************************************************************************/
805
saveCoreAnimation(const std::string & strFilename,int coreAnimationId)806 bool CalCoreModel::saveCoreAnimation(const std::string& strFilename, int coreAnimationId)
807 {
808 // check if the core animation id is valid
809 if((coreAnimationId < 0) || (coreAnimationId >= (int)m_vectorCoreAnimation.size()))
810 {
811 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
812 return false;
813 }
814
815 // save the core animation
816 if(!CalSaver::saveCoreAnimation(strFilename, m_vectorCoreAnimation[coreAnimationId].get()))
817 {
818 return false;
819 }
820
821 return true;
822 }
823
824 /*****************************************************************************/
825 /** Saves a core material.
826 *
827 * This function saves a core material to a file.
828 *
829 * @param strFilename The file to which the core material should be saved to.
830 * @param coreMaterialId The ID of the core material that should be saved.
831 *
832 * @return One of the following values:
833 * \li \b true if successful
834 * \li \b false if an error happend
835 *****************************************************************************/
836
saveCoreMaterial(const std::string & strFilename,int coreMaterialId)837 bool CalCoreModel::saveCoreMaterial(const std::string& strFilename, int coreMaterialId)
838 {
839 // check if the core material id is valid
840 if((coreMaterialId < 0) || (coreMaterialId >= (int)m_vectorCoreMaterial.size()))
841 {
842 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
843 return false;
844 }
845
846 // save the core animation
847 return CalSaver::saveCoreMaterial(strFilename, m_vectorCoreMaterial[coreMaterialId].get());
848 }
849
850 /*****************************************************************************/
851 /** Saves a core mesh.
852 *
853 * This function saves a core mesh to a file.
854 *
855 * @param strFilename The file to which the core mesh should be saved to.
856 * @param coreMeshId The ID of the core mesh that should be saved.
857 *
858 * @return One of the following values:
859 * \li \b true if successful
860 * \li \b false if an error happend
861 *****************************************************************************/
862
saveCoreMesh(const std::string & strFilename,int coreMeshId)863 bool CalCoreModel::saveCoreMesh(const std::string& strFilename, int coreMeshId)
864 {
865 // check if the core mesh id is valid
866 if((coreMeshId < 0) || (coreMeshId >= (int)m_vectorCoreMesh.size()))
867 {
868 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
869 return false;
870 }
871
872 // save the core animation
873 return CalSaver::saveCoreMesh(strFilename, m_vectorCoreMesh[coreMeshId].get());
874 }
875
876 /*****************************************************************************/
877 /** Saves the core skeleton.
878 *
879 * This function saves the core skeleton to a file.
880 *
881 * @param strFilename The file to which the core skeleton should be saved to.
882 *
883 * @return One of the following values:
884 * \li \b true if successful
885 * \li \b false if an error happend
886 *****************************************************************************/
887
saveCoreSkeleton(const std::string & strFilename)888 bool CalCoreModel::saveCoreSkeleton(const std::string& strFilename)
889 {
890 // check if we have a core skeleton in this code model
891 if(!m_pCoreSkeleton)
892 {
893 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
894 return false;
895 }
896
897 // save the core skeleton
898 return CalSaver::saveCoreSkeleton(strFilename, m_pCoreSkeleton.get());
899 }
900
901 /*****************************************************************************/
902 /** Sets a core material ID.
903 *
904 * This function sets a core material ID for a core material thread / core
905 * material set pair.
906 *
907 * @param coreMaterialThreadId The ID of the core material thread.
908 * @param coreMaterialSetId The ID of the core maetrial set.
909 * @param coreMaterialId The ID of the core maetrial.
910 *
911 * @return One of the following values:
912 * \li \b true if successful
913 * \li \b false if an error happend
914 *****************************************************************************/
915
setCoreMaterialId(int coreMaterialThreadId,int coreMaterialSetId,int coreMaterialId)916 bool CalCoreModel::setCoreMaterialId(int coreMaterialThreadId, int coreMaterialSetId, int coreMaterialId)
917 {
918 // find the core material thread
919 std::map<int, std::map<int, int> >::iterator iteratorCoreMaterialThread;
920 iteratorCoreMaterialThread = m_mapmapCoreMaterialThread.find(coreMaterialThreadId);
921 if(iteratorCoreMaterialThread == m_mapmapCoreMaterialThread.end())
922 {
923 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
924 return false;
925 }
926
927 // get the core material thread
928 std::map<int, int>& coreMaterialThread = (*iteratorCoreMaterialThread).second;
929
930 // remove a possible entry in the core material thread
931 coreMaterialThread.erase(coreMaterialSetId);
932
933 // set the given set id in the core material thread to the given core material id
934 coreMaterialThread.insert(std::make_pair(coreMaterialSetId, coreMaterialId));
935
936 return true;
937 }
938
939 /*****************************************************************************/
940 /** Sets the core skeleton.
941 *
942 * This function sets the core skeleton of the core model instance..
943 *
944 * @param pCoreSkeleton The core skeleton that should be set.
945 *****************************************************************************/
946
setCoreSkeleton(CalCoreSkeleton * pCoreSkeleton)947 void CalCoreModel::setCoreSkeleton(CalCoreSkeleton *pCoreSkeleton)
948 {
949 if(pCoreSkeleton == 0)
950 {
951 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
952 return;
953 }
954 m_pCoreSkeleton = pCoreSkeleton;
955 }
956
957 /*****************************************************************************/
958 /** Stores user data.
959 *
960 * This function stores user data in the core model instance.
961 *
962 * @param userData The user data that should be stored.
963 *****************************************************************************/
964
setUserData(Cal::UserData userData)965 void CalCoreModel::setUserData(Cal::UserData userData)
966 {
967 m_userData = userData;
968 }
969
970 /*****************************************************************************/
971 /** Creates or overwrites a string-to-boneId mapping
972 *
973 * This function makes a bone ID reference-able by a string name.
974 *
975 * @param strBoneName The string that will be associated with the ID.
976 * @param boneId The ID number of the bone that will be referenced by the string.
977 *****************************************************************************/
978
addBoneName(const std::string & strBoneName,int boneId)979 void CalCoreModel::addBoneName(const std::string& strBoneName, int boneId)
980 {
981 //Make sure the skeleton has been loaded first
982 if (m_pCoreSkeleton)
983 {
984 //Map the bone ID to the name
985 m_pCoreSkeleton->mapCoreBoneName(boneId, strBoneName);
986 }
987 }
988
989 /*****************************************************************************/
990 /** Retrieves the ID of the bone referenced by a string
991 *
992 * This function returns a bone ID
993 *
994 * @param strBoneName A string that is associated with a bone ID number.
995 * @return Returns:
996 * \li \b -1 if there is no bone ID associated with the input string
997 * \li \b the ID number of the bone asssociated with the input string
998 *****************************************************************************/
999
getBoneId(const std::string & strBoneName) const1000 int CalCoreModel::getBoneId(const std::string& strBoneName) const
1001 {
1002 if (m_pCoreSkeleton)
1003 {
1004 return m_pCoreSkeleton->getCoreBoneId(strBoneName);
1005 }
1006 return -1;
1007 }
1008
1009 /*****************************************************************************/
1010 /** Creates or overwrites a string-to-animation ID mapping
1011 *
1012 * This function makes an animation ID reference-able by a string name.
1013 * Note that we don't verify that the ID is valid because the animation
1014 * may be added later.
1015 * Also, if there is already a helper with this name, it will be overwritten
1016 * without warning.
1017 *
1018 * @param strAnimationName The string that will be associated with the ID.
1019 * @param coreAnimationId The ID number of the animation to be referenced by the string.
1020 *****************************************************************************/
1021
addAnimationName(const std::string & strAnimationName,int coreAnimationId)1022 bool CalCoreModel::addAnimationName(const std::string& strAnimationName, int coreAnimationId)
1023 {
1024 // check if the core animation id is valid
1025 if((coreAnimationId < 0) || (coreAnimationId >= (int)m_vectorCoreAnimation.size()))
1026 {
1027 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
1028 return false;
1029 }
1030
1031 m_vectorCoreAnimation[ coreAnimationId ]->setName(strAnimationName);
1032 m_animationName[ strAnimationName ] = coreAnimationId;
1033 return true;
1034 }
1035
1036 /*****************************************************************************/
1037 /** Retrieves the ID of the animation referenced by a string
1038 *
1039 * This function returns an animation ID
1040 *
1041 * @param strAnimationName A string that is associated with an anim ID number.
1042 * @return Returns:
1043 * \li \b -1 if there is no anim ID associated with the input string
1044 * \li \b the ID number of the anim asssociated with the input string
1045 *****************************************************************************/
1046
getCoreAnimationId(const std::string & strAnimationName)1047 int CalCoreModel::getCoreAnimationId(const std::string& strAnimationName)
1048 {
1049 if (m_animationName.count( strAnimationName ) < 1)
1050 {
1051 return -1;
1052 }
1053
1054 if (getCoreAnimation(m_animationName[strAnimationName]) == NULL)
1055 {
1056 return -1;
1057 }
1058
1059 return m_animationName[strAnimationName];
1060 }
1061
1062 /*****************************************************************************/
1063 /** Creates or overwrites a string-to-core-material ID mapping
1064 *
1065 * This function makes a core material ID reference-able by a string name.
1066 * Note that we don't verify that the ID is valid because the material
1067 * may be added later.
1068 * Also, if there is already a helper with this name, it will be overwritten
1069 * without warning.
1070 *
1071 * @param strMaterialName The string that will be associated with the ID.
1072 * @param coreMaterialId The core ID number of the material to be referenced by the string.
1073 *****************************************************************************/
1074
addMaterialName(const std::string & strMaterialName,int coreMaterialId)1075 bool CalCoreModel::addMaterialName(const std::string& strMaterialName, int coreMaterialId)
1076 {
1077
1078 // check if the core material id is valid
1079 if((coreMaterialId < 0) || (coreMaterialId >= (int)m_vectorCoreMaterial.size()))
1080 {
1081 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
1082 return false;
1083 }
1084
1085 m_vectorCoreMaterial[ coreMaterialId ]->setName(strMaterialName);
1086 m_materialName[ strMaterialName ] = coreMaterialId;
1087 return true;
1088 }
1089
1090 /*****************************************************************************/
1091 /** Retrieves the ID of the core material referenced by a string
1092 *
1093 * This function returns a core material ID
1094 *
1095 * @param strMaterialName A string that is associated with a core material ID number.
1096 * @return Returns:
1097 * \li \b -1 if there is no core material ID associated with the input string
1098 * \li \b the core ID number of the material asssociated with the input string
1099 *****************************************************************************/
1100
getCoreMaterialId(const std::string & strMaterialName)1101 int CalCoreModel::getCoreMaterialId(const std::string& strMaterialName)
1102 {
1103 if (m_materialName.count( strMaterialName ) < 1)
1104 {
1105 return -1;
1106 }
1107
1108 if (getCoreMaterial(m_materialName[strMaterialName]) == NULL)
1109 {
1110 return -1;
1111 }
1112
1113 return m_materialName[strMaterialName];
1114 }
1115
1116
1117 /*****************************************************************************/
1118 /** Creates or overwrites a string-to-core-mesh ID mapping
1119 *
1120 * This function makes a core mesh ID reference-able by a string name.
1121 * Note that we don't verify that the ID is valid because the mesh
1122 * may be added later.
1123 * Also, if there is already a helper with this name, it will be overwritten
1124 * without warning.
1125 *
1126 * @param strMeshName The string that will be associated with the ID.
1127 * @param coreMeshId The core ID number of the mesh to be referenced by the string.
1128 *****************************************************************************/
1129
addMeshName(const std::string & strMeshName,int coreMeshId)1130 bool CalCoreModel::addMeshName(const std::string& strMeshName, int coreMeshId)
1131 {
1132 // check if the core mesh id is valid
1133 if((coreMeshId < 0) || (coreMeshId >= (int)m_vectorCoreMesh.size()))
1134 {
1135 CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
1136 return false;
1137 }
1138
1139 m_vectorCoreMesh[ coreMeshId ]->setName(strMeshName);
1140 m_meshName[ strMeshName ] = coreMeshId;
1141 return true;
1142 }
1143
1144 /*****************************************************************************/
1145 /** Retrieves the ID of the core mesh referenced by a string
1146 *
1147 * This function returns a core mesh ID
1148 *
1149 * @param strMeshName A string that is associated with a core mesh ID number.
1150 * @return Returns:
1151 * \li \b -1 if there is no core mesh ID associated with the input string
1152 * \li \b the core ID number of the mesh asssociated with the input string
1153 *****************************************************************************/
1154
getCoreMeshId(const std::string & strMeshName)1155 int CalCoreModel::getCoreMeshId(const std::string& strMeshName)
1156 {
1157 if (m_meshName.count( strMeshName ) < 1)
1158 {
1159 return -1;
1160 }
1161
1162 if (getCoreMesh(m_meshName[strMeshName]) == NULL)
1163 {
1164 return -1;
1165 }
1166
1167 return m_meshName[strMeshName];
1168 }
1169
1170 /*****************************************************************************/
1171 /** Scale the core model.
1172 *
1173 * This function rescale all data that are in the core model instance
1174 *
1175 * @param factor A float with the scale factor
1176 *
1177 *****************************************************************************/
1178
1179
scale(float factor)1180 void CalCoreModel::scale(float factor)
1181 {
1182 m_pCoreSkeleton->scale(factor);
1183
1184 for(size_t animationId = 0; animationId < m_vectorCoreAnimation.size(); animationId++)
1185 {
1186 m_vectorCoreAnimation[animationId]->scale(factor);
1187 }
1188
1189 for(size_t meshId = 0; meshId < m_vectorCoreMesh.size(); meshId++)
1190 {
1191 m_vectorCoreMesh[meshId]->scale(factor);
1192 }
1193
1194 }
1195
1196 //****************************************************************************//
1197