40#include <Inventor/SbBox.h>
41#include <Inventor/actions/SoAction.h>
42#include <Inventor/fields/SoSFFloat.h>
43#include <Inventor/misc/SoChildList.h>
44#include <Inventor/nodes/SoSeparator.h>
45#include <Inventor/nodes/SoIndexedFaceSet.h>
46#include <Inventor/nodes/SoNormal.h>
47#include <Inventor/nodes/SoCoordinate3.h>
48#include <Inventor/nodes/SoNormalBinding.h>
49#include <Inventor/SoPrimitiveVertex.h>
50#include <Inventor/elements/SoTextureCoordinateElement.h>
60 static bool first =
true;
63 SO_NODE_INIT_CLASS(
SoTrd,SoShape,
"Shape");
69 SO_NODE_CONSTRUCTOR(
SoTrd);
71 SO_NODE_ADD_FIELD(
fDx1,(1.0));
72 SO_NODE_ADD_FIELD(
fDx2,(1.0));
73 SO_NODE_ADD_FIELD(
fDy1,(1.0));
74 SO_NODE_ADD_FIELD(
fDy2,(1.0));
75 SO_NODE_ADD_FIELD(
fDz,(1.0));
77 children =
new SoChildList(
this);
89 SoState *state = action->getState();
93 SbBool useTexFunction=
94 (SoTextureCoordinateElement::getType(state) ==
95 SoTextureCoordinateElement::FUNCTION);
100 const SoTextureCoordinateElement *tce = NULL;
102 if (useTexFunction) {
103 tce = SoTextureCoordinateElement::getInstance(state);
109 SbVec3f point, normal;
114#define GEN_VERTEX(pv,x,y,z,s,t,nx,ny,nz) \
115 point.setValue(x,y,z); \
116 normal.setValue(nx,ny,nz); \
117 if (useTexFunction) { \
118 texCoord=tce->get(point,normal); \
124 pv.setPoint(point); \
125 pv.setNormal(normal); \
126 pv.setTextureCoords(texCoord); \
131 const int NPOINTS=8, NFACES=6, NINDICES = NFACES*5;
132 int indices[NINDICES] = {3,2,1,0, SO_END_FACE_INDEX,
133 4,5,6,7, SO_END_FACE_INDEX,
134 0,1,5,4, SO_END_FACE_INDEX,
135 1,2,6,5, SO_END_FACE_INDEX,
136 2,3,7,6, SO_END_FACE_INDEX,
137 3,0,4,7, SO_END_FACE_INDEX};
141 float points[NPOINTS][3];
142 points[0][0] =
fDx1.getValue();
143 points[0][1] =
fDy1.getValue();
144 points[0][2] = -
fDz.getValue();
146 points[1][0] = -
fDx1.getValue();
147 points[1][1] =
fDy1.getValue();
148 points[1][2] = -
fDz.getValue();
150 points[2][0] = -
fDx1.getValue();
151 points[2][1] = -
fDy1.getValue();
152 points[2][2] = -
fDz.getValue();
154 points[3][0] =
fDx1.getValue();
155 points[3][1] = -
fDy1.getValue();
156 points[3][2] = -
fDz.getValue();
158 points[4][0] =
fDx2.getValue();
159 points[4][1] =
fDy2.getValue();
160 points[4][2] =
fDz.getValue();
162 points[5][0] = -
fDx2.getValue();
163 points[5][1] =
fDy2.getValue();
164 points[5][2] =
fDz.getValue();
166 points[6][0] = -
fDx2.getValue();
167 points[6][1] = -
fDy2.getValue();
168 points[6][2] =
fDz.getValue();
170 points[7][0] =
fDx2.getValue();
171 points[7][1] = -
fDy2.getValue();
172 points[7][2] =
fDz.getValue();
176 float st1 =
FSIN(t1);
177 float st2 =
FSIN(t2);
178 float ct1 =
FCOS(t1);
179 float ct2 =
FCOS(t2);
181 float normals[NFACES][3];
183 normals[0][0] = 0 ; normals[0][1] = 0; normals [0][2] = -1;
185 normals[1][0] = 0 ; normals[1][1] = 0; normals [1][2] = 1;
187 normals[2][0] = 0 ; normals[2][1] = ct2; normals [2][2] = -st2;
189 normals[3][0] = -ct1; normals[3][1] = 0; normals [3][2] = -st1;
191 normals[4][0] = 0 ; normals[4][1] = -ct2; normals [4][2] = -st2;
193 normals[5][0] = ct1; normals[5][1] = 0; normals [5][2] = -st1;
197 for (
int nf=0;nf<NFACES;nf++) {
198 beginShape(action,TRIANGLE_FAN);
199 index = indices[nf * 5];
200 x = points[index][0];
201 y = points[index][1];
202 z = points[index][2];
203 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
204 index = indices[nf * 5 + 1];
205 x = points[index][0];
206 y = points[index][1];
207 z = points[index][2];
208 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
209 index = indices[nf * 5 + 2];
210 x = points[index][0];
211 y = points[index][1];
212 z = points[index][2];
213 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
214 index = indices[nf * 5 + 3];
215 x = points[index][0];
216 y = points[index][1];
217 z = points[index][2];
218 GEN_VERTEX(pv,x,y,z,0.0,0.0,normals[nf][0],normals[nf][1],normals[nf][2]);
231 float fDx=
fDx1.getValue(),fDy=
fDy1.getValue();
233 if (
fDx2.getValue() > fDx) fDx =
fDx2.getValue();
234 if (
fDy2.getValue() > fDy) fDy =
fDy2.getValue();
236 SbVec3f vmin(-fDx,-fDy,-
fDz.getValue()),
237 vmax( fDx, fDy,
fDz.getValue());
239 center.setValue(0,0,0);
240 box.setBounds(vmin,vmax);
247void SoTrd::updateChildren() {
252 assert(children->getLength()==1);
253 SoSeparator *sep = (SoSeparator *) ( *children)[0];
254 SoCoordinate3 *theCoordinates = (SoCoordinate3 *) ( sep->getChild(0));
255 SoNormal *theNormals = (SoNormal *) ( sep->getChild(1));
256 SoNormalBinding *theNormalBinding = (SoNormalBinding *) ( sep->getChild(2));
257 SoIndexedFaceSet *theFaceSet = (SoIndexedFaceSet *) ( sep->getChild(3));
259 const int NPOINTS=8, NFACES=6, NINDICES = NFACES*5;
260 float points[NPOINTS][3];
261 float normals[NFACES][3]= {{0,0,-1}, {0,0,1}, {0,1,0}, {-1, 0, 0}, {0, -1, 0}, {1,0,0}};
269 indices[NINDICES] = {3,2,1,0, SO_END_FACE_INDEX,
270 4,5,6,7, SO_END_FACE_INDEX,
271 0,1,5,4, SO_END_FACE_INDEX,
272 1,2,6,5, SO_END_FACE_INDEX,
273 2,3,7,6, SO_END_FACE_INDEX,
274 3,0,4,7, SO_END_FACE_INDEX};
278 points[0][0] =
fDx1.getValue(); points[0][1] =
fDy1.getValue(); points[0][2] = -
fDz.getValue();
279 points[1][0] = -
fDx1.getValue(); points[1][1] =
fDy1.getValue(); points[1][2] = -
fDz.getValue();
280 points[2][0] = -
fDx1.getValue(); points[2][1] = -
fDy1.getValue(); points[2][2] = -
fDz.getValue();
281 points[3][0] =
fDx1.getValue(); points[3][1] = -
fDy1.getValue(); points[3][2] = -
fDz.getValue();
282 points[4][0] =
fDx2.getValue(); points[4][1] =
fDy2.getValue(); points[4][2] =
fDz.getValue();
283 points[5][0] = -
fDx2.getValue(); points[5][1] =
fDy2.getValue(); points[5][2] =
fDz.getValue();
284 points[6][0] = -
fDx2.getValue(); points[6][1] = -
fDy2.getValue(); points[6][2] =
fDz.getValue();
285 points[7][0] =
fDx2.getValue(); points[7][1] = -
fDy2.getValue(); points[7][2] =
fDz.getValue();
289 float st1 =
FSIN(t1);
290 float st2 =
FSIN(t2);
291 float ct1 =
FCOS(t1);
292 float ct2 =
FCOS(t2);
294 normals[0][0] = 0 ; normals[0][1] = 0; normals [0][2] = -1;
295 normals[1][0] = 0 ; normals[1][1] = 0; normals [1][2] = 1;
296 normals[2][0] = 0 ; normals[2][1] = ct2; normals [2][2] = -st2;
297 normals[3][0] = -ct1; normals[3][1] = 0; normals [3][2] = -st1;
298 normals[4][0] = 0 ; normals[4][1] = -ct2; normals [4][2] = -st2;
299 normals[5][0] = ct1; normals[5][1] = 0; normals [5][2] = -st1;
301 for (
int np=0;np<NPOINTS;np++) theCoordinates->point.set1Value(np,points[np][0],points[np][1],points[np][2]);
302 theFaceSet->coordIndex.setValues(0,NINDICES,indices);
303 for (
int nf=0;nf<NFACES;nf++) theNormals->vector.set1Value(nf,normals[nf][0],normals[nf][1],normals[nf][2]);
304 theNormalBinding->value=SoNormalBinding::PER_FACE;
308void SoTrd::generateChildren() {
315 assert(children->getLength() ==0);
316 SoSeparator *sep =
new SoSeparator();
317 SoCoordinate3 *theCoordinates =
new SoCoordinate3();
318 SoNormal *theNormals =
new SoNormal();
319 SoNormalBinding *theNormalBinding =
new SoNormalBinding();
320 SoIndexedFaceSet *theFaceSet =
new SoIndexedFaceSet();
324 sep->addChild(theCoordinates);
325 sep->addChild(theNormals);
326 sep->addChild(theNormalBinding);
327 sep->addChild(theFaceSet);
328 children->append(sep);
337 if (children->getLength() == 0) generateChildren();
#define GEN_VERTEX(pv, x, y, z, s, t, nx, ny, nz)
SoSFFloat fDx2
half-length of x, at +fDz
virtual void generatePrimitives(SoAction *action)
Generate Primitives, required.
SoTrd()
Constructor, required.
virtual SoChildList * getChildren() const
GetChildList, required whenever the class has hidden children.
SoSFFloat fDy2
half-length of y, at +fDz
virtual ~SoTrd()
Destructor, required.
SoSFFloat fDy1
half-length of y, at -fDz
SoSFNode alternateRep
Alternate rep - required.
virtual void clearAlternateRep()
We better be able to clear it, too!
virtual void generateAlternateRep()
virtual void computeBBox(SoAction *action, SbBox3f &box, SbVec3f ¢er)
compute bounding Box, required
SoSFFloat fDx1
half-length of x, at -fDz
SoSFFloat fDz
half-length along Z