Model.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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() : Drawable(),
  11. _mesh(NULL), _material(NULL), _partCount(0), _partMaterials(NULL), _skin(NULL)
  12. {
  13. }
  14. Model::Model(Mesh* mesh) : Drawable(),
  15. _mesh(mesh), _material(NULL), _partCount(0), _partMaterials(NULL), _skin(NULL)
  16. {
  17. GP_ASSERT(mesh);
  18. _partCount = mesh->getPartCount();
  19. }
  20. Model::~Model()
  21. {
  22. SAFE_RELEASE(_material);
  23. if (_partMaterials)
  24. {
  25. for (unsigned int i = 0; i < _partCount; ++i)
  26. {
  27. SAFE_RELEASE(_partMaterials[i]);
  28. }
  29. SAFE_DELETE_ARRAY(_partMaterials);
  30. }
  31. SAFE_RELEASE(_mesh);
  32. SAFE_DELETE(_skin);
  33. }
  34. Model* Model::create(Mesh* mesh)
  35. {
  36. GP_ASSERT(mesh);
  37. mesh->addRef();
  38. return new Model(mesh);
  39. }
  40. Mesh* Model::getMesh() const
  41. {
  42. return _mesh;
  43. }
  44. unsigned int Model::getMeshPartCount() const
  45. {
  46. GP_ASSERT(_mesh);
  47. return _mesh->getPartCount();
  48. }
  49. Material* Model::getMaterial(int partIndex)
  50. {
  51. GP_ASSERT(partIndex == -1 || partIndex >= 0);
  52. Material* m = NULL;
  53. if (partIndex < 0)
  54. return _material;
  55. if (partIndex >= (int)_partCount)
  56. return NULL;
  57. // Look up explicitly specified part material.
  58. if (_partMaterials)
  59. {
  60. m = _partMaterials[partIndex];
  61. }
  62. if (m == NULL)
  63. {
  64. // Return the shared material.
  65. m = _material;
  66. }
  67. return m;
  68. }
  69. void Model::setMaterial(Material* material, int partIndex)
  70. {
  71. GP_ASSERT(partIndex == -1 || (partIndex >= 0 && partIndex < (int)getMeshPartCount()));
  72. Material* oldMaterial = NULL;
  73. if (partIndex == -1)
  74. {
  75. oldMaterial = _material;
  76. // Set new shared material.
  77. if (material)
  78. {
  79. _material = material;
  80. _material->addRef();
  81. }
  82. }
  83. else if (partIndex >= 0 && partIndex < (int)getMeshPartCount())
  84. {
  85. // Ensure mesh part count is up-to-date.
  86. validatePartCount();
  87. // Release existing part material and part binding.
  88. if (_partMaterials)
  89. {
  90. oldMaterial = _partMaterials[partIndex];
  91. }
  92. else
  93. {
  94. // Allocate part arrays for the first time.
  95. if (_partMaterials == NULL)
  96. {
  97. _partMaterials = new Material*[_partCount];
  98. memset(_partMaterials, 0, sizeof(Material*) * _partCount);
  99. }
  100. }
  101. // Set new part material.
  102. if (material)
  103. {
  104. _partMaterials[partIndex] = material;
  105. material->addRef();
  106. }
  107. }
  108. // Release existing material and binding.
  109. if (oldMaterial)
  110. {
  111. for (unsigned int i = 0, tCount = oldMaterial->getTechniqueCount(); i < tCount; ++i)
  112. {
  113. Technique* t = oldMaterial->getTechniqueByIndex(i);
  114. GP_ASSERT(t);
  115. for (unsigned int j = 0, pCount = t->getPassCount(); j < pCount; ++j)
  116. {
  117. GP_ASSERT(t->getPassByIndex(j));
  118. t->getPassByIndex(j)->setVertexAttributeBinding(NULL);
  119. }
  120. }
  121. SAFE_RELEASE(oldMaterial);
  122. }
  123. if (material)
  124. {
  125. // Hookup vertex attribute bindings for all passes in the new material.
  126. for (unsigned int i = 0, tCount = material->getTechniqueCount(); i < tCount; ++i)
  127. {
  128. Technique* t = material->getTechniqueByIndex(i);
  129. GP_ASSERT(t);
  130. for (unsigned int j = 0, pCount = t->getPassCount(); j < pCount; ++j)
  131. {
  132. Pass* p = t->getPassByIndex(j);
  133. GP_ASSERT(p);
  134. VertexAttributeBinding* b = VertexAttributeBinding::create(_mesh, p->getEffect());
  135. p->setVertexAttributeBinding(b);
  136. SAFE_RELEASE(b);
  137. }
  138. }
  139. // Apply node binding for the new material.
  140. if (_node)
  141. {
  142. setMaterialNodeBinding(material);
  143. }
  144. }
  145. }
  146. Material* Model::setMaterial(const char* vshPath, const char* fshPath, const char* defines, int partIndex)
  147. {
  148. // Try to create a Material with the given parameters.
  149. Material* material = Material::create(vshPath, fshPath, defines);
  150. if (material == NULL)
  151. {
  152. GP_ERROR("Failed to create material for model.");
  153. return NULL;
  154. }
  155. // Assign the material to us.
  156. setMaterial(material, partIndex);
  157. // Release the material since we now have a reference to it.
  158. material->release();
  159. return material;
  160. }
  161. Material* Model::setMaterial(const char* materialPath, int partIndex)
  162. {
  163. // Try to create a Material from the specified material file.
  164. Material* material = Material::create(materialPath);
  165. if (material == NULL)
  166. {
  167. GP_ERROR("Failed to create material for model.");
  168. return NULL;
  169. }
  170. // Assign the material to us
  171. setMaterial(material, partIndex);
  172. // Release the material since we now have a reference to it
  173. material->release();
  174. return material;
  175. }
  176. bool Model::hasMaterial(unsigned int partIndex) const
  177. {
  178. return (partIndex < _partCount && _partMaterials && _partMaterials[partIndex]);
  179. }
  180. MeshSkin* Model::getSkin() const
  181. {
  182. return _skin;
  183. }
  184. void Model::setSkin(MeshSkin* skin)
  185. {
  186. if (_skin != skin)
  187. {
  188. // Free the old skin
  189. SAFE_DELETE(_skin);
  190. // Assign the new skin
  191. _skin = skin;
  192. if (_skin)
  193. _skin->_model = this;
  194. }
  195. }
  196. void Model::setNode(Node* node)
  197. {
  198. Drawable::setNode(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 (size_t 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 (size_t 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(getNode());
  349. }
  350. }
  351. Drawable* Model::clone(NodeCloneContext& context)
  352. {
  353. Model* model = Model::create(getMesh());
  354. if (!model)
  355. {
  356. GP_ERROR("Failed to clone model.");
  357. return NULL;
  358. }
  359. if (getSkin())
  360. {
  361. model->setSkin(getSkin()->clone(context));
  362. }
  363. if (getMaterial())
  364. {
  365. Material* materialClone = getMaterial()->clone(context);
  366. if (!materialClone)
  367. {
  368. GP_ERROR("Failed to clone material for model.");
  369. return model;
  370. }
  371. model->setMaterial(materialClone);
  372. materialClone->release();
  373. }
  374. if (_partMaterials)
  375. {
  376. GP_ASSERT(_partCount == model->_partCount);
  377. for (unsigned int i = 0; i < _partCount; ++i)
  378. {
  379. if (_partMaterials[i])
  380. {
  381. Material* materialClone = _partMaterials[i]->clone(context);
  382. model->setMaterial(materialClone, i);
  383. materialClone->release();
  384. }
  385. }
  386. }
  387. return model;
  388. }
  389. void Model::validatePartCount()
  390. {
  391. GP_ASSERT(_mesh);
  392. unsigned int partCount = _mesh->getPartCount();
  393. if (_partCount != partCount)
  394. {
  395. // Allocate new arrays and copy old items to them.
  396. if (_partMaterials)
  397. {
  398. Material** oldArray = _partMaterials;
  399. _partMaterials = new Material*[partCount];
  400. memset(_partMaterials, 0, sizeof(Material*) * partCount);
  401. if (oldArray)
  402. {
  403. for (unsigned int i = 0; i < _partCount; ++i)
  404. {
  405. _partMaterials[i] = oldArray[i];
  406. }
  407. }
  408. SAFE_DELETE_ARRAY(oldArray);
  409. }
  410. // Update local part count.
  411. _partCount = _mesh->getPartCount();
  412. }
  413. }
  414. }