fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / State / Shader / SHL / OSGSimpleSHLVariableChunk.cpp
blob5316cc89ac0bd068ee32bfb525f381116efac438
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #include "OSGSimpleSHLVariableChunk.h"
50 #include "OSGShaderVariables.h"
52 OSG_BEGIN_NAMESPACE
54 // Documentation for this class is emitted in the
55 // OSGSimpleSHLVariableChunkBase.cpp file.
56 // To modify it, please change the .fcd file (OSGSimpleSHLVariableChunk.fcd) and
57 // regenerate the base file.
59 /***************************************************************************\
60 * Class variables *
61 \***************************************************************************/
63 #if 0
64 volatile UInt16 SimpleSHLVariableChunk::_uiChunkCounter = 1;
65 #endif
67 /***************************************************************************\
68 * Class methods *
69 \***************************************************************************/
71 void SimpleSHLVariableChunk::initMethod(InitPhase ePhase)
73 Inherited::initMethod(ePhase);
75 if(ePhase == TypeObject::SystemPost)
81 /***************************************************************************\
82 * Instance methods *
83 \***************************************************************************/
85 /*-------------------------------------------------------------------------*\
86 - private -
87 \*-------------------------------------------------------------------------*/
89 /*----------------------- constructors & destructors ----------------------*/
91 SimpleSHLVariableChunk::SimpleSHLVariableChunk(void) :
92 Inherited( ),
93 _uiChunkId(0)
97 SimpleSHLVariableChunk::SimpleSHLVariableChunk(
98 const SimpleSHLVariableChunk &source) :
100 Inherited(source),
101 _uiChunkId(0 )
105 SimpleSHLVariableChunk::~SimpleSHLVariableChunk(void)
109 void SimpleSHLVariableChunk::onCreate(const SimpleSHLVariableChunk *source)
111 Inherited::onCreate(source);
113 if(GlobalSystemState == Startup)
114 return;
116 _uiChunkId = ShaderExecutableVarChunk::_uiChunkCounter++;
119 void SimpleSHLVariableChunk::onCreateAspect(
120 const SimpleSHLVariableChunk *createAspect,
121 const SimpleSHLVariableChunk *source )
123 Inherited::onCreateAspect(createAspect, source);
125 _uiChunkId = createAspect->_uiChunkId;
128 const StateChunkClass *SimpleSHLVariableChunk::getClass(void) const
130 return ShaderExecutableVarChunk::getStaticClass();
133 UInt16 SimpleSHLVariableChunk::getChunkId(void)
135 return _uiChunkId;
138 /*----------------------------- class specific ----------------------------*/
140 void SimpleSHLVariableChunk::changed(ConstFieldMaskArg whichField,
141 UInt32 origin,
142 BitVector details)
144 if(0x0000 != (whichField & VariablesFieldMask))
146 ShaderProgramVariables *pVars = _sfVariables.getValue();
148 if(pVars != NULL)
150 if(details == 0x0001)
152 // be save reset all locations
154 if(pVars->getMFVariables()->size() == 0)
156 editMFVariableLocations()->clear();
158 else
160 editMFVariableLocations()->resize(
161 pVars->getMFVariables()->size(),
162 -1);
164 std::fill(editMFVariableLocations()->begin(),
165 editMFVariableLocations()->end (),
166 -1);
172 Inherited::changed(whichField, origin, details);
175 bool SimpleSHLVariableChunk::subUniformVariable(const Char8 *name)
177 if(_sfVariables.getValue() != NULL)
179 return _sfVariables.getValue()->subUniformVariable(name, NULL, NULL);
181 else
183 return false;
187 void SimpleSHLVariableChunk::clearUniformVariables(void)
189 if(_sfVariables.getValue() == NULL)
191 _sfVariables.getValue()->clearUniformVariables();
195 void SimpleSHLVariableChunk::dump( UInt32 ,
196 const BitVector ) const
198 SLOG << "Dump SimpleSHLVariableChunk NI" << std::endl;
201 void SimpleSHLVariableChunk::activate(DrawEnv *pEnv, UInt32)
203 updateVariables(pEnv);
206 void SimpleSHLVariableChunk::changeFrom(DrawEnv *pEnv,
207 StateChunk *pOther,
208 UInt32 )
210 SimpleSHLVariableChunk *pOld =
211 dynamic_cast<SimpleSHLVariableChunk *>(pOther);
213 if(pOld == NULL)
215 FWARNING(( "SimpleSHLChunk::changeFrom: caught non-SHLChunk!\n"));
216 return;
219 // SimpleSHLChunk didn't change so do nothing.
220 if(pOld == this)
221 return;
223 updateVariables(pEnv);
226 void SimpleSHLVariableChunk::deactivate(DrawEnv *, UInt32)
230 void SimpleSHLVariableChunk::updateVariables(DrawEnv *pEnv)
232 UInt32 uiProgram = pEnv->getActiveShader();
234 if(uiProgram == 0)
235 return;
237 const ShaderProgramVariables::MFVariablesType *pMFVars = NULL;
239 if(_sfVariables.getValue() != NULL)
241 pMFVars = _sfVariables.getValue()->getMFVariables();
244 if(pMFVars == NULL || pMFVars->size() == 0)
246 return;
249 MFInt32 &vVarLocations = *this->editMFVariableLocations();
251 OSG_ASSERT(pMFVars->size() == vVarLocations.size());
253 MFInt32::iterator mLocIt = vVarLocations.begin();
255 ShaderProgramVariables::MFVariablesType::const_iterator mVarIt =
256 pMFVars->begin();
257 ShaderProgramVariables::MFVariablesType::const_iterator mVarEnd =
258 pMFVars->end ();
260 bool warnUnknown = true;
262 for(; mVarIt != mVarEnd; ++mVarIt, ++mLocIt)
264 ShaderVariable *pVar = *mVarIt;
266 if(pVar == NULL)
267 continue;
269 osgUniformShaderVariableSwitch(pEnv, pVar,
270 *mLocIt, uiProgram, warnUnknown);
274 OSG_END_NAMESPACE