Model.cpp 13 KB

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