Debian Codegraph @

libSoXt/src/viewers/SoXtViewer.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-93 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 : SoXtViewer
46 |
47 | Author(s) : Alain Dumesny
48 |
49 ______________ S I L I C O N G R A P H I C S I N C . ____________
50 _______________________________________________________________________
51 */
52
53 #include <inttypes.h>
54 #include <cstdio>
55 #include <cmath>
56 #include <cstring>
57
58 #include <X11/Intrinsic.h>
59 #include <X11/Xlib.h>
60 #include <X11/keysym.h>
61
62 #ifdef __sgi
63 #include <X11/extensions/SGIStereo.h>
64 #endif
65
66 #include <GL/gl.h>
67 #include <GL/glu.h>
68 #include <Inventor/SbBox.h>
69 #include <Inventor/SbLinear.h>
70 #include <Inventor/SbPList.h>
71 #include <Inventor/SbViewportRegion.h>
72 #include <Inventor/SoDB.h>
73 #include <Inventor/SoPath.h>
74 #include <Inventor/SoPath.h>
75 #include <Inventor/SoPickedPoint.h>
76 #include <Inventor/actions/SoGetBoundingBoxAction.h>
77 #include <Inventor/actions/SoRayPickAction.h>
78 #include <Inventor/actions/SoSearchAction.h>
79 #include <Inventor/errors/SoDebugError.h>
80 #include <Inventor/nodes/SoPackedColor.h>
81 #include <Inventor/nodes/SoMaterialBinding.h>
82 #include <Inventor/nodes/SoComplexity.h>
83 #include <Inventor/nodes/SoDirectionalLight.h>
84 #include <Inventor/nodes/SoDrawStyle.h>
85 #include <Inventor/nodes/SoGroup.h>
86 #include <Inventor/nodes/SoLightModel.h>
87 #include <Inventor/nodes/SoOrthographicCamera.h>
88 #include <Inventor/nodes/SoPerspectiveCamera.h>
89 #include <Inventor/nodes/SoResetTransform.h>
90 #include <Inventor/nodes/SoRotation.h>
91 #include <Inventor/nodes/SoSeparator.h>
92 #include <Inventor/nodes/SoSwitch.h>
93 #include <Inventor/nodes/SoLocateHighlight.h>
94 #include <Inventor/sensors/SoFieldSensor.h>
95 #include <Inventor/fields/SoSFTime.h>
96 #include <Inventor/Xt/devices/SoXtInputFocus.h>
97 #include <Inventor/Xt/SoXtClipboard.h>
98 #include <Inventor/Xt/viewers/SoXtViewer.h>
99
100 // keep a pointer to global time, since we are going to access it a lot.
101 SoSFTime *SoXtViewer::viewerRealTime = NULL;
102
103
104 ////////////////////////////////////////////////////////////////////////
105 //
106 // Constructor.
107 //
108 // Use: protected
109
110 SoXtViewer::SoXtViewer(
111 Widget parent,
112 const char *name,
113 SbBool buildInsideParent,
114 SoXtViewer::Type t,
115 SbBool buildNow)
116 : SoXtRenderArea(
117 parent,
118 name,
119 buildInsideParent,
120 TRUE, // getMouseInput
121 TRUE, // getKeyboardInput
122 FALSE) // buildNow
123 //
124 ////////////////////////////////////////////////////////////////////////
125 {
126 // init local vars
127 type = t;
128 camera = NULL;
129 cameraType = SoPerspectiveCamera::getClassTypeId();
130 createdCamera = FALSE;
131 viewingFlag = TRUE;
132 altSwitchBack = FALSE;
133 cursorEnabledFlag = TRUE;
134 interactiveFlag = FALSE;
135 startCBList = new SoCallbackList;
136 finishCBList = new SoCallbackList;
137 interactiveCount = 0;
138 bufferType = isDoubleBuffer() ? BUFFER_DOUBLE : BUFFER_SINGLE;
139 stereoOffset = 3.0;
140 #ifdef __sgi
141 useSGIStereoExt = FALSE;
142 #endif
143 sceneSize = 0.0; // not computed yet.
144 viewerSpeed = 1.0; // default. SoXtFullViewer add UI to increase/decrease
145
146 // add Enter and Leave notify events for the viewers
147 inputFocus = new SoXtInputFocus;
148 registerDevice(inputFocus);
149
150 if (! viewerRealTime)
151 viewerRealTime = (SoSFTime *) SoDB::getGlobalField("realTime");
152
153 // init auto clipping stuff
154 autoClipFlag = TRUE;
155 minimumNearPlane = 0.001;
156 autoClipBboxAction = new SoGetBoundingBoxAction(SbVec2s(1,1)); // ??? no valid size yet
157
158 // copy/paste support
159 clipboard = NULL;
160
161 // init seek animation variables
162 seekDistance = 50.0;
163 seekDistAsPercentage = TRUE;
164 seekModeFlag = FALSE;
165 detailSeekFlag = TRUE;
166 seekAnimTime = 2.0;
167 seekAnimationSensor = new SoFieldSensor(SoXtViewer::seekAnimationSensorCB, this);
168
169 //
170 // build the small internal graph (nodes used for draw style stuff)
171 //
172 sceneRoot = new SoSeparator(4);
173 drawStyleSwitch = new SoSwitch(6);
174 drawStyleNode = new SoDrawStyle;
175 lightModelNode = new SoLightModel;
176 colorNode = new SoPackedColor;
177 matBindingNode = new SoMaterialBinding;
178 complexityNode = new SoComplexity;
179 sceneGraph = NULL;
180
181 // note: we cannot setSceneGraph on the renderArea in the constructor
182 // since it calls virtual functions, and all of our members aren't
183 // initialized yet. We'll call it the first time our setSceneGraph
184 // is called.
185 sceneRoot->ref();
186 sceneRoot->renderCaching.setValue(SoSeparator::OFF); // no caching there
187 sceneRoot->renderCulling.setValue(SoSeparator::OFF); // no culling there
188 sceneRoot->addChild(drawStyleSwitch);
189 drawStyleSwitch->addChild(drawStyleNode);
190 drawStyleSwitch->addChild(lightModelNode);
191 drawStyleSwitch->addChild(colorNode);
192 drawStyleSwitch->addChild(matBindingNode);
193 drawStyleSwitch->addChild(complexityNode);
194
195 // set the draw style vars and fields that don't change - once we
196 // have a context, will will use glGetString() to pick a better default
197 // draw style.
198 stillDrawStyle = VIEW_AS_IS;
199 interactiveDrawStyle = VIEW_SAME_AS_STILL;
200 checkForDrawStyle = TRUE;
201 drawStyleSwitch->whichChild = SO_SWITCH_NONE;
202
203 drawStyleNode->setOverride(TRUE); // only use style field
204 drawStyleNode->pointSize = 3.0;
205 drawStyleNode->lineWidth.setIgnored(TRUE);
206 drawStyleNode->linePattern.setIgnored(TRUE);
207
208 lightModelNode->setOverride(TRUE);
209
210 colorNode->setOverride(TRUE);
211
212 matBindingNode->setOverride(TRUE);
213 matBindingNode->value = SoMaterialBinding::OVERALL;
214
215 complexityNode->setOverride(TRUE);
216 complexityNode->textureQuality = 0; // always turn texture off under switch
217 complexityNode->value = 0.15;
218
219 addStartCallback(SoXtViewer::drawStyleStartCallback);
220 addFinishCallback(SoXtViewer::drawStyleFinishCallback);
221
222 //
223 // headlightGroup - we have a rotation which keeps the headlight
224 // moving whenever the camera moves, and a reset xform so
225 // that the rest of the scene is not affected by the first rot.
226 // these leaves the direction field in the headlight open for the
227 // user to edit, allowing for the direction to change w.r.t. the camera.
228 //
229 headlightGroup = new SoGroup(3);
230 headlightRot = new SoRotation;
231 headlightNode = new SoDirectionalLight;
232 headlightGroup->ref();
233 headlightGroup->addChild(headlightRot);
234 headlightGroup->addChild(headlightNode);
235 headlightGroup->addChild(new SoResetTransform);
236 headlightNode->direction.setValue(SbVec3f(.2, -.2, -.9797958971));
237 headlightFlag = TRUE;
238
239 // Build the widget tree, and let SoXtComponent know about our base widget.
240 if (buildNow) {
241 Widget w = buildWidget(getParentWidget());
242 setBaseWidget(w);
243 }
244 }
245
246 ////////////////////////////////////////////////////////////////////////
247 //
248 // Destructor.
249 //
250 // Use: protected
251
252 SoXtViewer::~SoXtViewer()
253 //
254 ////////////////////////////////////////////////////////////////////////
255 {
256 // detach everything
257 if ( sceneGraph != NULL )
258 setSceneGraph(NULL);
259 sceneRoot->unref();
260
261 // delete everything
262 delete inputFocus;
263 delete seekAnimationSensor;
264 delete clipboard;
265 delete autoClipBboxAction;
266 delete startCBList;
267 delete finishCBList;
268 headlightGroup->unref();
269 }
270
271 ////////////////////////////////////////////////////////////////////////
272 //
273 // Description:
274 // Set a new user supplied scene graph.
275 //
276 // use: virtual public
277 //
278 void
279 SoXtViewer::setSceneGraph(SoNode *newScene)
280 //
281 ////////////////////////////////////////////////////////////////////////
282 {
283 // if we haven't already given the render area a scene graph sceneRoot,
284 // give it the scene graph now. This is a one shot deal, which
285 // cannot be done in the constructor.
286 if (SoXtRenderArea::getSceneGraph() == NULL)
287 SoXtRenderArea::setSceneGraph(sceneRoot);
288
289 // draw new scene graphs to the front buffer by default since
290 // the scene will be different (we might has well see something
291 // happening for the first redraw).
292 if (isDrawToFrontBufferEnable())
293 drawToFrontBuffer = TRUE;
294
295 //
296 // detach everything that depends on the old sceneGraph
297 //
298 if ( sceneGraph != NULL ) {
299 setCamera(NULL);
300 sceneRoot->removeChild(sceneGraph);
301 }
302
303 sceneGraph = newScene;
304
305 //
306 // now assign the new sceneGraph, find or create the new camera
307 // and attach things back.
308 //
309 if ( sceneGraph != NULL ) {
310 sceneRoot->addChild(sceneGraph);
311
312 // search for first camera in the scene
313 SoSearchAction sa;
314 sa.setType(SoCamera::getClassTypeId());
315 sa.setSearchingAll(FALSE); // don't look under off switches
316 sa.apply(sceneGraph);
317
318 SoCamera *newCamera = NULL;
319 if (sa.getPath())
320 newCamera = (SoCamera *)((SoFullPath *)sa.getPath())->getTail();
321
322 // if no camera found create one of the right kind...
323 if ( newCamera == NULL ) {
324
325 newCamera = (SoCamera*) cameraType.createInstance();
326 if (newCamera == NULL) {
327 #ifdef DEBUG
328 SoDebugError::post("SoXtViewer::setSceneGraph",
329 "unknown camera type!");
330 #endif
331 // ??? what should we do here ?
332 cameraType = SoPerspectiveCamera::getClassTypeId();
333 newCamera = new SoPerspectiveCamera;
334 }
335 createdCamera = TRUE;
336
337 if (type == SoXtViewer::BROWSER)
338 // add camera after drawstyle stuff
339 sceneRoot->insertChild(newCamera, 1);
340 else {
341 // check to make sure scene starts with at least a group node
342 if ( sceneGraph->isOfType(SoGroup::getClassTypeId()) )
343 ((SoGroup *)sceneGraph)->insertChild(newCamera, 0);
344 else {
345 // make scene start with a group node
346 SoGroup *group = new SoGroup;
347 group->addChild(newCamera);
348 group->addChild(sceneGraph);
349 sceneRoot->addChild(group);
350 sceneRoot->removeChild(sceneGraph);
351 sceneGraph = group;
352 }
353 }
354
355 newCamera->viewAll(sceneGraph, SbViewportRegion(getGlxSize()));
356 }
357
358 setCamera(newCamera);
359 }
360
361 // recompute the scene size variables...
362 recomputeSceneSize();
363 }
364
365 ////////////////////////////////////////////////////////////////////////
366 //
367 // Description:
368 // Recomputes the scene sizes...
369 //
370 // use: virtual public
371
372 void
373 SoXtViewer::recomputeSceneSize()
374 //
375 ////////////////////////////////////////////////////////////////////////
376 {
377 if (! sceneGraph || ! sceneRoot) {
378 sceneSize = 0.0;
379 return;
380 }
381
382 // Use assignment notation to disambiguate from expression (edison)
383 SoGetBoundingBoxAction bboxAct = SoGetBoundingBoxAction(SbViewportRegion(getGlxSize()));
384 bboxAct.apply(sceneRoot);
385 SbBox3f bbox = bboxAct.getBoundingBox();
386
387 if (bbox.isEmpty()) {
388 sceneSize = 0.0;
389 return;
390 }
391
392 float x, y, z;
393 bbox.getSize(x, y, z);
394 sceneSize = (x > z) ? x : z;
395 if (y > sceneSize)
396 sceneSize = y;
397 if (sceneSize <= 0.0)
398 sceneSize = 0.0;
399 }
400
401 ////////////////////////////////////////////////////////////////////////
402 //
403 // Description:
404 // Return the user supplied scene graph.
405 //
406 // use: public, virtual
407
408 SoNode *
409 SoXtViewer::getSceneGraph()
410 //
411 ////////////////////////////////////////////////////////////////////////
412 {
413 return sceneGraph;
414 }
415
416
417 ////////////////////////////////////////////////////////////////////////
418 //
419 // Description:
420 // Sets the camera to use.
421 //
422 // Use: virtual public
423 void
424 SoXtViewer::setCamera(SoCamera *newCamera)
425 //
426 ////////////////////////////////////////////////////////////////////////
427 {
428 // check for trivual return
429 if (camera == newCamera)
430 return;
431
432 //
433 // detach everything that depended on the old camera
434 //
435 if ( camera != NULL ) {
436
437 if (headlightFlag) {
438 setHeadlight(FALSE);
439 headlightFlag = TRUE; // can later be turned on
440 }
441
442 if (viewingFlag) {
443 setViewing(FALSE);
444 viewingFlag = TRUE; // can later be turned on
445 }
446
447 // remove the camera if we created one outside of the
448 // scene graph.
449 if (createdCamera && type == SoXtViewer::BROWSER) {
450 if (sceneRoot->findChild(camera) >= 0)
451 sceneRoot->removeChild(camera);
452 createdCamera = FALSE;
453 }
454
455 camera->unref();
456 }
457
458 camera = newCamera;
459
460 //
461 // attach everything that depends on the new camera
462 //
463 if ( camera != NULL) {
464 camera->ref();
465
466 if (headlightFlag) {
467 headlightFlag = FALSE; // enables the routine to be called
468 setHeadlight(TRUE);
469 }
470
471 if (viewingFlag) {
472 viewingFlag = FALSE; // enables the routine to be called
473 setViewing(TRUE);
474 }
475
476 saveHomePosition();
477 }
478 }
479
480 ////////////////////////////////////////////////////////////////////////
481 //
482 // Description:
483 // Set the camera type to create.
484 //
485 // Use: virtual public
486 void
487 SoXtViewer::setCameraType(SoType type)
488 //
489 ////////////////////////////////////////////////////////////////////////
490 {
491 if (type.isDerivedFrom(SoPerspectiveCamera::getClassTypeId()) ||
492 type.isDerivedFrom(SoOrthographicCamera::getClassTypeId()))
493 cameraType = type;
494 #ifdef DEBUG
495 else
496 SoDebugError::post("SoXtViewer::setCameraType",
497 "unknown camera type!");
498 #endif
499 }
500
501 ////////////////////////////////////////////////////////////////////////
502 //
503 // Description:
504 // See the whole scene from the camera
505 //
506 // Use: public
507 void
508 SoXtViewer::viewAll()
509 //
510 ////////////////////////////////////////////////////////////////////////
511 {
512 if ( camera != NULL )
513 camera->viewAll(sceneGraph,SbViewportRegion(getGlxSize()));
514 }
515
516 ////////////////////////////////////////////////////////////////////////
517 //
518 // Description:
519 // Sets the viewing mode.
520 //
521 // Use: virtual public
522 void
523 SoXtViewer::setViewing(SbBool flag)
524 //
525 ////////////////////////////////////////////////////////////////////////
526 {
527 if (flag == viewingFlag)
528 return;
529
530 viewingFlag = flag;
531
532 // if we are goind into viewing mode, then de-highlight any
533 // currently highlighted nodes (since the object will never receive
534 // any motion events).
535 if (viewingFlag) {
536 SoGLRenderAction *glAct = getGLRenderAction();
537 if (glAct)
538 SoLocateHighlight::turnOffCurrentHighlight(glAct);
539 }
540 }
541
542 ////////////////////////////////////////////////////////////////////////
543 //
544 // Description:
545 // Enables/Disable the viewer cursor on the window.
546 //
547 // Use: virtual public
548 void
549 SoXtViewer::setCursorEnabled(SbBool flag)
550 //
551 ////////////////////////////////////////////////////////////////////////
552 {
553 cursorEnabledFlag = flag;
554 }
555
556 ////////////////////////////////////////////////////////////////////////
557 //
558 // Description:
559 // Sets the auto clipping mode
560 //
561 // Use: public
562 void
563 SoXtViewer::setAutoClipping(SbBool flag)
564 //
565 ////////////////////////////////////////////////////////////////////////
566 {
567 if (autoClipFlag == flag)
568 return;
569
570 autoClipFlag = flag;
571
572 // cause a redraw to correctly place the near and far plane now that
573 // auto clipping is on.
574 if (autoClipFlag)
575 scheduleRedraw();
576 }
577
578 ////////////////////////////////////////////////////////////////////////
579 //
580 // Description:
581 // sets stereo mode
582 //
583 // Use: virtual public
584 void
585 SoXtViewer::setStereoViewing(SbBool flag)
586 //
587 ////////////////////////////////////////////////////////////////////////
588 {
589 if (flag == isStereoViewing())
590 return;
591
592 // First, check to see if the OpenGL stereo visual can be created
593 setStereoBuffer(flag);
594
595 #ifdef __sgi
596 // since OpenGL stereo failed, see if the SGI extension will work
597 // by checking whether the X server supports it....
598 int first_event, first_error;
599 if (flag != isStereoViewing() &&
600 XSGIStereoQueryExtension(getDisplay(), &first_event, &first_error)) {
601
602 if (flag) {
603 // make sure the current window will support stereo
604 // ??? if we havn't been managed yet, just assume this visual
605 // ??? will support stereo viewing (see bug
606 if (! getNormalWindow())
607 useSGIStereoExt = TRUE;
608 else if (XSGIQueryStereoMode(getDisplay(), getNormalWindow()) !=
609 X_STEREO_UNSUPPORTED)
610 // stereo will be turned on in the rendering....
611 useSGIStereoExt = TRUE;
612
613 // save the camera original aspect ratio since it will be updated
614 // during rendering to strech the objects. We will restore it
615 // when stereo if OFF.
616 camStereoOrigAspect = camera->aspectRatio.getValue();
617 camStereoOrigVPMapping = camera->viewportMapping.getValue();
618 }
619 else {
620 // turn stereo off on the window
621 useSGIStereoExt = FALSE;
622
623 // clear the left/right buffers to prevent gost images from
624 // the other view...(until the user resets the monitor with setmon)
625 if (isRGBMode()) {
626 SbColor color = getBackgroundColor();
627 glClearColor(color[0], color[1], color[2], 0);
628 }
629 else
630 glClearIndex(getBackgroundIndex());
631
632 glDrawBuffer(GL_FRONT_AND_BACK);
633
634 XSGISetStereoBuffer(getDisplay(), getNormalWindow(), STEREO_BUFFER_LEFT);
635 XSync(getDisplay(), False);
636 glClear(GL_COLOR_BUFFER_BIT);
637
638 XSGISetStereoBuffer(getDisplay(), getNormalWindow(), STEREO_BUFFER_RIGHT);
639 XSync(getDisplay(), False);
640 glClear(GL_COLOR_BUFFER_BIT);
641
642 glDrawBuffer( isDoubleBuffer() ? GL_BACK : GL_FRONT);
643
644 // restore the camera original aspect ratio (saved above)
645 camera->aspectRatio = camStereoOrigAspect;
646 camera->viewportMapping = camStereoOrigVPMapping;
647 }
648
649 // now cause a redraw to see the affect since we havn't changed
650 // the actual visual (unlike OpenGL)
651 if (flag == isStereoViewing())
652 scheduleRedraw();
653 }
654 #endif
655 }
656
657 ////////////////////////////////////////////////////////////////////////
658 //
659 // Description:
660 // gets stereo mode
661 //
662 // Use: virtual public
663 SbBool
664 SoXtViewer::isStereoViewing()
665 //
666 ////////////////////////////////////////////////////////////////////////
667 {
668 #ifdef __sgi
669 return (isStereoBuffer() || useSGIStereoExt);
670 #else
671 // done in SoXtGLWidget
672 return isStereoBuffer();
673 #endif
674 }
675
676 ////////////////////////////////////////////////////////////////////////
677 //
678 // Description:
679 // adds a directional light to the scene graph that is a headlight
680 // positioned over the left shoulder of the camera. It has a sensor
681 // on the camera, so that it always is pointing in the same direction
682 // as the camera. The sensor is not delayed, so that it is "always"
683 // accurate.
684 //
685 // Use: virtual public
686 //
687 void
688 SoXtViewer::setHeadlight(SbBool insertFlag)
689 //
690 ////////////////////////////////////////////////////////////////////////
691 {
692 // check for trivial return
693 if (camera == NULL || headlightFlag == insertFlag) {
694 headlightFlag = insertFlag;
695 return;
696 }
697
698 //
699 // find the camera parent to insert/remove the headlight
700 //
701 SoSearchAction sa;
702 if (insertFlag)
703 sa.setNode(camera);
704 else {
705 sa.setNode(headlightGroup);
706 sa.setSearchingAll(TRUE); // find under OFF switches for removal
707 }
708 sa.apply(sceneRoot);
709 SoFullPath *fullPath = (SoFullPath *) sa.getPath();
710 if (!fullPath) {
711 #if DEBUG
712 SoDebugError::post("SoXtViewer::setHeadlight",
713 insertFlag ? "ERROR: cannot find camera in graph" :
714 "ERROR: cannot find headlight in graph");
715 #endif
716 return;
717 }
718 SoGroup *parent = (SoGroup *) fullPath->getNodeFromTail(1);
719
720 headlightFlag = insertFlag;
721
722 //
723 // inserts/remove the headlight group node
724 //
725 if (headlightFlag) {
726 int camIndex;
727
728 // check to make sure that the camera parent is not a switch node
729 // (VRML camera viewpoints are kept under a switch node). Otherwise
730 // we will insert the headlight right before the switch node.
731 if (parent->isOfType(SoSwitch::getClassTypeId())) {
732 SoNode *switchNode = parent;
733 parent = (SoGroup *) fullPath->getNodeFromTail(2);
734 camIndex = parent->findChild(switchNode);
735 }
736 else
737 camIndex = parent->findChild(camera);
738
739 // return if headlight is already there (this should be an error !)
740 if (parent->findChild(headlightGroup) >= 0)
741 return;
742
743 // insert the light group right after the camera
744 if (camIndex >= 0)
745 parent->insertChild(headlightGroup, camIndex+1);
746 }
747 else {
748 if (parent->findChild(headlightGroup) >= 0)
749 parent->removeChild(headlightGroup);
750 }
751 }
752
753 ////////////////////////////////////////////////////////////////////////
754 //
755 // Description:
756 // Sets the drawing style.
757 //
758 // Use: virtual public
759 void
760 SoXtViewer::setDrawStyle(
761 SoXtViewer::DrawType type, SoXtViewer::DrawStyle style)
762 //
763 ////////////////////////////////////////////////////////////////////////
764 {
765 // prevent us from picking a default draw style if the user already
766 // has done so...
767 checkForDrawStyle = FALSE;
768
769 if (type == STILL) {
770 if (stillDrawStyle == style)
771 return;
772 if (style == VIEW_SAME_AS_STILL) {
773 #ifdef DEBUG
774 SoDebugError::post("SoXtViewer::setDrawStyle",
775 "illegal VIEW_SAME_AS_STILL draw style passed for STILL !");
776 #endif
777 return;
778 }
779 stillDrawStyle = style;
780
781 if (! interactiveFlag || interactiveDrawStyle == VIEW_SAME_AS_STILL
782 || (interactiveDrawStyle == VIEW_NO_TEXTURE && style != VIEW_AS_IS))
783 setCurrentDrawStyle(style);
784 else if (interactiveFlag && interactiveDrawStyle == VIEW_NO_TEXTURE &&
785 style == VIEW_AS_IS)
786 setCurrentDrawStyle(interactiveDrawStyle);
787 }
788 else {
789 // else it type == INTERACTIVE
790
791 if (interactiveDrawStyle == style)
792 return;
793 interactiveDrawStyle = style;
794
795 if (interactiveFlag) {
796 if (style == VIEW_SAME_AS_STILL ||
797 (style == VIEW_NO_TEXTURE && stillDrawStyle != VIEW_AS_IS))
798 setCurrentDrawStyle(stillDrawStyle);
799 else
800 setCurrentDrawStyle(style);
801 }
802 }
803 }
804
805 ////////////////////////////////////////////////////////////////////////
806 //
807 // Description:
808 // Sets the current drawing style. This only changes the nodes to
809 // match what is passed (called from multiple places) and doesn't
810 // affect the current state.
811 //
812 // Use: private
813 void
814 SoXtViewer::setCurrentDrawStyle(SoXtViewer::DrawStyle style)
815 //
816 ////////////////////////////////////////////////////////////////////////
817 {
818 if (style != VIEW_AS_IS)
819 drawStyleSwitch->whichChild = SO_SWITCH_ALL;
820
821 switch(style) {
822 case VIEW_AS_IS:
823 drawStyleSwitch->whichChild = SO_SWITCH_NONE;
824 break;
825
826 case VIEW_HIDDEN_LINE:
827 // texture is always off under the switch node.
828 // List only stuff common to both rendering passes
829 // (the rest is done when rendering)
830 drawStyleNode->style.setIgnored(FALSE);
831 drawStyleNode->pointSize.setIgnored(TRUE);
832 lightModelNode->model = SoLightModel::BASE_COLOR;
833 lightModelNode->model.setIgnored(FALSE);
834 complexityNode->type.setIgnored(TRUE);
835 complexityNode->value.setIgnored(TRUE);
836 break;
837
838 case VIEW_NO_TEXTURE:
839 case VIEW_LOW_COMPLEXITY:
840 // texture is always off under the switch node
841 drawStyleNode->style.setIgnored(TRUE);
842 drawStyleNode->pointSize.setIgnored(TRUE);
843 lightModelNode->model.setIgnored(TRUE);
844 colorNode->orderedRGBA.setIgnored(TRUE);
845 matBindingNode->value.setIgnored(TRUE);
846 complexityNode->type.setIgnored(TRUE);
847 complexityNode->value.setIgnored(style != VIEW_LOW_COMPLEXITY);
848 break;
849
850 case VIEW_LINE:
851 case VIEW_LOW_RES_LINE:
852 case VIEW_POINT:
853 case VIEW_LOW_RES_POINT:
854 // texture is always off under the switch node
855 drawStyleNode->style = (style == VIEW_LINE || style == VIEW_LOW_RES_LINE) ?
856 SoDrawStyle::LINES : SoDrawStyle::POINTS;
857 drawStyleNode->style.setIgnored(FALSE);
858 drawStyleNode->pointSize.setIgnored(style != VIEW_POINT && style != VIEW_LOW_RES_POINT);
859 lightModelNode->model = SoLightModel::BASE_COLOR;
860 lightModelNode->model.setIgnored(FALSE);
861 colorNode->orderedRGBA.setIgnored(TRUE);
862 matBindingNode->value.setIgnored(TRUE);
863
864 // Force a lower complexity for the low res draw styles
865 // ??? this only works if the object didn't have
866 // ??? something lower in the first place...
867 if (style == VIEW_LOW_RES_LINE || style == VIEW_LOW_RES_POINT) {
868 complexityNode->type = SoComplexity::OBJECT_SPACE;
869 complexityNode->type.setIgnored(FALSE);
870 complexityNode->value.setIgnored(FALSE);
871 }
872 else {
873 complexityNode->type.setIgnored(TRUE);
874 complexityNode->value.setIgnored(TRUE);
875 }
876 break;
877
878 case VIEW_BBOX:
879 // texture is always off under the switch node
880 drawStyleNode->style = SoDrawStyle::LINES;
881 drawStyleNode->style.setIgnored(FALSE);
882 drawStyleNode->pointSize.setIgnored(TRUE);
883 lightModelNode->model = SoLightModel::BASE_COLOR;
884 lightModelNode->model.setIgnored(FALSE);
885 colorNode->orderedRGBA.setIgnored(TRUE);
886 matBindingNode->value.setIgnored(TRUE);
887 complexityNode->type = SoComplexity::BOUNDING_BOX;
888 complexityNode->type.setIgnored(FALSE);
889 complexityNode->value.setIgnored(TRUE);
890 break;
891
892 case VIEW_SAME_AS_STILL:
893 #ifdef DEBUG
894 SoDebugError::post("SoXtViewer::setCurrentDrawStyle", "VIEW_SAME_AS_STILL was passed !");
895 #endif
896 break;
897 }
898
899 setZbufferState();
900 }
901
902 ////////////////////////////////////////////////////////////////////////
903 //
904 // Description:
905 // Gets the drawing style.
906 //
907 // Use: public
908 SoXtViewer::DrawStyle
909 SoXtViewer::getDrawStyle(SoXtViewer::DrawType type)
910 //
911 ////////////////////////////////////////////////////////////////////////
912 {
913 return (type == STILL ? stillDrawStyle : interactiveDrawStyle);
914 }
915
916 ////////////////////////////////////////////////////////////////////////
917 //
918 // Description:
919 // redefine this routine to also correctly set the buffering type
920 //
921 // Use: virtual public
922 void
923 SoXtViewer::setNormalVisual(XVisualInfo *vis)
924 //
925 ////////////////////////////////////////////////////////////////////////
926 {
927 // call parent class
928 SoXtRenderArea::setNormalVisual(vis);
929
930 // now update the buffering type
931 if (isDoubleBuffer())
932 setBufferingType(BUFFER_DOUBLE);
933 else
934 setBufferingType(BUFFER_SINGLE);
935 }
936
937 ////////////////////////////////////////////////////////////////////////
938 //
939 // Description:
940 // redefine this routine from SoXtGLWidget to call the viewer
941 // SoXtViewer::setBufferingType() method which is a superset.
942 //
943 // Use: virtual public
944 //
945 void
946 SoXtViewer::setDoubleBuffer(SbBool flag)
947 //
948 ////////////////////////////////////////////////////////////////////////
949 {
950 setBufferingType(flag ? SoXtViewer::BUFFER_DOUBLE : SoXtViewer::BUFFER_SINGLE);
951 }
952
953 ////////////////////////////////////////////////////////////////////////
954 //
955 // Description:
956 // Sets the buffering style.
957 //
958 // Use: virtual public
959 void
960 SoXtViewer::setBufferingType(SoXtViewer::BufferType type)
961 //
962 ////////////////////////////////////////////////////////////////////////
963 {
964 if (bufferType == type)
965 return;
966
967 // remove interactive callback
968 if (bufferType == BUFFER_INTERACTIVE) {
969 removeStartCallback(SoXtViewer::bufferStartCallback);
970 removeFinishCallback(SoXtViewer::bufferFinishCallback);
971 }
972
973 bufferType = type;
974
975 switch(bufferType) {
976 case BUFFER_SINGLE:
977 SoXtRenderArea::setDoubleBuffer(FALSE);
978 break;
979 case BUFFER_DOUBLE:
980 SoXtRenderArea::setDoubleBuffer(TRUE);
981 break;
982 case BUFFER_INTERACTIVE:
983 SoXtRenderArea::setDoubleBuffer(FALSE);
984 addStartCallback(SoXtViewer::bufferStartCallback);
985 addFinishCallback(SoXtViewer::bufferFinishCallback);
986 break;
987 }
988 }
989
990 ////////////////////////////////////////////////////////////////////////
991 //
992 // Description:
993 // Externally set the viewer into/out off seek mode (default OFF). Actual
994 // seeking will not happen until the viewer decides to (ex: mouse click).
995 //
996 // Note: setting the viewer out of seek mode while the camera is being
997 // animated will stop the animation to the current location.
998 //
999 // use: virtual protected
1000
1001 void
1002 SoXtViewer::setSeekMode(SbBool flag)
1003 //
1004 ////////////////////////////////////////////////////////////////////////
1005 {
1006 if (!isViewing())
1007 return;
1008
1009 // check if seek is being turned off while seek animation is happening
1010 if ( !flag && seekAnimationSensor->getAttachedField() ) {
1011 seekAnimationSensor->detach();
1012 seekAnimationSensor->unschedule();
1013 interactiveCountDec();
1014 }
1015
1016 seekModeFlag = flag;
1017 }
1018
1019 ////////////////////////////////////////////////////////////////////////
1020 //
1021 // Description:
1022 // Adjust the camera clipping planes before a redraw.
1023 //
1024 // use: virtual protected.
1025
1026 void
1027 SoXtViewer::actualRedraw()
1028 //
1029 ////////////////////////////////////////////////////////////////////////
1030 {
1031 if (isAutoClipping() && ! isStereoViewing())
1032 adjustCameraClippingPlanes();
1033
1034 // update the headlight if necessary
1035 if (headlightFlag && camera)
1036 headlightRot->rotation.setValue(camera->orientation.getValue());
1037
1038 // make sure that we have a valid sceneSize value - but don't compute
1039 // a new sceneSize value for every redraw since the walking speed should
1040 // really be constant.
1041 if (sceneSize == 0.0)
1042 recomputeSceneSize();
1043
1044 //
1045 // Check to see if we are in stereo mode, if so draw the scene
1046 // twice with the camera offseted between the two views, else
1047 // do a simple redraw.
1048 //
1049
1050 if ( isStereoViewing() && camera != NULL) {
1051
1052 // Check the camera type, since stereo is different:
1053 //
1054 // Ortho camera: setereo is accomplished by simply rorating
1055 // the camera (around the point of interest) by 6 degree.
1056 //
1057 // Perspective camera: we translate the camera and rotate
1058 // them to look at the same point of interest (idealy we also would
1059 // make sure the plane of convergence is exactly the same for
1060 // both perspective views, unfortunatly we cannot do this with
1061 // the current symetric view volumes).
1062 //
1063
1064 // save the camera original values to restore the camera after
1065 // both views are rendered. This means we will use this in between
1066 // left and right view for things like picking.
1067 SbVec3f camOrigPos = camera->position.getValue();
1068 SbRotation camOrigRot = camera->orientation.getValue();
1069
1070 // get the camera focal point
1071 SbMatrix mx;
1072 mx = camOrigRot;
1073 SbVec3f forward( -mx[2][0], -mx[2][1], -mx[2][2]);
1074 float radius = camera->focalDistance.getValue();
1075 SbVec3f center = camOrigPos + radius * forward;
1076
1077 #ifdef __sgi
1078 //
1079 // if we are splitting the screen in half (loose vertical resolution)
1080 // then change the aspect ratio to squish the objects to make them
1081 // look square again through the stereo glasses. This is done for
1082 // every redraw since we need to manually update the aspect ourself.
1083 //
1084 if (useSGIStereoExt) {
1085 SbVec2s windowSize = getGlxSize();
1086 camera->aspectRatio = 0.5 * windowSize[0] / (float) windowSize[1];
1087 camera->viewportMapping = SoCamera::LEAVE_ALONE;
1088 }
1089 #endif
1090
1091 //
1092 // change the camera for the LEFT eye view, and render
1093 //
1094 #ifdef __sgi
1095 if (useSGIStereoExt) {
1096 XSGISetStereoBuffer(getDisplay(), getNormalWindow(), STEREO_BUFFER_LEFT);
1097 XSync(getDisplay(), False);
1098 }
1099 else
1100 #endif
1101 glDrawBuffer( (isDoubleBuffer() && !drawToFrontBuffer) ?
1102 GL_BACK_LEFT : GL_FRONT_LEFT);
1103 // rotate the camera by - stereoOffset/2 degrees
1104 camera->orientation =
1105 SbRotation(SbVec3f(0, 1, 0), - stereoOffset * M_PI / 360.0) * camOrigRot;
1106
1107 // reposition camera to look at pt of interest
1108 mx = camera->orientation.getValue();
1109 forward.setValue( -mx[2][0], -mx[2][1], -mx[2][2]);
1110 camera->position = center - radius * forward;
1111
1112 if (isAutoClipping())
1113 adjustCameraClippingPlanes();
1114 doRendering();
1115
1116 //
1117 // change the camera for the RIGHT eye view, and render
1118 //
1119 #ifdef __sgi
1120 if (useSGIStereoExt) {
1121 XSGISetStereoBuffer(getDisplay(), getNormalWindow(), STEREO_BUFFER_RIGHT);
1122 XSync(getDisplay(), False);
1123 }
1124 else
1125 #endif
1126 glDrawBuffer( (isDoubleBuffer() && !drawToFrontBuffer) ?
1127 GL_BACK_RIGHT : GL_FRONT_RIGHT);
1128 // rotate the camera by + stereoOffset/2 degrees
1129 camera->orientation =
1130 SbRotation(SbVec3f(0, 1, 0), stereoOffset * M_PI / 360.0) * camOrigRot;
1131
1132 // reposition camera to look at pt of interest
1133 mx = camera->orientation.getValue();
1134 forward.setValue( -mx[2][0], -mx[2][1], -mx[2][2]);
1135 camera->position = center - radius * forward;
1136
1137 if (isAutoClipping())
1138 adjustCameraClippingPlanes();
1139 doRendering();
1140
1141
1142 //
1143 // reset the camera original values now that we are done rendering
1144 // the stereo views (leave aspect ratio to do correct picking).
1145 camera->enableNotify(FALSE); // don't cause a redraw
1146 camera->position = camOrigPos;
1147 camera->orientation = camOrigRot;
1148 camera->enableNotify(TRUE);
1149
1150 #ifdef __sgi
1151 if (! useSGIStereoExt)
1152 #endif
1153 // restore to draw to both buffer (viewer feedback)
1154 glDrawBuffer( (isDoubleBuffer() && !drawToFrontBuffer) ?
1155 GL_BACK : GL_FRONT);
1156 }
1157 //
1158 // else not stereo viewing, so do the regular rendering....
1159 //
1160 else
1161 doRendering();
1162 }
1163
1164 ////////////////////////////////////////////////////////////////////////
1165 //
1166 // Description:
1167 // Do a multiple pass rendering if necessary, else simply call
1168 // SoXtRenderAre::actualRedraw() method.
1169 //
1170 // use: private
1171 void
1172 SoXtViewer::doRendering()
1173 //
1174 ////////////////////////////////////////////////////////////////////////
1175 {
1176 //
1177 // check if we need two pass rendering for hidden line rendering
1178 //
1179
1180 SbBool drawHiddenLine =
1181 (stillDrawStyle == VIEW_HIDDEN_LINE && (! interactiveFlag ||
1182 interactiveDrawStyle == VIEW_NO_TEXTURE ||
1183 interactiveDrawStyle == VIEW_LOW_COMPLEXITY ||
1184 interactiveDrawStyle == VIEW_SAME_AS_STILL))
1185 || (interactiveFlag && interactiveDrawStyle == VIEW_HIDDEN_LINE);
1186
1187 if (camera != NULL && drawHiddenLine) {
1188
1189 // ??? what do we do about highlights ??
1190
1191 // the smaller the near clipping plane is relative to the far
1192 // plane, the smaller the zbuffer offset needs to be (because
1193 // the granularity will be pretty big). The closer the clipping
1194 // planes are relative to each other, the bigger the zbuffer offset
1195 // needs to be (because the zbuffer granularity will be small).
1196 // The scale factor was found empirically to work best with the
1197 // current settings of near/far.
1198 float zOffset = camera->nearDistance.getValue() /
1199 (40 * camera->farDistance.getValue());
1200
1201 //
1202 // render the first pass as solid, using the background color
1203 // for the object base color.
1204 //
1205
1206 drawStyleNode->style = SoDrawStyle::FILLED;
1207 colorNode->orderedRGBA = getBackgroundColor().getPackedValue();
1208 colorNode->orderedRGBA.setIgnored(FALSE);
1209 matBindingNode->value.setIgnored(FALSE);
1210
1211 // ??? this should match the SoXtRenderArea::actualRedraw()
1212 // ??? method exactly (apart for not clearing the z-buffer)
1213 glDepthRange(zOffset, 1); // enable wireframe to be draw on top
1214 getSceneManager()->render(isClearBeforeRender(), TRUE);
1215
1216 //
1217 // render the second pass as wireframe
1218 // (the first pass rendered the objects solid with base color
1219 // set to the background color to set the zbuffer values)
1220 //
1221
1222 drawStyleNode->style = SoDrawStyle::LINES;
1223 colorNode->orderedRGBA.setIgnored(TRUE);
1224 matBindingNode->value.setIgnored(TRUE);
1225
1226 // ??? this should match the SoXtRenderArea::actualRedraw()
1227 // ??? method exactly (apart for not clearing the color and z-buffer)
1228 glDepthRange(0,1-zOffset); // enable wireframe to be draw on top
1229 getSceneManager()->render(FALSE, FALSE);
1230
1231 glDepthRange(0, 1); // restore the range
1232 }
1233 else {
1234 // ??? this should match the SoXtRenderArea::actualRedraw()
1235 // ??? method exactly (apart for not clearing the z-buffer)
1236 getSceneManager()->render(isClearBeforeRender(), ! isZbufferOff());
1237 }
1238 }
1239
1240 ////////////////////////////////////////////////////////////////////////
1241 //
1242 // Description:
1243 // This is called when we are first mapped - this will check the
1244 // configuration of the machine we are running on and decide on what
1245 // draw style we should pick.
1246 //
1247 // use: virtual protected
1248
1249 void
1250 SoXtViewer::afterRealizeHook()
1251 //
1252 ////////////////////////////////////////////////////////////////////////
1253 {
1254 // call the base class
1255 SoXtRenderArea::afterRealizeHook();
1256
1257 // only do this once and only IF the user hasn't overwritten this
1258 if (! checkForDrawStyle)
1259 return;
1260 checkForDrawStyle = FALSE;
1261
1262 SbBool useTexture = TRUE; // true by default (and for new machines)
1263 const char *renderer = (const char *) glGetString(GL_RENDERER);
1264
1265 //
1266 // On the following SGI machines we don't want texture rendering
1267 // to be turned on by default (for speed reason) - machines not listed
1268 // below are assumed to be fast enough to leave VIEW_AS_IS draw style.
1269 //
1270
1271 if (strncmp((const char *)glGetString(GL_VENDOR), "SGI", 3) == 0) {
1272
1273 // Indy and XL
1274 if (useTexture && strncmp(renderer, "NEWPORT", 7) == 0)
1275 useTexture = FALSE;
1276 // Personal Iris
1277 if (useTexture && strncmp(renderer, "GR1", 3) == 0)
1278 useTexture = FALSE;
1279 // VGX and VGXT
1280 if (useTexture && strncmp(renderer, "VGX", 3) == 0)
1281 useTexture = FALSE;
1282 // Indigo Entry
1283 if (useTexture &&
1284 strncmp(renderer, "LG1", 3) == 0 ||
1285 strncmp(renderer, "LIGHT", 5) == 0)
1286 useTexture = FALSE;
1287 // XS, XZ, Elan, and Extreme
1288 if (useTexture && (
1289 strncmp(renderer, "GR2", 3) == 0 ||
1290 strncmp(renderer, "GR3", 3) == 0 ||
1291 strncmp(renderer, "GU1", 3) == 0))
1292 useTexture = FALSE;
1293 }
1294
1295 if (! useTexture)
1296 setDrawStyle(SoXtViewer::INTERACTIVE, SoXtViewer::VIEW_NO_TEXTURE);
1297 }
1298
1299 ////////////////////////////////////////////////////////////////////////
1300 //
1301 // Description:
1302 // Returns TRUE if the zbuffer should be off (based on the viewer
1303 // draw styles).
1304 //
1305 // Use: private
1306
1307 SbBool
1308 SoXtViewer::isZbufferOff()
1309 //
1310 ////////////////////////////////////////////////////////////////////////
1311 {
1312 DrawStyle style = (interactiveFlag ? interactiveDrawStyle : stillDrawStyle);
1313 if (interactiveFlag && interactiveDrawStyle == VIEW_SAME_AS_STILL)
1314 style = stillDrawStyle;
1315
1316 // for these draw styles, turn the zbuffer off
1317 return (style == VIEW_LOW_RES_LINE || style == VIEW_LOW_RES_POINT
1318 || style == VIEW_BBOX);
1319 }
1320
1321 ////////////////////////////////////////////////////////////////////////
1322 //
1323 // Description:
1324 // Sets the zbuffer state on the current window. This is called whenever
1325 // the windows changes (called by SoXtGLWidget::widgetChanged()) or when
1326 // the viewer draw style changes.
1327 //
1328 // Use: private
1329
1330 void
1331 SoXtViewer::setZbufferState()
1332 //
1333 ////////////////////////////////////////////////////////////////////////
1334 {
1335 if (getNormalWindow() == (Window) NULL)
1336 return;
1337
1338 glXMakeCurrent(getDisplay(), getNormalWindow(), getNormalContext());
1339
1340 if (isZbufferOff())
1341 glDisable(GL_DEPTH_TEST);
1342 else
1343 glEnable(GL_DEPTH_TEST);
1344 }
1345
1346 ////////////////////////////////////////////////////////////////////////
1347 //
1348 // Description:
1349 // saves the camera values for later restore.
1350 //
1351 // Use: virtual public
1352
1353 void
1354 SoXtViewer::saveHomePosition()
1355 //
1356 ////////////////////////////////////////////////////////////////////////
1357 {
1358 if (camera == NULL)
1359 return;
1360
1361 origPosition = camera->position.getValue();
1362 origOrientation = camera->orientation.getValue();
1363 origNearDistance = camera->nearDistance.getValue();
1364 origFarDistance = camera->farDistance.getValue();
1365 origFocalDistance = camera->focalDistance.getValue();
1366
1367 // save camera height (changed by zooming)
1368 if (camera->isOfType(SoPerspectiveCamera::getClassTypeId()))
1369 origHeight = ((SoPerspectiveCamera *)camera)->heightAngle.getValue();
1370 else if (camera->isOfType(SoOrthographicCamera::getClassTypeId()))
1371 origHeight = ((SoOrthographicCamera *)camera)->height.getValue();
1372 }
1373
1374 ////////////////////////////////////////////////////////////////////////
1375 //
1376 // Description:
1377 // reset the camera to it's saved values.
1378 //
1379 // Use: virtual public
1380
1381 void
1382 SoXtViewer::resetToHomePosition()
1383 //
1384 ////////////////////////////////////////////////////////////////////////
1385 {
1386 if (camera == NULL)
1387 return;
1388
1389 camera->position = origPosition;
1390 camera->orientation = origOrientation;
1391 camera->nearDistance = origNearDistance;
1392 camera->farDistance = origFarDistance;
1393 camera->focalDistance = origFocalDistance;
1394
1395 // restore camera height (changed by zooming)
1396 if (camera->isOfType(SoPerspectiveCamera::getClassTypeId()))
1397 ((SoPerspectiveCamera *)camera)->heightAngle.setValue(origHeight);
1398 else if (camera->isOfType(SoOrthographicCamera::getClassTypeId()))
1399 ((SoOrthographicCamera *)camera)->height.setValue(origHeight);
1400 }
1401
1402 ////////////////////////////////////////////////////////////////////////
1403 //
1404 // Description: _ HACK BUG WORKAOUND _
1405 //
1406 // This updates the event own internal knowledge about which buttons and
1407 // modifiers keys are down based of the event itself.
1408 //
1409 // ??? for some reason Xt does NOT update the event button and modifier
1410 // ??? state until AFTER the event has been sent to the widget. Knowing
1411 // ??? which mouse buttons and which modifiers keys are down when receiving
1412 // ??? ANY event (uncluding the event itself) is VERY useful and make
1413 // ??? the viewer mode chaging code MUCH simpler.
1414 //
1415 // Use: private - BUG WORKAROUND
1416 static void
1417 updateEventState(XAnyEvent *xe)
1418 //
1419 ////////////////////////////////////////////////////////////////////////
1420 {
1421 XButtonEvent *be;
1422 XKeyEvent *ke;
1423 KeySym keysym;
1424
1425 switch(xe->type) {
1426 case ButtonPress:
1427 be = (XButtonEvent *)xe;
1428 if (be->button == Button1)
1429 be->state = (be->state | Button1Mask);
1430 else if (be->button == Button2)
1431 be->state = (be->state | Button2Mask);
1432 break;
1433
1434 case ButtonRelease:
1435 be = (XButtonEvent *)xe;
1436 if (be->button == Button1)
1437 be->state = (be->state & ~Button1Mask);
1438 else if (be->button == Button2)
1439 be->state = (be->state & ~Button2Mask);
1440 break;
1441
1442 case KeyPress:
1443 case KeyRelease:
1444 ke = (XKeyEvent *)xe;
1445 keysym = XLookupKeysym(ke, 0);
1446
1447 if (keysym == XK_Control_L || keysym == XK_Control_R) {
1448 if (xe->type == KeyPress)
1449 ke->state = (ke->state | ControlMask);
1450 else
1451 ke->state = (ke->state & ~ControlMask);
1452 }
1453 break;
1454 }
1455 }
1456
1457 ////////////////////////////////////////////////////////////////////////
1458 //
1459 // Description:
1460 // Process a common set of events which are shared accross all
1461 // viewers. Returning TRUE if the event was processed.
1462 //
1463 // Use: protected
1464 SbBool
1465 SoXtViewer::processCommonEvents(XAnyEvent *xe)
1466 //
1467 ////////////////////////////////////////////////////////////////////////
1468 {
1469 KeySym keysym;
1470
1471 // check if the application wants to handle the event itself
1472 // instead of giving it to the viewer. This can be used to disable
1473 // some simple viewer functionality (like the Arrow keys or Esc key).
1474 // ??? this is a simple work around for bug #113991 - Xt translation
1475 // ??? tables would be better than dealing with events directly.
1476 if (SoXtRenderArea::invokeAppCB(xe))
1477 return TRUE;
1478
1479 //
1480 // check for special key which turns viewing on/off
1481 //
1482 if (xe->type == KeyPress) {
1483 XKeyEvent *ke = (XKeyEvent *)xe;
1484 keysym = XLookupKeysym(ke, 0);
1485 if (keysym == XK_Escape) {
1486 setViewing( !isViewing() ); // toggle the viewing mode...
1487 return TRUE;
1488 }
1489 else if (!isViewing() && (keysym == XK_Alt_L || keysym == XK_Alt_R)
1490 && !(ke->state & Button1Mask || ke->state & Button2Mask)) {
1491 // Alt-key goes from PICK to VIEW if
1492 // 1] we are not in VIEW mode already
1493 // 2] no mouse buttons are pressed
1494 //
1495 altSwitchBack = TRUE; // later return back
1496 setViewing(TRUE);
1497 return TRUE;
1498 }
1499 }
1500 else if (xe->type == KeyRelease) {
1501 keysym = XLookupKeysym((XKeyEvent *)xe, 0);
1502 if (altSwitchBack && (keysym == XK_Alt_L || keysym == XK_Alt_R)) {
1503 // if Alt-key, then return to PICK (if we had switched)
1504 setViewing(FALSE);
1505 altSwitchBack = FALSE; // clear the flag
1506 return TRUE;
1507 }
1508 }
1509 else if (xe->type == EnterNotify) {
1510 XCrossingEvent *ce = (XCrossingEvent *)xe;
1511 //
1512 // because the application might use Alt-key for motif menu
1513 // accelerators we might not receive a key-up event, so make sure
1514 // to reset any Alt mode (temporary viewing) when the mouse re-enters
1515 // the window.
1516 //
1517 if (! isViewing() && ce->state & Mod1Mask) {
1518 altSwitchBack = TRUE; // later return back
1519 setViewing(TRUE);
1520 }
1521 else if (altSwitchBack && !(ce->state & Mod1Mask)) {
1522 setViewing(FALSE);
1523 altSwitchBack = FALSE; // clear the flag
1524 }
1525 }
1526
1527 // send the event to the scene graph if viewing is off
1528 if ( !isViewing() ) {
1529 // prevent renderArea from sending the event to the app twice
1530 // since it is done above...
1531 SoXtRenderAreaEventCB *saveFunc = appEventHandler;
1532 appEventHandler = NULL;
1533 SoXtRenderArea::processEvent(xe);
1534 appEventHandler = saveFunc;
1535
1536 return TRUE;
1537 }
1538
1539 // if no camera discard events
1540 if (camera == NULL)
1541 return TRUE;
1542
1543 SbBool handled = TRUE;
1544
1545 // ??? workaround what seem to be an Xt bug...
1546 updateEventState(xe);
1547
1548 switch(xe->type) {
1549 case KeyPress:
1550 switch ( keysym ) {
1551 case XK_Home:
1552 resetToHomePosition();
1553 break;
1554 case XK_s:
1555 setSeekMode( !isSeekMode() );
1556 // ??? this is kind of a hack, but it is needed
1557 // ??? until a better solution is found
1558 if ( isSeekMode() && interactiveCount != 0 ) {
1559 interactiveCount = 0;
1560 finishCBList->invokeCallbacks(this);
1561 }
1562 break;
1563 case XK_Left:
1564 case XK_Up:
1565 case XK_Right:
1566 case XK_Down:
1567 arrowKeyPressed(keysym);
1568 break;
1569 default:
1570 handled = FALSE;
1571 break;
1572 }
1573 break;
1574
1575 default:
1576 handled = FALSE;
1577 break;
1578 }
1579
1580 return handled;
1581 }
1582
1583 ////////////////////////////////////////////////////////////////////////
1584 //
1585 // Description:
1586 // Increment the intercative viewing counter.
1587 //
1588 // Use: protected.
1589
1590 void
1591 SoXtViewer::interactiveCountInc()
1592 //
1593 ////////////////////////////////////////////////////////////////////////
1594 {
1595 interactiveCount++;
1596
1597 if (interactiveCount == 1)
1598 startCBList->invokeCallbacks(this);
1599 }
1600
1601 ////////////////////////////////////////////////////////////////////////
1602 //
1603 // Description:
1604 // Decrement the intercative viewing counter.
1605 //
1606 // Use: protected.
1607
1608 void
1609 SoXtViewer::interactiveCountDec()
1610 //
1611 ////////////////////////////////////////////////////////////////////////
1612 {
1613 if (interactiveCount > 0) {
1614 interactiveCount--;
1615 if (interactiveCount == 0)
1616 finishCBList->invokeCallbacks(this);
1617 }
1618 }
1619
1620 ////////////////////////////////////////////////////////////////////////
1621 //
1622 // Description:
1623 // This routine is used by subclasses to initiate the seek animation. Given a
1624 // screen mouse location, this routine will return the picked point
1625 // and the normal at that point. It will also schedule the sensor to animate
1626 // if necessary. The routine retuns TRUE if something got picked...
1627 //
1628 // Note: if detailSeek is on, the point and normal correspond to the exact
1629 // 3D location under the cursor.
1630 // if detailSeek if off, the object bbox center and the camera
1631 // orientation are instead returned.
1632 //
1633 // Use: protected.
1634
1635 SbBool
1636 SoXtViewer::seekToPoint(const SbVec2s &mouseLocation)
1637 //
1638 ////////////////////////////////////////////////////////////////////////
1639 {
1640 if (camera == NULL) {
1641 setSeekMode(FALSE);
1642 return FALSE;
1643 }
1644
1645 // do the picking
1646 // Use assignment notation to disambiguate from expression (edison)
1647 SoRayPickAction pick = SoRayPickAction(SbViewportRegion(getGlxSize()));
1648 pick.setPoint(mouseLocation);
1649 pick.setRadius(1.0);
1650 pick.setPickAll(FALSE); // pick only the closest object
1651 pick.apply(sceneRoot);
1652
1653 // makes sure something got picked
1654 SoPickedPoint *pp = pick.getPickedPoint();
1655 if ( pp == NULL ) {
1656 setSeekMode(FALSE);
1657 return FALSE;
1658 }
1659
1660 //
1661 // Get picked point and normal if detailtSeek
1662 //
1663 if (detailSeekFlag) {
1664
1665 seekPoint = pp->getPoint();
1666 seekNormal = pp->getNormal();
1667
1668 // check to make sure normal points torward the camera, else
1669 // flip the normal around
1670 if ( seekNormal.dot(camera->position.getValue() - seekPoint) < 0 )
1671 seekNormal.negate();
1672 }
1673 //
1674 // else get object bounding box as the seek point and the camera
1675 // orientation as the normal.
1676 //
1677 else {
1678 // get center of object's bounding box
1679 // Use assignment notation to disambiguate from expression (edison)
1680 SoGetBoundingBoxAction bba = SoGetBoundingBoxAction(SbViewportRegion(getGlxSize()));
1681 bba.apply(pp->getPath());
1682 SbBox3f bbox = bba.getBoundingBox();
1683 seekPoint = bbox.getCenter();
1684
1685 // keep the camera oriented the same way
1686 SbMatrix mx;
1687 mx = camera->orientation.getValue();
1688 seekNormal.setValue(mx[2][0], mx[2][1], mx[2][2]);
1689 }
1690
1691
1692 //
1693 // now check if animation sensor needs to be scheduled
1694 //
1695
1696 computeSeekVariables = TRUE;
1697 if (seekAnimTime == 0) {
1698
1699 // jump to new location, no animation needed
1700 interpolateSeekAnimation(1.0);
1701 }
1702 else {
1703 // schedule sensor and call viewer start callbacks
1704 if ( ! seekAnimationSensor->getAttachedField() ) {
1705 seekAnimationSensor->attach(viewerRealTime);
1706 seekAnimationSensor->schedule();
1707 interactiveCountInc();
1708 }
1709
1710 seekStartTime = viewerRealTime->getValue();
1711 }
1712
1713 return TRUE; // successfull
1714 }
1715
1716 ////////////////////////////////////////////////////////////////////////
1717 //
1718 // Description:
1719 // computes what the final camera seek orientation should be.
1720 //
1721 // Use: virtual protected
1722
1723 void
1724 SoXtViewer::computeSeekFinalOrientation()
1725 //
1726 ////////////////////////////////////////////////////////////////////////
1727 {
1728 SbMatrix mx;
1729 SbVec3f viewVector;
1730
1731 // find the camera final orientation
1732 if ( isDetailSeek() ) {
1733
1734 // get the camera new orientation
1735 mx = camera->orientation.getValue();
1736 viewVector.setValue(-mx[2][0], -mx[2][1], -mx[2][2]);
1737 SbRotation changeOrient;
1738 changeOrient.setValue(viewVector, seekPoint - camera->position.getValue());
1739 newCamOrientation = camera->orientation.getValue() * changeOrient;
1740 }
1741 else
1742 newCamOrientation = camera->orientation.getValue();
1743 }
1744
1745 ////////////////////////////////////////////////////////////////////////
1746 //
1747 // Description:
1748 //
1749 // Use: virtual protected
1750
1751 void
1752 SoXtViewer::interpolateSeekAnimation(float t)
1753 //
1754 ////////////////////////////////////////////////////////////////////////
1755 {
1756 if (camera == NULL)
1757 return;
1758
1759 //
1760 // check if camera new and old position/orientation have already
1761 // been computed.
1762 //
1763 if (computeSeekVariables) {
1764 SbMatrix mx;
1765 SbVec3f viewVector;
1766
1767 // save camera starting point
1768 oldCamPosition = camera->position.getValue();
1769 oldCamOrientation = camera->orientation.getValue();
1770
1771 // compute the distance the camera will be from the seek point
1772 // and update the camera focalDistance.
1773 float dist;
1774 if ( seekDistAsPercentage ) {
1775 SbVec3f seekVec(seekPoint - camera->position.getValue());
1776 dist = seekVec.length() * (seekDistance / 100.0);
1777 }
1778 else
1779 dist = seekDistance;
1780 camera->focalDistance = dist;
1781
1782 // let subclasses have a chance to redefine what the
1783 // camera final orientation should be.
1784 computeSeekFinalOrientation();
1785
1786 // find the camera final position based on orientation and distance
1787 mx = newCamOrientation;
1788 viewVector.setValue(-mx[2][0], -mx[2][1], -mx[2][2]);
1789 newCamPosition = seekPoint - dist * viewVector;
1790
1791 computeSeekVariables = FALSE;
1792 }
1793
1794
1795 //
1796 // Now position the camera according to the animation time
1797 //
1798
1799 // use and ease-in ease-out approach
1800 float cos_t = 0.5 - 0.5 * cosf(t * M_PI);
1801
1802 // get camera new rotation
1803 camera->orientation = SbRotation::slerp(oldCamOrientation, newCamOrientation, cos_t);
1804
1805 // get camera new position
1806 camera->position = oldCamPosition + (newCamPosition - oldCamPosition) * cos_t;
1807 }
1808
1809 ////////////////////////////////////////////////////////////////////////
1810 //
1811 // Description:
1812 // Adjust the camera clipping planes based on the scene bounding
1813 // box. (called before every redraws)
1814 //
1815 // use: virtual protected
1816
1817 void
1818 SoXtViewer::adjustCameraClippingPlanes()
1819 //
1820 ////////////////////////////////////////////////////////////////////////
1821 {
1822 if (camera == NULL)
1823 return;
1824
1825 // get the scene bounding box
1826 autoClipBboxAction->setViewportRegion(SbViewportRegion(getGlxSize()));
1827 autoClipBboxAction->apply(sceneRoot);
1828
1829 SbXfBox3f xfbbox = autoClipBboxAction->getXfBoundingBox();
1830
1831 // get camera transformation and apply to xfbbox
1832 // to align the bounding box to camera space.
1833 // This will enable us to simply use the z values of the
1834 // transformed bbox for near and far plane values.
1835 SbMatrix mx;
1836 mx.setTranslate(- camera->position.getValue());
1837 xfbbox.transform(mx);
1838 mx = camera->orientation.getValue().inverse();
1839 xfbbox.transform(mx);
1840
1841 // get screen align bbox and figure the near and far plane values
1842 SbBox3f bbox = xfbbox.project();
1843 // take negative value and opposite to what one might think
1844 // because the camera points down the -Z axis
1845 float far = - bbox.getMin()[2];
1846 float near = - bbox.getMax()[2];
1847
1848 // scene is behind the camera so don't change the planes
1849 if (far < 0)
1850 return;
1851
1852 // check for minimum near plane value (Value will be negative
1853 // when the camera is inside the bounding box).
1854 // Note: there needs to be a minimum near value for perspective
1855 // camera because of zbuffer resolution problem (plus the values
1856 // has to be positive). There is no such restriction for
1857 // an Orthographic camera (you can see behind you).
1858 if (! camera->isOfType(SoOrthographicCamera::getClassTypeId())) {
1859 if (near < (minimumNearPlane * far))
1860 near = minimumNearPlane * far;
1861 }
1862
1863 // give the near and far distances a little bit of slack in case
1864 // the object lies against the bounding box, otherwise the object
1865 // will be poping in and out of view.
1866 // (example: a cube is the same as it's bbox)
1867 near *= 0.999;
1868 far *= 1.001;
1869
1870 // finally assign camera plane values
1871 if (camera->nearDistance.getValue() != near)
1872 camera->nearDistance = near;
1873 if (camera->farDistance.getValue() != far)
1874 camera->farDistance = far;
1875 }
1876
1877 ////////////////////////////////////////////////////////////////////////
1878 //
1879 // Copy the camera onto the clipboard.
1880 //
1881 // Use: private
1882 //
1883 void
1884 SoXtViewer::copyView(Time eventTime)
1885 //
1886 ////////////////////////////////////////////////////////////////////////
1887 {
1888 if (camera == NULL)
1889 return;
1890
1891 if (clipboard == NULL)
1892 clipboard = new SoXtClipboard(getWidget());
1893
1894 clipboard->copy(camera, eventTime);
1895 }
1896
1897 ////////////////////////////////////////////////////////////////////////
1898 //
1899 // Retrieve the selection from the X server and paste it when it
1900 // arrives (in our pasteDone callback).
1901 //
1902 // Use: private
1903 //
1904 void
1905 SoXtViewer::pasteView(Time eventTime)
1906 //
1907 ////////////////////////////////////////////////////////////////////////
1908 {
1909 if (clipboard == NULL)
1910 clipboard = new SoXtClipboard(getWidget());
1911
1912 clipboard->paste(eventTime, SoXtViewer::pasteDoneCB, this);
1913 }
1914
1915
1916 ////////////////////////////////////////////////////////////////////////
1917 //
1918 // This is called by Xt when the data is ready to be pasted.
1919 //
1920 // Use: static, private
1921 //
1922 void
1923 SoXtViewer::pasteDoneCB(void *userData, SoPathList *pathList)
1924 //
1925 ////////////////////////////////////////////////////////////////////////
1926 {
1927 SoCamera *newCamera = NULL;
1928
1929 // search for a camera in the paste data
1930 for (int i = 0; i < pathList->getLength(); i++) {
1931 SoFullPath *fullP = (SoFullPath *) (*pathList)[i];
1932 if (fullP->getTail()->isOfType(SoCamera::getClassTypeId())) {
1933 newCamera = (SoCamera *) fullP->getTail();
1934 break;
1935 }
1936 }
1937
1938 if (newCamera != NULL)
1939 ((SoXtViewer *) userData)->changeCameraValues(newCamera);
1940
1941 // We delete the callback data when done with it.
1942 delete pathList;
1943 }
1944
1945 ////////////////////////////////////////////////////////////////////////
1946 //
1947 // Change the values of our camera to newCamera.
1948 //??? animate from old values to new?
1949 //
1950 // Use: virtual, protected
1951 //
1952 void
1953 SoXtViewer::changeCameraValues(SoCamera *newCamera)
1954 //
1955 ////////////////////////////////////////////////////////////////////////
1956 {
1957 if (camera == NULL)
1958 return;
1959
1960 // only paste cameras of the same type
1961 if (camera->getTypeId() != newCamera->getTypeId())
1962 return;
1963
1964 // give our camera the values of the new camera
1965 camera->position = newCamera->position;
1966 camera->orientation = newCamera->orientation;
1967 camera->nearDistance = newCamera->nearDistance;
1968 camera->farDistance = newCamera->farDistance;
1969 camera->focalDistance = newCamera->focalDistance;
1970
1971 // get the height or heightAngle
1972 if (camera->isOfType(SoPerspectiveCamera::getClassTypeId()))
1973 ((SoPerspectiveCamera *)camera)->heightAngle =
1974 ((SoPerspectiveCamera *)newCamera)->heightAngle;
1975 else
1976 ((SoOrthographicCamera *)camera)->height =
1977 ((SoOrthographicCamera *)newCamera)->height;
1978 }
1979
1980 ////////////////////////////////////////////////////////////////////////
1981 //
1982 // Description:
1983 // Toggles the current camera type (perspective <--> orthographic)
1984 //
1985 // Use: virtual protected
1986 //
1987 void
1988 SoXtViewer::toggleCameraType()
1989 //
1990 ////////////////////////////////////////////////////////////////////////
1991 {
1992 if (camera == NULL)
1993 return;
1994
1995 // create the camera of the opposite kind and compute the wanted height
1996 // or heightAngle of the new camera.
1997 SoCamera *newCam;
1998 if (camera->isOfType(SoPerspectiveCamera::getClassTypeId())) {
1999 float angle = ((SoPerspectiveCamera *)camera)->heightAngle.getValue();
2000 float height = camera->focalDistance.getValue() * tanf(angle/2);
2001 newCam = new SoOrthographicCamera;
2002 ((SoOrthographicCamera *)newCam)->height = 2 * height;
2003 }
2004 else if (camera->isOfType(SoOrthographicCamera::getClassTypeId())) {
2005 float height = ((SoOrthographicCamera *)camera)->height.getValue() / 2;
2006 float angle = atanf(height / camera->focalDistance.getValue());
2007 newCam = new SoPerspectiveCamera;
2008 ((SoPerspectiveCamera *)newCam)->heightAngle = 2 * angle;
2009 }
2010 else {
2011 #ifdef DEBUG
2012 SoDebugError::post("SoXtViewer::toggleCameraType", "unknown camera type!");
2013 #endif
2014 return;
2015 }
2016
2017 newCam->ref();
2018
2019 // copy common stuff from the old to the new camera
2020 newCam->viewportMapping = camera->viewportMapping.getValue();
2021 newCam->position = camera->position.getValue();
2022 newCam->orientation = camera->orientation.getValue();
2023 newCam->aspectRatio = camera->aspectRatio.getValue();
2024 newCam->focalDistance = camera->focalDistance.getValue();
2025
2026 // search for the old camera and replace it by the new camera
2027 SoSearchAction sa;
2028 sa.setNode(camera);
2029 sa.apply(sceneRoot);
2030 SoFullPath *fullCamPath = (SoFullPath *) sa.getPath();
2031 if (fullCamPath) {
2032 SoGroup *parent = (SoGroup *)fullCamPath->getNode(fullCamPath->getLength() - 2);
2033 parent->insertChild(newCam, parent->findChild(camera));
2034 SoCamera *oldCam = camera;
2035 setCamera(newCam);
2036
2037 // remove the old camera if it is still there (setCamera() might
2038 // have removed it) and set the created flag to true (for next time)
2039 if (parent->findChild(oldCam) >= 0)
2040 parent->removeChild(oldCam);
2041 createdCamera = TRUE;
2042 }
2043 #ifdef DEBUG
2044 else
2045 SoDebugError::post("SoXtViewer::toggleCameraType", "camera not found!");
2046 #endif
2047
2048 newCam->unref();
2049 }
2050
2051 ////////////////////////////////////////////////////////////////////////
2052 //
2053 // Description:
2054 // Called by the processCommonEvent routine whenever the arrow keys
2055 // are pressed. Translate the camera in the viewing plane in the arrow
2056 // direction half a screen at a time.
2057 //
2058 // Use: private
2059 //
2060 void
2061 SoXtViewer::arrowKeyPressed(KeySym key)
2062 //
2063 ////////////////////////////////////////////////////////////////////////
2064 {
2065 if (camera == NULL)
2066 return;
2067
2068 // get the camera near plane height value
2069 float dist;
2070 if (camera->isOfType(SoPerspectiveCamera::getClassTypeId())) {
2071 float angle = ((SoPerspectiveCamera *)camera)->heightAngle.getValue();
2072 float length = camera->nearDistance.getValue();
2073 dist = length * tanf(angle);
2074 }
2075 else if (camera->isOfType(SoOrthographicCamera::getClassTypeId()))
2076 dist = ((SoOrthographicCamera *)camera)->height.getValue();
2077 dist /= 2.0;
2078
2079 // get camera right/left/up/down direction
2080 SbMatrix mx;
2081 mx = camera->orientation.getValue();
2082 SbVec3f dir;
2083 switch(key) {
2084 case XK_Up:
2085 dir.setValue(mx[1][0], mx[1][1], mx[1][2]);
2086 break;
2087 case XK_Down:
2088 dir.setValue(-mx[1][0], -mx[1][1], -mx[1][2]);
2089 break;
2090 case XK_Right:
2091 dir.setValue(mx[0][0], mx[0][1], mx[0][2]);
2092 dist *= camera->aspectRatio.getValue();
2093 break;
2094 case XK_Left:
2095 dir.setValue(-mx[0][0], -mx[0][1], -mx[0][2]);
2096 dist *= camera->aspectRatio.getValue();
2097 break;
2098 }
2099
2100 // finally reposition the camera
2101 camera->position = camera->position.getValue() + dist * dir;
2102 }
2103
2104 //
2105 ////////////////////////////////////////////////////////////////////////
2106 // static callbacks stubs
2107 ////////////////////////////////////////////////////////////////////////
2108 //
2109
2110
2111
2112 void
2113 SoXtViewer::bufferStartCallback(void *, SoXtViewer *v)
2114 {
2115 v->SoXtRenderArea::setDoubleBuffer(TRUE);
2116 }
2117
2118 void
2119 SoXtViewer::bufferFinishCallback(void *, SoXtViewer *v)
2120 {
2121 v->SoXtRenderArea::setDoubleBuffer(FALSE);
2122 }
2123
2124 void
2125 SoXtViewer::drawStyleStartCallback(void *, SoXtViewer *v)
2126 {
2127 v->interactiveFlag = TRUE; // must happen first
2128
2129 // if still and move draw styles are the same, return...
2130 if (v->interactiveDrawStyle == VIEW_SAME_AS_STILL ||
2131 v->interactiveDrawStyle == v->stillDrawStyle)
2132 return;
2133
2134 // if we have "MOVE NO TEXTURE", then we have nothing
2135 // to do if we have a current draw style (since they all have
2136 // texturing turned off in the first place).
2137 if (v->interactiveDrawStyle == VIEW_NO_TEXTURE &&
2138 v->stillDrawStyle != VIEW_AS_IS)
2139 return;
2140
2141 v->setCurrentDrawStyle(v->interactiveDrawStyle);
2142 }
2143
2144 void
2145 SoXtViewer::drawStyleFinishCallback(void *, SoXtViewer *v)
2146 {
2147 v->interactiveFlag = FALSE; // must happen first
2148
2149 // if still and move draw styles are the same, return...
2150 if (v->interactiveDrawStyle == VIEW_SAME_AS_STILL ||
2151 v->interactiveDrawStyle == v->stillDrawStyle)
2152 return;
2153
2154 // if we have "MOVE NO TEXTURE", then we have nothing
2155 // to do if we have a current draw style (since they all have
2156 // texturing turned off in the first place).
2157 if (v->interactiveDrawStyle == VIEW_NO_TEXTURE &&
2158 v->stillDrawStyle != VIEW_AS_IS)
2159 return;
2160
2161 v->setCurrentDrawStyle(v->stillDrawStyle);
2162 }
2163
2164 ////////////////////////////////////////////////////////////////////////
2165 //
2166 // Called whenever the seek animation sensor fires. Finds the amount
2167 // of time since we started the seek and call the subclasses routine
2168 // to do the correct interpolation.
2169 //
2170 // Use: static private
2171 //
2172 void
2173 SoXtViewer::seekAnimationSensorCB(void *p, SoSensor *)
2174 //
2175 ////////////////////////////////////////////////////////////////////////
2176 {
2177 SoXtViewer *v = (SoXtViewer *)p;
2178
2179 // get the time difference
2180 SbTime time = viewerRealTime->getValue();
2181 float sec = float((time - v->seekStartTime).getValue());
2182 if (sec == 0.0)
2183 sec = 1.0/72.0; // at least one frame (needed for first call)
2184 float t = (sec / v->seekAnimTime);
2185
2186 // check to make sure the values are correctly clipped
2187 if (t > 1.0)
2188 t = 1.0;
2189 else if ((1.0 - t) < 0.0001)
2190 t = 1.0; // this will be the last one...
2191
2192 // call subclasses to interpolate the animation
2193 v->interpolateSeekAnimation(t);
2194
2195 // stops seek if this was the last interval
2196 if (t == 1.0)
2197 v->setSeekMode(FALSE);
2198 }
2199
2200
2201
2202 //
2203 ////////////////////////////////////////////////////////////////////////
2204 // viewer feedback convenience routines
2205 ////////////////////////////////////////////////////////////////////////
2206 //
2207
2208
2209
2210 /*
2211 * Defines
2212 */
2213
2214 // color used in feedback
2215 #define DARK_COLOR glColor3ub(90, 90, 90)
2216 #define LIGHT_COLOR glColor3ub(230, 230, 230)
2217
2218 #define LINE_THIN 3 // line thickness used in feedback
2219 #define LINE_THICK (LINE_THIN + 2)
2220 #define CROSS 8 // size of cross hair at screen center for Roll
2221 #define RADIUS 15 // radius of center circle (in pix) for Roll
2222 #define ANGLE_LEN 14 // angular size in degrees of Roll anchor
2223
2224
2225 /*
2226 * Globals
2227 */
2228
2229 #define ARROW_SIZE 6.0 // size in pix of arrow head
2230
2231 // anchor arrow head description
2232 static float arrow_data[3][3] = {
2233 { -ARROW_SIZE, 0, 0 },
2234 { 0, 2*ARROW_SIZE, 0 },
2235 { ARROW_SIZE, 0, 0 }
2236 };
2237
2238
2239 /*
2240 * Macros
2241 */
2242
2243 #define DRAW_ARROW_MACRO \
2244 DARK_COLOR; \
2245 glBegin(GL_POLYGON); \
2246 glVertex3fv(arrow_data[0]); \
2247 glVertex3fv(arrow_data[1]); \
2248 glVertex3fv(arrow_data[2]); \
2249 glEnd(); \
2250 LIGHT_COLOR; \
2251 glLineWidth(1); \
2252 glBegin(GL_LINE_LOOP); \
2253 glVertex3fv(arrow_data[0]); \
2254 glVertex3fv(arrow_data[1]); \
2255 glVertex3fv(arrow_data[2]); \
2256 glEnd();
2257
2258 ////////////////////////////////////////////////////////////////////////
2259 //
2260 // Description:
2261 // Sets the default ortho projection when doing viewer feedback. The
2262 // zbuffer/lighting is automatically turned off.
2263 //
2264 // Use: static protected
2265 //
2266 void
2267 SoXtViewer::setFeedbackOrthoProjection(const SbVec2s &size)
2268 //
2269 ////////////////////////////////////////////////////////////////////////
2270 {
2271 // push the gl state to revert it back later....
2272 glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_LIGHTING_BIT | GL_LINE_BIT);
2273
2274 // ??? should we worry about restoring this matrix later ?
2275 glViewport(0, 0, size[0], size[1]);
2276 glMatrixMode(GL_PROJECTION);
2277 glLoadIdentity();
2278 glOrtho(0, size[0], 0, size[1], -1, 1);
2279
2280 // disable zbuffer and lighting....
2281 glDisable(GL_DEPTH_TEST);
2282 glDisable(GL_LIGHTING);
2283 }
2284
2285 ////////////////////////////////////////////////////////////////////////
2286 //
2287 // Description:
2288 // restores the state that was changed when setFeedbackOrthoProjection()
2289 // is called.
2290 //
2291 // Use: static protected
2292 //
2293 void
2294 SoXtViewer::restoreGLStateAfterFeedback()
2295 //
2296 ////////////////////////////////////////////////////////////////////////
2297 {
2298 // restore the gl state that were saved in setFeedbackOrthoProjection()
2299 glPopAttrib();
2300 }
2301
2302 ////////////////////////////////////////////////////////////////////////
2303 //
2304 // Description:
2305 // Draws a simple 2 colored cross at the given location.
2306 //
2307 // Use: static protected
2308 //
2309 void
2310 SoXtViewer::drawViewerCrossFeedback(SbVec2s loc)
2311 //
2312 ////////////////////////////////////////////////////////////////////////
2313 {
2314 LIGHT_COLOR;
2315 glLineWidth(4);
2316 glBegin(GL_LINES);
2317 glVertex2s(loc[0]-CROSS, loc[1]);
2318 glVertex2s(loc[0]+CROSS, loc[1]);
2319 glVertex2s(loc[0], loc[1]-CROSS);
2320 glVertex2s(loc[0], loc[1]+CROSS);
2321 glEnd();
2322
2323 DARK_COLOR;
2324 glLineWidth(2);
2325 glBegin(GL_LINES);
2326 glVertex2s(loc[0]-CROSS+1, loc[1]);
2327 glVertex2s(loc[0]+CROSS-1, loc[1]);
2328 glVertex2s(loc[0], loc[1]-CROSS+1);
2329 glVertex2s(loc[0], loc[1]+CROSS-1);
2330 glEnd();
2331 }
2332
2333 ////////////////////////////////////////////////////////////////////////
2334 //
2335 // Description:
2336 // draws the anchor roll feedback given the point of rotation and the
2337 // current mouse location.
2338 //
2339 // Use: static protected
2340 //
2341 void
2342 SoXtViewer::drawViewerRollFeedback(SbVec2s center, SbVec2s loc)
2343 //
2344 ////////////////////////////////////////////////////////////////////////
2345 {
2346 // get angle and distance of mouse from center of rotation
2347 float ang, dist;
2348 float vx = loc[0] - center[0];
2349 float vy = loc[1] - center[1];
2350 if (vx==0 && vy==0) {
2351 ang = 0;
2352 dist = 3; // minimum size (given the circle thickness)
2353 }
2354 else {
2355 ang = atan2(vy, vx) * 180.0 / M_PI;
2356 dist = sqrtf(vx*vx + vy*vy);
2357 if (dist < 3)
2358 dist = 3; // minimum size (given the circle thickness)
2359 }
2360 float cirAng = -ang + 90; // gluPartialDisk() angle is REALLY backward !!
2361
2362 static GLUquadricObj *quad = NULL;
2363 if (! quad) quad = gluNewQuadric();
2364
2365 // draw all of the circles (first inner, then outer)
2366 glTranslatef(center[0], center[1], 0);
2367 LIGHT_COLOR;
2368 gluDisk(quad, RADIUS, RADIUS+LINE_THICK, 20, 2);
2369 gluPartialDisk(quad, dist-2, dist+LINE_THICK-2, 20, 2, cirAng - ANGLE_LEN, 2 * ANGLE_LEN);
2370 DARK_COLOR;
2371 gluDisk(quad, RADIUS+1, RADIUS+LINE_THICK-1, 20, 2);
2372 gluPartialDisk(quad, dist-1, dist+LINE_THICK-3, 20, 2, cirAng - ANGLE_LEN, 2 * ANGLE_LEN);
2373 glTranslatef(-center[0], -center[1], 0); // undo the translation
2374
2375 // draw connecting line from center to outer circle
2376 glLineWidth(LINE_THICK);
2377 LIGHT_COLOR;
2378 glBegin(GL_LINES);
2379 glVertex2s(center[0], center[1]);
2380 glVertex2s(loc[0], loc[1]);
2381 glEnd();
2382 glLineWidth(LINE_THIN);
2383 DARK_COLOR;
2384 glBegin(GL_LINES);
2385 glVertex2s(center[0], center[1]);
2386 glVertex2s(loc[0], loc[1]);
2387 glEnd();
2388
2389 // draw the CCW arrow
2390 glPushMatrix();
2391 glTranslatef(center[0], center[1], 0);
2392 glRotatef(ang+ANGLE_LEN, 0, 0, 1);
2393 glTranslatef(dist, 0, 0);
2394 DRAW_ARROW_MACRO
2395 glPopMatrix();
2396
2397 // draw the CW arrow
2398 glPushMatrix();
2399 glTranslatef(center[0], center[1], 0);
2400 glRotatef(ang-ANGLE_LEN, 0, 0, 1);
2401 glTranslatef(dist, 0, 0);
2402 glScalef(1, -1, 1);
2403 DRAW_ARROW_MACRO
2404 glPopMatrix();
2405 }
2406