Model.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. GP_ASSERT(mesh);
  14. _partCount = mesh->getPartCount();
  15. }
  16. Model::~Model()
  17. {
  18. SAFE_RELEASE(_material);
  19. if (_partMaterials)
  20. {
  21. for (unsigned int i = 0; i < _partCount; ++i)
  22. {
  23. SAFE_RELEASE(_partMaterials[i]);
  24. }
  25. SAFE_DELETE_ARRAY(_partMaterials);
  26. }
  27. SAFE_RELEASE(_mesh);
  28. SAFE_DELETE(_skin);
  29. }
  30. Model* Model::create(Mesh* mesh)
  31. {
  32. GP_ASSERT(mesh);
  33. mesh->addRef();
  34. return new Model(mesh);
  35. }
  36. Mesh* Model::getMesh() const
  37. {
  38. return _mesh;
  39. }
  40. unsigned int Model::getMeshPartCount() const
  41. {
  42. GP_ASSERT(_mesh);
  43. return _mesh->getPartCount();
  44. }
  45. Material* Model::getMaterial(int partIndex)
  46. {
  47. GP_ASSERT(partIndex == -1 || partIndex >= 0);
  48. Material* m = NULL;
  49. if (partIndex < 0)
  50. return _material;
  51. if (partIndex >= (int)_partCount)
  52. return NULL;
  53. // Look up explicitly specified part material.
  54. if (_partMaterials)
  55. {
  56. m = _partMaterials[partIndex];
  57. }
  58. if (m == NULL)
  59. {
  60. // Return the shared material.
  61. m = _material;
  62. }
  63. return m;
  64. }
  65. void Model::setMaterial(Material* material, int partIndex)
  66. {
  67. GP_ASSERT(partIndex == -1 || (partIndex >= 0 && partIndex < (int)getMeshPartCount()));
  68. Material* oldMaterial = NULL;
  69. if (partIndex == -1)
  70. {
  71. oldMaterial = _material;
  72. // Set new shared material.
  73. if (material)
  74. {
  75. _material = material;
  76. _material->addRef();
  77. }
  78. }
  79. else if (partIndex >= 0 && partIndex < (int)getMeshPartCount())
  80. {
  81. // Ensure mesh part count is up-to-date.
  82. validatePartCount();
  83. // Release existing part material and part binding.
  84. if (_partMaterials)
  85. {
  86. oldMaterial = _partMaterials[partIndex];
  87. }
  88. else
  89. {
  90. // Allocate part arrays for the first time.
  91. if (_partMaterials == NULL)
  92. {
  93. _partMaterials = new Material*[_partCount];
  94. memset(_partMaterials, 0, sizeof(Material*) * _partCount);
  95. }
  96. }
  97. // Set new part material.
  98. if (material)
  99. {
  100. _partMaterials[partIndex] = material;
  101. material->addRef();
  102. }
  103. }
  104. // Release existing material and binding.
  105. if (oldMaterial)
  106. {
  107. for (unsigned int i = 0, tCount = oldMaterial->getTechniqueCount(); i < tCount; ++i)
  108. {
  109. Technique* t = oldMaterial->getTechniqueByIndex(i);
  110. GP_ASSERT(t);
  111. for (unsigned int j = 0, pCount = t->getPassCount(); j < pCount; ++j)
  112. {
  113. GP_ASSERT(t->getPassByIndex(j));
  114. t->getPassByIndex(j)->setVertexAttributeBinding(NULL);
  115. }
  116. }
  117. SAFE_RELEASE(oldMaterial);
  118. }
  119. if (material)
  120. {
  121. // Hookup vertex attribute bindings for all passes in the new material.
  122. for (unsigned int i = 0, tCount = material->getTechniqueCount(); i < tCount; ++i)
  123. {
  124. Technique* t = material->getTechniqueByIndex(i);
  125. GP_ASSERT(t);
  126. for (unsigned int j = 0, pCount = t->getPassCount(); j < pCount; ++j)
  127. {
  128. Pass* p = t->getPassByIndex(j);
  129. GP_ASSERT(p);
  130. VertexAttributeBinding* b = VertexAttributeBinding::create(_mesh, p->getEffect());
  131. p->setVertexAttributeBinding(b);
  132. SAFE_RELEASE(b);
  133. }
  134. }
  135. // Apply node binding for the new material.
  136. if (_node)
  137. {
  138. setMaterialNodeBinding(material);
  139. }
  140. }
  141. }
  142. Material* Model::setMaterial(const char* vshPath, const char* fshPath, const char* defines, int partIndex)
  143. {
  144. // Try to create a Material with the given parameters.
  145. Material* material = Material::create(vshPath, fshPath, defines);
  146. if (material == NULL)
  147. {
  148. GP_ERROR("Failed to create material for model.");
  149. return NULL;
  150. }
  151. // Assign the material to us.
  152. setMaterial(material, partIndex);
  153. // Release the material since we now have a reference to it.
  154. material->release();
  155. return material;
  156. }
  157. Material* Model::setMaterial(const char* materialPath, int partIndex)
  158. {
  159. // Try to create a Material from the specified material file.
  160. Material* material = Material::create(materialPath);
  161. if (material == NULL)
  162. {
  163. GP_ERROR("Failed to create material for model.");
  164. return NULL;
  165. }
  166. // Assign the material to us
  167. setMaterial(material, partIndex);
  168. // Release the material since we now have a reference to it
  169. material->release();
  170. return material;
  171. }
  172. bool Model::hasMaterial(unsigned int partIndex) const
  173. {
  174. return (partIndex < _partCount && _partMaterials && _partMaterials[partIndex]);
  175. }
  176. MeshSkin* Model::getSkin() const
  177. {
  178. return _skin;
  179. }
  180. void Model::setSkin(MeshSkin* skin)
  181. {
  182. if (_skin != skin)
  183. {
  184. // Free the old skin
  185. SAFE_DELETE(_skin);
  186. // Assign the new skin
  187. _skin = skin;
  188. if (_skin)
  189. _skin->_model = this;
  190. }
  191. }
  192. Node* Model::getNode() const
  193. {
  194. return _node;
  195. }
  196. void Model::setNode(Node* node)
  197. {
  198. _node = node;
  199. // Re-bind node related material parameters
  200. if (node)
  201. {
  202. if (_material)
  203. {
  204. setMaterialNodeBinding(_material);
  205. }
  206. if (_partMaterials)
  207. {
  208. for (unsigned int i = 0; i < _partCount; ++i)
  209. {
  210. if (_partMaterials[i])
  211. {
  212. setMaterialNodeBinding(_partMaterials[i]);
  213. }
  214. }
  215. }
  216. }
  217. }
  218. static bool drawWireframe(Mesh* mesh)
  219. {
  220. switch (mesh->getPrimitiveType())
  221. {
  222. case Mesh::TRIANGLES:
  223. {
  224. unsigned int vertexCount = mesh->getVertexCount();
  225. for (unsigned int i = 0; i < vertexCount; i += 3)
  226. {
  227. GL_ASSERT( glDrawArrays(GL_LINE_LOOP, i, 3) );
  228. }
  229. }
  230. return true;
  231. case Mesh::TRIANGLE_STRIP:
  232. {
  233. unsigned int vertexCount = mesh->getVertexCount();
  234. for (unsigned int i = 2; i < vertexCount; ++i)
  235. {
  236. GL_ASSERT( glDrawArrays(GL_LINE_LOOP, i-2, 3) );
  237. }
  238. }
  239. return true;
  240. default:
  241. // not supported
  242. return false;
  243. }
  244. }
  245. static bool drawWireframe(MeshPart* part)
  246. {
  247. unsigned int indexCount = part->getIndexCount();
  248. unsigned int indexSize = 0;
  249. switch (part->getIndexFormat())
  250. {
  251. case Mesh::INDEX8:
  252. indexSize = 1;
  253. break;
  254. case Mesh::INDEX16:
  255. indexSize = 2;
  256. break;
  257. case Mesh::INDEX32:
  258. indexSize = 4;
  259. break;
  260. default:
  261. GP_ERROR("Unsupported index format (%d).", part->getIndexFormat());
  262. return false;
  263. }
  264. switch (part->getPrimitiveType())
  265. {
  266. case Mesh::TRIANGLES:
  267. {
  268. for (unsigned int i = 0; i < indexCount; i += 3)
  269. {
  270. GL_ASSERT( glDrawElements(GL_LINE_LOOP, 3, part->getIndexFormat(), ((const GLvoid*)(i*indexSize))) );
  271. }
  272. }
  273. return true;
  274. case Mesh::TRIANGLE_STRIP:
  275. {
  276. for (unsigned int i = 2; i < indexCount; ++i)
  277. {
  278. GL_ASSERT( glDrawElements(GL_LINE_LOOP, 3, part->getIndexFormat(), ((const GLvoid*)((i-2)*indexSize))) );
  279. }
  280. }
  281. return true;
  282. default:
  283. // not supported
  284. return false;
  285. }
  286. }
  287. unsigned int Model::draw(bool wireframe)
  288. {
  289. GP_ASSERT(_mesh);
  290. unsigned int partCount = _mesh->getPartCount();
  291. if (partCount == 0)
  292. {
  293. // No mesh parts (index buffers).
  294. if (_material)
  295. {
  296. Technique* technique = _material->getTechnique();
  297. GP_ASSERT(technique);
  298. unsigned int passCount = technique->getPassCount();
  299. for (unsigned int i = 0; i < passCount; ++i)
  300. {
  301. Pass* pass = technique->getPassByIndex(i);
  302. GP_ASSERT(pass);
  303. pass->bind();
  304. GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) );
  305. if (!wireframe || !drawWireframe(_mesh))
  306. {
  307. GL_ASSERT( glDrawArrays(_mesh->getPrimitiveType(), 0, _mesh->getVertexCount()) );
  308. }
  309. pass->unbind();
  310. }
  311. }
  312. }
  313. else
  314. {
  315. for (unsigned int i = 0; i < partCount; ++i)
  316. {
  317. MeshPart* part = _mesh->getPart(i);
  318. GP_ASSERT(part);
  319. // Get the material for this mesh part.
  320. Material* material = getMaterial(i);
  321. if (material)
  322. {
  323. Technique* technique = material->getTechnique();
  324. GP_ASSERT(technique);
  325. unsigned int passCount = technique->getPassCount();
  326. for (unsigned int j = 0; j < passCount; ++j)
  327. {
  328. Pass* pass = technique->getPassByIndex(j);
  329. GP_ASSERT(pass);
  330. pass->bind();
  331. GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, part->_indexBuffer) );
  332. if (!wireframe || !drawWireframe(part))
  333. {
  334. GL_ASSERT( glDrawElements(part->getPrimitiveType(), part->getIndexCount(), part->getIndexFormat(), 0) );
  335. }
  336. pass->unbind();
  337. }
  338. }
  339. }
  340. }
  341. return partCount;
  342. }
  343. void Model::setMaterialNodeBinding(Material *material)
  344. {
  345. GP_ASSERT(material);
  346. if (_node)
  347. {
  348. material->setNodeBinding(_node);
  349. unsigned int techniqueCount = material->getTechniqueCount();
  350. for (unsigned int i = 0; i < techniqueCount; ++i)
  351. {
  352. Technique* technique = material->getTechniqueByIndex(i);
  353. GP_ASSERT(technique);
  354. technique->setNodeBinding(_node);
  355. unsigned int passCount = technique->getPassCount();
  356. for (unsigned int j = 0; j < passCount; ++j)
  357. {
  358. Pass* pass = technique->getPassByIndex(j);
  359. GP_ASSERT(pass);
  360. pass->setNodeBinding(_node);
  361. }
  362. }
  363. }
  364. }
  365. Model* Model::clone(NodeCloneContext &context)
  366. {
  367. Model* model = Model::create(getMesh());
  368. if (!model)
  369. {
  370. GP_ERROR("Failed to clone model.");
  371. return NULL;
  372. }
  373. if (getSkin())
  374. {
  375. model->setSkin(getSkin()->clone(context));
  376. }
  377. if (getMaterial())
  378. {
  379. Material* materialClone = getMaterial()->clone(context);
  380. if (!materialClone)
  381. {
  382. GP_ERROR("Failed to clone material for model.");
  383. return model;
  384. }
  385. model->setMaterial(materialClone);
  386. materialClone->release();
  387. }
  388. if (_partMaterials)
  389. {
  390. GP_ASSERT(_partCount == model->_partCount);
  391. for (unsigned int i = 0; i < _partCount; ++i)
  392. {
  393. if (_partMaterials[i])
  394. {
  395. Material* materialClone = _partMaterials[i]->clone(context);
  396. model->setMaterial(materialClone, i);
  397. materialClone->release();
  398. }
  399. }
  400. }
  401. return model;
  402. }
  403. void Model::validatePartCount()
  404. {
  405. GP_ASSERT(_mesh);
  406. unsigned int partCount = _mesh->getPartCount();
  407. if (_partCount != partCount)
  408. {
  409. // Allocate new arrays and copy old items to them.
  410. if (_partMaterials)
  411. {
  412. Material** oldArray = _partMaterials;
  413. _partMaterials = new Material*[partCount];
  414. memset(_partMaterials, 0, sizeof(Material*) * partCount);
  415. if (oldArray)
  416. {
  417. for (unsigned int i = 0; i < _partCount; ++i)
  418. {
  419. _partMaterials[i] = oldArray[i];
  420. }
  421. }
  422. SAFE_DELETE_ARRAY(oldArray);
  423. }
  424. // Update local part count.
  425. _partCount = _mesh->getPartCount();
  426. }
  427. }
  428. }