Node.cpp 6.7 KB

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