ccMaterial.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #ifndef CC_MATERIAL_HEADER
  18. #define CC_MATERIAL_HEADER
  19. //Local
  20. #include "ccColorTypes.h"
  21. #include "ccSerializableObject.h"
  22. //Qt
  23. #include <QSharedPointer>
  24. #include <QOpenGLTexture>
  25. #include <QtGui/qopengl.h>
  26. class QImage;
  27. class QOpenGLContext;
  28. //! Mesh (triangle) material
  29. class QCC_DB_LIB_API ccMaterial : public ccSerializableObject
  30. {
  31. public:
  32. //! Const + Shared type
  33. using CShared = QSharedPointer<const ccMaterial>;
  34. //! Shared type
  35. using Shared = QSharedPointer<ccMaterial>;
  36. //! Default constructor
  37. ccMaterial(const QString& name = QString("default"));
  38. //! Copy constructor
  39. ccMaterial(const ccMaterial& mtl);
  40. //! Destructor
  41. ~ccMaterial();
  42. //! Returns the material name
  43. inline const QString& getName() const { return m_name; }
  44. //! Returns the texture filename (if any)
  45. inline const QString& getTextureFilename() const { return m_textureFilename; }
  46. //! Sets the material name
  47. inline void setName(const QString& name) { m_name = name; }
  48. //! Sets diffuse color (both front and back)
  49. void setDiffuse(const ccColor::Rgbaf& color);
  50. //! Sets diffuse color (front)
  51. inline void setDiffuseFront(const ccColor::Rgbaf& color) { m_diffuseFront = color; }
  52. //! Sets diffuse color (back)
  53. inline void setDiffuseBack(const ccColor::Rgbaf& color) { m_diffuseBack = color; }
  54. //! Returns front diffuse color
  55. inline const ccColor::Rgbaf& getDiffuseFront() const { return m_diffuseFront; }
  56. //! Returns back diffuse color
  57. inline const ccColor::Rgbaf& getDiffuseBack() const { return m_diffuseBack; }
  58. //! Sets ambient color
  59. inline void setAmbient(const ccColor::Rgbaf& color) { m_ambient = color; }
  60. //! Returns ambient color
  61. inline const ccColor::Rgbaf& getAmbient() const { return m_ambient; }
  62. //! Sets specular color
  63. inline void setSpecular(const ccColor::Rgbaf& color) { m_specular = color; }
  64. //! Returns specular color
  65. inline const ccColor::Rgbaf& getSpecular() const { return m_specular; }
  66. //! Sets emission color
  67. inline void setEmission(const ccColor::Rgbaf& color) { m_emission = color; }
  68. //! Returns emission color
  69. inline const ccColor::Rgbaf& getEmission() const { return m_emission; }
  70. //! Sets shininess (both front - 100% - and back - 80%)
  71. void setShininess(float val);
  72. //! Sets shininess (front)
  73. inline void setShininessFront(float val) { m_shininessFront = val; }
  74. //! Sets shininess (back)
  75. inline void setShininessBack(float val) { m_shininessBack = val; }
  76. //! Returns front shininess
  77. inline float getShininessFront() const { return m_shininessFront; }
  78. //! Returns back shininess
  79. inline float getShininessBack() const { return m_shininessBack; }
  80. //! Sets transparency (all colors)
  81. void setTransparency(float val);
  82. //! Apply parameters (OpenGL)
  83. void applyGL(const QOpenGLContext* context, bool lightEnabled, bool skipDiffuse) const;
  84. //! Returns whether the material has an associated texture or not
  85. bool hasTexture() const;
  86. //! Sets texture
  87. /** If no filename is provided, a random one will be generated.
  88. **/
  89. void setTexture(QImage image, QString absoluteFilename = QString(), bool mirrorImage = true);
  90. //! Loads texture from file (and set it if successful)
  91. /** If the filename is not already in DB, the corresponding file will be loaded.
  92. \return whether the file could be loaded (or is already in DB) or not
  93. **/
  94. bool loadAndSetTexture(const QString& absoluteFilename);
  95. //! Returns the texture (if any)
  96. const QImage getTexture() const;
  97. //! Returns the texture ID (if any)
  98. GLuint getTextureID() const;
  99. //! Helper: makes all active GL light sources neutral (i.e. 'gray')
  100. /** \warning an OpenGL context must be active!
  101. **/
  102. static void MakeLightsNeutral(const QOpenGLContext* context);
  103. //! Returns the texture image associated to a given name
  104. static QImage GetTexture(const QString& absoluteFilename);
  105. //! Adds a texture to the global texture DB
  106. static void AddTexture(QImage image, const QString& absoluteFilename);
  107. //! Release all texture objects
  108. /** Should be called BEFORE the global shared context is destroyed.
  109. **/
  110. static void ReleaseTextures();
  111. //! Release the texture
  112. /** \warning Make sure no more materials are using this texture!
  113. **/
  114. void releaseTexture();
  115. //! Compares this material with another one
  116. /** \return true if both materials are equivalent or false otherwise
  117. **/
  118. bool compare(const ccMaterial& mtl) const;
  119. //inherited from ccSerializableObject
  120. bool isSerializable() const override { return true; }
  121. /** \warning Doesn't save the texture image!
  122. **/
  123. bool toFile(QFile& out, short dataVersion) const override;
  124. bool fromFile(QFile& in, short dataVersion, int flags, LoadedIDMap& oldToNewIDMap) override;
  125. short minimumFileVersion() const override;
  126. //! Returns unique identifier (UUID)
  127. inline QString getUniqueIdentifier() const { return m_uniqueID; }
  128. //! Sets the texture minification and magnification filters
  129. void setTextureMinMagFilters(QOpenGLTexture::Filter minificationFilter, QOpenGLTexture::Filter magnificationFilter);
  130. protected:
  131. QString m_name;
  132. QString m_textureFilename;
  133. QString m_uniqueID;
  134. ccColor::Rgbaf m_diffuseFront;
  135. ccColor::Rgbaf m_diffuseBack;
  136. ccColor::Rgbaf m_ambient;
  137. ccColor::Rgbaf m_specular;
  138. ccColor::Rgbaf m_emission;
  139. float m_shininessFront;
  140. float m_shininessBack;
  141. QOpenGLTexture::Filter m_texMinificationFilter;
  142. QOpenGLTexture::Filter m_texMagnificationFilter;
  143. };
  144. #endif //CC_MATERIAL_HEADER