ccMaterialDB.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //##########################################################################
  2. //# #
  3. //# CLOUDCOMPARE #
  4. //# #
  5. //# This program is free software; you can redistribute it and/or modify #
  6. //# it under the terms of the GNU General Public License as published by #
  7. //# the Free Software Foundation; version 2 or later of the License. #
  8. //# #
  9. //# This program is distributed in the hope that it will be useful, #
  10. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  11. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  12. //# GNU General Public License for more details. #
  13. //# #
  14. //# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
  15. //# #
  16. //##########################################################################
  17. //Always first
  18. //#include "ccIncludeGL.h"
  19. #include "ccLog.h"
  20. //Qt
  21. #include <QFileSystemWatcher>
  22. #include <QFileInfo>
  23. #include <QImage>
  24. #include <QOpenGLTexture>
  25. //! Smart texture database
  26. class ccMaterialDB : public QObject
  27. {
  28. Q_OBJECT
  29. public:
  30. ccMaterialDB()
  31. : m_initialized(false)
  32. {}
  33. void init()
  34. {
  35. if (!m_initialized)
  36. {
  37. connect(&m_watcher, &QFileSystemWatcher::fileChanged, this, &ccMaterialDB::onFileChanged);
  38. m_initialized = true;
  39. }
  40. }
  41. void onFileChanged(const QString& filename)
  42. {
  43. if (!m_textures.contains(filename))
  44. {
  45. assert(false);
  46. m_watcher.removePath(filename);
  47. return;
  48. }
  49. if (QFileInfo(filename).exists()) //make sure the image still exists
  50. {
  51. ccLog::Warning(tr("File '%1' has been updated").arg(filename));
  52. QImage image;
  53. if (image.load(filename))
  54. {
  55. //update the texture
  56. m_textures[filename].image = image;
  57. openGLTextures.remove(filename);
  58. }
  59. else
  60. {
  61. ccLog::Warning(tr("Failed to load the new version of the file"));
  62. }
  63. }
  64. else
  65. {
  66. ccLog::Warning(tr("File '%1' has been deleted or renamed").arg(filename));
  67. }
  68. }
  69. inline bool hasTexture(const QString& filename) const
  70. {
  71. return m_textures.contains(filename);
  72. }
  73. inline QImage getTexture(const QString& filename) const
  74. {
  75. return m_textures.contains(filename) ? m_textures[filename].image : QImage();
  76. }
  77. void addTexture(const QString& filename, const QImage& image)
  78. {
  79. if (!m_initialized)
  80. init();
  81. if (m_textures.contains(filename))
  82. {
  83. ++m_textures[filename].counter;
  84. }
  85. else
  86. {
  87. m_textures[filename].image = image;
  88. m_textures[filename].counter = 1;
  89. m_watcher.addPath(filename);
  90. }
  91. }
  92. void increaseTextureCounter(const QString& filename)
  93. {
  94. if (m_textures.contains(filename))
  95. {
  96. assert(m_textures[filename].counter >= 1);
  97. ++m_textures[filename].counter;
  98. }
  99. else
  100. {
  101. assert(false);
  102. }
  103. }
  104. void releaseTexture(const QString& filename)
  105. {
  106. if (m_textures.contains(filename))
  107. {
  108. if (m_textures[filename].counter > 1)
  109. {
  110. --m_textures[filename].counter;
  111. }
  112. else
  113. {
  114. removeTexture(filename);
  115. }
  116. }
  117. }
  118. void removeTexture(const QString& filename)
  119. {
  120. m_textures.remove(filename);
  121. m_watcher.removePath(filename);
  122. assert(QOpenGLContext::currentContext());
  123. openGLTextures.remove(filename);
  124. }
  125. QMap<QString, QSharedPointer<QOpenGLTexture> > openGLTextures;
  126. protected:
  127. struct TextureInfo
  128. {
  129. QImage image;
  130. unsigned counter = 0;
  131. };
  132. bool m_initialized;
  133. QFileSystemWatcher m_watcher;
  134. QMap<QString, TextureInfo> m_textures;
  135. };