/libogre/src/Util.cpp

http://github.com/sirikata/sirikata · C++ · 89 lines · 54 code · 15 blank · 20 comment · 10 complexity · 52293bb89bc388161a8da91c27e4ad6c MD5 · raw file

  1. // Copyright (c) 2011 Sirikata Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can
  3. // be found in the LICENSE file.
  4. #include <sirikata/ogre/Util.hpp>
  5. #include <boost/lexical_cast.hpp>
  6. #include <sirikata/core/transfer/URL.hpp>
  7. namespace Sirikata {
  8. namespace Graphics {
  9. // Utility to compute full hash of the content of a visual. This is needed
  10. // because the hash of some date (e.g. a collada file) may be the same, but
  11. // some textures may differ (the relative names are the same, but the data
  12. // the absolute names point to differs).
  13. String ogreVisualName(Mesh::VisualPtr vis, TextureBindingsMapPtr bindings) {
  14. // To compute a hash of the entire visual, we just combine the hashes of the
  15. // original and all the dependent resources, then hash the result. The
  16. // approach right now uses strings and could do somethiing like xor'ing
  17. // everything instead, but this is easy and shouldn't happen all that often.
  18. String data = vis->hash.toString();
  19. // This assumes we're using a map or some sorted container so we get
  20. // consistent ordering.
  21. for(TextureBindingsMap::const_iterator tex_it = bindings->begin(); tex_it != bindings->end(); tex_it++)
  22. data += tex_it->second;
  23. return String("vis_") + SHA256::computeDigest(data).toString();
  24. }
  25. String ogreMaterialName(const Mesh::MaterialEffectInfo& mat, const Transfer::URI& parent_uri, TextureBindingsMapPtr bindings) {
  26. std::ostringstream data;
  27. // We have to deal with all the properties of the material.
  28. // A couple of things are
  29. data << "shininess_" << mat.shininess << "_";
  30. data << "reflectivity_" << mat.reflectivity << "_";
  31. for(uint32 texn = 0; texn < mat.textures.size(); texn++) {
  32. data << "tex" << texn << "_(";
  33. const Mesh::MaterialEffectInfo::Texture& tex = mat.textures[texn];
  34. data << "affecting_" << (int)tex.affecting << "_";
  35. if (tex.uri.length() == 0) {
  36. data << "color_" << tex.color << "_";
  37. }
  38. else {
  39. Transfer::URL url(parent_uri);
  40. assert(!url.empty());
  41. Transfer::URI mat_uri( Transfer::URL(url.context(), tex.uri).toString() );
  42. TextureBindingsMap::const_iterator where = bindings->find(mat_uri.toString());
  43. if (where != bindings->end())
  44. data << "tex_" << where->second << "_";
  45. data << "texCoord_" << tex.texCoord << "_";
  46. // filter, sampler type?
  47. data << "wrapS_" << (int)tex.wrapS << "_";
  48. data << "wrapT_" << (int)tex.wrapT << "_";
  49. data << "wrapU_" << (int)tex.wrapU << "_";
  50. data << "maxMipLevel_" << tex.maxMipLevel << "_";
  51. data << "mipBias_" << tex.mipBias << "_";
  52. }
  53. data << ")_";
  54. }
  55. return String("mat_") + SHA256::computeDigest(data.str()).toString();
  56. }
  57. String ogreBillboardMaterialName(TextureBindingsMapPtr bindings) {
  58. // Billboard materials only need to deal with textures.
  59. String data = "";
  60. for(TextureBindingsMap::const_iterator tex_it = bindings->begin(); tex_it != bindings->end(); tex_it++)
  61. data += tex_it->second;
  62. return String("matbb_") + SHA256::computeDigest(data).toString();
  63. }
  64. String ogreSkeletonName(Mesh::MeshdataPtr md, TextureBindingsMapPtr bindings) {
  65. // TODO(ewencp) this should really be some hash of the skeleton data, but
  66. // this works fine for now. We probably won't have a ton of reuse of
  67. // skeletons across different meshes anyway...
  68. return ogreVisualName(md, bindings) + "_skeleton";
  69. }
  70. String ogreLightName(const String& entityname, const String& meshname, int32 light_idx) {
  71. return entityname + "_light_" + meshname + boost::lexical_cast<String>(light_idx);
  72. }
  73. } // namespace Graphics
  74. } // namespace Sirikata