Model.cpp 8.7 KB

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