19 #include <Inventor/SbBox.h>
20 #include <Inventor/actions/SoGLRenderAction.h>
22 #include <Inventor/misc/SoChildList.h>
23 #include <Inventor/nodes/SoSeparator.h>
24 #include <Inventor/nodes/SoIndexedFaceSet.h>
25 #include <Inventor/nodes/SoNormal.h>
26 #include <Inventor/nodes/SoCoordinate3.h>
27 #include <Inventor/nodes/SoNormalBinding.h>
28 #include <Inventor/SoPrimitiveVertex.h>
29 #include <Inventor/elements/SoTextureCoordinateElement.h>
30 #include <Inventor/elements/SoGLCacheContextElement.h>
31 #include <Inventor/C/glue/gl.h>
44 SO_NODE_CONSTRUCTOR(
SoTubs);
47 SO_NODE_ADD_FIELD(pRMin, (0));
48 SO_NODE_ADD_FIELD(pRMax, (1));
49 SO_NODE_ADD_FIELD(pDz, (10));
50 SO_NODE_ADD_FIELD(pSPhi, (0));
51 SO_NODE_ADD_FIELD(pDPhi, ((
float)(2*
M_PI)));
52 SO_NODE_ADD_FIELD(pOverrideNPhi, (0));
53 SO_NODE_ADD_FIELD(alternateRep, (NULL));
54 SO_NODE_ADD_FIELD(drawEdgeLines, (
false));
56 m_children = std::make_unique<SoChildList>(
this);
58 setNodeType(EXTENSION);
65 [[maybe_unused]]
static const bool didInit = [&]() {
66 SO_NODE_INIT_CLASS(
SoTubs,SoShape,
"Shape");
77 SoState *state =
action->getState();
83 SbBool useTexFunction=
85 SoTextureCoordinateElement::FUNCTION);
90 const SoTextureCoordinateElement* tce = NULL;
93 tce = SoTextureCoordinateElement::getInstance(state);
99 SbVec3f point, normal;
104 #define GEN_VERTEX(pv,x,y,z,s,t,nx,ny,nz) \
105 point.setValue((float)(x),(float)(y),(float)(z)); \
106 normal.setValue((float)(nx),(float)(ny),(float)(nz)); \
107 if (useTexFunction) { \
108 texCoord=tce->get(point,normal); \
110 texCoord[0]=(float)(s); \
111 texCoord[1]=(float)(t); \
113 pv.setPoint(point); \
114 pv.setNormal(normal); \
115 pv.setTextureCoords(texCoord); \
123 double rMax=
pRMax.getValue(),rMin=
pRMin.getValue();
124 double zMax=
pDz.getValue(),zMin=-zMax;
126 double cosPhi1=
cos(phi1), sinPhi1=
sin(phi1);
128 const bool noPhiCutout=fabs(
pDPhi.getValue())==0.F || fabs(fabs(
pDPhi.getValue())-2.0*
M_PI)<0.01;
129 const bool disableLighting(glIsEnabled(GL_LIGHTING));
130 const bool transparencyOn(glIsEnabled(GL_BLEND));
136 double sinPhi,cosPhi;
137 beginShape(
action,TRIANGLE_STRIP);
140 for (
i = 0;
i<=NPHI;
i++) {
141 GEN_VERTEX(
pv,rMax*cosPhi,rMax*sinPhi,zMax,0.0,0.0,cosPhi,sinPhi,0);
142 GEN_VERTEX(
pv,rMax*cosPhi,rMax*sinPhi,zMin,1.0,1.0,cosPhi,sinPhi,0);
143 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
150 beginShape(
action,TRIANGLE_STRIP);
153 for (
i = 0;
i<=NPHI;
i++) {
154 GEN_VERTEX(
pv,rMin*cosPhi,rMin*sinPhi,zMax,0.0,0.0,-cosPhi,-sinPhi,0);
155 GEN_VERTEX(
pv,rMin*cosPhi,rMin*sinPhi,zMin,1.0,1.0,-cosPhi,-sinPhi,0);
156 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
170 beginShape(
action,TRIANGLE_STRIP);
173 GEN_VERTEX(
pv,rMax*cosPhi,rMax*sinPhi,zMax,0.0,0.0,sinPhi,-cosPhi,0);
174 GEN_VERTEX(
pv,rMax*cosPhi,rMax*sinPhi,zMin,1.0,1.0,sinPhi,-cosPhi,0);
175 GEN_VERTEX(
pv,rMin*cosPhi,rMin*sinPhi,zMax,1.0,0.0,sinPhi,-cosPhi,0);
176 GEN_VERTEX(
pv,rMin*cosPhi,rMin*sinPhi,zMin,0.0,1.0,sinPhi,-cosPhi,0);
181 beginShape(
action,TRIANGLE_STRIP);
184 GEN_VERTEX(
pv,rMax*cosPhi,rMax*sinPhi, zMax,0.0,0.0,-sinPhi,+cosPhi,0);
185 GEN_VERTEX(
pv,rMax*cosPhi,rMax*sinPhi, zMin,1.0,1.0,-sinPhi,+cosPhi,0);
186 GEN_VERTEX(
pv,rMin*cosPhi,rMin*sinPhi, zMax,1.0,0.0,-sinPhi,+cosPhi,0);
187 GEN_VERTEX(
pv,rMin*cosPhi,rMin*sinPhi, zMin,0.0,1.0,-sinPhi,+cosPhi,0);
191 if (disableLighting) glDisable(GL_LIGHTING);
192 if (transparencyOn) glDisable(GL_BLEND);
195 glVertex3f(rMax*cosPhi0,rMax*sinPhi0, zMax);
196 glVertex3f(rMax*cosPhi0,rMax*sinPhi0, zMin);
197 glVertex3f(rMin*cosPhi0,rMin*sinPhi0, zMax);
198 glVertex3f(rMin*cosPhi0,rMin*sinPhi0, zMin);
199 glVertex3f(rMax*cosPhi1,rMax*sinPhi1, zMax);
200 glVertex3f(rMax*cosPhi1,rMax*sinPhi1, zMin);
201 glVertex3f(rMin*cosPhi1,rMin*sinPhi1, zMax);
202 glVertex3f(rMin*cosPhi1,rMin*sinPhi1, zMin);
204 if (disableLighting) glEnable(GL_LIGHTING);
205 if (transparencyOn) glEnable(GL_BLEND);
213 beginShape(
action,TRIANGLE_FAN);
217 for (
i = 0;
i<=NPHI;
i++) {
218 GEN_VERTEX(
pv,rMax*cosPhi,rMax*sinPhi,zMax,1.0,1.0,0,0,1);
219 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
225 beginShape(
action,TRIANGLE_FAN);
229 for (
i = 0;
i<=NPHI;
i++) {
230 GEN_VERTEX(
pv,rMax*cosPhi,rMax*sinPhi,zMin,1.0,1.0,0,0,-1);
231 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
237 if (disableLighting) glDisable(GL_LIGHTING);
238 if (transparencyOn) glDisable(GL_BLEND);
239 glBegin(GL_LINE_STRIP);
242 if (!noPhiCutout) glVertex3f(0,0,zMax);
243 for (
i = 0;
i<=NPHI;
i++) {
244 glVertex3f(rMax*cosPhi,rMax*sinPhi,zMax);
245 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
247 if (!noPhiCutout) glVertex3f(0,0,zMax);
251 glBegin(GL_LINE_STRIP);
254 if (!noPhiCutout) glVertex3f(0,0,zMin);
255 for (
i = 0;
i<=NPHI;
i++) {
256 glVertex3f(rMax*cosPhi,rMax*sinPhi,zMin);
257 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
259 if (!noPhiCutout) glVertex3f(0,0,zMin);
263 if (disableLighting) glEnable(GL_LIGHTING);
264 if (transparencyOn) glEnable(GL_BLEND);
268 beginShape(
action,TRIANGLE_STRIP);
271 for (
i = 0;
i<=NPHI;
i++) {
272 GEN_VERTEX(
pv,rMin*cosPhi,rMin*sinPhi,zMax,0.0,0.0,0,0,1);
273 GEN_VERTEX(
pv,rMax*cosPhi,rMax*sinPhi,zMax,1.0,1.0,0,0,1);
274 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
280 beginShape(
action,TRIANGLE_STRIP);
283 for (
i = 0;
i<=NPHI;
i++) {
284 GEN_VERTEX(
pv,rMin*cosPhi,rMin*sinPhi,zMin,0.0,0.0,0,0,-1);
285 GEN_VERTEX(
pv,rMax*cosPhi,rMax*sinPhi,zMin,1.0,1.0,0,0,-1);
286 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
291 if (disableLighting) glDisable(GL_LIGHTING);
292 if (transparencyOn) glDisable(GL_BLEND);
293 glBegin(GL_LINE_STRIP);
296 if (!noPhiCutout) glVertex3f(rMin*cosPhi,rMin*sinPhi,zMax);
297 for (
i = 0;
i<=NPHI;
i++) {
298 glVertex3f(rMax*cosPhi,rMax*sinPhi,zMax);
299 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
301 if (!noPhiCutout) glVertex3f(rMin*cosPhi1,rMin*sinPhi1,zMax);
304 glBegin(GL_LINE_STRIP);
307 for (
i = 0;
i<=NPHI;
i++) {
308 glVertex3f(rMin*cosPhi,rMin*sinPhi,zMax);
309 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
313 glBegin(GL_LINE_STRIP);
316 if (!noPhiCutout) glVertex3f(rMin*cosPhi,rMin*sinPhi,zMin);
317 for (
i = 0;
i<=NPHI;
i++) {
318 glVertex3f(rMax*cosPhi,rMax*sinPhi,zMin);
319 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
321 if (!noPhiCutout) glVertex3f(rMin*cosPhi1,rMin*sinPhi1,zMin);
324 glBegin(GL_LINE_STRIP);
327 for (
i = 0;
i<=NPHI;
i++) {
328 glVertex3f(rMin*cosPhi,rMin*sinPhi,zMin);
329 inc(sinPhi, cosPhi, sinDeltaPhi, cosDeltaPhi);
333 if (disableLighting) glEnable(GL_LIGHTING);
334 if (transparencyOn) glEnable(GL_BLEND);
339 if (state&&state->isElementEnabled(SoGLCacheContextElement::getClassStackIndex())) {
341 SoGLCacheContextElement::shouldAutoCache(state, SoGLCacheContextElement::DO_AUTO_CACHE);
342 #if ((COIN_MAJOR_VERSION>=3)||((COIN_MAJOR_VERSION==2)&&(COIN_MINOR_VERSION>=5)))
343 SoGLCacheContextElement::incNumShapes(state);
358 -
pDz.getValue(),
pDz.getValue(),
370 SoCoordinate3 *theCoordinates = (SoCoordinate3 *) (
sep->getChild(0));
371 SoNormal *theNormals = (SoNormal *) (
sep->getChild(1));
372 SoNormalBinding *theNormalBinding = (SoNormalBinding *) (
sep->getChild(2));
373 SoIndexedFaceSet *theFaceSet = (SoIndexedFaceSet *) (
sep->getChild(3));
376 const int NPHI=96, NPOINTS=2*(2*NPHI+2), NFACES=4*NPHI+2, NINDICES = NFACES*5;
377 float points[NPOINTS][3],normals[NFACES][3];
381 static int32_t
indices[NINDICES];
385 double phi, pp, DeltaPhi;
394 for (
i = 0;
i< NPHI;
i++) {
403 for (
i=0;
i<NPHI;
i++) {
404 indices[5*1*NPHI + 5*
i+0] = 2*NPHI+2 + 2*
i+0;
405 indices[5*1*NPHI + 5*
i+1] = 2*NPHI+2 + 2*
i+1;
406 indices[5*1*NPHI + 5*
i+2] = 2*NPHI+2 + 2*
i+3;
407 indices[5*1*NPHI + 5*
i+3] = 2*NPHI+2 + 2*
i+2;
408 indices[5*1*NPHI + 5*
i+4] = SO_END_FACE_INDEX;
411 for (
i=0;
i<NPHI;
i++) {
414 indices[5*2*NPHI + 5*
i+2] = NPOINTS - (2*
i+4);
415 indices[5*2*NPHI + 5*
i+3] = NPOINTS - (2*
i+2);
416 indices[5*2*NPHI + 5*
i+4] = SO_END_FACE_INDEX;
419 for (
i=0;
i<NPHI;
i++) {
421 indices[5*3*NPHI + 5*
i+1] = NPOINTS - (2*
i+1);
422 indices[5*3*NPHI + 5*
i+2] = NPOINTS - (2*
i+3);
424 indices[5*3*NPHI + 5*
i+4] = SO_END_FACE_INDEX;
428 indices[5*4*NPHI +1] = 2*NPHI+1;
429 indices[5*4*NPHI +2] = 2*NPHI+3;
430 indices[5*4*NPHI +3] = 2*NPHI+2;
431 indices[5*4*NPHI +4] = SO_END_FACE_INDEX;
434 indices[5*4*NPHI +5 +1] = NPOINTS-2;
435 indices[5*4*NPHI +5 +2] = NPOINTS-1;
437 indices[5*4*NPHI +5 +4] = SO_END_FACE_INDEX;
443 indices[5*4*NPHI +1] = 2*NPHI+1;
444 indices[5*4*NPHI +2] = 2*NPHI+3;
445 indices[5*4*NPHI +3] = 2*NPHI+2;
446 indices[5*4*NPHI +4] = SO_END_FACE_INDEX;
449 indices[5*4*NPHI +5 +1] = NPOINTS-2;
450 indices[5*4*NPHI +5 +2] = NPOINTS-1;
452 indices[5*4*NPHI +5 +4] = SO_END_FACE_INDEX;
456 indices[5*4*NPHI +0] = SO_END_FACE_INDEX;
457 indices[5*4*NPHI +1] = SO_END_FACE_INDEX;
458 indices[5*4*NPHI +2] = SO_END_FACE_INDEX;
459 indices[5*4*NPHI +3] = SO_END_FACE_INDEX;
460 indices[5*4*NPHI +4] = SO_END_FACE_INDEX;
462 indices[5*4*NPHI +5 +0] = SO_END_FACE_INDEX;
463 indices[5*4*NPHI +5 +1] = SO_END_FACE_INDEX;
464 indices[5*4*NPHI +5 +2] = SO_END_FACE_INDEX;
465 indices[5*4*NPHI +5 +3] = SO_END_FACE_INDEX;
466 indices[5*4*NPHI +5 +4] = SO_END_FACE_INDEX;
470 for (
i = 0;
i<=NPHI;
i++) {
479 pp =
phi+DeltaPhi/2.0;
481 normals[
i][0] =
FCOS(pp);
482 normals[
i][1] =
FSIN(pp);
489 for (
i = 0;
i<=NPHI;
i++) {
496 pp =
phi-DeltaPhi/2.0;
498 normals[NPHI+
i][0] = -
FCOS(pp);
499 normals[NPHI+
i][1] = -
FSIN(pp);
500 normals[NPHI+
i][2] = 0;
505 for (
i=0;
i<NPHI;
i++) {
506 normals[2*NPHI+
i][0]=normals[2*NPHI+
i][1]=0;
507 normals[2*NPHI+
i][2]= 1.0;
510 for (
i=0;
i<NPHI;
i++) {
511 normals[3*NPHI+
i][0]=normals[3*NPHI+
i][1]=0;
512 normals[3*NPHI+
i][2]= -1.0;
516 normals[4*NPHI+0][0]=
FSIN(
phi);
517 normals[4*NPHI+0][1]= -
FCOS(
phi);
518 normals[4*NPHI+0][2]=0;
522 normals[4*NPHI+1][0]= -
FSIN(
phi);
523 normals[4*NPHI+1][1]= +
FCOS(
phi);
524 normals[4*NPHI+1][2]=0;
527 for (
int ni=0;ni<NINDICES;ni++) theFaceSet->coordIndex.set1Value(ni,
indices[ni]);
528 for (
int nf=0;nf<NFACES;nf++) theNormals->vector.set1Value(nf,normals[nf][0],normals[nf][1],normals[nf][2]);
529 theNormalBinding->value=SoNormalBinding::PER_FACE;
541 SoSeparator *
sep =
new SoSeparator();
542 SoCoordinate3 *theCoordinates =
new SoCoordinate3();
543 SoNormal *theNormals =
new SoNormal();
544 SoNormalBinding *theNormalBinding =
new SoNormalBinding();
545 SoIndexedFaceSet *theFaceSet =
new SoIndexedFaceSet();
549 sep->addChild(theCoordinates);
550 sep->addChild(theNormals);
551 sep->addChild(theNormalBinding);
552 sep->addChild(theFaceSet);