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 FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more 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 \*---------------------------------------------------------------------------*/
40 #include "OSGBlendChunk.h"
41 #include "OSGMakeTransparentGraphOp.h"
42 #include "OSGPolygonChunk.h"
43 #include "OSGSimpleMaterial.h"
45 #include "OSGGraphOpFactory.h"
52 //! Register the GraphOp with the factory
53 static bool registerOp(void)
55 GraphOpRefPtr newOp
= MakeTransparentGraphOp::create();
57 GraphOpFactory::the()->registerOp(newOp
);
61 static OSG::StaticInitFuncWrapper
registerOpWrapper(registerOp
);
66 MakeTransparentGraphOp::MakeTransparentGraphOp(const char* name
)
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
))
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
¤tList
= mmIt
->second
;
109 MaterialObjectList::iterator i
= currentList
.begin();
111 for(; i
!= currentList
.end(); ++i
)
112 i
->setMaterial(newMaterial
);
120 void MakeTransparentGraphOp::setParams(const std::string params
)
124 ps("transparency", _transparency
);
126 std::string out
= ps
.getUnusedParams();
129 FWARNING(("MakeTransparentGraphOp doesn't have parameters '%s'.\n",
134 std::string
MakeTransparentGraphOp::usage(void)
137 "MakeTransparent: make used Materials transparent\n"
138 " Based on MaterialMergeGraphOp, merges Materials and sets their\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
))
149 if(isInPreserveList(node
))
150 return Action::Continue
;
152 MaterialDrawable
*md
= dynamic_cast<MaterialDrawable
*>(node
->getCore());
155 addObject(MaterialObject(md
));
156 return Action::Continue
;
159 MaterialGroup
*mg
= dynamic_cast<MaterialGroup
*>(node
->getCore());
162 addObject(MaterialObject(mg
));
163 return Action::Continue
;
166 // Otherwise, keep looking.
167 return Action::Continue
;
170 Action::ResultE
MakeTransparentGraphOp::traverseLeave(
172 Action::ResultE res
)
177 void MakeTransparentGraphOp::addObject(MaterialObject m
)
179 Material
*mat
= m
.getMaterial();
183 _materialMap
[mat
].push_back(m
);
187 // Generally used to prevent templated functions from resolving to the
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
);
209 chunk
= Chunk::create();
216 void MakeTransparentGraphOp::applyTransparency(Material
*m
)
218 SimpleMaterial
*sm
= dynamic_cast<SimpleMaterial
*>(m
);
221 std::cout
<< "SimpleMaterial" << std::endl
;
222 sm
->setTransparency(1.0f
- (1.0f
- sm
->getTransparency()) *
224 sm
->setColorMaterial(GL_NONE
);
226 PolygonChunk
*polygonChunk
= getOrAddChunk
<PolygonChunk
>(sm
);
227 polygonChunk
->setCullFace(GL_BACK
);
231 ChunkMaterial
*cm
= dynamic_cast<ChunkMaterial
*>(m
);
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
);