1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
46 #include "OSGConfig.h"
50 #include "OSGAction.h"
52 #include "OSGStateChunk.h"
54 #include "OSGMaterialChunk.h"
56 #include "OSGSimpleMaterial.h"
57 #include "OSGChunkBlock.h"
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 /***************************************************************************\
68 \***************************************************************************/
70 /*-------------------------------------------------------------------------*\
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 /*-------------------------------------------------------------------------*\
96 \*-------------------------------------------------------------------------*/
98 void SimpleMaterial::initMethod(InitPhase ePhase
)
100 Inherited::initMethod(ePhase
);
103 void SimpleMaterial::resolveLinks(void)
105 Inherited::resolveLinks();
107 _materialChunk
= NULL
;
112 /***************************************************************************\
114 \***************************************************************************/
116 /*-------------------------------------------------------------------------*\
118 \*-------------------------------------------------------------------------*/
120 /*------------- constructors & destructors --------------------------------*/
122 SimpleMaterial::SimpleMaterial(void) :
124 _materialChunk(NULL
),
129 SimpleMaterial::SimpleMaterial(const SimpleMaterial
&source
) :
131 _materialChunk(source
._materialChunk
),
132 _blendChunk (source
._blendChunk
)
136 SimpleMaterial::~SimpleMaterial(void)
140 void SimpleMaterial::changed(ConstFieldMaskArg whichField
,
144 Inherited::changed(whichField
, origin
, details
);
147 /*-------------------------- your_category---------------------------------*/
150 void SimpleMaterial::rebuildState(void)
155 Real32 alpha
= 1.f
- _sfTransparency
.getValue();
159 _pState
->clearChunks();
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
);
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
)
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() ?
226 State::AutoSlotReplace
;
228 StateChunk
*chunk
= (*chunks
)[i
];
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
;
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
;
291 indentLog(uiIndent
, PLOG
);
292 PLOG
<< "}" << std::endl
;