Simple game engine with complete export to scripting language
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

327 行
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. 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