Debian Codegraph @

lib/database/src/so/nodes/SoShape.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 /*
38 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.2 $
44 |
45 | Classes:
46 | SoShape
47 |
48 | Author(s) : Paul S. Strauss, Dave Immel
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54
55 #include <GL/gl.h>
56 #include <GL/glu.h>
57 #include <Inventor/SbLinear.h>
58 #include <Inventor/SoDB.h>
59 #include <Inventor/SoPickedPoint.h>
60 #include <Inventor/SoPrimitiveVertex.h>
61 #include <Inventor/actions/SoCallbackAction.h>
62 #include <Inventor/actions/SoGLRenderAction.h>
63 #include <Inventor/actions/SoGetBoundingBoxAction.h>
64 #include <Inventor/actions/SoRayPickAction.h>
65 #include <Inventor/bundles/SoMaterialBundle.h>
66 #include <Inventor/details/SoDetail.h>
67 #include <Inventor/details/SoFaceDetail.h>
68 #include <Inventor/details/SoPointDetail.h>
69 #include <Inventor/elements/SoComplexityElement.h>
70 #include <Inventor/elements/SoComplexityTypeElement.h>
71 #include <Inventor/elements/SoDrawStyleElement.h>
72 #include <Inventor/elements/SoGLTextureEnabledElement.h>
73 #include <Inventor/elements/SoModelMatrixElement.h>
74 #include <Inventor/elements/SoPickStyleElement.h>
75 #include <Inventor/elements/SoProjectionMatrixElement.h>
76 #include <Inventor/elements/SoShapeHintsElement.h>
77 #include <Inventor/elements/SoTextureCoordinateElement.h>
78 #include <Inventor/elements/SoViewingMatrixElement.h>
79 #include <Inventor/elements/SoViewportRegionElement.h>
80 #include <Inventor/errors/SoDebugError.h>
81 #include <Inventor/misc/SoState.h>
82 #include <Inventor/nodes/SoCube.h>
83 #include <Inventor/nodes/SoShape.h>
84
85 SO_NODE_ABSTRACT_SOURCE(SoShape);
86
87 // This is used as a surrogate object when rendering or picking a
88 // shape whose complexity is set to BOUNDING_BOX.
89 SoCube *SoShape::bboxCube = NULL;
90
91 // Used during generatePrimitives:
92 SbBool SoShape::sendTexCoords = 0;
93 SoMaterialBundle *SoShape::matlBundle = NULL;
94
95 // Used while tesellating non-convex polygons
96 SoShape::TriangleShape SoShape::primShapeType;
97 SoFaceDetail *SoShape::faceDetail = NULL;
98 int SoShape::nestLevel = 0;
99 SoAction *SoShape::primAction = NULL;
100 int SoShape::primVertNum = 0;
101 int SoShape::polyVertNum = 0;
102 SoShape *SoShape::primShape = NULL;
103 SoPrimitiveVertex *SoShape::primVerts = NULL;
104 SoPointDetail *SoShape::vertDetails = NULL;
105 SoPrimitiveVertex *SoShape::polyVerts = NULL;
106 SoPointDetail *SoShape::polyDetails = NULL;
107 int SoShape::numPolyVertsAllocated = 0;
108 gluTESSELATOR *SoShape::tobj = NULL;
109
110 ////////////////////////////////////////////////////////////////////////
111 //
112 // Description:
113 // Overrides method in SoNode to return FALSE.
114 //
115 // Use: public
116
117 SbBool
118 SoShape::affectsState() const
119 //
120 ////////////////////////////////////////////////////////////////////////
121 {
122 return FALSE;
123 }
124
125 ////////////////////////////////////////////////////////////////////////
126 //
127 // Description:
128 // Constructor.
129 //
130 // Use: protected
131
132 SoShape::SoShape()
133 //
134 ////////////////////////////////////////////////////////////////////////
135 {
136 SO_NODE_CONSTRUCTOR(SoShape);
137 }
138
139 ////////////////////////////////////////////////////////////////////////
140 //
141 // Description:
142 // Destructor.
143 //
144 // Use: private
145
146 SoShape::~SoShape()
147 //
148 ////////////////////////////////////////////////////////////////////////
149 {
150 }
151
152 ////////////////////////////////////////////////////////////////////////
153 //
154 // Description:
155 // Computes bounding box for a shape, using the virtual
156 // computeBBox() method.
157 //
158 // Use: extender
159
160 void
161 SoShape::getBoundingBox(SoGetBoundingBoxAction *action)
162 //
163 ////////////////////////////////////////////////////////////////////////
164 {
165 SbBox3f bbox;
166 SbVec3f center;
167
168 computeBBox(action, bbox, center);
169
170 action->extendBy(bbox);
171
172 // Make sure the center is first transformed by the current local
173 // transformation matrix
174 action->setCenter(center, TRUE);
175 }
176
177 ////////////////////////////////////////////////////////////////////////
178 //
179 // Description:
180 // Implements rendering by rendering each primitive generated by
181 // subclass.
182 //
183 // Use: extender
184
185 void
186 SoShape::GLRender(SoGLRenderAction *action)
187 //
188 ////////////////////////////////////////////////////////////////////////
189 {
190 // First see if the object is visible and should be rendered now
191 if (shouldGLRender(action)) {
192
193 SoState *state = action->getState();
194
195 //
196 // Set up some info in instance that will be used during
197 // rendering of generated primitives
198 //
199
200 // Send the first material and remember it was sent
201 SoMaterialBundle mb(action);
202 matlBundle = &mb;
203 matlBundle->sendFirst();
204
205 // See if textures are enabled and we need to send texture coordinates
206 sendTexCoords = (SoGLTextureEnabledElement::get(state));
207
208 // Generate primitives to approximate the shape. Each
209 // primitive will be rendered separately (through callbacks).
210 generatePrimitives(action);
211 }
212 }
213
214 ////////////////////////////////////////////////////////////////////////
215 //
216 // Description:
217 // Implements picking along a ray by intersecting the ray with each
218 // primitive generated by subclass.
219 //
220 // Use: extender
221
222 void
223 SoShape::rayPick(SoRayPickAction *action)
224 //
225 ////////////////////////////////////////////////////////////////////////
226 {
227 // First see if the object is pickable
228 if (shouldRayPick(action)) {
229
230 // Compute the picking ray in the space of the shape
231 computeObjectSpaceRay(action);
232
233 // Generate primitives to approximate the shape. Each
234 // primitive will be intersected (through callbacks) with the
235 // ray.
236 generatePrimitives(action);
237 }
238 }
239
240 ////////////////////////////////////////////////////////////////////////
241 //
242 // Description:
243 // Implements the generation of primitives for the shape.
244 //
245 // Use: extender
246
247 void
248 SoShape::callback(SoCallbackAction *action)
249 //
250 ////////////////////////////////////////////////////////////////////////
251 {
252 // First see if the object should have primitives generated for it.
253 if (action->shouldGeneratePrimitives(this)) {
254
255 // Generate primitives to approximate the shape. Each primitive
256 // will be sent back to the application through callbacks.
257 generatePrimitives(action);
258 }
259 }
260
261 ////////////////////////////////////////////////////////////////////////
262 //
263 // Description:
264 // This can be used by subclasses when the complexity type is
265 // SCREEN_SPACE to determine how many window pixels are covered by
266 // the shape. It returns in rectSize the number of pixels in the
267 // window rectangle that covers the given 3D bounding box.
268 //
269 // Use: extender, static
270
271 void
272 SoShape::getScreenSize(SoState *state, const SbBox3f &boundingBox,
273 SbVec2s &rectSize)
274 //
275 ////////////////////////////////////////////////////////////////////////
276 {
277 SbMatrix objToScreen;
278 SbVec2s winSize;
279 SbVec3f min, max, screenPoint[8];
280 SbBox2f screenBox;
281 int i;
282
283 // Get the matrices from the state to convert from object to screen space
284 objToScreen = (SoModelMatrixElement::get(state) *
285 SoViewingMatrixElement::get(state) *
286 SoProjectionMatrixElement::get(state));
287
288 // Get the size of the window from the state
289 winSize = SoViewportRegionElement::get(state).getWindowSize();
290
291 // Transform the 8 vertices of the bounding box into screen space
292
293 boundingBox.getBounds(min, max);
294
295 objToScreen.multVecMatrix(SbVec3f(min[0], min[1], min[2]), screenPoint[0]);
296 objToScreen.multVecMatrix(SbVec3f(min[0], min[1], max[2]), screenPoint[1]);
297 objToScreen.multVecMatrix(SbVec3f(min[0], max[1], min[2]), screenPoint[2]);
298 objToScreen.multVecMatrix(SbVec3f(min[0], max[1], max[2]), screenPoint[3]);
299 objToScreen.multVecMatrix(SbVec3f(max[0], min[1], min[2]), screenPoint[4]);
300 objToScreen.multVecMatrix(SbVec3f(max[0], min[1], max[2]), screenPoint[5]);
301 objToScreen.multVecMatrix(SbVec3f(max[0], max[1], min[2]), screenPoint[6]);
302 objToScreen.multVecMatrix(SbVec3f(max[0], max[1], max[2]), screenPoint[7]);
303
304 for (i = 0; i < 8; i++)
305 screenBox.extendBy(SbVec2f((screenPoint[i][0] * winSize[0]),
306 (screenPoint[i][1] * winSize[1])));
307
308 // Get the size of the resulting box
309 SbVec2f boxSize;
310 screenBox.getSize(boxSize[0], boxSize[1]);
311
312 // Screen space size is actually half of this size. Test for
313 // overflow and use the maximum size if necessary.
314 boxSize /= 2.0;
315
316 if (boxSize[0] > SHRT_MAX)
317 rectSize[0] = SHRT_MAX;
318 else if (boxSize[0] < SHRT_MIN)
319 rectSize[0] = SHRT_MIN;
320 else
321 rectSize[0] = (short) boxSize[0];
322
323 if (boxSize[1] > SHRT_MAX)
324 rectSize[1] = SHRT_MAX;
325 else if (boxSize[1] < SHRT_MIN)
326 rectSize[1] = SHRT_MIN;
327 else
328 rectSize[1] = (short) boxSize[1];
329 }
330
331 ////////////////////////////////////////////////////////////////////////
332 //
333 // Description:
334 // Returns TRUE if the shape should be rendered now.
335 //
336 // Use: protected
337
338 SbBool
339 SoShape::shouldGLRender(SoGLRenderAction *action)
340 //
341 ////////////////////////////////////////////////////////////////////////
342 {
343 // SoNode has already checked for render abort, so don't need to
344 // do it now
345
346 // Check if the shape is invisible
347 if (SoDrawStyleElement::get(action->getState()) ==
348 SoDrawStyleElement::INVISIBLE)
349 return FALSE;
350
351 // If the shape is transparent and transparent objects are being
352 // delayed, don't render now
353 if (action->handleTransparency())
354 return FALSE;
355
356 // If the current complexity is BOUNDING_BOX, just render the
357 // cuboid surrounding the shape and tell the shape to stop
358 if (SoComplexityTypeElement::get(action->getState()) ==
359 SoComplexityTypeElement::BOUNDING_BOX) {
360 GLRenderBoundingBox(action);
361 return FALSE;
362 }
363
364 // Otherwise, go ahead and render the object
365 return TRUE;
366 }
367
368 ////////////////////////////////////////////////////////////////////////
369 //
370 // Description:
371 // Returns TRUE if the shape may be picked.
372 //
373 // Use: protected, static
374
375 SbBool
376 SoShape::shouldRayPick(SoRayPickAction *action)
377 //
378 ////////////////////////////////////////////////////////////////////////
379 {
380 SbBool shapeShouldPick;
381
382 switch (SoPickStyleElement::get(action->getState())) {
383
384 case SoPickStyleElement::SHAPE:
385 shapeShouldPick = TRUE;
386 break;
387
388 case SoPickStyleElement::BOUNDING_BOX:
389 // Just pick the cuboid surrounding the shape
390 rayPickBoundingBox(action);
391 shapeShouldPick = FALSE;
392 break;
393
394 case SoPickStyleElement::UNPICKABLE:
395 shapeShouldPick = FALSE;
396 break;
397 }
398
399 return shapeShouldPick;
400 }
401
402 ////////////////////////////////////////////////////////////////////////
403 //
404 // Description:
405 // This is called by a subclass before rendering to set up shape
406 // hints for a solid object.
407 //
408 // Use: protected
409
410 void
411 SoShape::beginSolidShape(SoGLRenderAction *action)
412 //
413 ////////////////////////////////////////////////////////////////////////
414 {
415 SoState *state = action->getState();
416 state->push();
417
418 // If the current draw style is not FILLED, we don't want to turn
419 // on backface culling. (It may already be on, but that's up to
420 // the application to decide.)
421 if (SoDrawStyleElement::get(action->getState()) !=
422 SoDrawStyleElement::FILLED) {
423 return;
424 }
425
426 // Turn on backface culling, using shape hints element, unless it
427 // is already set up ok. Save state first if changing things
428 SoShapeHintsElement::VertexOrdering oldOrder;
429 SoShapeHintsElement::ShapeType oldShape;
430 SoShapeHintsElement::FaceType oldFace;
431
432 SoShapeHintsElement::get(state, oldOrder, oldShape, oldFace);
433
434 if (oldOrder != SoShapeHintsElement::COUNTERCLOCKWISE ||
435 oldShape != SoShapeHintsElement::SOLID) {
436 SoShapeHintsElement::set(state,
437 SoShapeHintsElement::COUNTERCLOCKWISE,
438 SoShapeHintsElement::SOLID,
439 SoShapeHintsElement::FACE_TYPE_AS_IS);
440 }
441 }
442
443 ////////////////////////////////////////////////////////////////////////
444 //
445 // Description:
446 // This is called by a subclass after rendering, if
447 // beginSolidShape() was called beforehand.
448 //
449 // Use: protected
450
451 void
452 SoShape::endSolidShape(SoGLRenderAction *action)
453 //
454 ////////////////////////////////////////////////////////////////////////
455 {
456 action->getState()->pop();
457 }
458
459 ////////////////////////////////////////////////////////////////////////
460 //
461 // Description:
462 // Computes a picking ray in the object space of the shape
463 // instance. The picking ray is stored in the SoRayPickAction for
464 // later access by the subclass.
465 //
466 // Use: protected
467
468 void
469 SoShape::computeObjectSpaceRay(SoRayPickAction *action)
470 //
471 ////////////////////////////////////////////////////////////////////////
472 {
473 action->setObjectSpace();
474 }
475
476 ////////////////////////////////////////////////////////////////////////
477 //
478 // Description:
479 // Same as above, but allows subclass to specify a matrix to
480 // concatenate with the current transformation matrix.
481 //
482 // Use: protected
483
484 void
485 SoShape::computeObjectSpaceRay(SoRayPickAction *action, const SbMatrix &matrix)
486 //
487 ////////////////////////////////////////////////////////////////////////
488 {
489 action->setObjectSpace(matrix);
490 }
491
492 ////////////////////////////////////////////////////////////////////////
493 //
494 // Description:
495 // This is called during picking to create a detail containing
496 // extra info about a pick intersection on a triangle. The default
497 // method returns NULL.
498 //
499 // Use: protected, virtual
500
501 SoDetail *
502 SoShape::createTriangleDetail(SoRayPickAction *,
503 const SoPrimitiveVertex *,
504 const SoPrimitiveVertex *,
505 const SoPrimitiveVertex *,
506 SoPickedPoint *)
507 //
508 ////////////////////////////////////////////////////////////////////////
509 {
510 return NULL;
511 }
512
513 ////////////////////////////////////////////////////////////////////////
514 //
515 // Description:
516 // This is called during picking to create a detail containing
517 // extra info about a pick intersection on a line segment. The
518 // default method returns NULL.
519 //
520 // Use: protected, virtual
521
522 SoDetail *
523 SoShape::createLineSegmentDetail(SoRayPickAction *,
524 const SoPrimitiveVertex *,
525 const SoPrimitiveVertex *,
526 SoPickedPoint *)
527 //
528 ////////////////////////////////////////////////////////////////////////
529 {
530 return NULL;
531 }
532
533 ////////////////////////////////////////////////////////////////////////
534 //
535 // Description:
536 // This is called during picking to create a detail containing
537 // extra info about a pick intersection on a point. The default
538 // method returns NULL.
539 //
540 // Use: protected, virtual
541
542 SoDetail *
543 SoShape::createPointDetail(SoRayPickAction *, const SoPrimitiveVertex *,
544 SoPickedPoint *)
545 //
546 ////////////////////////////////////////////////////////////////////////
547 {
548 return NULL;
549 }
550
551 ////////////////////////////////////////////////////////////////////////
552 //
553 // Description:
554 // Invokes correct callbacks for triangle primitive generation.
555 //
556 // Use: protected
557
558 void
559 SoShape::invokeTriangleCallbacks(SoAction *action,
560 const SoPrimitiveVertex *v1,
561 const SoPrimitiveVertex *v2,
562 const SoPrimitiveVertex *v3)
563 //
564 ////////////////////////////////////////////////////////////////////////
565 {
566 const SoType &actionType = action->getTypeId();
567
568 // Treat rendering and picking cases specially
569 if (actionType.isDerivedFrom(SoRayPickAction::getClassTypeId()))
570 rayPickTriangle((SoRayPickAction *) action, v1, v2, v3);
571
572 else if (actionType.isDerivedFrom(SoGLRenderAction::getClassTypeId()))
573 GLRenderTriangle((SoGLRenderAction *) action, v1, v2, v3);
574
575 // Otherwise, this is invoked through the callback action, so
576 // invoke the triangle callbacks.
577 else {
578 SoCallbackAction *cbAct = (SoCallbackAction *) action;
579 cbAct->invokeTriangleCallbacks(this, v1, v2, v3);
580 }
581 }
582
583 ////////////////////////////////////////////////////////////////////////
584 //
585 // Description:
586 // Invokes correct callbacks for line segment primitive generation.
587 //
588 // Use: protected
589
590 void
591 SoShape::invokeLineSegmentCallbacks(SoAction *action,
592 const SoPrimitiveVertex *v1,
593 const SoPrimitiveVertex *v2)
594 //
595 ////////////////////////////////////////////////////////////////////////
596 {
597 const SoType &actionType = action->getTypeId();
598
599 // Treat rendering and picking cases specially
600 if (actionType.isDerivedFrom(SoRayPickAction::getClassTypeId()))
601 rayPickLineSegment((SoRayPickAction *) action, v1, v2);
602
603 else if (actionType.isDerivedFrom(SoGLRenderAction::getClassTypeId()))
604 GLRenderLineSegment((SoGLRenderAction *) action, v1, v2);
605
606 // Otherwise, this is invoked through the callback action, so
607 // invoke the triangle callbacks.
608 else {
609 SoCallbackAction *cbAct = (SoCallbackAction *) action;
610 cbAct->invokeLineSegmentCallbacks(this, v1, v2);
611 }
612 }
613
614 ////////////////////////////////////////////////////////////////////////
615 //
616 // Description:
617 // Invokes correct callbacks for point primitive generation.
618 //
619 // Use: protected
620
621 void
622 SoShape::invokePointCallbacks(SoAction *action, const SoPrimitiveVertex *v)
623 //
624 ////////////////////////////////////////////////////////////////////////
625 {
626 const SoType &actionType = action->getTypeId();
627
628 // Treat rendering and picking cases specially
629 if (actionType.isDerivedFrom(SoRayPickAction::getClassTypeId()))
630 rayPickPoint((SoRayPickAction *) action, v);
631
632 else if (actionType.isDerivedFrom(SoGLRenderAction::getClassTypeId()))
633 GLRenderPoint((SoGLRenderAction *) action, v);
634
635 // Otherwise, this is invoked through the callback action, so
636 // invoke the triangle callbacks.
637 else {
638 SoCallbackAction *cbAct = (SoCallbackAction *) action;
639 cbAct->invokePointCallbacks(this, v);
640 }
641 }
642
643 ////////////////////////////////////////////////////////////////////////
644 //
645 // Description:
646 // Begins a shape composed of triangles during primitive generation.
647 //
648 // Use: protected
649
650 void
651 SoShape::beginShape(SoAction *action, TriangleShape shapeType,
652 SoFaceDetail *_faceDetail)
653 //
654 ////////////////////////////////////////////////////////////////////////
655 {
656 if (primVerts == NULL) {
657 primVerts = new SoPrimitiveVertex[2];
658 vertDetails = new SoPointDetail[2];
659 }
660
661 primShapeType = shapeType;
662 primVertNum = 0;
663 primShape = this;
664 primAction = action;
665
666 // Save face detail unless we are called recursively
667 if (nestLevel++ == 0)
668 faceDetail = _faceDetail;
669
670 switch (shapeType) {
671
672 case TRIANGLE_STRIP:
673 case TRIANGLE_FAN:
674 case TRIANGLES:
675 // If the face detail is not NULL, get it ready to store the 3
676 // point details for each triangle
677 if (faceDetail != NULL)
678 faceDetail->setNumPoints(3);
679 break;
680
681 case POLYGON:
682 {
683 SoShapeHintsElement::VertexOrdering vo;
684 SoShapeHintsElement::ShapeType st;
685 SoShapeHintsElement::FaceType ft;
686 SoShapeHintsElement::get(action->getState(), vo, st, ft);
687
688 if (ft == SoShapeHintsElement::CONVEX) {
689 // Convex polygons can be drawn as triangle fans
690 primShapeType = TRIANGLE_FAN;
691 // Do the same stuff needed for TRIANGLE_FAN:
692 if (faceDetail != NULL)
693 faceDetail->setNumPoints(3);
694 }
695 else polyVertNum = 0;
696 }
697 break;
698 }
699 }
700
701 ////////////////////////////////////////////////////////////////////////
702 //
703 // Description:
704 // Adds a vertex to a shape composed of triangles during primitive
705 // generation.
706 //
707 // Use: protected
708
709 void
710 SoShape::shapeVertex(const SoPrimitiveVertex *v)
711 //
712 ////////////////////////////////////////////////////////////////////////
713 {
714 switch (primShapeType) {
715
716 case TRIANGLE_STRIP:
717 triangleVertex(v, primVertNum & 1);
718 break;
719
720 case TRIANGLE_FAN:
721 triangleVertex(v, primVertNum == 0 ? 0 : 1);
722 break;
723
724 case TRIANGLES:
725 triangleVertex(v, primVertNum == 2 ? -1 : primVertNum);
726 // Reset for next triangle if processed 3 vertices
727 if (primVertNum == 3)
728 primVertNum = 0;
729 break;
730
731 case POLYGON:
732 // Make sure there is enough room in polyVerts array
733 allocateVerts();
734 polyVerts[polyVertNum] = *v;
735
736 if (faceDetail != NULL) {
737
738 // Save point detail for given vertex in array
739 polyDetails[polyVertNum] =
740 * (const SoPointDetail *) v->getDetail();
741
742 // Store pointer to point detail in saved polygon vertex
743 polyVerts[polyVertNum].setDetail(&polyDetails[polyVertNum]);
744 }
745
746 ++polyVertNum;
747 break;
748 }
749 }
750
751 ////////////////////////////////////////////////////////////////////////
752 //
753 // Description:
754 // Implements shapeVertex() for one of the three triangle-based types.
755 //
756 // Use: private
757
758 void
759 SoShape::triangleVertex(const SoPrimitiveVertex *v, int vertToReplace)
760 //
761 ////////////////////////////////////////////////////////////////////////
762 {
763 if (faceDetail == NULL) {
764 // Generate a triangle if we have 3 vertices
765 if (primVertNum >= 2)
766 invokeTriangleCallbacks(primAction,
767 &primVerts[0], &primVerts[1], v);
768
769 // Save vertex in one of the two slots
770 if (vertToReplace >= 0)
771 primVerts[vertToReplace] = *v;
772 }
773
774 // If face detail was supplied, set it to contain the 3 point
775 // details. Make sure the primitive vertices all point to the
776 // face detail.
777 else {
778 const SoPointDetail *pd = (const SoPointDetail *) v->getDetail();
779 SoPrimitiveVertex pv = *v;
780
781 pv.setDetail(faceDetail);
782
783 // Generate a triangle if we have 3 vertices
784 if (primVertNum >= 2) {
785 faceDetail->setPoint(0, &vertDetails[0]);
786 faceDetail->setPoint(1, &vertDetails[1]);
787 faceDetail->setPoint(2, pd);
788 invokeTriangleCallbacks(primAction,
789 &primVerts[0], &primVerts[1], &pv);
790 }
791
792 // Save vertex and details in one of the two slots
793 if (vertToReplace >= 0) {
794 primVerts[vertToReplace] = pv;
795 vertDetails[vertToReplace] = *pd;
796 }
797 }
798
799 primVertNum++;
800 }
801
802 ////////////////////////////////////////////////////////////////////////
803 //
804 // Description:
805 // For polygons, re-allocates the polyVerts array if more space is
806 // needed to hold all vertices. We have to hold all of the
807 // vertices in memory because the tesellator can't decompose
808 // concave polygons until it has seen all of the vertices.
809 //
810 // Use: private
811
812 void
813 SoShape::allocateVerts()
814 //
815 ////////////////////////////////////////////////////////////////////////
816 {
817 // 8 vertices are allocated to begin with
818 if (polyVerts == NULL) {
819 polyVerts = new SoPrimitiveVertex[8];
820 polyDetails = new SoPointDetail[8];
821 numPolyVertsAllocated = 8;
822 }
823
824 else {
825 if (polyVertNum >= numPolyVertsAllocated) {
826 SoPrimitiveVertex *oldVerts = polyVerts;
827 SoPointDetail *oldDetails = polyDetails;
828
829 // Double storage
830 numPolyVertsAllocated = polyVertNum*2;
831 polyVerts = new SoPrimitiveVertex[numPolyVertsAllocated];
832 polyDetails = new SoPointDetail[numPolyVertsAllocated];
833
834 // Copy over old vertices and details
835 for (int i = 0; i < polyVertNum; i++) {
836 polyVerts[i] = oldVerts[i];
837 polyDetails[i] = oldDetails[i];
838 polyVerts[i].setDetail(&polyDetails[i]);
839 }
840
841 // Delete old storage
842 delete [] oldVerts;
843 delete [] oldDetails;
844 }
845 }
846 }
847
848 ////////////////////////////////////////////////////////////////////////
849 //
850 // Description:
851 // Ends a shape composed of triangles during primitive generation.
852 //
853 // Use: protected
854
855 void
856 SoShape::endShape()
857 //
858 ////////////////////////////////////////////////////////////////////////
859 {
860 int i;
861
862 switch (primShapeType) {
863 case TRIANGLE_STRIP:
864 case TRIANGLE_FAN:
865 case TRIANGLES:
866 primVertNum = 0;
867 break;
868
869 case POLYGON:
870 // Don't bother with degenerate polygons
871 if (polyVertNum < 3) {
872 polyVertNum = 0;
873 break;
874 }
875
876 // Concave polygons need to be tesselated; we'll use the
877 // GLU routines to do this:
878 if (tobj == NULL) {
879 tobj = gluNewTess();
880 gluTessCallback(tobj, (GLenum)GLU_BEGIN,
881 (void (*)())SoShape::beginCB);
882 gluTessCallback(tobj, (GLenum)GLU_END,
883 (void (*)())SoShape::endCB);
884 gluTessCallback(tobj, (GLenum)GLU_VERTEX,
885 (void (*)())SoShape::vtxCB);
886 gluTessCallback(tobj, (GLenum)GLU_ERROR,
887 (void (*)())SoShape::errorCB);
888 }
889 #ifdef GLU_VERSION_1_2
890 gluTessBeginPolygon(tobj, NULL);
891 gluTessBeginContour(tobj);
892 #else
893 gluBeginPolygon(tobj);
894 #endif
895
896 for (i = 0; i < polyVertNum; i++) {
897 const SbVec3f &t = polyVerts[i].getPoint();
898
899 GLdouble dv[3]; // glu requires double...
900 dv[0] = t[0]; dv[1] = t[1]; dv[2] = t[2];
901 gluTessVertex(tobj, dv, (void *)&polyVerts[i]);
902 }
903 #ifdef GLU_VERSION_1_2
904 gluTessEndContour(tobj);
905 gluTessEndPolygon(tobj);
906 #else
907 gluEndPolygon(tobj);
908 #endif
909
910 polyVertNum = 0;
911 break;
912 }
913
914 nestLevel--;
915 }
916
917 ////////////////////////////////////////////////////////////////////////
918 //
919 // Description:
920 // Called by the GLU tesselator when we are beginning a triangle
921 // strip, fan, or set of independent triangles.
922 //
923 // Use: static, private
924
925 void
926 SoShape::beginCB(GLenum primType)
927 //
928 ////////////////////////////////////////////////////////////////////////
929 {
930 switch(primType) {
931 case GL_TRIANGLE_STRIP:
932 primShape->beginShape(primShape->primAction, TRIANGLE_STRIP);
933 break;
934 case GL_TRIANGLE_FAN:
935 primShape->beginShape(primShape->primAction, TRIANGLE_FAN);
936 break;
937 case GL_TRIANGLES:
938 primShape->beginShape(primShape->primAction, TRIANGLES);
939 break;
940 }
941 }
942
943 ////////////////////////////////////////////////////////////////////////
944 //
945 // Description:
946 // Called by the GLU tesselator when we are generating primitives.
947 //
948 // Use: static, private
949
950 void
951 SoShape::vtxCB(void *data)
952 //
953 ////////////////////////////////////////////////////////////////////////
954 {
955 const SoPrimitiveVertex *v = (const SoPrimitiveVertex *)data;
956
957 primShape->shapeVertex(v);
958 }
959
960 ////////////////////////////////////////////////////////////////////////
961 //
962 // Description:
963 // Called by the GLU tesselator when we are done with the
964 // strip/fan/etc.
965 //
966 // Use: static, private
967
968 void
969 SoShape::endCB()
970 //
971 ////////////////////////////////////////////////////////////////////////
972 {
973 primShape->endShape();
974 }
975
976 ////////////////////////////////////////////////////////////////////////
977 //
978 // Description:
979 // Called by the GLU tesselator if there is an error (typically
980 // because the polygons self-intersects).
981 //
982 // Use: static, private
983
984 void
985 SoShape::errorCB(GLenum err)
986 //
987 ////////////////////////////////////////////////////////////////////////
988 {
989 SoDebugError::post("SoShape::errorCB",
990 "GLU error: %s", gluErrorString(err));
991 }
992
993 //
994 // This macro is used by the rendering methods to follow:
995 //
996
997 #define RENDER_VERTEX(pv) \
998 if (sendTexCoords) \
999 glTexCoord4fv(pv->getTextureCoords().getValue()); \
1000 matlBundle->send(pv->getMaterialIndex(), TRUE); \
1001 if (! matlBundle->isColorOnly()) \
1002 glNormal3fv(pv->getNormal().getValue()); \
1003 glVertex3fv(pv->getPoint().getValue())
1004
1005 ////////////////////////////////////////////////////////////////////////
1006 //
1007 // Description:
1008 // Renders a triangle primitive generated by a subclass.
1009 //
1010 // Use: private
1011
1012 void
1013 SoShape::GLRenderTriangle(SoGLRenderAction *,
1014 const SoPrimitiveVertex *v1,
1015 const SoPrimitiveVertex *v2,
1016 const SoPrimitiveVertex *v3)
1017 //
1018 ////////////////////////////////////////////////////////////////////////
1019 {
1020 glBegin(GL_TRIANGLES);
1021
1022 RENDER_VERTEX(v1);
1023 RENDER_VERTEX(v2);
1024 RENDER_VERTEX(v3);
1025
1026 glEnd();
1027 }
1028
1029 ////////////////////////////////////////////////////////////////////////
1030 //
1031 // Description:
1032 // Renders a line segment primitive generated by a subclass.
1033 //
1034 // Use: private
1035
1036 void
1037 SoShape::GLRenderLineSegment(SoGLRenderAction *,
1038 const SoPrimitiveVertex *v1,
1039 const SoPrimitiveVertex *v2)
1040 //
1041 ////////////////////////////////////////////////////////////////////////
1042 {
1043 glBegin(GL_LINES);
1044
1045 RENDER_VERTEX(v1);
1046 RENDER_VERTEX(v2);
1047
1048 glEnd();
1049 }
1050
1051 ////////////////////////////////////////////////////////////////////////
1052 //
1053 // Description:
1054 // Renders a point primitive generated by a subclass.
1055 //
1056 // Use: private
1057
1058 void
1059 SoShape::GLRenderPoint(SoGLRenderAction *, const SoPrimitiveVertex *v)
1060 //
1061 ////////////////////////////////////////////////////////////////////////
1062 {
1063 glBegin(GL_POINTS);
1064
1065 RENDER_VERTEX(v);
1066
1067 glEnd();
1068 }
1069
1070 #undef RENDER_VERTEX
1071
1072 ////////////////////////////////////////////////////////////////////////
1073 //
1074 // Description:
1075 // Picks a triangle primitive generated by a subclass.
1076 //
1077 // Use: private
1078
1079 void
1080 SoShape::rayPickTriangle(SoRayPickAction *action,
1081 const SoPrimitiveVertex *v1,
1082 const SoPrimitiveVertex *v2,
1083 const SoPrimitiveVertex *v3)
1084 //
1085 ////////////////////////////////////////////////////////////////////////
1086 {
1087 SbVec3f point;
1088 SbVec3f barycentric;
1089 SbBool onFrontSide;
1090 SoPickedPoint *pp;
1091
1092 if (action->intersect(v1->getPoint(), v2->getPoint(), v3->getPoint(),
1093 point, barycentric, onFrontSide) &&
1094 (pp = action->addIntersection(point)) != NULL) {
1095
1096 SbVec3f norm;
1097 SbVec4f texCoord;
1098 SoDetail *detail;
1099
1100 // Compute normal by interpolating vertex normals using
1101 // barycentric coordinates
1102 norm = (v1->getNormal() * barycentric[0] +
1103 v2->getNormal() * barycentric[1] +
1104 v3->getNormal() * barycentric[2]);
1105 norm.normalize();
1106 pp->setObjectNormal(norm);
1107
1108 // Compute texture coordinates the same way
1109 texCoord = (v1->getTextureCoords() * barycentric[0] +
1110 v2->getTextureCoords() * barycentric[1] +
1111 v3->getTextureCoords() * barycentric[2]);
1112 pp->setObjectTextureCoords(texCoord);
1113
1114 // Copy material index from closest detail, since it can't
1115 // be interpolated
1116 if (barycentric[0] < barycentric[1] && barycentric[0] < barycentric[2])
1117 pp->setMaterialIndex(v1->getMaterialIndex());
1118 else if (barycentric[1] < barycentric[2])
1119 pp->setMaterialIndex(v2->getMaterialIndex());
1120 else
1121 pp->setMaterialIndex(v3->getMaterialIndex());
1122
1123 // Create a detail for the specific shape
1124 detail = createTriangleDetail(action, v1, v2, v3, pp);
1125 if (detail != NULL)
1126 pp->setDetail(detail, this);
1127 }
1128 }
1129
1130 ////////////////////////////////////////////////////////////////////////
1131 //
1132 // Description:
1133 // Picks a line segment primitive generated by a subclass.
1134 //
1135 // Use: private
1136
1137 void
1138 SoShape::rayPickLineSegment(SoRayPickAction *action,
1139 const SoPrimitiveVertex *v1,
1140 const SoPrimitiveVertex *v2)
1141 //
1142 ////////////////////////////////////////////////////////////////////////
1143 {
1144 SbVec3f point;
1145 SoPickedPoint *pp;
1146
1147 if (action->intersect(v1->getPoint(), v2->getPoint(), point) &&
1148 (pp = action->addIntersection(point)) != NULL) {
1149
1150 float ratioFromV1;
1151 SbVec3f norm;
1152 SbVec4f texCoord;
1153 SoDetail *detail;
1154
1155 // Compute normal by interpolating vertex normals
1156 ratioFromV1 = ((point - v1->getPoint()).length() /
1157 (v2->getPoint() - v1->getPoint()).length());
1158 norm = (v1->getNormal() * (1.0 - ratioFromV1) +
1159 v2->getNormal() * ratioFromV1);
1160 norm.normalize();
1161 pp->setObjectNormal(norm);
1162
1163 // Compute texture coordinates the same way
1164 texCoord = (v1->getTextureCoords() * (1.0 - ratioFromV1) +
1165 v2->getTextureCoords() * ratioFromV1);
1166 pp->setObjectTextureCoords(texCoord);
1167
1168 // Copy material index from closer detail, since it can't be
1169 // interpolated
1170 if (ratioFromV1 < 0.5)
1171 pp->setMaterialIndex(v1->getMaterialIndex());
1172 else
1173 pp->setMaterialIndex(v2->getMaterialIndex());
1174
1175 // Create a detail for the specific shape
1176 detail = createLineSegmentDetail(action, v1, v2, pp);
1177 if (detail != NULL)
1178 pp->setDetail(detail, this);
1179 }
1180 }
1181
1182 ////////////////////////////////////////////////////////////////////////
1183 //
1184 // Description:
1185 // Picks a point primitive generated by a subclass.
1186 //
1187 // Use: private
1188
1189 void
1190 SoShape::rayPickPoint(SoRayPickAction *action, const SoPrimitiveVertex *v)
1191 //
1192 ////////////////////////////////////////////////////////////////////////
1193 {
1194 SoPickedPoint *pp;
1195
1196 if (action->intersect(v->getPoint()) &&
1197 (pp = action->addIntersection(v->getPoint())) != NULL) {
1198
1199 SoDetail *detail;
1200
1201 pp->setObjectNormal(v->getNormal());
1202 pp->setObjectTextureCoords(v->getTextureCoords());
1203 pp->setMaterialIndex(v->getMaterialIndex());
1204
1205 // Create a detail for the specific shape
1206 detail = createPointDetail(action, v, pp);
1207 if (detail != NULL)
1208 pp->setDetail(detail, this);
1209 }
1210 }
1211
1212 ////////////////////////////////////////////////////////////////////////
1213 //
1214 // Description:
1215 // Applies render action to the bounding box surrounding the shape.
1216 // This is used to render shapes when BOUNDING_BOX complexity is on.
1217 //
1218 // Use: protected
1219
1220 void
1221 SoShape::GLRenderBoundingBox(SoGLRenderAction *action)
1222 //
1223 ////////////////////////////////////////////////////////////////////////
1224 {
1225 // Create a surrogate cube to render, if not already done
1226 if (bboxCube == NULL) {
1227 bboxCube = new SoCube;
1228 bboxCube->ref();
1229 }
1230
1231 // Compute the bounding box of the shape, using the virtual
1232 // computeBBox() method. By using this method (rather than by
1233 // applying an SoGetBoundingBoxAction), we can make sure that any
1234 // elements used to compute the bounding box are known to any
1235 // currently open caches in the render action. Otherwise, objects
1236 // (such as 2D text) that use extra elements to compute bounding
1237 // boxes would not be rendered correctly when cached.
1238 SbBox3f box;
1239 SbVec3f center;
1240 computeBBox(action, box, center);
1241
1242 // Render the cube using a special method that is designed for
1243 // this task
1244 bboxCube->GLRenderBoundingBox(action, box);
1245 }
1246
1247 ////////////////////////////////////////////////////////////////////////
1248 //
1249 // Description:
1250 // Applies rayPick action to the bounding box surrounding the
1251 // shape. This is used to pick shapes when BOUNDING_BOX complexity
1252 // is on.
1253 //
1254 // Use: private
1255
1256 void
1257 SoShape::rayPickBoundingBox(SoRayPickAction *action)
1258 //
1259 ////////////////////////////////////////////////////////////////////////
1260 {
1261 // Create a surrogate cube to pick if not already done
1262 if (bboxCube == NULL) {
1263 bboxCube = new SoCube;
1264 bboxCube->ref();
1265 }
1266
1267 // Compute the bounding box of the shape, using the virtual
1268 // computeBBox() method
1269 SbBox3f box;
1270 SbVec3f center;
1271 computeBBox(action, box, center);
1272
1273 // Pick the cube using a special method that is designed for
1274 // this task
1275 bboxCube->rayPickBoundingBox(action, box);
1276 }