fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Material / Base / OSGSimpleMaterial.cpp
blobdf6ddc118ae1a0c35a2adfdf52e38e1cd1eaaa5c
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 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 forA PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License formore 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 "OSGGL.h"
50 #include "OSGAction.h"
52 #include "OSGStateChunk.h"
53 #include "OSGState.h"
54 #include "OSGMaterialChunk.h"
56 #include "OSGSimpleMaterial.h"
57 #include "OSGChunkBlock.h"
59 OSG_USING_NAMESPACE
61 // Documentation for this class is emited in the
62 // OSGSimpleMaterialBase.cpp file.
63 // To modify it, please change the .fcd file (OSGSimpleMaterial.fcd) and
64 // regenerate the base file.
66 /***************************************************************************\
67 * Class methods *
68 \***************************************************************************/
70 /*-------------------------------------------------------------------------*\
71 - protected -
72 \*-------------------------------------------------------------------------*/
74 /* Create the chunks needed by this Material, one for the material properties,
75 one for the optional transparency blending.
78 void SimpleMaterial::prepareLocalChunks(void)
80 if(_materialChunk == NULL)
82 _materialChunk = MaterialChunk::createLocal();
85 if(_blendChunk == NULL)
87 _blendChunk = BlendChunk ::createLocal();
89 _blendChunk->setSrcFactor (GL_SRC_ALPHA);
90 _blendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA);
94 /*-------------------------------------------------------------------------*\
95 - private -
96 \*-------------------------------------------------------------------------*/
98 void SimpleMaterial::initMethod(InitPhase ePhase)
100 Inherited::initMethod(ePhase);
103 void SimpleMaterial::resolveLinks(void)
105 Inherited::resolveLinks();
107 _materialChunk = NULL;
109 _blendChunk = NULL;
112 /***************************************************************************\
113 * Instance methods *
114 \***************************************************************************/
116 /*-------------------------------------------------------------------------*\
117 - private -
118 \*-------------------------------------------------------------------------*/
120 /*------------- constructors & destructors --------------------------------*/
122 SimpleMaterial::SimpleMaterial(void) :
123 Inherited ( ),
124 _materialChunk(NULL),
125 _blendChunk (NULL)
129 SimpleMaterial::SimpleMaterial(const SimpleMaterial &source) :
130 Inherited (source ),
131 _materialChunk(source._materialChunk),
132 _blendChunk (source._blendChunk )
136 SimpleMaterial::~SimpleMaterial(void)
140 void SimpleMaterial::changed(ConstFieldMaskArg whichField,
141 UInt32 origin,
142 BitVector details)
144 Inherited::changed(whichField, origin, details);
147 /*-------------------------- your_category---------------------------------*/
150 void SimpleMaterial::rebuildState(void)
152 Color3f v3;
153 Color4f v4;
155 Real32 alpha = 1.f - _sfTransparency.getValue();
157 if(_pState != NULL)
159 _pState->clearChunks();
161 else
163 _pState = State::createLocal();
165 _pState->setDefaultSortKey(this->getId());
168 prepareLocalChunks();
170 v3 = _sfAmbient.getValue();
171 v4.setValuesRGBA(v3[0], v3[1], v3[2], alpha);
173 _materialChunk->setAmbient(v4);
175 v3 = _sfDiffuse.getValue();
176 v4.setValuesRGBA(v3[0], v3[1], v3[2], alpha);
178 _materialChunk->setDiffuse(v4);
180 v3 = _sfSpecular.getValue();
181 v4.setValuesRGBA(v3[0], v3[1], v3[2], alpha);
183 _materialChunk->setSpecular(v4);
185 _materialChunk->setShininess(_sfShininess.getValue());
187 v3 = _sfEmission.getValue();
188 v4.setValuesRGBA(v3[0], v3[1], v3[2], alpha);
190 _materialChunk->setEmission(v4);
192 _materialChunk->setLit (_sfLit .getValue());
193 _materialChunk->setColorMaterial(_sfColorMaterial.getValue());
195 _pState->addChunk(_materialChunk);
197 if(isTransparent())
199 _pState->addChunk(_blendChunk);
202 // XXX DR This is a hack. Should call Inherited ?? GV
203 Inherited::addChunks(_pState);
206 bool SimpleMaterial::isTransparent(void) const
208 return ((getTransparency() > TypeTraits<Real32>::getDefaultEps()) ||
209 (Inherited::isTransparent() ) );
212 void SimpleMaterial::fill(ChunkBlock *pBlock)
214 if(pBlock == NULL)
215 return;
217 pBlock->clearChunks();
219 const MFUnrecStateChunkPtr *chunks = this->getMFChunks();
220 const MFInt32 *slots = this->getMFSlots ();
222 for(SizeT i = 0; i < chunks->size(); ++i)
224 int slot = i < slots->size() ?
225 (*slots)[i] :
226 State::AutoSlotReplace;
228 StateChunk *chunk = (*chunks)[i];
230 if(chunk != NULL)
231 pBlock->addChunk(chunk, slot);
234 if(_materialChunk != NULL)
236 pBlock->addChunk(_materialChunk);
239 if(_blendChunk != NULL && this->isTransparent() == true)
241 pBlock->addChunk(_blendChunk);
245 /*------------------------------- dump ----------------------------------*/
247 void SimpleMaterial::dump( UInt32 uiIndent,
248 const BitVector OSG_CHECK_ARG(bvFlags )) const
250 indentLog(uiIndent, PLOG);
251 PLOG << "SimpleMaterial at " << this << std::endl;
253 indentLog(uiIndent, PLOG);
254 PLOG << "{" << std::endl;
256 uiIndent += 4;
258 indentLog(uiIndent, PLOG);
259 PLOG << "\tambient: " << getAmbient() << std::endl;
261 indentLog(uiIndent, PLOG);
262 PLOG << "\tdiffuse: " << getDiffuse() << std::endl;
264 indentLog(uiIndent, PLOG);
265 PLOG << "\tspecular: " << getSpecular() << std::endl;
267 indentLog(uiIndent, PLOG);
268 PLOG << "\tshininess: " << getShininess() << std::endl;
270 indentLog(uiIndent, PLOG);
271 PLOG << "\temission: " << getEmission() << std::endl;
273 indentLog(uiIndent, PLOG);
274 PLOG << "\ttransparency: " << getTransparency() << std::endl;
276 indentLog(uiIndent, PLOG);
277 PLOG << "\tlit: " << getLit() << std::endl;
279 indentLog(uiIndent, PLOG);
280 PLOG << "\tChunks: " << std::endl;
282 for(MFUnrecStateChunkPtr::const_iterator i = _mfChunks.begin();
283 i != _mfChunks.end(); ++i)
285 indentLog(uiIndent, PLOG);
286 PLOG << "\t" << *i << std::endl;
289 uiIndent -= 4;
291 indentLog(uiIndent, PLOG);
292 PLOG << "}" << std::endl;