fixed: let the material fill the override chunk block
[opensg.git] / Source / System / GraphOp / OSGMakeTransparentGraphOp.cpp
blobe8890c978fe4eab4948563341bffde05d7c5963d
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 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 \*---------------------------------------------------------------------------*/
40 #include "OSGBlendChunk.h"
41 #include "OSGMakeTransparentGraphOp.h"
42 #include "OSGPolygonChunk.h"
43 #include "OSGSimpleMaterial.h"
44 #include "OSGGLEXT.h"
45 #include "OSGGraphOpFactory.h"
47 OSG_USING_NAMESPACE
49 namespace
52 //! Register the GraphOp with the factory
53 static bool registerOp(void)
55 GraphOpRefPtr newOp = MakeTransparentGraphOp::create();
57 GraphOpFactory::the()->registerOp(newOp);
58 return true;
61 static OSG::StaticInitFuncWrapper registerOpWrapper(registerOp);
63 } // namespace
66 MakeTransparentGraphOp::MakeTransparentGraphOp(const char* name)
67 : GraphOp (name),
68 _transparency(0.5 )
72 MakeTransparentGraphOp::~MakeTransparentGraphOp(void)
76 MakeTransparentGraphOpTransitPtr
77 MakeTransparentGraphOp::create(void)
79 return MakeTransparentGraphOpTransitPtr(new MakeTransparentGraphOp);
82 GraphOpTransitPtr MakeTransparentGraphOp::clone(void)
84 return GraphOpTransitPtr(new MakeTransparentGraphOp);
87 bool MakeTransparentGraphOp::traverse(Node *node)
89 // Find the materials.
90 if(!GraphOp::traverse(node))
91 return false;
93 // Now do the merge.
94 MaterialObjectMap::iterator mmIt = _materialMap.begin();
95 for(; mmIt != _materialMap.end(); ++mmIt)
97 Material *oldMaterial = mmIt->first;
98 MaterialUnrecPtr newMaterial =
99 dynamic_pointer_cast<Material>(deepClone(oldMaterial));
101 if(newMaterial != NULL)
103 std::cout << "Applying transparency: ";
105 applyTransparency(newMaterial);
107 // Put the new material in the objects in this subtree.
108 MaterialObjectList &currentList = mmIt->second;
109 MaterialObjectList::iterator i = currentList.begin();
111 for(; i != currentList.end(); ++i)
112 i->setMaterial(newMaterial);
116 return true;
120 void MakeTransparentGraphOp::setParams(const std::string params)
122 ParamSet ps(params);
124 ps("transparency", _transparency);
126 std::string out = ps.getUnusedParams();
127 if(out.length())
129 FWARNING(("MakeTransparentGraphOp doesn't have parameters '%s'.\n",
130 out.c_str()));
134 std::string MakeTransparentGraphOp::usage(void)
136 return
137 "MakeTransparent: make used Materials transparent\n"
138 " Based on MaterialMergeGraphOp, merges Materials and sets their\n"
139 " transparency.\n"
140 "Params: name (type, default)\n"
141 " transparency (Real32, 0.5f): transparency value\n";
144 Action::ResultE MakeTransparentGraphOp::traverseEnter(Node * const node)
146 if(isInExcludeList(node))
147 return Action::Skip;
149 if(isInPreserveList(node))
150 return Action::Continue;
152 MaterialDrawable *md = dynamic_cast<MaterialDrawable *>(node->getCore());
153 if(md != NULL)
155 addObject(MaterialObject(md));
156 return Action::Continue;
159 MaterialGroup *mg = dynamic_cast<MaterialGroup *>(node->getCore());
160 if (mg != NULL)
162 addObject(MaterialObject(mg));
163 return Action::Continue;
166 // Otherwise, keep looking.
167 return Action::Continue;
170 Action::ResultE MakeTransparentGraphOp::traverseLeave(
171 Node * const node,
172 Action::ResultE res )
174 return res;
177 void MakeTransparentGraphOp::addObject(MaterialObject m)
179 Material *mat = m.getMaterial();
180 if (mat == NULL)
181 return;
183 _materialMap[mat].push_back(m);
187 // Generally used to prevent templated functions from resolving to the
188 // same signature.
189 /*! \nohierarchy
192 template<typename T>
193 struct Type2Type {
194 typedef T Original;
198 template<typename Chunk>
199 typename Chunk::ObjUnrecPtr getOrAddChunk(
200 ChunkMaterial *cm, Type2Type<Chunk> = Type2Type<Chunk>())
202 OSG::StateChunk *stateChunk = cm->find(Chunk::getClassType());
204 typename Chunk::ObjUnrecPtr chunk =
205 dynamic_cast<typename Chunk::ObjCPtr>(stateChunk);
207 if(!chunk)
209 chunk = Chunk::create();
210 cm->addChunk(chunk);
212 return chunk;
216 void MakeTransparentGraphOp::applyTransparency(Material *m)
218 SimpleMaterial *sm = dynamic_cast<SimpleMaterial *>(m);
219 if (sm != NULL)
221 std::cout << "SimpleMaterial" << std::endl;
222 sm->setTransparency(1.0f - (1.0f - sm->getTransparency()) *
223 _transparency);
224 sm->setColorMaterial(GL_NONE);
226 PolygonChunk *polygonChunk = getOrAddChunk<PolygonChunk>(sm);
227 polygonChunk->setCullFace(GL_BACK);
228 return;
231 ChunkMaterial *cm = dynamic_cast<ChunkMaterial *>(m);
232 if (cm != NULL)
234 std::cout << "ChunkMaterial" << std::endl;
235 BlendChunk *blendChunk = getOrAddChunk<BlendChunk>(cm);
236 blendChunk->setColor(Color4f(1.f, 1.f, 1.f, 1.f - _transparency));
237 blendChunk->setSrcFactor(GL_CONSTANT_ALPHA);
238 blendChunk->setDestFactor(GL_ONE_MINUS_CONSTANT_ALPHA);
239 return;