Node.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include "Node.h"
  2. #define NODE 1
  3. #define JOINT 2
  4. namespace gameplay
  5. {
  6. Node::Node(void) :
  7. _childCount(0),
  8. _nextSibling(NULL), _previousSibling(NULL),
  9. _firstChild(NULL), _lastChild(NULL), _parent(NULL),
  10. camera(NULL), light(NULL), model(NULL), joint(false)
  11. {
  12. setIdentityMatrix(transform);
  13. }
  14. Node::~Node(void)
  15. {
  16. }
  17. unsigned int Node::getTypeId(void) const
  18. {
  19. return NODE_ID;
  20. }
  21. const char* Node::getElementName(void) const
  22. {
  23. return "Node";
  24. }
  25. void Node::writeBinary(FILE* file)
  26. {
  27. Object::writeBinary(file);
  28. // node type
  29. unsigned int type = joint ? JOINT : NODE;
  30. write(type, file);
  31. write(transform, 16, file);
  32. // children
  33. write(getChildCount(), file); // write number of children
  34. for (Node* node = getFirstChild(); node != NULL; node = node->getNextSibling())
  35. {
  36. node->writeBinary(file);
  37. }
  38. // camera
  39. if (camera != NULL)
  40. {
  41. camera->writeBinary(file);
  42. }
  43. else
  44. {
  45. write((unsigned char)0, file);
  46. }
  47. // light
  48. if (light != NULL && !light->isAmbient())
  49. {
  50. light->writeBinary(file);
  51. }
  52. else
  53. {
  54. write((unsigned char)0, file);
  55. }
  56. // mesh
  57. if (model != NULL)
  58. {
  59. model->writeBinary(file);
  60. }
  61. else
  62. {
  63. writeZero(file);
  64. }
  65. }
  66. void Node::writeText(FILE* file)
  67. {
  68. if (isJoint())
  69. {
  70. fprintf(file, "<%s id=\"%s\" type=\"%s\">\n", getElementName(), id.c_str(), "JOINT");
  71. }
  72. else
  73. {
  74. fprintElementStart(file);
  75. }
  76. fprintf(file, "<transform>");
  77. fprintfMatrix4f(file, transform);
  78. fprintf(file, "</transform>\n");
  79. // children
  80. for (Node* node = getFirstChild(); node != NULL; node = node->getNextSibling())
  81. {
  82. node->writeText(file);
  83. }
  84. // camera
  85. if (camera != NULL)
  86. {
  87. camera->writeText(file);
  88. }
  89. // light
  90. if (light != NULL && !light->isAmbient())
  91. {
  92. light->writeText(file);
  93. }
  94. // mesh
  95. if (model != NULL)
  96. {
  97. model->writeText(file);
  98. }
  99. fprintElementEnd(file);
  100. }
  101. void Node::addChild(Node* child)
  102. {
  103. // If this child is already parented, remove it from its parent
  104. if (child->_parent)
  105. {
  106. child->_parent->removeChild(child);
  107. }
  108. if (_firstChild == NULL)
  109. {
  110. // No children yet
  111. _firstChild = child;
  112. child->_previousSibling = NULL;
  113. }
  114. else
  115. {
  116. // We already have children, so append to them
  117. child->_previousSibling = _lastChild;
  118. _lastChild->_nextSibling = child;
  119. }
  120. child->_parent = this;
  121. child->_nextSibling = NULL;
  122. this->_lastChild = child;
  123. ++_childCount;
  124. }
  125. void Node::removeChild(Node* child)
  126. {
  127. // The child must have already been our child to remove it
  128. if (child->_parent != this)
  129. {
  130. return;
  131. }
  132. // Remove the child by un-linking it from our child list
  133. if (child->_nextSibling)
  134. {
  135. child->_nextSibling->_previousSibling = child->_previousSibling;
  136. }
  137. if (child->_previousSibling)
  138. {
  139. child->_previousSibling->_nextSibling = child->_nextSibling;
  140. }
  141. // Was this child our first or last child?
  142. if (child == _firstChild)
  143. _firstChild = child->_nextSibling;
  144. if (child == _lastChild)
  145. _lastChild = child->_previousSibling;
  146. // Remove parent and sibling info from the child, now that it is no longer parented
  147. child->_parent = NULL;
  148. child->_nextSibling = NULL;
  149. child->_previousSibling = NULL;
  150. --_childCount;
  151. }
  152. void Node::removeChildren()
  153. {
  154. Node* child;
  155. while ((child = _firstChild) != NULL)
  156. {
  157. removeChild(child);
  158. }
  159. }
  160. bool Node::hasChildren() const
  161. {
  162. return (_firstChild != NULL);
  163. }
  164. unsigned int Node::getChildCount() const
  165. {
  166. return _childCount;
  167. }
  168. Node* Node::getNextSibling() const
  169. {
  170. return _nextSibling;
  171. }
  172. Node* Node::getPreviousSibling() const
  173. {
  174. return _previousSibling;
  175. }
  176. Node* Node::getFirstChild() const
  177. {
  178. return _firstChild;
  179. }
  180. Node* Node::getLastChild() const
  181. {
  182. return _lastChild;
  183. }
  184. Node* Node::getParent() const
  185. {
  186. return _parent;
  187. }
  188. void Node::setCameraInstance(CameraInstance* cameraInstance)
  189. {
  190. camera = cameraInstance;
  191. }
  192. void Node::setLightInstance(LightInstance* lightInstance)
  193. {
  194. light = lightInstance;
  195. }
  196. void Node::setModel(Model* model)
  197. {
  198. this->model = model;
  199. }
  200. void Node::setTransformMatrix(float matrix[])
  201. {
  202. for (int i = 0; i < 16; i++)
  203. {
  204. transform[i] = matrix[i];
  205. }
  206. }
  207. void Node::setIsJoint(bool value)
  208. {
  209. joint = value;
  210. }
  211. bool Node::isJoint()
  212. {
  213. return joint;
  214. }
  215. Camera* Node::getCamera() const
  216. {
  217. if (camera)
  218. {
  219. return camera->getCamera();
  220. }
  221. return NULL;
  222. }
  223. Light* Node::getLight() const
  224. {
  225. if (light)
  226. {
  227. return light->getLight();
  228. }
  229. return NULL;
  230. }
  231. Model* Node::getModel() const
  232. {
  233. if (model)
  234. {
  235. return model;
  236. }
  237. return NULL;
  238. }
  239. Node* Node::getFirstCameraNode() const
  240. {
  241. if (hasCamera())
  242. {
  243. return (Node*)this;
  244. }
  245. for (Node* node = getFirstChild(); node != NULL; node = node->getNextSibling())
  246. {
  247. Node* n = node->getFirstCameraNode();
  248. if (n)
  249. {
  250. return n;
  251. }
  252. }
  253. return NULL;
  254. }
  255. bool Node::hasCamera() const
  256. {
  257. return camera != NULL;
  258. }
  259. bool Node::hasLight() const
  260. {
  261. return light != NULL;
  262. }
  263. }