Model.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. if (partIndex == -1)
  64. {
  65. // Release existing shared material and binding.
  66. if (_material)
  67. {
  68. _material->setMeshBinding(NULL);
  69. SAFE_RELEASE(_material);
  70. }
  71. // Set new shared material.
  72. if (material)
  73. {
  74. _material = material;
  75. _material->addRef();
  76. _material->setMeshBinding(_mesh);
  77. }
  78. }
  79. else if (partIndex >= 0 && partIndex < (int)getMeshPartCount())
  80. {
  81. // Ensure mesh part count is up-to-date.
  82. validatePartCount();
  83. // Release existing part material and part binding.
  84. if (_partMaterials)
  85. {
  86. if (_partMaterials[partIndex])
  87. {
  88. _partMaterials[partIndex]->setMeshBinding(NULL);
  89. SAFE_RELEASE(_partMaterials[partIndex]);
  90. }
  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. material->setMeshBinding(_mesh);
  107. }
  108. }
  109. // Apply node binding for the new material.
  110. if (material && _node)
  111. {
  112. setMaterialNodeBinding(material);
  113. }
  114. }
  115. Material* Model::setMaterial(const char* vshPath, const char* fshPath, const char* defines, int partIndex)
  116. {
  117. // Try to create a Material with the given parameters.
  118. Material* material = Material::create(vshPath, fshPath, defines);
  119. if (material == NULL)
  120. {
  121. return NULL;
  122. }
  123. // Assign the material to us.
  124. setMaterial(material, partIndex);
  125. // Release the material since we now have a reference to it.
  126. material->release();
  127. return material;
  128. }
  129. Material* Model::setMaterial(const char* materialPath, int partIndex)
  130. {
  131. // Try to create a Material from the specified material file.
  132. Material* material = Material::create(materialPath);
  133. if (material == NULL)
  134. {
  135. return NULL;
  136. }
  137. // Assign the material to us
  138. setMaterial(material, partIndex);
  139. // Release the material since we now have a reference to it
  140. material->release();
  141. return material;
  142. }
  143. MeshSkin* Model::getSkin()
  144. {
  145. return _skin;
  146. }
  147. void Model::setSkin(MeshSkin* skin)
  148. {
  149. if (_skin != skin)
  150. {
  151. // Free the old skin
  152. SAFE_DELETE(_skin);
  153. // Assign the new skin
  154. _skin = skin;
  155. _skin->_model = this;
  156. }
  157. }
  158. Node* Model::getNode() const
  159. {
  160. return _node;
  161. }
  162. void Model::setNode(Node* node)
  163. {
  164. _node = node;
  165. // Re-bind node related material parameters
  166. if (node)
  167. {
  168. if (_material)
  169. {
  170. setMaterialNodeBinding(_material);
  171. }
  172. if (_partMaterials)
  173. {
  174. for (unsigned int i = 0; i < _partCount; ++i)
  175. {
  176. if (_partMaterials[i])
  177. {
  178. setMaterialNodeBinding(_partMaterials[i]);
  179. }
  180. }
  181. }
  182. }
  183. }
  184. void Model::draw(bool wireframe)
  185. {
  186. unsigned int count = _mesh->getPartCount();
  187. if (count == 0)
  188. {
  189. // No mesh parts (index buffers).
  190. if (_material)
  191. {
  192. Technique* technique = _material->getTechnique();
  193. for (unsigned int i = 0, count = technique->getPassCount(); i < count; ++i)
  194. {
  195. Pass* pass = technique->getPass(i);
  196. pass->bind();
  197. if (wireframe)
  198. {
  199. for (unsigned int i = 0, count = _mesh->getVertexCount(); i < count; i += 3)
  200. {
  201. GL_ASSERT( glDrawArrays(GL_LINE_LOOP, i, 3) );
  202. }
  203. }
  204. else
  205. {
  206. GL_ASSERT( glDrawArrays(_mesh->getPrimitiveType(), 0, _mesh->getVertexCount()) );
  207. }
  208. pass->unbind();
  209. }
  210. }
  211. }
  212. else
  213. {
  214. for (unsigned int i = 0; i < count; ++i)
  215. {
  216. MeshPart* part = _mesh->getPart(i);
  217. // Get the material for this mesh part.
  218. Material* material;
  219. if (_partMaterials && i < _partCount && _partMaterials[i])
  220. {
  221. material = _partMaterials[i]; // Use part material
  222. }
  223. else
  224. {
  225. material = _material; // Use shared material
  226. }
  227. if (material)
  228. {
  229. Technique* technique = material->getTechnique();
  230. for (unsigned int i = 0, count = technique->getPassCount(); i < count; ++i)
  231. {
  232. Pass* pass = technique->getPass(i);
  233. pass->bind();
  234. GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, part->_indexBuffer) );
  235. if (wireframe)
  236. {
  237. for (unsigned int i = 0, count = part->getIndexCount(); i < count; i += 3)
  238. {
  239. GL_ASSERT( glDrawElements(GL_LINE_LOOP, 3, part->getIndexFormat(), (const GLvoid*)i) );
  240. }
  241. }
  242. else
  243. {
  244. GL_ASSERT( glDrawElements(part->getPrimitiveType(), part->getIndexCount(), part->getIndexFormat(), 0) );
  245. }
  246. pass->unbind();
  247. }
  248. }
  249. }
  250. }
  251. }
  252. void Model::validatePartCount()
  253. {
  254. unsigned int partCount = _mesh->getPartCount();
  255. if (_partCount != partCount)
  256. {
  257. // Allocate new arrays and copy old items to them.
  258. if (_partMaterials)
  259. {
  260. Material** oldArray = _partMaterials;
  261. _partMaterials = new Material*[partCount];
  262. memset(_partMaterials, 0, sizeof(Material*) * partCount);
  263. for (unsigned int i = 0; i < _partCount; ++i)
  264. {
  265. _partMaterials[i] = oldArray[i];
  266. }
  267. SAFE_DELETE_ARRAY(oldArray);
  268. }
  269. // Update local part count.
  270. _partCount = _mesh->getPartCount();
  271. }
  272. }
  273. void Model::setMaterialNodeBinding(Material *material)
  274. {
  275. if (_node)
  276. {
  277. material->setNodeBinding(_node);
  278. unsigned int techniqueCount = material->getTechniqueCount();
  279. for (unsigned int i = 0; i < techniqueCount; ++i)
  280. {
  281. Technique* technique = material->getTechnique(i);
  282. technique->setNodeBinding(_node);
  283. unsigned int passCount = technique->getPassCount();
  284. for (unsigned int j = 0; j < passCount; ++j)
  285. {
  286. Pass* pass = technique->getPass(j);
  287. pass->setNodeBinding(_node);
  288. }
  289. }
  290. }
  291. }
  292. }