VGearsRSDFile.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <OgreMaterialManager.h>
  16. #include <OgrePass.h>
  17. #include <OgreTechnique.h>
  18. #include "data/VGearsRSDFile.h"
  19. #include "common/VGearsStringUtil.h"
  20. #include "data/VGearsRSDFileSerializer.h"
  21. namespace VGears{
  22. const Ogre::String RSDFile::RESOURCE_TYPE("VGearsRSDFile");
  23. RSDFile::RSDFile(
  24. Ogre::ResourceManager *creator, const Ogre::String &name,
  25. Ogre::ResourceHandle handle, const Ogre::String &group, bool is_manual,
  26. Ogre::ManualResourceLoader *loader
  27. ) : Ogre::Resource(creator, name, handle, group, is_manual, loader)
  28. {createParamDictionary(RESOURCE_TYPE);}
  29. RSDFile::~RSDFile(){unload();}
  30. void RSDFile::loadImpl(){
  31. RSDFileSerializer serializer;
  32. Ogre::DataStreamPtr stream(
  33. Ogre::ResourceGroupManager::getSingleton().openResource(
  34. mName, mGroup, true, this
  35. )
  36. );
  37. serializer.ImportRSDFile(stream, this);
  38. Ogre::MaterialManager &material_manager(
  39. Ogre::MaterialManager::getSingleton()
  40. );
  41. const String material_base_name(GetMaterialBaseName());
  42. Ogre::MaterialPtr material;
  43. for (size_t i(0); i <= GetTextureNameCount(); ++ i){
  44. Ogre::String material_name(
  45. material_base_name + "/" + Ogre::StringConverter::toString(i)
  46. );
  47. material = material_manager.getByName(material_name, mGroup);
  48. if (material == nullptr){
  49. #ifndef NDEBUG
  50. Ogre::LogManager::getSingleton().stream()
  51. << " creating Material for RSDFile"
  52. << " '" << material_name << "'";
  53. #endif
  54. material = material_manager.create(material_name, mGroup);
  55. material->_notifyOrigin(getName());
  56. materials_.push_back(material);
  57. Ogre::Pass *pass(material->getTechnique(0)->getPass(0));
  58. pass->setVertexColourTracking(Ogre::TVC_AMBIENT);
  59. if (i > 0) AddTexture(pass, i - 1);
  60. }
  61. }
  62. }
  63. void RSDFile::unloadImpl(){
  64. polygon_name_.clear();
  65. material_name_.clear();
  66. group_name_.clear();
  67. texture_names_.clear();
  68. for (
  69. MaterialList::iterator it(materials_.begin());
  70. it != materials_.end();
  71. ++ it
  72. ){
  73. Ogre::MaterialManager::getSingleton().remove((*it)->getHandle());
  74. }
  75. materials_.clear();
  76. }
  77. size_t RSDFile::calculateSize() const{
  78. size_t size_texture_names(0);
  79. for (
  80. TextureNameList::const_iterator it(texture_names_.begin());
  81. it != texture_names_.end();
  82. ++ it
  83. ){
  84. size_texture_names += it->size();
  85. }
  86. return
  87. polygon_name_.size() + material_name_.size()
  88. + group_name_.size() + size_texture_names;
  89. }
  90. String RSDFile::GetMaterialBaseName() const{
  91. String base_name, ext, path;
  92. StringUtil::splitPath(getName(), path);
  93. StringUtil::splitBase(material_name_, base_name);
  94. StringUtil::toLowerCase(base_name);
  95. return path + base_name;
  96. }
  97. void RSDFile::AddTexture(Ogre::Pass *pass, const size_t index) const{
  98. String texture_name;
  99. StringUtil::splitPath(getName(), texture_name);
  100. texture_name += texture_names_[ index ];
  101. if (StringUtil::endsWith(texture_name, EXT_TIM)){
  102. StringUtil::toLowerCase(texture_name);
  103. if (
  104. !Ogre::ResourceGroupManager::getSingleton().resourceExists(
  105. mGroup, texture_name
  106. )
  107. ){
  108. texture_name = StringUtil::replaceAll(
  109. texture_name, EXT_TIM, EXT_TEX
  110. );
  111. }
  112. }
  113. if (!texture_name.empty()){
  114. pass->setAlphaRejectFunction(Ogre::CMPF_GREATER);
  115. pass->setAlphaRejectValue(0);
  116. Ogre::TextureUnitState *texture_unit(
  117. pass->createTextureUnitState()
  118. );
  119. texture_unit->setTextureName(texture_name);
  120. texture_unit->setNumMipmaps(0);
  121. texture_unit->setTextureFiltering(Ogre::TFO_NONE);
  122. }
  123. }
  124. void RSDFile::SetPolygonName(const String &polygon_name){
  125. polygon_name_ = polygon_name;
  126. }
  127. void RSDFile::SetMaterialName(const String &material_name){
  128. material_name_ = material_name;
  129. }
  130. void RSDFile::SetGroupName(const String &group_name){
  131. group_name_ = group_name;
  132. }
  133. const String& RSDFile::GetPolygonName() const{return polygon_name_;}
  134. const String&RSDFile::GetMaterialName() const{return material_name_;}
  135. const String& RSDFile::GetGroupName() const{return group_name_;}
  136. size_t RSDFile::GetTextureNameCount() const{return texture_names_.size();}
  137. RSDFile::TextureNameList& RSDFile::GetTextureNames(){return texture_names_;}
  138. }