Model.cpp 10 KB

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