Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Material.h
1 //
2 // Material.h for OSG
3 //
4 // Copyright (c) 2001-2011 Virtual Terrain Project
5 // Free for all uses, see license.txt for details.
6 //
7 
8 #ifndef VTOSG_MATERIAL
9 #define VTOSG_MATERIAL
10 
11 #include <osg/BlendFunc>
12 #include <osg/Material>
13 #include <osg/AlphaFunc>
14 
15 // Shorthand
16 #define FAB osg::Material::FRONT_AND_BACK
17 
20 
30 class vtMaterial : public osg::StateSet
31 {
32 public:
33  vtMaterial();
34 
35  void CopyFrom(vtMaterial *pFrom);
36 
37  void SetDiffuse(float r, float g, float b, float a = 1.0f);
38  RGBAf GetDiffuse() const;
39 
40  void SetSpecular(float r, float g, float b);
41  RGBf GetSpecular() const;
42 
43  void SetAmbient(float r, float g, float b);
44  RGBf GetAmbient() const;
45 
46  void SetEmission(float r, float g, float b);
47  RGBf GetEmission() const;
48 
49  void SetCulling(bool bCulling);
50  bool GetCulling() const;
51 
52  void SetLighting(bool bLighting);
53  bool GetLighting() const;
54 
55  void SetTransparent(bool bOn, bool bAdd = false);
56  bool GetTransparent() const;
57 
58  void SetWireframe(bool bOn);
59  bool GetWireframe() const;
60 
61  void SetTexture(osg::Image *pImage);
62  osg::Image *GetTexture() const;
63  void ModifiedTexture();
64 
65  void SetClamp(bool bClamp);
66  bool GetClamp() const;
67 
68  void SetMipMap(bool bMipMap);
69  bool GetMipMap() const;
70 
71  void SetDiffuse1(const RGBAf &c) { SetDiffuse(c.r, c.g, c.b, c.a); }
72  void SetDiffuse2(float f) { SetDiffuse(f, f, f); }
73 
74  void SetSpecular1(const RGBf &c) { SetSpecular(c.r, c.g, c.b); }
75  void SetSpecular2(float f) { SetSpecular(f, f, f); }
76 
77  void SetAmbient1(const RGBf &c) { SetAmbient(c.r, c.g, c.b); }
78  void SetAmbient2(float f) { SetAmbient(f, f, f); }
79 
80  void SetEmission1(const RGBf &c) { SetEmission(c.r, c.g, c.b); }
81  void SetEmission2(float f) { SetEmission(f, f, f); }
82 
83  // global option
84  static bool s_bTextureCompression;
85 
86  // remember this for convenience and referencing
87  osg::ref_ptr<osg::Image> m_Image;
88 
89  // the VT material object includes texture
90  osg::ref_ptr<osg::Material> m_pMaterial;
91  osg::ref_ptr<osg::Texture2D> m_pTexture;
92  osg::ref_ptr<osg::BlendFunc> m_pBlendFunc;
93  osg::ref_ptr<osg::AlphaFunc> m_pAlphaFunc;
94 };
95 typedef osg::ref_ptr<vtMaterial> vtMaterialPtr;
96 
100 class vtMaterialArray : public std::vector<vtMaterialPtr>, public osg::Referenced
101 {
102 public:
103  int Find(vtMaterial *mat);
104  int AddTextureMaterial(osg::Image *pImage,
105  bool bCulling, bool bLighting,
106  bool bTransp = false, bool bAdditive = false,
107  float fAmbient = 0.0f, float fDiffuse = 1.0f,
108  float fAlpha = 1.0f, float fEmissive = 0.0f,
109  bool bTexGen = false, bool bClamp = false,
110  bool bMipMap = false);
111  int AddTextureMaterial2(const char *fname,
112  bool bCulling, bool bLighting,
113  bool bTransp = false, bool bAdditive = false,
114  float fAmbient = 0.0f, float fDiffuse = 1.0f,
115  float fAlpha = 1.0f, float fEmissive = 0.0f,
116  bool bTexGen = false, bool bClamp = false,
117  bool bMipMap = false);
118  int AddRGBMaterial(const RGBf &diffuse, const RGBf &ambient,
119  bool bCulling = true, bool bLighting= true, bool bWireframe = false,
120  float fAlpha = 1.0f, float fEmissive = 0.0f);
121  int AddRGBMaterial1(const RGBf &diffuse,
122  bool bCulling = true, bool bLighting= true, bool bWireframe = false,
123  float fAlpha = 1.0f, float fEmissive = 0.0f);
124  void AddShadowMaterial(float fOpacity);
125  int FindByDiffuse(const RGBAf &rgba) const;
126  int FindByImage(const osg::Image *image) const;
127 
128  void CopyFrom(vtMaterialArray *pFromMats);
129 
130  void RemoveMaterial(vtMaterial *pMat);
131 
133  int AppendMaterial(vtMaterial *pMat);
134 };
135 typedef osg::ref_ptr<vtMaterialArray> vtMaterialArrayPtr;
136  // Group sg
138 
139 #endif // VTOSG_MATERIAL
140