Node.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. generateHeightmap();
  70. }
  71. void Node::writeText(FILE* file)
  72. {
  73. if (isJoint())
  74. {
  75. fprintf(file, "<%s id=\"%s\" type=\"%s\">\n", getElementName(), getId().c_str(), "JOINT");
  76. }
  77. else
  78. {
  79. fprintElementStart(file);
  80. }
  81. fprintf(file, "<transform>");
  82. fprintfMatrix4f(file, _transform.m);
  83. fprintf(file, "</transform>\n");
  84. // children
  85. for (Node* node = getFirstChild(); node != NULL; node = node->getNextSibling())
  86. {
  87. node->writeText(file);
  88. }
  89. // camera
  90. if (_camera != NULL)
  91. {
  92. _camera->writeText(file);
  93. }
  94. // light
  95. if (_light != NULL && !_light->isAmbient())
  96. {
  97. _light->writeText(file);
  98. }
  99. // mesh
  100. if (_model != NULL)
  101. {
  102. _model->writeText(file);
  103. }
  104. fprintElementEnd(file);
  105. generateHeightmap();
  106. }
  107. void Node::generateHeightmap()
  108. {
  109. // Is this node flagged to have a heightmap generated for it?
  110. const std::vector<std::string>& heightmapNodes = EncoderArguments::getInstance()->getHeightmapNodeIds();
  111. if (std::find(heightmapNodes.begin(), heightmapNodes.end(), getId()) != heightmapNodes.end())
  112. {
  113. Mesh* mesh = _model ? _model->getMesh() : NULL;
  114. if (mesh)
  115. {
  116. DEBUGPRINT_VARG("> Generating heightmap for node: %s\n", getId().c_str());
  117. std::string heightmapFilename(EncoderArguments::getInstance()->getOutputDirPath());
  118. heightmapFilename += "/heightmap_";
  119. heightmapFilename += getId();
  120. heightmapFilename += ".png";
  121. mesh->generateHeightmap(heightmapFilename.c_str());
  122. }
  123. }
  124. }
  125. void Node::addChild(Node* child)
  126. {
  127. // If this child is already parented, remove it from its parent
  128. if (child->_parent)
  129. {
  130. child->_parent->removeChild(child);
  131. }
  132. if (_firstChild == NULL)
  133. {
  134. // No children yet
  135. _firstChild = child;
  136. child->_previousSibling = NULL;
  137. }
  138. else
  139. {
  140. // We already have children, so append to them
  141. child->_previousSibling = _lastChild;
  142. _lastChild->_nextSibling = child;
  143. }
  144. child->_parent = this;
  145. child->_nextSibling = NULL;
  146. this->_lastChild = child;
  147. ++_childCount;
  148. }
  149. void Node::removeChild(Node* child)
  150. {
  151. // The child must have already been our child to remove it
  152. if (child->_parent != this)
  153. {
  154. return;
  155. }
  156. // Remove the child by un-linking it from our child list
  157. if (child->_nextSibling)
  158. {
  159. child->_nextSibling->_previousSibling = child->_previousSibling;
  160. }
  161. if (child->_previousSibling)
  162. {
  163. child->_previousSibling->_nextSibling = child->_nextSibling;
  164. }
  165. // Was this child our first or last child?
  166. if (child == _firstChild)
  167. {
  168. _firstChild = child->_nextSibling;
  169. }
  170. if (child == _lastChild)
  171. {
  172. _lastChild = child->_previousSibling;
  173. }
  174. // Remove parent and sibling info from the child, now that it is no longer parented
  175. child->_parent = NULL;
  176. child->_nextSibling = NULL;
  177. child->_previousSibling = NULL;
  178. --_childCount;
  179. }
  180. void Node::removeChildren()
  181. {
  182. Node* child;
  183. while ((child = _firstChild) != NULL)
  184. {
  185. removeChild(child);
  186. }
  187. }
  188. bool Node::hasChildren() const
  189. {
  190. return (_firstChild != NULL);
  191. }
  192. unsigned int Node::getChildCount() const
  193. {
  194. return _childCount;
  195. }
  196. Node* Node::getNextSibling() const
  197. {
  198. return _nextSibling;
  199. }
  200. Node* Node::getPreviousSibling() const
  201. {
  202. return _previousSibling;
  203. }
  204. Node* Node::getFirstChild() const
  205. {
  206. return _firstChild;
  207. }
  208. Node* Node::getLastChild() const
  209. {
  210. return _lastChild;
  211. }
  212. Node* Node::getParent() const
  213. {
  214. return _parent;
  215. }
  216. void Node::setCamera(Camera* camera)
  217. {
  218. _camera = camera;
  219. }
  220. void Node::setLight(Light* light)
  221. {
  222. _light = light;
  223. }
  224. void Node::setModel(Model* model)
  225. {
  226. _model = model;
  227. }
  228. const Matrix& Node::getTransformMatrix() const
  229. {
  230. return _transform;
  231. }
  232. void Node::setTransformMatrix(float matrix[])
  233. {
  234. memcpy(_transform.m, matrix, 16 * sizeof(float));
  235. }
  236. const Matrix& Node::getWorldMatrix() const
  237. {
  238. if (_parent)
  239. {
  240. Matrix::multiply(_parent->getWorldMatrix().m, _transform.m, _worldTransform.m);
  241. }
  242. else
  243. {
  244. memcpy(_worldTransform.m, _transform.m, 16 * sizeof(float));
  245. }
  246. return _worldTransform;
  247. }
  248. void Node::resetTransformMatrix()
  249. {
  250. Matrix::setIdentity(_transform.m);
  251. }
  252. void Node::setIsJoint(bool value)
  253. {
  254. _joint = value;
  255. }
  256. bool Node::isJoint() const
  257. {
  258. return _joint;
  259. }
  260. Camera* Node::getCamera() const
  261. {
  262. return _camera;
  263. }
  264. Light* Node::getLight() const
  265. {
  266. return _light;
  267. }
  268. Model* Node::getModel() const
  269. {
  270. if (_model)
  271. {
  272. return _model;
  273. }
  274. return NULL;
  275. }
  276. Node* Node::getFirstCameraNode() const
  277. {
  278. if (hasCamera())
  279. {
  280. return const_cast<Node*>(this);
  281. }
  282. for (Node* node = getFirstChild(); node != NULL; node = node->getNextSibling())
  283. {
  284. Node* n = node->getFirstCameraNode();
  285. if (n)
  286. {
  287. return n;
  288. }
  289. }
  290. return NULL;
  291. }
  292. bool Node::hasCamera() const
  293. {
  294. return _camera != NULL;
  295. }
  296. bool Node::hasLight() const
  297. {
  298. return _light != NULL;
  299. }
  300. }