Model.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #include "Base.h"
  2. #include "Model.h"
  3. #include "MeshPart.h"
  4. #include "Scene.h"
  5. #include "Technique.h"
  6. #include "Pass.h"
  7. #include "Node.h"
  8. namespace gameplay
  9. {
  10. Model::Model(Mesh* mesh) :
  11. _mesh(mesh), _material(NULL), _partCount(0), _partMaterials(NULL), _node(NULL), _skin(NULL)
  12. {
  13. _partCount = mesh->getPartCount();
  14. }
  15. Model::~Model()
  16. {
  17. SAFE_RELEASE(_material);
  18. if (_partMaterials)
  19. {
  20. for (unsigned int i = 0; i < _partCount; ++i)
  21. {
  22. SAFE_RELEASE(_partMaterials[i]);
  23. }
  24. SAFE_DELETE_ARRAY(_partMaterials);
  25. }
  26. SAFE_RELEASE(_mesh);
  27. SAFE_DELETE(_skin);
  28. }
  29. Model* Model::create(Mesh* mesh)
  30. {
  31. mesh->addRef();
  32. return new Model(mesh);
  33. }
  34. Mesh* Model::getMesh() const
  35. {
  36. return _mesh;
  37. }
  38. unsigned int Model::getMeshPartCount() const
  39. {
  40. return _mesh->getPartCount();
  41. }
  42. Material* Model::getMaterial(int partIndex)
  43. {
  44. assert(partIndex == -1 || (partIndex >= 0 && partIndex < (int)getMeshPartCount()));
  45. Material* m = NULL;
  46. if (partIndex >= 0 && partIndex < (int)_partCount)
  47. {
  48. // Look up explicitly specified part material.
  49. if (_partMaterials)
  50. {
  51. m = _partMaterials[partIndex];
  52. }
  53. }
  54. if (m == NULL)
  55. {
  56. // Return the shared material.
  57. m = _material;
  58. }
  59. return m;
  60. }
  61. void Model::setMaterial(Material* material, int partIndex)
  62. {
  63. assert(partIndex == -1 || (partIndex >= 0 && partIndex < (int)getMeshPartCount()));
  64. Material* oldMaterial = NULL;
  65. if (partIndex == -1)
  66. {
  67. oldMaterial = _material;
  68. // Set new shared material.
  69. if (material)
  70. {
  71. _material = material;
  72. _material->addRef();
  73. }
  74. }
  75. else if (partIndex >= 0 && partIndex < (int)getMeshPartCount())
  76. {
  77. // Ensure mesh part count is up-to-date.
  78. validatePartCount();
  79. // Release existing part material and part binding.
  80. if (_partMaterials)
  81. {
  82. oldMaterial = _partMaterials[partIndex];
  83. }
  84. else
  85. {
  86. // Allocate part arrays for the first time.
  87. if (_partMaterials == NULL)
  88. {
  89. _partMaterials = new Material*[_partCount];
  90. memset(_partMaterials, 0, sizeof(Material*) * _partCount);
  91. }
  92. }
  93. // Set new part material.
  94. if (material)
  95. {
  96. _partMaterials[partIndex] = material;
  97. material->addRef();
  98. }
  99. }
  100. // Release existing material and binding.
  101. if (oldMaterial)
  102. {
  103. for (unsigned int i = 0, tCount = material->getTechniqueCount(); i < tCount; ++i)
  104. {
  105. Technique* t = material->getTechnique(i);
  106. for (unsigned int j = 0, pCount = t->getPassCount(); j < pCount; ++j)
  107. {
  108. t->getPass(j)->setVertexAttributeBinding(NULL);
  109. }
  110. }
  111. SAFE_RELEASE(oldMaterial);
  112. }
  113. if (material)
  114. {
  115. // Hookup vertex attribute bindings for all passes in the new material.
  116. for (unsigned int i = 0, tCount = material->getTechniqueCount(); i < tCount; ++i)
  117. {
  118. Technique* t = material->getTechnique(i);
  119. for (unsigned int j = 0, pCount = t->getPassCount(); j < pCount; ++j)
  120. {
  121. Pass* p = t->getPass(j);
  122. VertexAttributeBinding* b = VertexAttributeBinding::create(_mesh, p->getEffect());
  123. p->setVertexAttributeBinding(b);
  124. SAFE_RELEASE(b);
  125. }
  126. }
  127. // Apply node binding for the new material.
  128. if (_node)
  129. {
  130. setMaterialNodeBinding(material);
  131. }
  132. }
  133. }
  134. Material* Model::setMaterial(const char* vshPath, const char* fshPath, const char* defines, int partIndex)
  135. {
  136. // Try to create a Material with the given parameters.
  137. Material* material = Material::create(vshPath, fshPath, defines);
  138. if (material == NULL)
  139. {
  140. return NULL;
  141. }
  142. // Assign the material to us.
  143. setMaterial(material, partIndex);
  144. // Release the material since we now have a reference to it.
  145. material->release();
  146. return material;
  147. }
  148. Material* Model::setMaterial(const char* materialPath, int partIndex)
  149. {
  150. // Try to create a Material from the specified material file.
  151. Material* material = Material::create(materialPath);
  152. if (material == NULL)
  153. {
  154. return NULL;
  155. }
  156. // Assign the material to us
  157. setMaterial(material, partIndex);
  158. // Release the material since we now have a reference to it
  159. material->release();
  160. return material;
  161. }
  162. bool Model::hasMaterial(unsigned int partIndex) const
  163. {
  164. return (partIndex < _partCount && _partMaterials && _partMaterials[partIndex]);
  165. }
  166. MeshSkin* Model::getSkin() const
  167. {
  168. return _skin;
  169. }
  170. void Model::setSkin(MeshSkin* skin)
  171. {
  172. if (_skin != skin)
  173. {
  174. // Free the old skin
  175. SAFE_DELETE(_skin);
  176. // Assign the new skin
  177. _skin = skin;
  178. if (_skin)
  179. _skin->_model = this;
  180. }
  181. }
  182. Node* Model::getNode() const
  183. {
  184. return _node;
  185. }
  186. void Model::setNode(Node* node)
  187. {
  188. _node = node;
  189. // Re-bind node related material parameters
  190. if (node)
  191. {
  192. if (_material)
  193. {
  194. setMaterialNodeBinding(_material);
  195. }
  196. if (_partMaterials)
  197. {
  198. for (unsigned int i = 0; i < _partCount; ++i)
  199. {
  200. if (_partMaterials[i])
  201. {
  202. setMaterialNodeBinding(_partMaterials[i]);
  203. }
  204. }
  205. }
  206. }
  207. }
  208. void Model::draw(bool wireframe)
  209. {
  210. unsigned int partCount = _mesh->getPartCount();
  211. if (partCount == 0)
  212. {
  213. // No mesh parts (index buffers).
  214. if (_material)
  215. {
  216. Technique* technique = _material->getTechnique();
  217. unsigned int passCount = technique->getPassCount();
  218. for (unsigned int i = 0; i < passCount; ++i)
  219. {
  220. Pass* pass = technique->getPass(i);
  221. pass->bind();
  222. GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) );
  223. if (wireframe && (_mesh->getPrimitiveType() == Mesh::TRIANGLES || _mesh->getPrimitiveType() == Mesh::TRIANGLE_STRIP))
  224. {
  225. unsigned int vertexCount = _mesh->getVertexCount();
  226. for (unsigned int j = 0; j < vertexCount; j += 3)
  227. {
  228. GL_ASSERT( glDrawArrays(GL_LINE_LOOP, j, 3) );
  229. }
  230. }
  231. else
  232. {
  233. GL_ASSERT( glDrawArrays(_mesh->getPrimitiveType(), 0, _mesh->getVertexCount()) );
  234. }
  235. pass->unbind();
  236. }
  237. }
  238. }
  239. else
  240. {
  241. for (unsigned int i = 0; i < partCount; ++i)
  242. {
  243. MeshPart* part = _mesh->getPart(i);
  244. // Get the material for this mesh part.
  245. Material* material = getMaterial(i);
  246. if (material)
  247. {
  248. Technique* technique = material->getTechnique();
  249. unsigned int passCount = technique->getPassCount();
  250. for (unsigned int j = 0; j < passCount; ++j)
  251. {
  252. Pass* pass = technique->getPass(j);
  253. pass->bind();
  254. GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, part->_indexBuffer) );
  255. if (wireframe && (_mesh->getPrimitiveType() == Mesh::TRIANGLES || _mesh->getPrimitiveType() == Mesh::TRIANGLE_STRIP))
  256. {
  257. unsigned int indexCount = part->getIndexCount();
  258. unsigned int indexSize = 0;
  259. switch (part->getIndexFormat())
  260. {
  261. case Mesh::INDEX8:
  262. indexSize = 1;
  263. break;
  264. case Mesh::INDEX16:
  265. indexSize = 2;
  266. break;
  267. case Mesh::INDEX32:
  268. indexSize = 4;
  269. break;
  270. }
  271. for (unsigned int k = 0; k < indexCount; k += 3)
  272. {
  273. GL_ASSERT( glDrawElements(GL_LINE_LOOP, 3, part->getIndexFormat(), ((const GLvoid*)(k*indexSize))) );
  274. }
  275. }
  276. else
  277. {
  278. GL_ASSERT( glDrawElements(part->getPrimitiveType(), part->getIndexCount(), part->getIndexFormat(), 0) );
  279. }
  280. pass->unbind();
  281. }
  282. }
  283. }
  284. }
  285. }
  286. void Model::validatePartCount()
  287. {
  288. unsigned int partCount = _mesh->getPartCount();
  289. if (_partCount != partCount)
  290. {
  291. // Allocate new arrays and copy old items to them.
  292. if (_partMaterials)
  293. {
  294. Material** oldArray = _partMaterials;
  295. _partMaterials = new Material*[partCount];
  296. memset(_partMaterials, 0, sizeof(Material*) * partCount);
  297. for (unsigned int i = 0; i < _partCount; ++i)
  298. {
  299. _partMaterials[i] = oldArray[i];
  300. }
  301. SAFE_DELETE_ARRAY(oldArray);
  302. }
  303. // Update local part count.
  304. _partCount = _mesh->getPartCount();
  305. }
  306. }
  307. Model* Model::clone(NodeCloneContext &context)
  308. {
  309. Model* model = Model::create(getMesh());
  310. if (getSkin())
  311. {
  312. model->setSkin(getSkin()->clone());
  313. }
  314. Material* materialClone = getMaterial()->clone(context);
  315. model->setMaterial(materialClone); // TODO: Don't forget material parts
  316. materialClone->release();
  317. return model;
  318. }
  319. void Model::setMaterialNodeBinding(Material *material)
  320. {
  321. if (_node)
  322. {
  323. material->setNodeBinding(_node);
  324. unsigned int techniqueCount = material->getTechniqueCount();
  325. for (unsigned int i = 0; i < techniqueCount; ++i)
  326. {
  327. Technique* technique = material->getTechnique(i);
  328. technique->setNodeBinding(_node);
  329. unsigned int passCount = technique->getPassCount();
  330. for (unsigned int j = 0; j < passCount; ++j)
  331. {
  332. Pass* pass = technique->getPass(j);
  333. pass->setNodeBinding(_node);
  334. }
  335. }
  336. }
  337. }
  338. }