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.
 
 
 

323 lines
8.1 KiB

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