root/OpenSceneGraph/trunk/src/osg/Material.cpp @ 13041

Revision 13041, 13.2 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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#include <osg/Material>
14#include <osg/BoundsChecking>
15#include <osg/Notify>
16
17using namespace osg;
18
19Material::Material()
20{
21    _colorMode = OFF;
22
23    _ambientFrontAndBack = true;
24    _ambientFront.set(0.2f, 0.2f, 0.2f, 1.0f);
25    _ambientBack.set(0.2f, 0.2f, 0.2f, 1.0f);
26
27    _diffuseFrontAndBack = true;
28    _diffuseFront.set(0.8f, 0.8f, 0.8f, 1.0f);
29    _diffuseBack.set(0.8f, 0.8f, 0.8f, 1.0f);
30
31    _specularFrontAndBack = true;
32    _specularFront.set(0.0f, 0.0f, 0.0f, 1.0f);
33    _specularBack.set(0.0f, 0.0f, 0.0f, 1.0f);
34
35    _emissionFrontAndBack = true;
36    _emissionFront.set(0.0f, 0.0f, 0.0f, 1.0f);
37    _emissionBack.set(0.0f, 0.0f, 0.0f, 1.0f);
38
39    _shininessFrontAndBack = true;
40    _shininessFront = 0.0f;
41    _shininessBack = 0.0f;
42}
43
44
45Material::~Material()
46{
47}
48
49Material& Material:: operator = (const Material& rhs)
50{
51    if (&rhs==this) return *this;
52
53    _colorMode= rhs._colorMode;
54    _ambientFrontAndBack= rhs._ambientFrontAndBack;
55    _ambientFront= rhs._ambientFront;
56    _ambientBack= rhs._ambientBack;
57    _diffuseFrontAndBack= rhs._diffuseFrontAndBack;
58    _diffuseFront= rhs._diffuseFront;
59    _diffuseBack= rhs._diffuseBack;
60    _specularFrontAndBack= rhs._specularFrontAndBack;
61    _specularFront= rhs._specularFront;
62    _specularBack= rhs._specularBack;
63    _emissionFrontAndBack= rhs._emissionFrontAndBack;
64    _emissionFront= rhs._emissionFront;
65    _emissionBack= rhs._emissionBack;
66    _shininessFrontAndBack= rhs._shininessFrontAndBack;
67    _shininessFront= rhs._shininessFront;
68    _shininessBack= rhs._shininessBack;
69
70    return *this;
71}
72
73
74void Material::setAmbient(Face face, const Vec4& ambient )
75{
76    switch(face)
77    {
78        case(FRONT):
79            _ambientFrontAndBack = false;
80            _ambientFront = ambient;
81            //clampArray4BetweenRange(_ambientFront,0.0f,1.0f,"osg::Material::setAmbient(..)");
82            break;
83        case(BACK):
84            _ambientFrontAndBack = false;
85            _ambientBack = ambient;
86            //clampArray4BetweenRange(_ambientBack,0.0f,1.0f,"Material::setAmbient(..)");
87            break;
88        case(FRONT_AND_BACK):
89            _ambientFrontAndBack = true;
90            _ambientFront = ambient;
91            //clampArray4BetweenRange(_ambientFront,0.0f,1.0f,"Material::setAmbient(..)");
92            _ambientBack = _ambientFront;
93            break;
94        default:
95            OSG_NOTICE<<"Notice: invalid Face passed to Material::setAmbient()."<<std::endl;
96    }
97}
98
99
100const Vec4& Material::getAmbient(Face face) const
101{
102    switch(face)
103    {
104        case(FRONT):
105            return _ambientFront;
106        case(BACK):
107            return _ambientBack;
108        case(FRONT_AND_BACK):
109            if (!_ambientFrontAndBack)
110            {
111                OSG_NOTICE<<"Notice: Material::getAmbient(FRONT_AND_BACK) called on material "<< std::endl;
112                OSG_NOTICE<<"        with separate FRONT and BACK ambient colors."<< std::endl;
113            }
114            return _ambientFront;
115    }
116    OSG_NOTICE<<"Notice: invalid Face passed to Material::getAmbient()."<< std::endl;
117    return _ambientFront;
118}
119
120
121void Material::setDiffuse(Face face, const Vec4& diffuse )
122{
123    switch(face)
124    {
125        case(FRONT):
126            _diffuseFrontAndBack = false;
127            _diffuseFront = diffuse;
128            //clampArray4BetweenRange(_diffuseFront,0.0f,1.0f,"Material::setDiffuse(..)");
129            break;
130        case(BACK):
131            _diffuseFrontAndBack = false;
132            _diffuseBack = diffuse;
133            //clampArray4BetweenRange(_diffuseBack,0.0f,1.0f,"Material::setDiffuse(..)");
134            break;
135        case(FRONT_AND_BACK):
136            _diffuseFrontAndBack = true;
137            _diffuseFront = diffuse;
138            //clampArray4BetweenRange(_diffuseFront,0.0f,1.0f,"Material::setDiffuse(..)");
139            _diffuseBack = _diffuseFront;
140            break;
141        default:
142            OSG_NOTICE<<"Notice: invalid Face passed to Material::setDiffuse()."<< std::endl;
143            break;
144    }
145}
146
147
148const Vec4& Material::getDiffuse(Face face) const
149{
150    switch(face)
151    {
152        case(FRONT):
153            return _diffuseFront;
154        case(BACK):
155            return _diffuseBack;
156        case(FRONT_AND_BACK):
157            if (!_diffuseFrontAndBack)
158            {
159                OSG_NOTICE<<"Notice: Material::getDiffuse(FRONT_AND_BACK) called on material "<< std::endl;
160                OSG_NOTICE<<"        with separate FRONT and BACK diffuse colors."<< std::endl;
161            }
162            return _diffuseFront;
163    }
164    OSG_NOTICE<<"Notice: invalid Face passed to Material::getDiffuse()."<< std::endl;
165    return _diffuseFront;
166}
167
168
169void Material::setSpecular(Face face, const Vec4& specular )
170{
171    switch(face)
172    {
173        case(FRONT):
174            _specularFrontAndBack = false;
175            _specularFront = specular;
176            //clampArray4BetweenRange(_specularFront,0.0f,1.0f,"Material::setSpecular(..)");
177            break;
178        case(BACK):
179            _specularFrontAndBack = false;
180            _specularBack = specular;
181            //clampArray4BetweenRange(_specularBack,0.0f,1.0f,"Material::setSpecular(..)");
182            break;
183        case(FRONT_AND_BACK):
184            _specularFrontAndBack = true;
185            _specularFront = specular;
186            //clampArray4BetweenRange(_specularFront,0.0f,1.0f,"Material::setSpecular(..)");
187            _specularBack = _specularFront;
188            break;
189        default:
190            OSG_NOTICE<<"Notice: invalid Face passed to Material::setSpecular()."<< std::endl;
191            break;
192    }
193}
194
195
196const Vec4& Material::getSpecular(Face face) const
197{
198    switch(face)
199    {
200        case(FRONT):
201            return _specularFront;
202        case(BACK):
203            return _specularBack;
204        case(FRONT_AND_BACK):
205            if (!_specularFrontAndBack)
206            {
207                OSG_NOTICE<<"Notice: Material::getSpecular(FRONT_AND_BACK) called on material "<< std::endl;
208                OSG_NOTICE<<"        with separate FRONT and BACK specular colors."<< std::endl;
209            }
210            return _specularFront;
211    }
212    OSG_NOTICE<<"Notice: invalid Face passed to Material::getSpecular()."<< std::endl;
213    return _specularFront;
214}
215
216
217void Material::setEmission(Face face, const Vec4& emission )
218{
219    switch(face)
220    {
221        case(FRONT):
222            _emissionFrontAndBack = false;
223            _emissionFront = emission;
224            //clampArray4BetweenRange(_emissionFront,0.0f,1.0f,"Material::setEmission(..)");
225            break;
226        case(BACK):
227            _emissionFrontAndBack = false;
228            _emissionBack = emission;
229            //clampArray4BetweenRange(_emissionBack,0.0f,1.0f,"Material::setEmission(..)");
230            break;
231        case(FRONT_AND_BACK):
232            _emissionFrontAndBack = true;
233            _emissionFront = emission;
234            //clampArray4BetweenRange(_emissionFront,0.0f,1.0f,"Material::setEmission(..)");
235            _emissionBack = _emissionFront;
236            break;
237        default:
238            OSG_NOTICE<<"Notice: invalid Face passed to Material::setEmission()."<< std::endl;
239            break;
240    }
241}
242
243
244const Vec4& Material::getEmission(Face face) const
245{
246    switch(face)
247    {
248        case(FRONT):
249            return _emissionFront;
250        case(BACK):
251            return _emissionBack;
252        case(FRONT_AND_BACK):
253            if (!_emissionFrontAndBack)
254            {
255                OSG_NOTICE<<"Notice: Material::getEmission(FRONT_AND_BACK) called on material "<< std::endl;
256                OSG_NOTICE<<"        with separate FRONT and BACK emission colors."<< std::endl;
257            }
258            return _emissionFront;
259    }
260    OSG_NOTICE<<"Notice: invalid Face passed to Material::getEmission()."<< std::endl;
261    return _emissionFront;
262}
263
264
265void Material::setShininess(Face face, float shininess )
266{
267    clampBetweenRange(shininess,0.0f,128.0f,"Material::setShininess()");
268
269    switch(face)
270    {
271        case(FRONT):
272            _shininessFrontAndBack = false;
273            _shininessFront = shininess;
274            break;
275        case(BACK):
276            _shininessFrontAndBack = false;
277            _shininessBack = shininess;
278            break;
279        case(FRONT_AND_BACK):
280            _shininessFrontAndBack = true;
281            _shininessFront = shininess;
282            _shininessBack = shininess;
283            break;
284        default:
285            OSG_NOTICE<<"Notice: invalid Face passed to Material::setShininess()."<< std::endl;
286            break;
287    }
288}
289
290
291float Material::getShininess(Face face) const
292{
293    switch(face)
294    {
295        case(FRONT):
296            return _shininessFront;
297        case(BACK):
298            return _shininessBack;
299        case(FRONT_AND_BACK):
300            if (!_shininessFrontAndBack)
301            {
302                OSG_NOTICE<<"Notice: Material::getShininess(FRONT_AND_BACK) called on material "<< std::endl;
303                OSG_NOTICE<<"        with separate FRONT and BACK shininess colors."<< std::endl;
304            }
305            return _shininessFront;
306    }
307    OSG_NOTICE<<"Notice: invalid Face passed to Material::getShininess()."<< std::endl;
308    return _shininessFront;
309}
310
311void Material::setTransparency(Face face,float transparency)
312{
313   //clampBetweenRange(transparency,0.0f,1.0f,"Material::setTransparency()");
314
315   if (face==FRONT || face==FRONT_AND_BACK)
316   {
317        _ambientFront[3] = 1.0f-transparency;
318        _diffuseFront[3] = 1.0f-transparency;
319        _specularFront[3] = 1.0f-transparency;
320        _emissionFront[3] = 1.0f-transparency;
321    }
322
323    if (face==BACK || face==FRONT_AND_BACK)
324    {
325        _ambientBack[3] = 1.0f-transparency;
326        _diffuseBack[3] = 1.0f-transparency;
327        _specularBack[3] = 1.0f-transparency;
328        _emissionBack[3] = 1.0f-transparency;
329    }
330}
331
332void Material::setAlpha(Face face,float alpha)
333{
334   clampBetweenRange(alpha,0.0f,1.0f,"Material::setAlpha()");
335
336   if (face==FRONT || face==FRONT_AND_BACK)
337   {
338        _ambientFront[3] = alpha;
339        _diffuseFront[3] = alpha;
340        _specularFront[3] = alpha;
341        _emissionFront[3] = alpha;
342    }
343
344    if (face==BACK || face==FRONT_AND_BACK)
345    {
346        _ambientBack[3] = alpha;
347        _diffuseBack[3] = alpha;
348        _specularBack[3] = alpha;
349        _emissionBack[3] = alpha;
350    }
351}
352
353void Material::apply(State&) const
354{
355#ifdef OSG_GL_FIXED_FUNCTION_AVAILABLE
356
357#ifdef OSG_GL1_AVAILABLE
358    if (_colorMode==OFF)
359    {
360        glDisable(GL_COLOR_MATERIAL);
361
362        glColor4fv(_diffuseFront.ptr());
363    }
364    else
365    {
366        glColorMaterial(GL_FRONT_AND_BACK,(GLenum)_colorMode);
367        glEnable(GL_COLOR_MATERIAL);
368        switch(_colorMode)
369        {
370            case(AMBIENT): glColor4fv(_ambientFront.ptr()); break;
371            case(DIFFUSE): glColor4fv(_diffuseFront.ptr()); break;
372            case(SPECULAR): glColor4fv(_specularFront.ptr()); break;
373            case(EMISSION): glColor4fv(_emissionFront.ptr()); break;
374            case(AMBIENT_AND_DIFFUSE): glColor4fv(_diffuseFront.ptr()); break;
375            case(OFF): break;
376        }
377    }
378#endif
379
380    if (_colorMode!=AMBIENT && _colorMode!=AMBIENT_AND_DIFFUSE)
381    {
382        if (_ambientFrontAndBack)
383        {
384            glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, _ambientFront.ptr() );
385        }
386        else
387        {
388            glMaterialfv( GL_FRONT, GL_AMBIENT, _ambientFront.ptr() );
389            glMaterialfv( GL_BACK, GL_AMBIENT, _ambientBack.ptr() );
390        }
391    }
392
393    if (_colorMode!=DIFFUSE && _colorMode!=AMBIENT_AND_DIFFUSE)
394    {
395        if (_diffuseFrontAndBack)
396        {
397            glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, _diffuseFront.ptr() );
398        }
399        else
400        {
401            glMaterialfv( GL_FRONT, GL_DIFFUSE, _diffuseFront.ptr() );
402            glMaterialfv( GL_BACK, GL_DIFFUSE, _diffuseBack.ptr() );
403        }
404    }
405
406    if (_colorMode!=SPECULAR)
407    {
408        if (_specularFrontAndBack)
409        {
410            glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, _specularFront.ptr() );
411        }
412        else
413        {
414            glMaterialfv( GL_FRONT, GL_SPECULAR, _specularFront.ptr() );
415            glMaterialfv( GL_BACK, GL_SPECULAR, _specularBack.ptr() );
416        }
417    }
418
419    if (_colorMode!=EMISSION)
420    {
421        if (_emissionFrontAndBack)
422        {
423            glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, _emissionFront.ptr() );
424        }
425        else
426        {
427            glMaterialfv( GL_FRONT, GL_EMISSION, _emissionFront.ptr() );
428            glMaterialfv( GL_BACK, GL_EMISSION, _emissionBack.ptr() );
429        }
430    }
431
432    if (_shininessFrontAndBack)
433    {
434        glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, _shininessFront );
435    }
436    else
437    {
438        glMaterialf( GL_FRONT, GL_SHININESS, _shininessFront );
439        glMaterialf( GL_BACK, GL_SHININESS, _shininessBack );
440    }
441#else
442    OSG_NOTICE<<"Warning: Material::apply(State&) - not supported."<<std::endl;
443#endif
444}
Note: See TracBrowser for help on using the browser.