38 #ifdef G4VIS_BUILD_OI_DRIVER
45 #include <Inventor/SbBox.h>
46 #include <Inventor/actions/SoGLRenderAction.h>
47 #include <Inventor/actions/SoAction.h>
48 #include <Inventor/fields/SoSFFloat.h>
49 #include <Inventor/misc/SoChildList.h>
50 #include <Inventor/nodes/SoSeparator.h>
51 #include <Inventor/nodes/SoIndexedFaceSet.h>
52 #include <Inventor/nodes/SoNormal.h>
53 #include <Inventor/nodes/SoCoordinate3.h>
54 #include <Inventor/nodes/SoNormalBinding.h>
55 #include <Inventor/SoPrimitiveVertex.h>
56 #include <Inventor/elements/SoTextureCoordinateElement.h>
66 SO_NODE_CONSTRUCTOR(SoTrap);
69 SO_NODE_ADD_FIELD(pDz, (1.0));
70 SO_NODE_ADD_FIELD(pTheta, (0.0));
71 SO_NODE_ADD_FIELD(pPhi, (0.0));
72 SO_NODE_ADD_FIELD(pDy1, (1.0));
73 SO_NODE_ADD_FIELD(pDx1, (1.0));
74 SO_NODE_ADD_FIELD(pDx2, (1.0));
75 SO_NODE_ADD_FIELD(pDy2, (1.0));
76 SO_NODE_ADD_FIELD(pDx3, (1.0));
77 SO_NODE_ADD_FIELD(pDx4, (1.0));
78 SO_NODE_ADD_FIELD(pAlp1, (0.0));
79 SO_NODE_ADD_FIELD(pAlp2, (0.0));
80 SO_NODE_ADD_FIELD(alternateRep, (NULL));
81 children =
new SoChildList(
this);
93 SO_NODE_INIT_CLASS(SoTrap,SoShape,
"Shape");
100 SoPrimitiveVertex pv;
103 SoState *state = action->getState();
107 SbBool useTexFunction=
108 (SoTextureCoordinateElement::getType(state) ==
109 SoTextureCoordinateElement::FUNCTION);
114 const SoTextureCoordinateElement *tce = NULL;
116 if (useTexFunction) {
117 tce = SoTextureCoordinateElement::getInstance(state);
128 #define GEN_VERTEX(pv,x,y,z,s,t,nx,ny,nz) \
129 point.setValue(x,y,z); \
130 normal.setValue(nx,ny,nz); \
131 if (useTexFunction) { \
132 texCoord=tce->get(point,normal); \
138 pv.setPoint(point); \
139 pv.setNormal(normal); \
140 pv.setTextureCoords(texCoord); \
145 const int NPOINTS=8, NFACES=6, NINDICES = NFACES*5;
146 int indices[NINDICES] = {3,2,1,0, SO_END_FACE_INDEX,
147 4,5,6,7, SO_END_FACE_INDEX,
148 0,1,5,4, SO_END_FACE_INDEX,
149 1,2,6,5, SO_END_FACE_INDEX,
150 2,3,7,6, SO_END_FACE_INDEX,
151 3,0,4,7, SO_END_FACE_INDEX};
159 float points[NPOINTS][3];
160 points[0][0] =
pDx2.getValue()+
pDy1.getValue()*Talp1;
161 points[0][1] =
pDy1.getValue();
162 points[0][2] = -
pDz.getValue();
164 points[1][0] = -
pDx2.getValue()+
pDy1.getValue()*Talp1;
165 points[1][1] =
pDy1.getValue();
166 points[1][2] = -
pDz.getValue();
168 points[2][0] = -
pDx1.getValue()-
pDy1.getValue()*Talp1;
169 points[2][1] = -
pDy1.getValue();
170 points[2][2] = -
pDz.getValue();
172 points[3][0] =
pDx1.getValue()-
pDy1.getValue()*Talp1;
173 points[3][1] = -
pDy1.getValue();
174 points[3][2] = -
pDz.getValue();
176 points[4][0] =
pDx4.getValue()+
pDy2.getValue()*Talp2;
177 points[4][1] =
pDy2.getValue();
178 points[4][2] =
pDz.getValue();
180 points[5][0] = -
pDx4.getValue()+
pDy2.getValue()*Talp2;
181 points[5][1] =
pDy2.getValue();
182 points[5][2] =
pDz.getValue();
184 points[6][0] = -
pDx3.getValue()-
pDy2.getValue()*Talp2;
185 points[6][1] = -
pDy2.getValue();
186 points[6][2] =
pDz.getValue();
188 points[7][0] =
pDx3.getValue()-
pDy2.getValue()*Talp2;
189 points[7][1] = -
pDy2.getValue();
190 points[7][2] =
pDz.getValue();
194 points[i][0] -=
pDz.getValue()*TthetaCphi;
195 points[i][1] -=
pDz.getValue()*TthetaSphi;
198 points[i][0] +=
pDz.getValue()*TthetaCphi;
199 points[i][1] +=
pDz.getValue()*TthetaSphi;
202 SbVec3f normals[NFACES];
204 for (nf=0;nf<NFACES;nf++) {
205 int j0 = indices[5*nf + 0];
206 int j1 = indices[5*nf + 1];
207 int j2 = indices[5*nf + 2];
208 SbVec3f p0(points[j0][0],points[j0][1],points[j0][2]);
209 SbVec3f p1(points[j1][0],points[j1][1],points[j1][2]);
210 SbVec3f p2(points[j2][0],points[j2][1],points[j2][2]);
211 normals[nf] = (p1-p0).
cross(p2-p0);
212 normals[nf].normalize();
217 for (nf=0;nf<NFACES;nf++) {
218 beginShape(action,TRIANGLE_FAN);
219 index = indices[nf * 5];
220 x = points[index][0];
221 y = points[index][1];
222 z = points[index][2];
223 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
224 index = indices[nf * 5 + 1];
225 x = points[index][0];
226 y = points[index][1];
227 z = points[index][2];
228 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
229 index = indices[nf * 5 + 2];
230 x = points[index][0];
231 y = points[index][1];
232 z = points[index][2];
233 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
234 index = indices[nf * 5 + 3];
235 x = points[index][0];
236 y = points[index][1];
237 z = points[index][2];
238 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
251 float pDx=
pDx1.getValue(),pDy=
pDy1.getValue();
253 if (
pDx2.getValue() > pDx) pDx =
pDx2.getValue();
254 if (
pDx3.getValue() > pDx) pDx =
pDx3.getValue();
255 if (
pDx4.getValue() > pDx) pDx =
pDx4.getValue();
256 if (
pDy2.getValue() > pDy) pDy =
pDy2.getValue();
261 if (Xalp< Xalp2) Xalp=Xalp2;
262 pDx +=
FFABS(TthetaCphi*
pDz.getValue());
264 pDy +=
FFABS(TthetaSphi*
pDz.getValue());
267 center.setValue(0,0,0);
268 box.setBounds(SbVec3f(-pDx,-pDy,-
pDz.getValue()),
269 SbVec3f( pDx, pDy,
pDz.getValue()));
282 SoSeparator *sep = (SoSeparator *) ( *
children)[0];
283 SoCoordinate3 *theCoordinates = (SoCoordinate3 *) ( sep->getChild(0));
284 SoNormal *theNormals = (SoNormal *) ( sep->getChild(1));
285 SoNormalBinding *theNormalBinding = (SoNormalBinding *) ( sep->getChild(2));
286 SoIndexedFaceSet *theFaceSet = (SoIndexedFaceSet *) ( sep->getChild(3));
288 const int NPOINTS=8, NFACES=6, NINDICES = NFACES*5;
289 float points[NPOINTS][3];
296 indices[NINDICES] = {3,2,1,0, SO_END_FACE_INDEX,
297 4,5,6,7, SO_END_FACE_INDEX,
298 0,1,5,4, SO_END_FACE_INDEX,
299 1,2,6,5, SO_END_FACE_INDEX,
300 2,3,7,6, SO_END_FACE_INDEX,
301 3,0,4,7, SO_END_FACE_INDEX};
310 points[0][0] =
pDx2.getValue()+
pDy1.getValue()*Talp1;
311 points[0][1] =
pDy1.getValue();
312 points[0][2] = -
pDz.getValue();
314 points[1][0] = -
pDx2.getValue()+
pDy1.getValue()*Talp1;
315 points[1][1] =
pDy1.getValue();
316 points[1][2] = -
pDz.getValue();
318 points[2][0] = -
pDx1.getValue()-
pDy1.getValue()*Talp1;
319 points[2][1] = -
pDy1.getValue();
320 points[2][2] = -
pDz.getValue();
322 points[3][0] =
pDx1.getValue()-
pDy1.getValue()*Talp1;
323 points[3][1] = -
pDy1.getValue();
324 points[3][2] = -
pDz.getValue();
326 points[4][0] =
pDx4.getValue()+
pDy2.getValue()*Talp2;
327 points[4][1] =
pDy2.getValue();
328 points[4][2] =
pDz.getValue();
330 points[5][0] = -
pDx4.getValue()+
pDy2.getValue()*Talp2;
331 points[5][1] =
pDy2.getValue();
332 points[5][2] =
pDz.getValue();
334 points[6][0] = -
pDx3.getValue()-
pDy2.getValue()*Talp2;
335 points[6][1] = -
pDy2.getValue();
336 points[6][2] =
pDz.getValue();
338 points[7][0] =
pDx3.getValue()-
pDy2.getValue()*Talp2;
339 points[7][1] = -
pDy2.getValue();
340 points[7][2] =
pDz.getValue();
344 points[i][0] -=
pDz.getValue()*TthetaCphi;
345 points[i][1] -=
pDz.getValue()*TthetaSphi;
348 points[i][0] +=
pDz.getValue()*TthetaCphi;
349 points[i][1] +=
pDz.getValue()*TthetaSphi;
352 for (
int np=0;np<NPOINTS;np++) theCoordinates->point.set1Value(np,points[np][0],points[np][1],points[np][2]);
353 theFaceSet->coordIndex.setValues(0,NINDICES,indices);
354 theNormals->vector.deleteValues(0);
355 theNormals->vector.insertSpace(0,6);
356 for (
int n=0;
n<6;
n++) {
357 int i0 = 5*
n+0,i1=5*
n+1,i2=5*
n+2;
358 int j0 = theFaceSet->coordIndex[i0];
359 int j1 = theFaceSet->coordIndex[i1];
360 int j2 = theFaceSet->coordIndex[i2];
361 SbVec3f p0= theCoordinates->point[j0];
362 SbVec3f p1= theCoordinates->point[j1];
363 SbVec3f p2= theCoordinates->point[j2];
364 SbVec3f normal = (p1-p0).
cross(p2-p0);
366 theNormals->vector.set1Value(
n,normal);
368 theNormalBinding->value=SoNormalBinding::PER_FACE;
380 SoSeparator *sep =
new SoSeparator();
381 SoCoordinate3 *theCoordinates =
new SoCoordinate3();
382 SoNormal *theNormals =
new SoNormal();
383 SoNormalBinding *theNormalBinding =
new SoNormalBinding();
384 SoIndexedFaceSet *theFaceSet =
new SoIndexedFaceSet();
388 sep->addChild(theCoordinates);
389 sep->addChild(theNormals);
390 sep->addChild(theNormalBinding);
391 sep->addChild(theFaceSet);