Debian Codegraph @

apps/tools/ivfix/IfHolder.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/nodes/SoCoordinate3.h>
38 #include <Inventor/nodes/SoIndexedFaceSet.h>
39 #include <Inventor/nodes/SoIndexedShape.h>
40 #include <Inventor/nodes/SoIndexedTriangleStripSet.h>
41 #include <Inventor/nodes/SoMaterial.h>
42 #include <Inventor/nodes/SoMaterialBinding.h>
43 #include <Inventor/nodes/SoNormal.h>
44 #include <Inventor/nodes/SoNormalBinding.h>
45 #include <Inventor/nodes/SoSeparator.h>
46 #include <Inventor/nodes/SoTextureCoordinate2.h>
47 #include <Inventor/nodes/SoTextureCoordinateBinding.h>
48
49 #include "IfHolder.h"
50
51 /////////////////////////////////////////////////////////////////////////////
52 //
53 // Constructor.
54 //
55 /////////////////////////////////////////////////////////////////////////////
56
57 IfHolder::IfHolder(SoNode *graphRoot, SbBool _doStrips,
58 SbBool _doNormals, SbBool _doTexCoords)
59 {
60 origRoot = graphRoot;
61 origRoot->ref();
62
63 doStrips = _doStrips;
64 doNormals = _doNormals;
65 doTexCoords = _doTexCoords;
66
67 root = new SoSeparator(7);
68 root->ref();
69
70 coords = new SoCoordinate3;
71 root->addChild(coords);
72
73 materialBinding = new SoMaterialBinding;
74 root->addChild(materialBinding);
75
76 if (doNormals) {
77 normals = new SoNormal;
78 normalBinding = new SoNormalBinding;
79 root->addChild(normals);
80 root->addChild(normalBinding);
81 }
82 else {
83 normals = NULL;
84 normalBinding = NULL;
85 }
86
87 if (doTexCoords) {
88 texCoords = new SoTextureCoordinate2;
89 texCoordBinding = new SoTextureCoordinateBinding;
90 root->addChild(texCoords);
91 root->addChild(texCoordBinding);
92 }
93 else {
94 texCoords = NULL;
95 texCoordBinding = NULL;
96 }
97
98 if (doStrips) {
99 stripSet = new SoIndexedTriangleStripSet;
100 faceSet = NULL;
101 triSet = stripSet;
102 }
103 else {
104 stripSet = NULL;
105 faceSet = new SoIndexedFaceSet;
106 triSet = faceSet;
107 }
108
109 root->addChild(triSet);
110 }
111
112 /////////////////////////////////////////////////////////////////////////////
113 //
114 // Destructor.
115 //
116 /////////////////////////////////////////////////////////////////////////////
117
118 IfHolder::~IfHolder()
119 {
120 origRoot->unref();
121 root->unref();
122 }
123
124 /////////////////////////////////////////////////////////////////////////////
125 //
126 // Converts the scene graph to use an SoVertexProperty node for
127 // the properties. The given material is used in case the
128 // materials need to be copied into the SoVertexProperty node.
129 //
130 /////////////////////////////////////////////////////////////////////////////
131
132 void
133 IfHolder::convertToVertexProperty(SoMaterial *mtl)
134 {
135 // Create a new SoVertexProperty node
136 SoVertexProperty *vp = new SoVertexProperty;
137 vp->ref();
138
139 // Copy the field values from the property nodes
140 vp->vertex = coords->point;
141 if (doNormals) {
142 vp->normal = normals->vector;
143 vp->normalBinding = normalBinding->value;
144 }
145 if (doTexCoords)
146 vp->texCoord = texCoords->point;
147 vp->materialBinding = materialBinding->value;
148
149 // Copy the materials only if there are materials and they are not overall
150 if (mtl != NULL &&
151 materialBinding->value.getValue() != SoMaterialBinding::OVERALL) {
152
153 // Need to pack the diffuse colors and transparency values
154 // into the orderedRGBA field
155 int num = mtl->diffuseColor.getNum();
156 int numT = mtl->transparency.getNum();
157 vp->orderedRGBA.setNum(num);
158 uint32_t *rgba = vp->orderedRGBA.startEditing();
159 const SbColor *d = mtl->diffuseColor.getValues(0);
160 const float *t = mtl->transparency.getValues(0);
161 for (int i = 0; i < num; i++)
162 rgba[i] = d[i].getPackedValue(numT == 1 ? t[0] : t[i]);
163 vp->orderedRGBA.finishEditing();
164 }
165
166 // Store the SoVertexProperty node in the shape
167 triSet->vertexProperty = vp;
168 vp->unref();
169
170 // Remove the old property nodes from the graph, leaving just the
171 // shape
172 triSet->ref();
173 root->removeAllChildren();
174 root->addChild(triSet);
175 triSet->unref();
176 }