openscenegraph
Material
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSG_MATERIAL
15#define OSG_MATERIAL 1
16
17#include <osg/Vec4>
18#include <osg/StateAttribute>
19
20#ifndef OSG_GL_FIXED_FUNCTION_AVAILABLE
21 #define GL_AMBIENT 0x1200
22 #define GL_DIFFUSE 0x1201
23 #define GL_SPECULAR 0x1202
24 #define GL_EMISSION 0x1600
25 #define GL_AMBIENT_AND_DIFFUSE 0x1602
26 #define GL_COLOR_MATERIAL 0x0B57
27#endif
28
29namespace osg {
30/** Material - encapsulates OpenGL glMaterial state.*/
31class OSG_EXPORT Material : public StateAttribute
32{
33 public :
34
35 Material();
36
37 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
38 Material(const Material& mat,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
39 StateAttribute(mat,copyop),
40 _colorMode(mat._colorMode),
41 _ambientFrontAndBack(mat._ambientFrontAndBack),
42 _ambientFront(mat._ambientFront),
43 _ambientBack(mat._ambientBack),
44 _diffuseFrontAndBack(mat._diffuseFrontAndBack),
45 _diffuseFront(mat._diffuseFront),
46 _diffuseBack(mat._diffuseBack),
47 _specularFrontAndBack(mat._specularFrontAndBack),
48 _specularFront(mat._specularFront),
49 _specularBack(mat._specularBack),
50 _emissionFrontAndBack(mat._emissionFrontAndBack),
51 _emissionFront(mat._emissionFront),
52 _emissionBack(mat._emissionBack),
53 _shininessFrontAndBack(mat._shininessFrontAndBack),
54 _shininessFront(mat._shininessFront),
55 _shininessBack(mat._shininessBack) {}
56
57 META_StateAttribute(osg, Material, MATERIAL);
58
59 /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
60 virtual int compare(const StateAttribute& sa) const
61 {
62 // check the types are equal and then create the rhs variable
63 // used by the COMPARE_StateAttribute_Parameter macros below.
64 COMPARE_StateAttribute_Types(Material,sa)
65
66 // compare each parameter in turn against the rhs.
67 COMPARE_StateAttribute_Parameter(_colorMode)
68 COMPARE_StateAttribute_Parameter(_ambientFrontAndBack)
69 COMPARE_StateAttribute_Parameter(_ambientFront)
70 COMPARE_StateAttribute_Parameter(_ambientBack)
71 COMPARE_StateAttribute_Parameter(_diffuseFrontAndBack)
72 COMPARE_StateAttribute_Parameter(_diffuseFront)
73 COMPARE_StateAttribute_Parameter(_diffuseBack)
74 COMPARE_StateAttribute_Parameter(_specularFrontAndBack)
75 COMPARE_StateAttribute_Parameter(_specularFront)
76 COMPARE_StateAttribute_Parameter(_specularBack)
77 COMPARE_StateAttribute_Parameter(_emissionFrontAndBack)
78 COMPARE_StateAttribute_Parameter(_emissionFront)
79 COMPARE_StateAttribute_Parameter(_emissionBack)
80 COMPARE_StateAttribute_Parameter(_shininessFrontAndBack)
81 COMPARE_StateAttribute_Parameter(_shininessFront)
82 COMPARE_StateAttribute_Parameter(_shininessBack)
83
84 return 0; // passed all the above comparison macros, must be equal.
85 }
86
87 Material& operator = (const Material& rhs);
88
89 virtual bool getModeUsage(StateAttribute::ModeUsage& /*usage*/) const
90 {
91 // note, since Material does it's own glEnable/glDisable of GL_COLOR_MATERIAL
92 // we shouldn't declare usage of that mode, so commenting out the below usage.
93 // usage.usesMode(GL_COLOR_MATERIAL);
94 return true;
95 }
96
97 virtual void apply(State& state) const;
98
99 enum Face {
100 FRONT = GL_FRONT,
101 BACK = GL_BACK,
102 FRONT_AND_BACK = GL_FRONT_AND_BACK
103 };
104
105 enum ColorMode {
106 AMBIENT = GL_AMBIENT,
107 DIFFUSE = GL_DIFFUSE,
108 SPECULAR = GL_SPECULAR,
109 EMISSION = GL_EMISSION,
110 AMBIENT_AND_DIFFUSE = GL_AMBIENT_AND_DIFFUSE,
111 OFF
112 };
113
114 inline void setColorMode(ColorMode mode) { _colorMode = mode; }
115 inline ColorMode getColorMode() const { return _colorMode; }
116
117 void setAmbient( Face face, const Vec4& ambient );
118 const Vec4& getAmbient(Face face) const;
119 inline bool getAmbientFrontAndBack() const { return _ambientFrontAndBack; }
120
121 void setDiffuse( Face face, const Vec4& diffuse );
122 const Vec4& getDiffuse(Face face) const;
123 inline bool getDiffuseFrontAndBack() const { return _diffuseFrontAndBack; }
124
125 /** Set specular value of specified face(s) of the material,
126 * valid specular[0..3] range is 0.0 to 1.0.
127 */
128 void setSpecular( Face face, const Vec4& specular );
129
130 /** Get the specular value for specified face. */
131 const Vec4& getSpecular(Face face) const;
132
133 /** Return whether specular values are equal for front and back faces
134 * or not.
135 */
136 inline bool getSpecularFrontAndBack() const { return _specularFrontAndBack; }
137
138 /** Set emission value of specified face(s) of the material,
139 * valid emission[0..3] range is 0.0 to 1.0.
140 */
141 void setEmission( Face face, const Vec4& emission );
142
143 /** Get the emission value for specified face. */
144 const Vec4& getEmission(Face face) const;
145
146 /** Return whether emission values are equal for front and back faces
147 * or not.
148 */
149 inline bool getEmissionFrontAndBack() const { return _emissionFrontAndBack; }
150
151 /** Set shininess of specified face(s) of the material.
152 * valid shininess range is 0.0 to 128.0.
153 */
154 void setShininess(Face face, float shininess );
155
156 /** Get the shininess value for specified face. */
157 float getShininess(Face face) const;
158
159 /** Return whether shininess values are equal for front and back faces
160 * or not.
161 */
162 inline bool getShininessFrontAndBack() const { return _shininessFrontAndBack; }
163
164 /** Set the alpha value of ambient, diffuse, specular and emission
165 * colors of specified face, to 1-transparency.
166 * Valid transparency range is 0.0 to 1.0.
167 */
168 void setTransparency(Face face,float trans);
169
170 /** Set the alpha value of ambient, diffuse, specular and emission
171 * colors. Valid transparency range is 0.0 to 1.0.
172 */
173 void setAlpha(Face face,float alpha);
174
175 protected :
176
177 virtual ~Material();
178
179 ColorMode _colorMode;
180
181 bool _ambientFrontAndBack;
182 Vec4 _ambientFront; // r, g, b, w
183 Vec4 _ambientBack; // r, g, b, w
184
185 bool _diffuseFrontAndBack;
186 Vec4 _diffuseFront; // r, g, b, w
187 Vec4 _diffuseBack; // r, g, b, w
188
189 bool _specularFrontAndBack;
190 Vec4 _specularFront; // r, g, b, w
191 Vec4 _specularBack; // r, g, b, w
192
193 bool _emissionFrontAndBack;
194 Vec4 _emissionFront; // r, g, b, w
195 Vec4 _emissionBack; // r, g, b, w
196
197 bool _shininessFrontAndBack;
198 float _shininessFront; // values 0 - 128.0
199 float _shininessBack; // values 0 - 128.0
200
201};
202
203}
204
205#endif