38 #ifdef G4VIS_BUILD_OI_DRIVER
46 #include <Inventor/SbBox.h>
47 #include <Inventor/fields/SoSFFloat.h>
48 #include <Inventor/misc/SoChildList.h>
49 #include <Inventor/nodes/SoSeparator.h>
50 #include <Inventor/nodes/SoCube.h>
51 #include <Inventor/nodes/SoScale.h>
52 #include <Inventor/actions/SoAction.h>
53 #include <Inventor/nodes/SoIndexedFaceSet.h>
54 #include <Inventor/SoPrimitiveVertex.h>
55 #include <Inventor/elements/SoTextureCoordinateElement.h>
63 SO_NODE_CONSTRUCTOR(SoBox);
66 SO_NODE_ADD_FIELD(fDx, (1.0));
67 SO_NODE_ADD_FIELD(fDy, (1.0));
68 SO_NODE_ADD_FIELD(fDz, (1.0));
69 SO_NODE_ADD_FIELD(alternateRep, (NULL));
70 children =
new SoChildList(
this);
82 SO_NODE_INIT_CLASS(SoBox,SoShape,
"Shape");
92 SoState *state = action->getState();
96 SbBool useTexFunction=
97 (SoTextureCoordinateElement::getType(state) ==
98 SoTextureCoordinateElement::FUNCTION);
103 const SoTextureCoordinateElement *tce = NULL;
105 if (useTexFunction) {
106 tce = SoTextureCoordinateElement::getInstance(state);
112 SbVec3f point, normal;
117 #define GEN_VERTEX(pv,x,y,z,s,t,nx,ny,nz) \
118 point.setValue(x,y,z); \
119 normal.setValue(nx,ny,nz); \
120 if (useTexFunction) { \
121 texCoord=tce->get(point,normal); \
127 pv.setPoint(point); \
128 pv.setNormal(normal); \
129 pv.setTextureCoords(texCoord); \
134 const int NPOINTS=8, NFACES=6, NINDICES = NFACES*5;
135 int indices[NINDICES] = {3,2,1,0, SO_END_FACE_INDEX,
136 4,5,6,7, SO_END_FACE_INDEX,
137 0,1,5,4, SO_END_FACE_INDEX,
138 1,2,6,5, SO_END_FACE_INDEX,
139 2,3,7,6, SO_END_FACE_INDEX,
140 3,0,4,7, SO_END_FACE_INDEX};
144 float points[NPOINTS][3];
145 points[0][0] =
fDx.getValue();
146 points[0][1] =
fDy.getValue();
147 points[0][2] = -
fDz.getValue();
149 points[1][0] = -
fDx.getValue();
150 points[1][1] =
fDy.getValue();
151 points[1][2] = -
fDz.getValue();
153 points[2][0] = -
fDx.getValue();
154 points[2][1] = -
fDy.getValue();
155 points[2][2] = -
fDz.getValue();
157 points[3][0] =
fDx.getValue();
158 points[3][1] = -
fDy.getValue();
159 points[3][2] = -
fDz.getValue();
161 points[4][0] =
fDx.getValue();
162 points[4][1] =
fDy.getValue();
163 points[4][2] =
fDz.getValue();
165 points[5][0] = -
fDx.getValue();
166 points[5][1] =
fDy.getValue();
167 points[5][2] =
fDz.getValue();
169 points[6][0] = -
fDx.getValue();
170 points[6][1] = -
fDy.getValue();
171 points[6][2] =
fDz.getValue();
173 points[7][0] =
fDx.getValue();
174 points[7][1] = -
fDy.getValue();
175 points[7][2] =
fDz.getValue();
177 float normals[NFACES][3];
179 normals[0][0] = 0 ; normals[0][1] = 0; normals [0][2] = -1;
181 normals[1][0] = 0 ; normals[1][1] = 0; normals [1][2] = 1;
183 normals[2][0] = 0 ; normals[2][1] = 1; normals [2][2] = 0;
185 normals[3][0] = -1 ; normals[3][1] = 0; normals [3][2] = 0;
187 normals[4][0] = 0 ; normals[4][1] = -1; normals [4][2] = 0;
189 normals[5][0] = 1 ; normals[5][1] = 0; normals [5][2] = 0;
193 for (
int nf=0;nf<NFACES;nf++) {
194 beginShape(action,TRIANGLE_FAN);
195 index = indices[nf * 5];
196 x = points[
index][0];
197 y = points[
index][1];
198 z = points[
index][2];
199 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
200 index = indices[nf * 5 + 1];
201 x = points[
index][0];
202 y = points[
index][1];
203 z = points[
index][2];
204 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
205 index = indices[nf * 5 + 2];
206 x = points[
index][0];
207 y = points[
index][1];
208 z = points[
index][2];
209 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
210 index = indices[nf * 5 + 3];
211 x = points[
index][0];
212 y = points[
index][1];
213 z = points[
index][2];
214 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
227 SbVec3f vmin(-
fDx.getValue(),-
fDy.getValue(),-
fDz.getValue()),
228 vmax(
fDx.getValue(),
fDy.getValue(),
fDz.getValue());
229 center.setValue(0,0,0);
230 box.setBounds(vmin,vmax);
237 void SoBox::updateChildren() {
242 assert(children->getLength()==1);
243 SoSeparator *sep = (SoSeparator *) ( *children)[0];
244 SoScale *
scale = (SoScale *)( sep->getChild(0));
246 scale->scaleFactor.setValue(
fDx.getValue(),
fDy.getValue(),
fDz.getValue());
250 void SoBox::generateChildren() {
255 assert(children->getLength() ==0);
256 SoSeparator *sep =
new SoSeparator();
257 SoScale *scale =
new SoScale();
258 SoCube *cube =
new SoCube();
260 sep->addChild(scale);
262 children->append(sep);
271 if (children->getLength() == 0) generateChildren();
virtual SoChildList * getChildren() const
GetChildList, required whenever the class has hidden children.
virtual void computeBBox(SoAction *action, SbBox3f &box, SbVec3f ¢er)
compute bounding Box, required
SoSFFloat fDy
Half-length along Y.
static void initClass()
Class Initializer, required.
virtual void generateAlternateRep()
SoSFNode alternateRep
Alternate rep - for use by users without HEPVis shared objects.
virtual void generatePrimitives(SoAction *action)
Generate Primitives, required.
SoSFFloat fDz
Half-length along Z.
virtual ~SoBox()
Destructor, required.
virtual void clearAlternateRep()
We better be able to clear it, too!
SoSFFloat fDx
Half-length along X.