Advertisement
microwerx

simplematerial

Dec 30th, 2017
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.86 KB | None | 0 0
  1. // SSPHH/Fluxions/Unicornfish/Viperfish/Hatchetfish/Sunfish/KASL/GLUT Extensions
  2. // Copyright (C) 2017 Jonathan Metzgar
  3. // All rights reserved.
  4. //
  5. // This program is free software : you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as
  7. // published by the Free Software Foundation, either version 3 of the
  8. // License, or (at your option) any later version.
  9. //
  10. // This program 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 Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with this program.If not, see <https://www.gnu.org/licenses/>.
  17. //
  18. // For any other type of licensing, please contact me at jmetzgar@outlook.com
  19. #ifndef FLUXIONS_SIMPLE_MATERIALS_HPP
  20. #define FLUXIONS_SIMPLE_MATERIALS_HPP
  21.  
  22. #include <fluxions_stdcxx.hpp>
  23. #include <fluxions_fileio.hpp>
  24. #include <fluxions_simple_texture.hpp>
  25. #include <fluxions_resource_manager.hpp>
  26. #include <fluxions_simple_property.hpp>
  27.  
  28. namespace Fluxions
  29. {
  30.     using namespace std;
  31.  
  32.  
  33.     struct SimpleMaterial
  34.     {
  35.         Vector3f Kd;
  36.         Vector3f Ks;
  37.         Vector3f Ke;
  38.         Vector3f Ka;
  39.         Vector3f Tf; // Translucency;   // Tf
  40.         float Tr = 0.0f; // TranslucencyLevel -- same as (1 - d)
  41.         Vector3f Refract;
  42.         Vector3f Opacity;
  43.         float Ns = 0.0f; // Specular exponent [0 <= Ns <= 1000]
  44.         float Ni = 0.0f;
  45.         float d = 1.0f; // dissolved -- same as (1 - Tr)
  46.  
  47.         float PBm = 0.0f; // for roughness
  48.         float PBk = 0.0f; // for metals
  49.         float PBKdm = 0.0f; // for diffuse roughness (Oren-Nayer)
  50.         float PBKsm = 0.0f; // for specular roughness (Cook-Torrance)
  51.         float PBior = 0.0f; // for Fresnel reflection (Cook-Torrance)
  52.         float PBGGXgamma = 0.11f; // for GGX BRDF (0.0 - 1.0 maps to 1.0 to 10.0 in the shader)
  53.         string PBmap;
  54.  
  55.         float EmissionGlossiness;
  56.         float ReflectFresnel;
  57.         float RefractGlossiness;
  58.         float ReflectGlossiness;
  59.         float AnisotropyAmount;
  60.         float AnisotropyRotation;
  61.         Vector3f AttenuationColor;
  62.         float AttenuationDistance;
  63.         Vector3f ScatteringAlbedo;
  64.         float MeanCosine;
  65.         vector<string> RefractMode;
  66.         bool HasRoundedCorners = false;
  67.         float RoundedCorners;
  68.         bool IsPortal = false;
  69.         bool HasIesProfile = false;
  70.         string IesProfileFilename;
  71.         Matrix4f IesProfileMatrix;
  72.         bool IesProfileKeepSharp;
  73.         vector<string> Invisible;
  74.         string map_Ka;
  75.         string map_Kd;
  76.         string map_Ks;
  77.         string map_Ke;
  78.         string map_d;
  79.         string map_opacity;
  80.         string map_Ns;
  81.         string map_Ni;
  82.         string map_Tr;// translucency;
  83.         string map_Tf;//Tf translucencyLevel;
  84.         string map_refract;
  85.         string map_anisotropy;
  86.         string map_anisorotation;
  87.         string map_scatteringAlbedo;
  88.         string map_attenuation;
  89.         string map_normal;
  90.         string map_bump;
  91.  
  92.         SimpleAssociativePropertyList Properties;
  93.     };
  94.  
  95.  
  96.     struct SimpleMap
  97.     {
  98.         string mapName;
  99.         GLuint mapId;
  100.         string pathname;
  101.         string shader;
  102.         GLuint textureId;
  103.         GLuint samplerId;
  104.         GLint unitId; // we're not using GLenum here because unitId can be < 0 and no overflow is expected.
  105.         GLint map_loc;
  106.         GLint map_mix_loc;
  107.         SimpleTexture textureObject;
  108.     };
  109.  
  110.  
  111.     ostream & WriteMaterial(ostream &ostr, const SimpleMaterial &mtl);
  112.     istream & ReadMaterial(istream &istr, SimpleMaterial &mtl);
  113.     void SetMaterialDefaults(SimpleMaterial &mtl);
  114.  
  115.  
  116.     struct SimpleMaterialLibrary
  117.     {
  118.         TResourceManager<SimpleMap> maps;
  119.         TResourceManager<SimpleMaterial> mtls;
  120.         FilePathInfo fpi;
  121.         string name;
  122.  
  123.         SimpleMaterialLibrary() { }
  124.         ~SimpleMaterialLibrary() { }
  125.     };
  126.  
  127.  
  128.     class SimpleMaterialSystem
  129.     {
  130.     private:
  131.         string defaultName = "<INVALIDMATERIAL>";
  132.         string currentMtlLibName;
  133.         GLuint currentMtlLibId = 0;
  134.         string currentMtlName;
  135.         GLuint currentMtlId = 0;
  136.  
  137.         SimpleMaterialLibrary *currentMtlLibPtr = nullptr;
  138.         SimpleMaterial *currentMtlPtr = nullptr;
  139.  
  140.         TResourceManager<SimpleMaterialLibrary> mtllibs;
  141.         TResourceManager<string> shaderMaps;
  142.  
  143.         map<string, string> maps_paths;
  144.         string defaultMapPath = "<UNKNOWN>";
  145.  
  146.         void SynchronizeIds();
  147.     public:
  148.         SimpleMaterialSystem();
  149.         ~SimpleMaterialSystem();
  150.  
  151.         void Clear();
  152.  
  153.         auto begin() -> decltype(mtllibs.begin()) { return mtllibs.begin(); }
  154.         auto end() -> decltype(mtllibs.end()) { return mtllibs.end(); }
  155.         auto size() -> decltype(mtllibs.size()) { return mtllibs.size(); }
  156.  
  157.         bool Save(const string &path);
  158.         bool Load(const string &mtllibName, const string &filename);
  159.         bool Save(const string &mtllibName, const string &filename);
  160.  
  161.         SimpleMaterialLibrary *CreateLibrary(const string &name);
  162.         SimpleMaterial *CreateMaterial(const string &name);
  163.  
  164.         void DeleteLibrary(const string &name);
  165.         void DeleteMaterial(const string &name);
  166.         void DeleteLibraryMaterial(const string &mtllibName, const string &mtlName);
  167.  
  168.         SimpleMaterialLibrary *SetLibrary(const string &name);
  169.         SimpleMaterial *SetMaterial(const string &name);
  170.         SimpleMaterial *SetLibraryMaterial(const string &mtllibName, const string &mtlName);
  171.         const SimpleMaterial *GetLibraryMaterial(const string &mtllibName, const string &mtlName) const;
  172.         SimpleMaterial *GetCurrentMaterial() { return currentMtlPtr; }
  173.  
  174.         const string & GetLibraryName();
  175.         const string & GetMaterialName();
  176.         const string & GetMaterialName(GLuint id);
  177.  
  178.         GLuint GetLibraryId();
  179.         GLuint GetMaterialId();
  180.         GLuint GetLibraryId(const string &name);
  181.         GLuint GetMaterialId(const string &name);
  182.         GLuint GetLibraryMaterialId(const string &mtllibName, const string &mtlName);
  183.  
  184.         bool AddMap(const string &path, const string &filename);
  185.         bool AddMapShader(const string &name, const string &shader);
  186.         void LoadMaps();
  187.         SimpleMap *GetTextureMap(const string &name);
  188.         const SimpleMap *GetTextureMap(const string &name) const;
  189.  
  190.         const string & GetMapPath(const string & mapName) const;
  191.     };
  192. }
  193.  
  194. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement