2
0

Model.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. MeshSkin* Model::getSkin()
  162. {
  163. return _skin;
  164. }
  165. void Model::setSkin(MeshSkin* skin)
  166. {
  167. if (_skin != skin)
  168. {
  169. // Free the old skin
  170. SAFE_DELETE(_skin);
  171. // Assign the new skin
  172. _skin = skin;
  173. _skin->_model = this;
  174. }
  175. }
  176. Node* Model::getNode() const
  177. {
  178. return _node;
  179. }
  180. void Model::setNode(Node* node)
  181. {
  182. _node = node;
  183. // Re-bind node related material parameters
  184. if (node)
  185. {
  186. if (_material)
  187. {
  188. setMaterialNodeBinding(_material);
  189. }
  190. if (_partMaterials)
  191. {
  192. for (unsigned int i = 0; i < _partCount; ++i)
  193. {
  194. if (_partMaterials[i])
  195. {
  196. setMaterialNodeBinding(_partMaterials[i]);
  197. }
  198. }
  199. }
  200. }
  201. }
  202. void Model::draw(bool wireframe)
  203. {
  204. unsigned int partCount = _mesh->getPartCount();
  205. if (partCount == 0)
  206. {
  207. // No mesh parts (index buffers).
  208. if (_material)
  209. {
  210. GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) );
  211. Technique* technique = _material->getTechnique();
  212. unsigned int passCount = technique->getPassCount();
  213. for (unsigned int i = 0; i < passCount; ++i)
  214. {
  215. Pass* pass = technique->getPass(i);
  216. pass->bind();
  217. if (wireframe && (_mesh->getPrimitiveType() == Mesh::TRIANGLES || _mesh->getPrimitiveType() == Mesh::TRIANGLE_STRIP))
  218. {
  219. unsigned int vertexCount = _mesh->getVertexCount();
  220. for (unsigned int j = 0; j < vertexCount; j += 3)
  221. {
  222. GL_ASSERT( glDrawArrays(GL_LINE_LOOP, j, 3) );
  223. }
  224. }
  225. else
  226. {
  227. GL_ASSERT( glDrawArrays(_mesh->getPrimitiveType(), 0, _mesh->getVertexCount()) );
  228. }
  229. pass->unbind();
  230. }
  231. }
  232. }
  233. else
  234. {
  235. for (unsigned int i = 0; i < partCount; ++i)
  236. {
  237. MeshPart* part = _mesh->getPart(i);
  238. // Get the material for this mesh part.
  239. Material* material;
  240. if (_partMaterials && i < _partCount && _partMaterials[i])
  241. {
  242. material = _partMaterials[i]; // Use part material
  243. }
  244. else
  245. {
  246. material = _material; // Use shared material
  247. }
  248. if (material)
  249. {
  250. Technique* technique = material->getTechnique();
  251. unsigned int passCount = technique->getPassCount();
  252. for (unsigned int j = 0; j < passCount; ++j)
  253. {
  254. Pass* pass = technique->getPass(j);
  255. pass->bind();
  256. GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, part->_indexBuffer) );
  257. if (wireframe && (_mesh->getPrimitiveType() == Mesh::TRIANGLES || _mesh->getPrimitiveType() == Mesh::TRIANGLE_STRIP))
  258. {
  259. unsigned int indexCount = part->getIndexCount();
  260. unsigned int indexSize = 0;
  261. switch (part->getIndexFormat())
  262. {
  263. case Mesh::INDEX8:
  264. indexSize = 1;
  265. break;
  266. case Mesh::INDEX16:
  267. indexSize = 2;
  268. break;
  269. case Mesh::INDEX32:
  270. indexSize = 4;
  271. break;
  272. }
  273. for (unsigned int k = 0; k < indexCount; k += 3)
  274. {
  275. GL_ASSERT( glDrawElements(GL_LINE_LOOP, 3, part->getIndexFormat(), ((const GLvoid*)(k*indexSize))) );
  276. }
  277. }
  278. else
  279. {
  280. GL_ASSERT( glDrawElements(part->getPrimitiveType(), part->getIndexCount(), part->getIndexFormat(), 0) );
  281. }
  282. pass->unbind();
  283. }
  284. }
  285. }
  286. }
  287. }
  288. void Model::validatePartCount()
  289. {
  290. unsigned int partCount = _mesh->getPartCount();
  291. if (_partCount != partCount)
  292. {
  293. // Allocate new arrays and copy old items to them.
  294. if (_partMaterials)
  295. {
  296. Material** oldArray = _partMaterials;
  297. _partMaterials = new Material*[partCount];
  298. memset(_partMaterials, 0, sizeof(Material*) * partCount);
  299. for (unsigned int i = 0; i < _partCount; ++i)
  300. {
  301. _partMaterials[i] = oldArray[i];
  302. }
  303. SAFE_DELETE_ARRAY(oldArray);
  304. }
  305. // Update local part count.
  306. _partCount = _mesh->getPartCount();
  307. }
  308. }
  309. void Model::setMaterialNodeBinding(Material *material)
  310. {
  311. if (_node)
  312. {
  313. material->setNodeBinding(_node);
  314. unsigned int techniqueCount = material->getTechniqueCount();
  315. for (unsigned int i = 0; i < techniqueCount; ++i)
  316. {
  317. Technique* technique = material->getTechnique(i);
  318. technique->setNodeBinding(_node);
  319. unsigned int passCount = technique->getPassCount();
  320. for (unsigned int j = 0; j < passCount; ++j)
  321. {
  322. Pass* pass = technique->getPass(j);
  323. pass->setNodeBinding(_node);
  324. }
  325. }
  326. }
  327. }
  328. }