Node.cpp 6.0 KB

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