Model.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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->getTechnique(i);
  109. GP_ASSERT(t);
  110. for (unsigned int j = 0, pCount = t->getPassCount(); j < pCount; ++j)
  111. {
  112. GP_ASSERT(t->getPass(j));
  113. t->getPass(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->getTechnique(i);
  124. GP_ASSERT(t);
  125. for (unsigned int j = 0, pCount = t->getPassCount(); j < pCount; ++j)
  126. {
  127. Pass* p = t->getPass(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. void Model::draw(bool wireframe)
  218. {
  219. GP_ASSERT(_mesh);
  220. unsigned int partCount = _mesh->getPartCount();
  221. if (partCount == 0)
  222. {
  223. // No mesh parts (index buffers).
  224. if (_material)
  225. {
  226. Technique* technique = _material->getTechnique();
  227. GP_ASSERT(technique);
  228. unsigned int passCount = technique->getPassCount();
  229. for (unsigned int i = 0; i < passCount; ++i)
  230. {
  231. Pass* pass = technique->getPass(i);
  232. GP_ASSERT(pass);
  233. pass->bind();
  234. GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) );
  235. if (wireframe && (_mesh->getPrimitiveType() == Mesh::TRIANGLES || _mesh->getPrimitiveType() == Mesh::TRIANGLE_STRIP))
  236. {
  237. unsigned int vertexCount = _mesh->getVertexCount();
  238. for (unsigned int j = 0; j < vertexCount; j += 3)
  239. {
  240. GL_ASSERT( glDrawArrays(GL_LINE_LOOP, j, 3) );
  241. }
  242. }
  243. else
  244. {
  245. GL_ASSERT( glDrawArrays(_mesh->getPrimitiveType(), 0, _mesh->getVertexCount()) );
  246. }
  247. pass->unbind();
  248. }
  249. }
  250. }
  251. else
  252. {
  253. for (unsigned int i = 0; i < partCount; ++i)
  254. {
  255. MeshPart* part = _mesh->getPart(i);
  256. GP_ASSERT(part);
  257. // Get the material for this mesh part.
  258. Material* material = getMaterial(i);
  259. if (material)
  260. {
  261. Technique* technique = material->getTechnique();
  262. GP_ASSERT(technique);
  263. unsigned int passCount = technique->getPassCount();
  264. for (unsigned int j = 0; j < passCount; ++j)
  265. {
  266. Pass* pass = technique->getPass(j);
  267. GP_ASSERT(pass);
  268. pass->bind();
  269. GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, part->_indexBuffer) );
  270. if (wireframe && (_mesh->getPrimitiveType() == Mesh::TRIANGLES || _mesh->getPrimitiveType() == Mesh::TRIANGLE_STRIP))
  271. {
  272. unsigned int indexCount = part->getIndexCount();
  273. unsigned int indexSize = 0;
  274. switch (part->getIndexFormat())
  275. {
  276. case Mesh::INDEX8:
  277. indexSize = 1;
  278. break;
  279. case Mesh::INDEX16:
  280. indexSize = 2;
  281. break;
  282. case Mesh::INDEX32:
  283. indexSize = 4;
  284. break;
  285. default:
  286. GP_ERROR("Unsupported index format (%d).", part->getIndexFormat());
  287. continue;
  288. }
  289. for (unsigned int k = 0; k < indexCount; k += 3)
  290. {
  291. GL_ASSERT( glDrawElements(GL_LINE_LOOP, 3, part->getIndexFormat(), ((const GLvoid*)(k*indexSize))) );
  292. }
  293. }
  294. else
  295. {
  296. GL_ASSERT( glDrawElements(part->getPrimitiveType(), part->getIndexCount(), part->getIndexFormat(), 0) );
  297. }
  298. pass->unbind();
  299. }
  300. }
  301. }
  302. }
  303. }
  304. void Model::validatePartCount()
  305. {
  306. GP_ASSERT(_mesh);
  307. unsigned int partCount = _mesh->getPartCount();
  308. if (_partCount != partCount)
  309. {
  310. // Allocate new arrays and copy old items to them.
  311. if (_partMaterials)
  312. {
  313. Material** oldArray = _partMaterials;
  314. _partMaterials = new Material*[partCount];
  315. memset(_partMaterials, 0, sizeof(Material*) * partCount);
  316. if (oldArray)
  317. {
  318. for (unsigned int i = 0; i < _partCount; ++i)
  319. {
  320. _partMaterials[i] = oldArray[i];
  321. }
  322. }
  323. SAFE_DELETE_ARRAY(oldArray);
  324. }
  325. // Update local part count.
  326. _partCount = _mesh->getPartCount();
  327. }
  328. }
  329. Model* Model::clone(NodeCloneContext &context)
  330. {
  331. Model* model = Model::create(getMesh());
  332. if (!model)
  333. {
  334. GP_ERROR("Failed to clone model.");
  335. return NULL;
  336. }
  337. if (getSkin())
  338. {
  339. model->setSkin(getSkin()->clone(context));
  340. }
  341. if (getMaterial())
  342. {
  343. Material* materialClone = getMaterial()->clone(context);
  344. if (!materialClone)
  345. {
  346. GP_ERROR("Failed to clone material for model.");
  347. return model;
  348. }
  349. model->setMaterial(materialClone); // TODO: Don't forget material parts
  350. materialClone->release();
  351. }
  352. return model;
  353. }
  354. void Model::setMaterialNodeBinding(Material *material)
  355. {
  356. GP_ASSERT(material);
  357. if (_node)
  358. {
  359. material->setNodeBinding(_node);
  360. unsigned int techniqueCount = material->getTechniqueCount();
  361. for (unsigned int i = 0; i < techniqueCount; ++i)
  362. {
  363. Technique* technique = material->getTechnique(i);
  364. GP_ASSERT(technique);
  365. technique->setNodeBinding(_node);
  366. unsigned int passCount = technique->getPassCount();
  367. for (unsigned int j = 0; j < passCount; ++j)
  368. {
  369. Pass* pass = technique->getPass(j);
  370. GP_ASSERT(pass);
  371. pass->setNodeBinding(_node);
  372. }
  373. }
  374. }
  375. }
  376. }