Simple game engine with complete export to scripting language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ModelManager.cpp 8.1 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include "ModelManager.h"
  2. #include "MeshManager.h"
  3. #include "ShaderManager.h"
  4. #include "Utilities/StringUtilities.h"
  5. #include "Utilities/Log.h"
  6. #include "tinyxml.h"
  7. #include "physfs.h"
  8. #include <iostream>
  9. using namespace std;
  10. namespace BlueCore
  11. {
  12. //------------------------------------------------------------------------------
  13. void Model::render(RenderDevice *device) const
  14. {
  15. // ModelMesh->render();
  16. glEnable(GL_LIGHTING);
  17. glDepthFunc ( GL_LEQUAL );
  18. glEnable ( GL_DEPTH_TEST );
  19. glDepthMask ( GL_TRUE );
  20. glEnableClientState (GL_VERTEX_ARRAY );
  21. glEnableClientState (GL_TEXTURE_COORD_ARRAY );
  22. glEnableClientState (GL_NORMAL_ARRAY );
  23. glMaterialfv (GL_FRONT, GL_SHININESS, &LitPass.Shininess);
  24. glMaterialfv (GL_FRONT, GL_SPECULAR, ( GLfloat * ) &LitPass.Specular );
  25. // setup shader
  26. if (LitPass.Program)
  27. {
  28. device->setShaderProgram(LitPass.Program);
  29. int textureId = 0;
  30. std::vector<Model::TextureUnit>::const_iterator titer;
  31. for (titer = LitPass.Textures.begin(); titer != LitPass.Textures.end(); titer++)
  32. {
  33. device->setTexture(textureId, ( *titer ).mTexture );
  34. device->setTexture(LitPass.Program, textureId, ( *titer ).mName);
  35. textureId++;
  36. }
  37. if (LitPass.Tangents)
  38. {
  39. //device->setTangentBuffer(LitPass.Program, &ModelMesh->TangentBuffer, "tangent");
  40. //device->setTangentBuffer(LitPass.Program, &ModelMesh->BitangentBuffer, "bitangent");
  41. }
  42. }
  43. /*
  44. else
  45. {
  46. int textureId = 0;
  47. std::vector<Model::TextureUnit>::iterator titer;
  48. for (titer = pass.Textures.begin(); titer != pass.Textures.end(); titer++)
  49. {
  50. Renderer::getSingleton()->bindTexture(( *titer ).mTexture, textureId );
  51. ShaderManager::getSingleton()->useTexture(pass.Program, textureId, ( *titer ).mName);
  52. textureId++;
  53. break;
  54. }
  55. }
  56. // render subsets
  57. SubsetVector::iterator iter;
  58. for (iter = mesh->surface_subsets.begin(); iter
  59. != mesh->surface_subsets.end(); iter++)
  60. {
  61. Subset subset = *iter;
  62. mesh->index_buffer.draw(subset.first, subset.count);
  63. }
  64. */
  65. ModelMesh->render();
  66. device->setShaderProgram(0);
  67. glActiveTextureARB (GL_TEXTURE2_ARB );
  68. glDisable (GL_TEXTURE_2D );
  69. glActiveTextureARB (GL_TEXTURE1_ARB );
  70. glDisable (GL_TEXTURE_2D );
  71. glActiveTextureARB (GL_TEXTURE0_ARB );
  72. glDisable (GL_TEXTURE_2D );
  73. /*
  74. // cleanup
  75. if (pass.Program && ShaderManager::getSingleton()->usingShaders() )
  76. {
  77. ShaderManager::getSingleton()->useShaderProgram( 0);
  78. if (pass.Tangents)
  79. {
  80. ShaderManager::getSingleton()->disableTangentBuffer(pass.Program, "tangent");
  81. ShaderManager::getSingleton()->disableTangentBuffer(pass.Program, "bitangent");
  82. }
  83. }
  84. glPopMatrix();
  85. glActiveTextureARB (GL_TEXTURE2_ARB );
  86. glDisable (GL_TEXTURE_2D );
  87. glActiveTextureARB (GL_TEXTURE1_ARB );
  88. glDisable (GL_TEXTURE_2D );
  89. glActiveTextureARB (GL_TEXTURE0_ARB );
  90. glDisable (GL_TEXTURE_2D );
  91. */
  92. /*
  93. glDisableClientState (GL_VERTEX_ARRAY );
  94. glDisableClientState (GL_TEXTURE_COORD_ARRAY );
  95. glDisableClientState (GL_NORMAL_ARRAY );
  96. */
  97. }
  98. //------------------------------------------------------------------------------
  99. ModelManager::ModelManager(TextureManager *texturemanager,
  100. ShaderManager* shadermanager, MeshManager* meshmanager) :
  101. _TextureManager(texturemanager), _ShaderManager(shadermanager),
  102. _MeshManager(meshmanager)
  103. {
  104. clog << ">>> ModelManager constructed ..."<< endline;
  105. }
  106. //------------------------------------------------------------------------------
  107. ModelManager::~ModelManager()
  108. {
  109. clog << ">>> ModelManager destructed ..."<< endline;
  110. }
  111. //------------------------------------------------------------------------------
  112. void ModelManager::parseRenderPassDefinition(
  113. Model::RenderPassDefinition &Definition,
  114. const TiXmlElement* DefinitionElement)
  115. {
  116. const TiXmlElement * TextureElement =
  117. DefinitionElement->FirstChildElement("Texture");
  118. while (TextureElement)
  119. {
  120. Model::TextureUnit unit;
  121. unit.mName = TextureElement->Attribute("name");
  122. const char *file = TextureElement->Attribute("file");
  123. if (file)
  124. unit.mTexture = _TextureManager->loadTexture(file);
  125. else
  126. unit.mTexture = 0;
  127. Definition.Textures.push_back(unit);
  128. TextureElement = TextureElement->NextSiblingElement("Texture");
  129. }
  130. const TiXmlElement * ShaderElement =
  131. DefinitionElement->FirstChildElement("Shader");
  132. if (ShaderElement)
  133. {
  134. Definition.Program
  135. = _ShaderManager->loadShaderProgram(ShaderElement->Attribute("name") );
  136. const char *t = ShaderElement->Attribute("tangents");
  137. if (t && string("true") == t)
  138. Definition.Tangents = true;
  139. else
  140. Definition.Tangents = false;
  141. }
  142. else
  143. Definition.Program = 0;
  144. const TiXmlElement * MaterialElement =
  145. DefinitionElement->FirstChildElement("Material");
  146. if (MaterialElement)
  147. {
  148. double s;
  149. MaterialElement->QueryDoubleAttribute("shininess", &s);
  150. Definition.Shininess = s;
  151. std::vector<std::string> components;
  152. explode(MaterialElement->Attribute("specular"), components);
  153. for (unsigned int i = 0; i < components.size(); i++)
  154. {
  155. Definition.Specular[i] = atof(components[i].c_str() );
  156. if (i == 3)
  157. break;
  158. }
  159. }
  160. Definition.Enabled = true;
  161. }
  162. //------------------------------------------------------------------------------
  163. Model *ModelManager::loadModel(const string &name)
  164. {
  165. // check if this model is already loaded
  166. ModelContainer::const_iterator result;
  167. result = _Models.find(name);
  168. if (result != _Models.end() )
  169. {
  170. return result->second.pointer();
  171. }
  172. string filename = name + ".model.xml";
  173. // load the document
  174. PHYSFS_file *file = PHYSFS_openRead(filename.c_str());
  175. if ( !file)
  176. {
  177. clog << "!!! XML-File '"<< name << "' not found"<< endline;
  178. return 0;
  179. }
  180. unsigned int fileSize = PHYSFS_fileLength(file);
  181. Buffer<char> buffer(fileSize);
  182. PHYSFS_read(file, buffer.data(), 1, buffer.size() );
  183. buffer[buffer.count() - 1] = 0;
  184. PHYSFS_close(file);
  185. TiXmlDocument *document = new TiXmlDocument();
  186. document->Parse(buffer.data() );
  187. if (document->Error() )
  188. {
  189. clog << "!!! Error loading XML-File'"<< name << "': ";
  190. clog << document->ErrorRow() << ","<< document->ErrorCol();
  191. clog << " "<< document->ErrorDesc() << endline;
  192. return 0;
  193. }
  194. Model *model = new Model();
  195. if (document == 0)
  196. {
  197. clog << "!!! Model '"<< name << "' not found!"<< endline;
  198. return 0;
  199. }
  200. const TiXmlElement *model_element = document->FirstChildElement("Model");
  201. if (model_element)
  202. {
  203. const TiXmlElement* MeshElement =
  204. model_element->FirstChildElement("Mesh");
  205. if (MeshElement)
  206. {
  207. model->ModelMesh
  208. = _MeshManager->loadMesh(MeshElement->Attribute("file") );
  209. }
  210. const TiXmlElement * ShadowMeshElement =
  211. model_element->FirstChildElement("ShadowMesh");
  212. if (ShadowMeshElement)
  213. {
  214. model->ShadowMesh
  215. = _MeshManager->loadMesh(ShadowMeshElement->Attribute("file") );
  216. }
  217. else
  218. {
  219. model->ShadowMesh = 0;
  220. }
  221. const TiXmlElement* definition_element;
  222. definition_element
  223. = model_element->FirstChildElement("AmbientRenderPass");
  224. if (definition_element)
  225. parseRenderPassDefinition(model->AmbientPass, definition_element);
  226. else
  227. model->AmbientPass.Enabled = false;
  228. definition_element = model_element->FirstChildElement("LitRenderPass");
  229. if (definition_element)
  230. parseRenderPassDefinition(model->LitPass, definition_element);
  231. else
  232. model->LitPass.Enabled = false;
  233. definition_element
  234. = model_element->FirstChildElement("DefaultRenderPass");
  235. if (definition_element)
  236. parseRenderPassDefinition(model->DefaultPass, definition_element);
  237. else
  238. model->DefaultPass.Enabled = false;
  239. }
  240. _Models[name] = model;
  241. return model;
  242. }
  243. #if 0
  244. //------------------------------------------------------------------------------
  245. void ModelManager::unload()
  246. {
  247. ModelContainer::iterator iter;
  248. for (iter = _Models.begin(); iter != _Models.end(); iter++)
  249. {
  250. clog << ">>> ModelManager: unload " << iter->first << endlog;
  251. delete iter->second;
  252. }
  253. _Models.clear();
  254. }
  255. #endif
  256. } // namespace BlueCore