Node.cpp 5.8 KB

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