root/OpenSceneGraph/trunk/src/osgFX/MultiTextureControl.cpp @ 13041

Revision 13041, 5.5 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
14#include <osg/TexEnv>
15#include <osg/TexEnvCombine>
16
17#include <osgFX/MultiTextureControl>
18
19using namespace osg;
20using namespace osgFX;
21
22MultiTextureControl::MultiTextureControl()
23{
24}
25
26MultiTextureControl::MultiTextureControl(const MultiTextureControl& copy, const osg::CopyOp& copyop):
27    Group(copy,copyop),
28    _textureWeightList(copy._textureWeightList)
29{
30    updateStateSet();
31}
32
33void MultiTextureControl::setTextureWeight(unsigned int unit, float weight)
34{
35    if (unit >= _textureWeightList.size())
36    {
37        _textureWeightList.resize(unit+1,0.0f);
38    }
39    _textureWeightList[unit] = weight;
40
41    updateStateSet();
42}
43
44void MultiTextureControl::updateStateSet()
45{
46    osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
47
48    unsigned int numTextureUnitsOn = 0;
49    unsigned int unit;
50    for(unit=0;unit<_textureWeightList.size();++unit)
51    {
52        if (_textureWeightList[unit]>0.0f) ++numTextureUnitsOn;
53    }
54
55    if (numTextureUnitsOn<=1)
56    {
57        for(unit=0;unit<_textureWeightList.size();++unit)
58        {
59            if (_textureWeightList[unit]>0.0f)
60            {
61                osg::TexEnv* texenv = new osg::TexEnv(osg::TexEnv::MODULATE);
62                stateset->setTextureAttribute(unit, texenv);
63                stateset->setTextureMode(unit, GL_TEXTURE_2D, osg::StateAttribute::ON);
64            }
65            else
66            {
67                stateset->setTextureMode(unit, GL_TEXTURE_2D, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
68            }
69        }
70
71    }
72    else if (_textureWeightList.size()==2)
73    {
74        {
75            osg::TexEnvCombine* texenv = new osg::TexEnvCombine;
76            texenv->setCombine_RGB(osg::TexEnvCombine::INTERPOLATE);
77            texenv->setSource0_RGB(osg::TexEnvCombine::TEXTURE0+0);
78            texenv->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
79            texenv->setSource1_RGB(osg::TexEnvCombine::TEXTURE0+1);
80            texenv->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
81            texenv->setSource2_RGB(osg::TexEnvCombine::CONSTANT);
82            texenv->setOperand2_RGB(osg::TexEnvCombine::SRC_COLOR);
83
84            float r = _textureWeightList[0]/(_textureWeightList[0]+_textureWeightList[1]);
85            texenv->setConstantColor(osg::Vec4(r,r,r,r));
86
87            stateset->setTextureAttribute(0, texenv);
88        }
89
90        {
91            osg::TexEnvCombine* texenv = new osg::TexEnvCombine;
92            texenv->setCombine_RGB(osg::TexEnvCombine::MODULATE);
93            texenv->setSource0_RGB(osg::TexEnvCombine::PREVIOUS);
94            texenv->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
95            texenv->setSource1_RGB(osg::TexEnvCombine::PRIMARY_COLOR);
96            texenv->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
97
98            stateset->setTextureAttribute(1, texenv);
99        }
100    }
101    else if (_textureWeightList.size()==3)
102    {
103        float b = (_textureWeightList[0]+_textureWeightList[1])/(_textureWeightList[0]+_textureWeightList[1]+_textureWeightList[2]);
104        float a = _textureWeightList[0]/(_textureWeightList[0]+_textureWeightList[1]);
105
106        {
107            osg::TexEnvCombine* texenv = new osg::TexEnvCombine;
108            texenv->setCombine_RGB(osg::TexEnvCombine::INTERPOLATE);
109            texenv->setSource0_RGB(osg::TexEnvCombine::TEXTURE0+0);
110            texenv->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
111            texenv->setSource1_RGB(osg::TexEnvCombine::TEXTURE0+1);
112            texenv->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
113            texenv->setSource2_RGB(osg::TexEnvCombine::CONSTANT);
114            texenv->setOperand2_RGB(osg::TexEnvCombine::SRC_COLOR);
115
116            texenv->setConstantColor(osg::Vec4(a,a,a,a));
117
118            stateset->setTextureAttribute(0, texenv);
119        }
120
121        {
122            osg::TexEnvCombine* texenv = new osg::TexEnvCombine;
123            texenv->setCombine_RGB(osg::TexEnvCombine::INTERPOLATE);
124            texenv->setSource0_RGB(osg::TexEnvCombine::PREVIOUS);
125            texenv->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
126            texenv->setSource1_RGB(osg::TexEnvCombine::TEXTURE0+2);
127            texenv->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
128            texenv->setSource2_RGB(osg::TexEnvCombine::CONSTANT);
129            texenv->setOperand2_RGB(osg::TexEnvCombine::SRC_COLOR);
130
131            texenv->setConstantColor(osg::Vec4(b,b,b,b));
132
133            stateset->setTextureAttribute(1, texenv);
134        }
135
136        {
137            osg::TexEnvCombine* texenv = new osg::TexEnvCombine;
138            texenv->setCombine_RGB(osg::TexEnvCombine::MODULATE);
139            texenv->setSource0_RGB(osg::TexEnvCombine::PREVIOUS);
140            texenv->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
141            texenv->setSource1_RGB(osg::TexEnvCombine::PRIMARY_COLOR);
142            texenv->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
143
144            stateset->setTextureAttribute(2, texenv);
145        }
146    }
147
148    setStateSet(stateset.get());
149}
Note: See TracBrowser for help on using the browser.