Debian Codegraph @

apps/tools/ivfix/IfReplacer.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 #include <Inventor/actions/SoSearchAction.h>
38 #include <Inventor/elements/SoLazyElement.h>
39 #include <Inventor/nodes/SoBaseColor.h>
40 #include <Inventor/nodes/SoGroup.h>
41 #include <Inventor/nodes/SoMaterial.h>
42 #include <Inventor/nodes/SoPackedColor.h>
43
44 #include "IfAssert.h"
45 #include "IfReplacer.h"
46
47 /////////////////////////////////////////////////////////////////////////////
48 //
49 // Constructor.
50 //
51 /////////////////////////////////////////////////////////////////////////////
52
53 IfReplacer::IfReplacer()
54 {
55 }
56
57 /////////////////////////////////////////////////////////////////////////////
58 //
59 // Destructor.
60 //
61 /////////////////////////////////////////////////////////////////////////////
62
63 IfReplacer::~IfReplacer()
64 {
65 }
66
67 /////////////////////////////////////////////////////////////////////////////
68 //
69 // Replaces nodes in the given scene, in place.
70 //
71 /////////////////////////////////////////////////////////////////////////////
72
73 void
74 IfReplacer::replace(SoNode *sceneRoot)
75 {
76 // Replace all SoBaseColor and SoPackedColor nodes with SoMaterial
77 // nodes, which are easier to deal with
78 replaceMaterials(sceneRoot, SoBaseColor::getClassTypeId());
79 replaceMaterials(sceneRoot, SoPackedColor::getClassTypeId());
80 }
81
82 /////////////////////////////////////////////////////////////////////////////
83 //
84 // Replaces all nodes of the given type with SoMaterial nodes.
85 //
86 /////////////////////////////////////////////////////////////////////////////
87
88 void
89 IfReplacer::replaceMaterials(SoNode *sceneRoot, const SoType &typeToReplace)
90 {
91 // Find all nodes of the given type
92 SoSearchAction sa;
93 sa.setType(typeToReplace);
94 sa.setInterest(SoSearchAction::ALL);
95 sa.apply(sceneRoot);
96
97 // Replace the tail of each path with a material. To do this, we
98 // need to apply an SoCallbackAction to the path to gather the
99 // material components.
100 for (int i = 0; i < sa.getPaths().getLength(); i++) {
101
102 // Cast the path to a full path, just in case
103 SoFullPath *path = (SoFullPath *) sa.getPaths()[i];
104 ASSERT(path->getTail()->isOfType(typeToReplace));
105
106 // The path better have at least one group above the material
107 if (path->getLength() < 2 ||
108 ! path->getNodeFromTail(1)->isOfType(SoGroup::getClassTypeId()))
109 continue;
110
111 // Create a material node that represents the material in
112 // effect at the tail of the path
113 SoMaterial *newMaterial = createMaterialForPath(path);
114 newMaterial->ref();
115
116 // Replace the tail node with that material
117 SoGroup *parent = (SoGroup *) path->getNodeFromTail(1);
118 parent->replaceChild(path->getTail(), newMaterial);
119
120 newMaterial->unref();
121 }
122 }
123
124 /////////////////////////////////////////////////////////////////////////////
125 //
126 // Creates and returns a material node that represents the material in
127 // effect at the tail of the given path.
128 //
129 /////////////////////////////////////////////////////////////////////////////
130
131 SoMaterial *
132 IfReplacer::createMaterialForPath(SoPath *path)
133 {
134 material = NULL;
135
136 // Apply an SoCallbackAction to the path, and create the material
137 // after we get to the tail node
138 SoCallbackAction cba;
139 cba.addPostTailCallback(materialTailCB, this);
140 cba.apply(path);
141
142 ASSERT(material != NULL);
143
144 return material;
145 }
146
147 /////////////////////////////////////////////////////////////////////////////
148 //
149 // Stores a material created during traversal of a path.
150 //
151 /////////////////////////////////////////////////////////////////////////////
152
153 void
154 IfReplacer::storeMaterial(SoCallbackAction *cba)
155 {
156 // Create a new material
157 material = new SoMaterial;
158
159 // Copy the values from the elements in the state into the
160
161 SoState *state = cba->getState();
162 SoLazyElement *elt = SoLazyElement::getInstance(cba->getState());
163
164 // Most values can have only one value
165 material->ambientColor = SoLazyElement::getAmbient(state);
166 material->specularColor = SoLazyElement::getSpecular(state);
167 material->emissiveColor = SoLazyElement::getEmissive(state);
168 material->shininess = SoLazyElement::getShininess(state);
169
170 // If the element contains packed colors, unpack each one into a
171 // diffuse color and a transparency value. If they are not packed,
172 // set the diffuse colors and transparency values separately.
173
174 if (elt->isPacked()) {
175 int num = elt->getNumDiffuse();
176 material->diffuseColor.setNum(num);
177 material->transparency.setNum(num);
178 SbColor *colors = material->diffuseColor.startEditing();
179 float *trans = material->transparency.startEditing();
180 const uint32_t *eltPacked = elt->getPackedPointer();
181 for (int i = 0; i < num; i++)
182 colors[i].setPackedValue(eltPacked[i], trans[i]);
183 material->diffuseColor.finishEditing();
184 material->transparency.finishEditing();
185 }
186
187 else {
188 int i, num = elt->getNumDiffuse();
189 material->diffuseColor.setNum(num);
190 SbColor *colors = material->diffuseColor.startEditing();
191 const SbColor *eltColors = elt->getDiffusePointer();
192 for (i = 0; i < num; i++)
193 colors[i] = eltColors[i];
194 material->diffuseColor.finishEditing();
195
196 num = elt->getNumTransparencies();
197 material->transparency.setNum(num);
198 float *trans = material->transparency.startEditing();
199 const float *eltTrans = elt->getTransparencyPointer();
200 for (i = 0; i < num; i++)
201 trans[i] = eltTrans[i];
202 material->transparency.finishEditing();
203 }
204 }