Debian Codegraph @

libSoXt/src/viewers/SoXtWalkVwr.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.1.1.1 $
44 |
45 | Classes : SoXtWalkViewer
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 #include <inttypes.h>
53 #include <cmath>
54
55 #include <X11/Intrinsic.h>
56 #include <X11/Xlib.h>
57 #include <X11/keysym.h>
58
59 #include <Xm/LabelG.h>
60 #include <Xm/Form.h>
61 #include <Xm/PushBG.h>
62 #include <Sgm/ThumbWheel.h>
63
64 #include <Inventor/SoDB.h>
65 #include <Inventor/SoPickedPoint.h>
66 #include <Inventor/sensors/SoFieldSensor.h>
67 #include <Inventor/fields/SoSFTime.h>
68 #include <Inventor/nodes/SoCamera.h>
69 #include <Inventor/nodes/SoOrthographicCamera.h>
70 #include <Inventor/nodes/SoPerspectiveCamera.h>
71 #include <Inventor/nodes/SoSeparator.h>
72 #include <Inventor/actions/SoRayPickAction.h>
73 #include <Inventor/Xt/SoXtCursors.h>
74 #include <Inventor/Xt/viewers/SoXtWalkViewer.h>
75 #include <Inventor/Xt/SoXtResource.h>
76 #include <GL/gl.h>
77
78
79 /*
80 * Defines
81 */
82 enum ViewerModes {
83 PICK_MODE,
84 VIEW_MODE,
85 WALK_MODE_ACTIVE,
86 PAN_MODE,
87 PAN_MODE_ACTIVE,
88 TILT_MODE_ACTIVE,
89 SEEK_MODE,
90 SET_UP_MODE
91 };
92
93 #define REC_SIZE 12 // size of the square region on the screen
94 #define CROSS 12 // cross feedback size
95 #define TURN_SPEED 5.0
96 #define WALK_SPEED_FACTOR 6.0
97 #define WHEEL_DOLLY_FACTOR 0.5
98 #define WHEEL_HEIGHT_FACTOR 0.01
99
100
101 // Resources for labels.
102 typedef struct {
103 char *walkViewer;
104 char *wvPrefSheet;
105 } RES_LABELS;
106 static RES_LABELS rl;
107 static char *defaultLabel[]={
108 "Walk Viewer",
109 "Walk Viewer Preference Sheet",
110 };
111
112
113 ////////////////////////////////////////////////////////////////////////
114 //
115 // Public constructor - build the widget right now
116 //
117 SoXtWalkViewer::SoXtWalkViewer(
118 Widget parent,
119 const char *name,
120 SbBool buildInsideParent,
121 SoXtFullViewer::BuildFlag b,
122 SoXtViewer::Type t)
123 : SoXtConstrainedViewer(
124 parent,
125 name,
126 buildInsideParent,
127 b,
128 t,
129 FALSE) // tell base class not to build just yet
130 //
131 ////////////////////////////////////////////////////////////////////////
132 {
133 // In this case, render area is what the app wants, so buildNow = TRUE
134 constructorCommon(TRUE);
135 }
136
137 ////////////////////////////////////////////////////////////////////////
138 //
139 // SoEXTENDER constructor - the subclass tells us whether to build or not
140 //
141 SoXtWalkViewer::SoXtWalkViewer(
142 Widget parent,
143 const char *name,
144 SbBool buildInsideParent,
145 SoXtFullViewer::BuildFlag b,
146 SoXtViewer::Type t,
147 SbBool buildNow)
148 : SoXtConstrainedViewer(
149 parent,
150 name,
151 buildInsideParent,
152 b,
153 t,
154 FALSE) // tell base class not to build just yet
155 //
156 ////////////////////////////////////////////////////////////////////////
157 {
158 // In this case, render area may be what the app wants,
159 // or it may want a subclass of render area. Pass along buildNow
160 // as it was passed to us.
161 constructorCommon(buildNow);
162 }
163
164 ////////////////////////////////////////////////////////////////////////
165 //
166 // Called by the constructors
167 //
168 // private
169 //
170 void
171 SoXtWalkViewer::constructorCommon(SbBool buildNow)
172 //
173 ////////////////////////////////////////////////////////////////////////
174 {
175 // init local vars
176 mode = isViewing() ? VIEW_MODE : PICK_MODE;
177 createdCursors = FALSE;
178 walkCursor = panCursor = tiltCursor = seekCursor = upCursor = 0;
179 setClassName("SoXtWalkViewer");
180
181 // init animation variables
182 animatingFlag = FALSE;
183 animationSensor = new SoFieldSensor(SoXtWalkViewer::animationSensorCB, this);
184
185
186 addFinishCallback(SoXtWalkViewer::setFocalPointFinishCallback, this);
187
188 // Build the widget tree, and let SoXtComponent know about our base widget.
189 if (buildNow) {
190
191 // get resources...
192
193 SoXtResource xr(getParentWidget());
194 if (!xr.getResource( "walkViewer", "WalkViewer", rl.walkViewer ))
195 rl.walkViewer = defaultLabel[0];
196 if (!xr.getResource( "walkViewerPreferenceSheet","WalkViewerPreferenceSheet",rl.wvPrefSheet ))
197 rl.wvPrefSheet = defaultLabel[1];
198
199 // assign decoration titles
200 setPopupMenuString( rl.walkViewer );
201 setPrefSheetString( rl.wvPrefSheet );
202
203 Widget w = buildWidget(getParentWidget());
204 setBaseWidget(w);
205 }
206 }
207
208 ////////////////////////////////////////////////////////////////////////
209 //
210 // Description:
211 // Destructor.
212 //
213 // Use: public
214
215 SoXtWalkViewer::~SoXtWalkViewer()
216 //
217 ////////////////////////////////////////////////////////////////////////
218 {
219 delete animationSensor;
220
221 // free the viewer cursors
222 if (getDisplay()) {
223 Display *display = getDisplay();
224 if (walkCursor) XFreeCursor(display, walkCursor);
225 if (panCursor) XFreeCursor(display, panCursor);
226 if (tiltCursor) XFreeCursor(display, tiltCursor);
227 if (seekCursor) XFreeCursor(display, seekCursor);
228 if (upCursor) XFreeCursor(display, upCursor);
229 }
230 }
231
232 ////////////////////////////////////////////////////////////////////////
233 //
234 // Description:
235 // Call the base class and sets the correct cursors on the window
236 //
237 // Use: virtual public
238 void
239 SoXtWalkViewer::setViewing(SbBool flag)
240 //
241 ////////////////////////////////////////////////////////////////////////
242 {
243 if (flag == viewingFlag)
244 return;
245
246 // call the base class
247 SoXtConstrainedViewer::setViewing(flag);
248
249 switchMode(isViewing() ? VIEW_MODE : PICK_MODE);
250 }
251
252 ////////////////////////////////////////////////////////////////////////
253 //
254 // Description:
255 // Enables/Disable the viewer cursor on the window.
256 //
257 // Use: virtual public
258 void
259 SoXtWalkViewer::setCursorEnabled(SbBool flag)
260 //
261 ////////////////////////////////////////////////////////////////////////
262 {
263 if (flag == cursorEnabledFlag)
264 return;
265
266 cursorEnabledFlag = flag;
267
268 if (! isViewing())
269 return;
270
271 Display *display = getDisplay();
272 Widget w = getRenderAreaWidget();
273 Window window = (w != NULL) ? XtWindow(w) : 0;
274 if (! window)
275 return;
276
277 updateCursor();
278 }
279
280 ////////////////////////////////////////////////////////////////////////
281 //
282 // Description:
283 // redefines this to force the camera to be perspective (since
284 // orthographic camera don't any sense in this viewer).
285 //
286 // Use: virtual public
287 void
288 SoXtWalkViewer::setCamera(SoCamera *newCamera)
289 //
290 ////////////////////////////////////////////////////////////////////////
291 {
292 if (camera == newCamera)
293 return;
294
295 // call parent class
296 SoXtConstrainedViewer::setCamera(newCamera);
297
298 // now make sure the camera is not orthographic, else switch to
299 // perspective.
300 if (camera != NULL &&
301 camera->isOfType(SoOrthographicCamera::getClassTypeId()))
302 toggleCameraType();
303 }
304
305 ////////////////////////////////////////////////////////////////////////
306 //
307 // Description:
308 // This is redefined to prevent the camera type from being changed.
309 //
310 // Use: virtual public
311 void
312 #ifdef DEBUG
313 SoXtWalkViewer::setCameraType(SoType type)
314 #else
315 SoXtWalkViewer::setCameraType(SoType)
316 #endif
317 //
318 ////////////////////////////////////////////////////////////////////////
319 {
320 #ifdef DEBUG
321 if (! type.isDerivedFrom(SoPerspectiveCamera::getClassTypeId()))
322 SoDebugError::post("SoXtWalkViewer::setCameraType()",
323 "ignored - must be perspective camera");
324 #endif
325 }
326
327 ////////////////////////////////////////////////////////////////////////
328 //
329 // Description:
330 // Call the base class and switches to the correct viewer state.
331 //
332 // Use: virtual public
333 void
334 SoXtWalkViewer::setSeekMode(SbBool flag)
335 //
336 ////////////////////////////////////////////////////////////////////////
337 {
338 if (!isViewing())
339 return;
340
341 // call the base class
342 SoXtConstrainedViewer::setSeekMode(flag);
343
344 switchMode(isSeekMode() ? SEEK_MODE : VIEW_MODE);
345 }
346
347 ////////////////////////////////////////////////////////////////////////
348 //
349 // Description:
350 // Brings the viewer help card (called by "?" push button)
351 //
352 // Use: virtual protected
353 void
354 SoXtWalkViewer::openViewerHelpCard()
355 //
356 ////////////////////////////////////////////////////////////////////////
357 {
358 // tell the base class to open the file for us
359 openHelpCard("SoXtWalkViewer.help");
360 }
361
362 ////////////////////////////////////////////////////////////////////////
363 //
364 // Description:
365 // Process the given event to do viewing stuff
366 //
367 // Use: virtual protected
368 void
369 SoXtWalkViewer::processEvent(XAnyEvent *xe)
370 //
371 ////////////////////////////////////////////////////////////////////////
372 {
373 if ( processCommonEvents(xe) )
374 return;
375
376 if (!createdCursors)
377 updateCursor();
378
379 XButtonEvent *be;
380 XMotionEvent *me;
381 XKeyEvent *ke;
382 XCrossingEvent *ce;
383 KeySym keysym;
384
385 SbVec2s raSize = getGlxSize();
386
387 switch(xe->type) {
388 case ButtonPress:
389 case ButtonRelease:
390 be = (XButtonEvent *)xe;
391 if (be->button != Button1 && be->button != Button2)
392 break;
393
394 locator[0] = be->x;
395 locator[1] = raSize[1] - be->y;
396
397 if (mode == SEEK_MODE) {
398 if (xe->type == ButtonPress)
399 seekToPoint(locator);
400 }
401 else if (mode == SET_UP_MODE) {
402 if (xe->type == ButtonPress) {
403 findUpDirection(locator);
404 switchMode(VIEW_MODE);
405 }
406 }
407 else
408 updateViewerMode(be->state);
409 break;
410
411 case KeyPress:
412 case KeyRelease:
413 ke = (XKeyEvent *)xe;
414 keysym = XLookupKeysym(ke, 0);
415
416 locator[0] = ke->x;
417 locator[1] = raSize[1] - ke->y;
418 if (keysym == XK_Control_L || keysym == XK_Control_R)
419 updateViewerMode(ke->state);
420 else if (keysym == XK_u && xe->type == KeyPress) {
421 if (isSeekMode())
422 setSeekMode(FALSE);
423 switchMode( (mode == SET_UP_MODE) ? VIEW_MODE : SET_UP_MODE );
424 }
425 break;
426
427 case MotionNotify:
428 me = (XMotionEvent *)xe;
429 locator[0] = me->x;
430 locator[1] = raSize[1] - me->y;
431 if (mode == TILT_MODE_ACTIVE)
432 rotateCamera();
433 break;
434
435 case LeaveNotify:
436 case EnterNotify:
437 ce = (XCrossingEvent *)xe;
438 //
439 // because the application might use Ctrl-key for motif menu
440 // accelerators we might not receive a key-up event, so make sure
441 // to reset any Ctrl mode if we loose focus, but don't do anything
442 // if Ctrl-key is not down (nothing to do) or if a mouse button
443 // is down (we will get another leaveNotify).
444 //
445 if (! (ce->state & ControlMask))
446 break;
447 if (ce->state & Button1Mask || ce->state & Button2Mask)
448 break;
449 if (xe->type == LeaveNotify)
450 switchMode(VIEW_MODE);
451 else
452 updateViewerMode(ce->state);
453 break;
454 }
455 }
456
457 ////////////////////////////////////////////////////////////////////////
458 //
459 // Description:
460 // sets the viewer mode based on what keys and buttons are being pressed
461 //
462 // Use: private
463 void
464 SoXtWalkViewer::updateViewerMode(unsigned int state)
465 //
466 ////////////////////////////////////////////////////////////////////////
467 {
468 // ??? WARNING - this routine ONLY works because of
469 // ??? SoXtViewer::updateEventState() which is called for us
470 // ??? by SoXtViewer::processCommonEvents().
471 // ??? (XEvent state for button and modifier keys is not updated
472 // ??? until after the event is received. WEIRD)
473
474 // LEFT+MIDDLE down
475 if (state & Button1Mask && state & Button2Mask) {
476 switchMode(TILT_MODE_ACTIVE);
477 }
478
479 // LEFT down
480 else if (state & Button1Mask) {
481 if (state & ControlMask)
482 switchMode(PAN_MODE_ACTIVE);
483 else
484 switchMode(WALK_MODE_ACTIVE);
485 }
486
487 // MIDDLE DOWN
488 else if (state & Button2Mask) {
489 if (state & ControlMask)
490 switchMode(TILT_MODE_ACTIVE);
491 else
492 switchMode(PAN_MODE_ACTIVE);
493 }
494
495 // no buttons down...
496 else {
497 if (state & ControlMask)
498 switchMode(PAN_MODE);
499 else
500 switchMode(VIEW_MODE);
501 }
502 }
503
504 ////////////////////////////////////////////////////////////////////////
505 //
506 // Description:
507 // switches to the specified viewer mode
508 //
509 // Use: private
510 void
511 SoXtWalkViewer::switchMode(int newMode)
512 //
513 ////////////////////////////////////////////////////////////////////////
514 {
515 // assign new mode
516 SbBool redrawNeeded = FALSE;
517 int prevMode = mode;
518 mode = newMode;
519
520 // update the cursor
521 updateCursor();
522
523 // check the old viewer mode
524 switch (prevMode) {
525 case WALK_MODE_ACTIVE:
526 case PAN_MODE_ACTIVE:
527 stopAnimating();
528 redrawNeeded = TRUE;
529 break;
530 case TILT_MODE_ACTIVE:
531 redrawNeeded = TRUE;
532 interactiveCountDec();
533 break;
534 }
535
536 // switch to new viewer mode
537 switch (newMode) {
538 case PICK_MODE:
539 stopAnimating();
540 break;
541
542 case WALK_MODE_ACTIVE:
543 case PAN_MODE_ACTIVE:
544 startPos = locator;
545 startAnimating();
546 redrawNeeded = TRUE;
547 break;
548
549 case TILT_MODE_ACTIVE:
550 startPos = prevPos = locator;
551 redrawNeeded = TRUE;
552 interactiveCountInc();
553 break;
554 }
555
556 if (redrawNeeded)
557 scheduleRedraw();
558 }
559
560 ////////////////////////////////////////////////////////////////////////
561 //
562 // Description:
563 // updates the viewer cursor
564 //
565 // Use: private
566 void
567 SoXtWalkViewer::updateCursor()
568 //
569 ////////////////////////////////////////////////////////////////////////
570 {
571 Widget w = getRenderAreaWidget();
572 Display *display = w ? XtDisplay(w) : NULL;
573 Window window = w ? XtWindow(w) : 0;
574
575 if (! window)
576 return;
577
578 if (! createdCursors)
579 defineCursors();
580
581 // the viewer cursor are not enabled, then we don't set a new cursor.
582 // Instead erase the old viewer cursor.
583 if (! cursorEnabledFlag) {
584 XUndefineCursor(display, window);
585 return;
586 }
587
588 // ...else set the right cursor for the viewer mode....
589 switch(mode) {
590 case PICK_MODE:
591 XUndefineCursor(display, window);
592 break;
593
594 case VIEW_MODE:
595 case WALK_MODE_ACTIVE:
596 XDefineCursor(display, window, walkCursor);
597 break;
598
599 case PAN_MODE:
600 case PAN_MODE_ACTIVE:
601 XDefineCursor(display, window, panCursor);
602 break;
603
604 case TILT_MODE_ACTIVE:
605 XDefineCursor(display, window, tiltCursor);
606 break;
607
608 case SEEK_MODE:
609 XDefineCursor(display, window, seekCursor);
610 break;
611
612 case SET_UP_MODE:
613 XDefineCursor(display, window, upCursor);
614 break;
615 }
616 }
617
618 ////////////////////////////////////////////////////////////////////////
619 //
620 // Description:
621 // Starts ongoing animation
622 //
623 // Use: private
624 void
625 SoXtWalkViewer::startAnimating()
626 //
627 ////////////////////////////////////////////////////////////////////////
628 {
629 if (! animatingFlag) {
630 animationSensor->attach(viewerRealTime);
631 animationSensor->schedule();
632 prevAnimTime = viewerRealTime->getValue();
633 interactiveCountInc();
634 animatingFlag = TRUE;
635 }
636 }
637
638 ////////////////////////////////////////////////////////////////////////
639 //
640 // Description:
641 // Stops ongoing animation (if any)
642 //
643 // Use: private
644 void
645 SoXtWalkViewer::stopAnimating()
646 //
647 ////////////////////////////////////////////////////////////////////////
648 {
649 if (animatingFlag) {
650 animatingFlag = FALSE;
651 animationSensor->detach();
652 animationSensor->unschedule();
653 interactiveCountDec();
654 }
655 }
656
657 ////////////////////////////////////////////////////////////////////////
658 //
659 // Description:
660 // draws viewer feedback during a render area redraw of the scene.
661 //
662 // Use: virtual protected
663 void
664 SoXtWalkViewer::actualRedraw()
665 //
666 ////////////////////////////////////////////////////////////////////////
667 {
668 // have the base class draw the scene
669 SoXtConstrainedViewer::actualRedraw();
670
671 // now draw the viewer feedback
672 if (isViewing() && (mode == WALK_MODE_ACTIVE || mode == PAN_MODE_ACTIVE
673 || mode == TILT_MODE_ACTIVE)) {
674
675 setFeedbackOrthoProjection(getGlxSize());
676
677 // draw simple starting point feedback
678 glLineWidth(1);
679 glColor3ub(215,215,215);
680 if (mode == WALK_MODE_ACTIVE || mode == PAN_MODE_ACTIVE) {
681 glBegin(GL_LINE_LOOP);
682 glVertex2s(startPos[0] - REC_SIZE, startPos[1] - REC_SIZE);
683 glVertex2s(startPos[0] - REC_SIZE, startPos[1] + REC_SIZE);
684 glVertex2s(startPos[0] + REC_SIZE, startPos[1] + REC_SIZE);
685 glVertex2s(startPos[0] + REC_SIZE, startPos[1] - REC_SIZE);
686 glEnd();
687 }
688 else { // TILT_MODE_ACTIVE
689 glBegin(GL_LINES);
690 glVertex2s(startPos[0] - CROSS, startPos[1]);
691 glVertex2s(startPos[0] + CROSS, startPos[1]);
692 glVertex2s(startPos[0], startPos[1] - CROSS);
693 glVertex2s(startPos[0], startPos[1] + CROSS);
694 glEnd();
695 }
696
697 // now restore state
698 restoreGLStateAfterFeedback();
699 }
700 }
701
702 ////////////////////////////////////////////////////////////////////////
703 //
704 // Description:
705 // Redefine this to add an extra thumbwheel on the left side to change
706 // the eye level.
707 //
708 // Use: virtual protected
709 Widget
710 SoXtWalkViewer::buildLeftTrim(Widget parent)
711 //
712 ////////////////////////////////////////////////////////////////////////
713 {
714 int n;
715 Arg args[12];
716
717 // create a form to hold all the parts
718 Widget form = XtCreateWidget("LeftTrimForm", xmFormWidgetClass, parent, NULL, 0);
719
720 // create all the parts
721 buildLeftWheel(form);
722
723 // allocate extra thumbwheel
724 n = 0;
725 XtSetArg(args[n], XmNvalue, 0); n++;
726 XtSetArg(args[n], SgNangleRange, 0); n++;
727 XtSetArg(args[n], SgNunitsPerRotation, 360); n++;
728 XtSetArg(args[n], SgNshowHomeButton, FALSE); n++;
729 XtSetArg(args[n], XmNhighlightThickness, 0); n++;
730 XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
731 Widget wheel = SgCreateThumbWheel(form, NULL, args, n);
732
733 XtAddCallback(wheel, XmNvalueChangedCallback,
734 (XtCallbackProc) SoXtWalkViewer::heightWheelCB, (XtPointer) this);
735 XtAddCallback(wheel, XmNdragCallback,
736 (XtCallbackProc) SoXtWalkViewer::heightWheelCB, (XtPointer) this);
737 heightWheelVal = 0;
738
739 Widget label = XtCreateWidget("H", xmLabelGadgetClass, form, NULL, 0);
740 Widget butForm = buildAppButtons(form);
741
742 //
743 // layout
744 //
745
746 n = 0;
747 XtSetArg(args[n], XmNrightAttachment, XmNONE); n++;
748 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
749 XtSetArg(args[n], XmNleftOffset, 3); n++;
750 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
751 XtSetArg(args[n], XmNtopAttachment, XmNONE); n++;
752 XtSetValues(leftWheel, args, n);
753
754 n = 0;
755 XtSetArg(args[n], XmNrightAttachment, XmNONE); n++;
756 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
757 XtSetArg(args[n], XmNleftOffset, 7); n++;
758 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
759 XtSetArg(args[n], XmNbottomWidget, leftWheel); n++;
760 XtSetArg(args[n], XmNbottomOffset, 10); n++;
761 XtSetArg(args[n], XmNtopAttachment, XmNONE); n++;
762 XtSetValues(label, args, n);
763
764 n = 0;
765 XtSetArg(args[n], XmNrightAttachment, XmNONE); n++;
766 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
767 XtSetArg(args[n], XmNleftOffset, 3); n++;
768 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
769 XtSetArg(args[n], XmNbottomWidget, label); n++;
770 XtSetArg(args[n], XmNtopAttachment, XmNONE); n++;
771 XtSetValues(wheel, args, n);
772
773 n = 0;
774 XtSetArg(args[n], XmNrightAttachment, XmNONE); n++;
775 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
776 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
777 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
778 XtSetArg(args[n], XmNbottomWidget, wheel); n++;
779 XtSetValues(butForm, args, n);
780
781 // manage children
782 XtManageChild(leftWheel);
783 XtManageChild(label);
784 XtManageChild(wheel);
785 XtManageChild(butForm);
786
787 return form;
788 }
789
790 ////////////////////////////////////////////////////////////////////////
791 //
792 // Description:
793 // Redefine this routine to add some viewer specific stuff.
794 //
795 // Use: virtual protected
796 void
797 SoXtWalkViewer::createPrefSheet()
798 //
799 ////////////////////////////////////////////////////////////////////////
800 {
801 // create the preference sheet shell and form widget
802 Widget shell, form;
803 createPrefSheetShellAndForm(shell, form);
804
805 // create all of the parts
806 Widget widgetList[10];
807 int num = 0;
808 createDefaultPrefSheetParts(widgetList, num, form);
809 widgetList[num++] = createSpeedPrefSheetGuts(form);
810
811 layoutPartsAndMapPrefSheet(widgetList, num, form, shell);
812 }
813
814 ////////////////////////////////////////////////////////////////////////
815 //
816 // Description:
817 // This routine is used to define cursors (can only be called after
818 // window has been realized).
819 //
820 // Use: private
821
822 void
823 SoXtWalkViewer::defineCursors()
824 //
825 ////////////////////////////////////////////////////////////////////////
826 {
827 XColor foreground, background;
828 Pixmap source, mask;
829 Display *display = getDisplay();
830 Drawable d = DefaultRootWindow(display);
831
832 // set color
833 foreground.red = 65535;
834 foreground.green = foreground.blue = 0;
835 background.red = background.green = background.blue = 65535;
836
837 // walk cursor
838 source = XCreateBitmapFromData(display, d,
839 so_xt_walk_bits, so_xt_walk_width, so_xt_walk_height);
840 mask = XCreateBitmapFromData(display, d,
841 so_xt_walk_mask_bits, so_xt_walk_width, so_xt_walk_height);
842 walkCursor = XCreatePixmapCursor(display, source, mask,
843 &foreground, &background, so_xt_walk_x_hot, so_xt_walk_y_hot);
844 XFreePixmap(display, source);
845 XFreePixmap(display, mask);
846
847 // panning cursor
848 source = XCreateBitmapFromData(display, d,
849 so_xt_pan_bits, so_xt_pan_width, so_xt_pan_height);
850 panCursor = XCreatePixmapCursor(display, source, source,
851 &foreground, &foreground, so_xt_pan_x_hot, so_xt_pan_y_hot);
852 XFreePixmap(display, source);
853
854 // tilt cursor
855 source = XCreateBitmapFromData(display, d,
856 so_xt_tilt_bits, so_xt_tilt_width, so_xt_tilt_height);
857 mask = XCreateBitmapFromData(display, d,
858 so_xt_tilt_mask_bits, so_xt_tilt_width, so_xt_tilt_height);
859 tiltCursor = XCreatePixmapCursor(display, source, mask,
860 &foreground, &background, so_xt_tilt_x_hot, so_xt_tilt_y_hot);
861 XFreePixmap(display, source);
862 XFreePixmap(display, mask);
863
864 // seek cursor
865 source = XCreateBitmapFromData(display, d,
866 so_xt_target_bits, so_xt_target_width, so_xt_target_height);
867 seekCursor = XCreatePixmapCursor(display, source, source,
868 &foreground, &foreground, so_xt_target_x_hot, so_xt_target_y_hot);
869 XFreePixmap(display, source);
870
871 // up direction cursor
872 source = XCreateBitmapFromData(display, d,
873 so_xt_normal_vec_bits, so_xt_normal_vec_width, so_xt_normal_vec_height);
874 upCursor = XCreatePixmapCursor(display, source, source,
875 &foreground, &foreground, so_xt_normal_vec_x_hot, so_xt_normal_vec_y_hot);
876 XFreePixmap(display, source);
877
878 createdCursors = TRUE;
879 }
880
881 ////////////////////////////////////////////////////////////////////////
882 //
883 // Description:
884 // Called when we rotate the camera (tilt up/down and rotate sideway)
885 //
886 // Use: private
887
888 void
889 SoXtWalkViewer::rotateCamera()
890 //
891 ////////////////////////////////////////////////////////////////////////
892 {
893 if (camera == NULL)
894 return;
895
896 SbVec2s raSize = getGlxSize();
897
898 //
899 // tilt the head up/down (constrained to 180 degree)
900 //
901
902 float mouseY = (locator[1] - prevPos[1]) / float(raSize[1]);
903 if (mouseY)
904 SoXtConstrainedViewer::tiltCamera(mouseY * M_PI);
905
906 //
907 // rotate the camera left/right around the Up direction (no roll alowed)
908 //
909
910 float mouseX = (locator[0] - prevPos[0]) / float(raSize[0]);
911 if (mouseX) {
912 float angle = - mouseX * M_PI;
913 SbRotation rot(upDirection, angle);
914 camera->orientation = camera->orientation.getValue() * rot;
915 }
916
917 prevPos = locator;
918 }
919
920 ////////////////////////////////////////////////////////////////////////
921 //
922 // Description:
923 // Called by the animation sensor to change the camera position
924 //
925 // Use: private
926
927 void
928 SoXtWalkViewer::doCameraAnimation()
929 //
930 ////////////////////////////////////////////////////////////////////////
931 {
932 if (camera == NULL)
933 return;
934
935 SbVec2s raSize = getGlxSize();
936
937 //
938 // get time interval since last call
939 //
940 SbTime time = viewerRealTime->getValue();
941 float sec = float((time - prevAnimTime).getValue());
942 prevAnimTime = time;
943
944 // make sure to have at least a delta time for the first call.
945 if (sec == 0.0)
946 sec = 1.0/72.0;
947
948 //
949 // move forward/backward and sideway
950 //
951 if (mode == WALK_MODE_ACTIVE) {
952
953 // turn the camera left/right using the distance^2 which gives a nice
954 // gradual speedup.
955 float mouseX = (locator[0] - startPos[0]) / float(raSize[0]);
956 float angle = TURN_SPEED * (mouseX * mouseX) * sec;
957 if (angle != 0.0) {
958 if (mouseX < 0)
959 angle = -angle;
960 SbRotation rot(upDirection, -angle);
961 camera->orientation = camera->orientation.getValue() * rot;
962 }
963
964 // move forward/backward
965 float mouseY = (locator[1] - startPos[1]) / float(raSize[1]);
966 float dist = viewerSpeed * sceneSize * (mouseY * mouseY) * sec
967 * WALK_SPEED_FACTOR;
968 if (mouseY < 0)
969 dist = -dist;
970 if (dist != 0.0)
971 dollyCamera(dist);
972 }
973 //
974 // else pan in the viewer focal plane.
975 //
976 else if (mode == PAN_MODE_ACTIVE) {
977
978 // move the camera up/down/left/right using the distance^2 which
979 // gives a nice gradual speedup.
980 float mouseX = (locator[0] - startPos[0]) / float(raSize[0]);
981 float mouseY = (locator[1] - startPos[1]) / float(raSize[1]);
982 float distX = viewerSpeed * sceneHeight * (mouseX * mouseX) * sec
983 * WALK_SPEED_FACTOR;
984 float distY = viewerSpeed * sceneHeight * (mouseY * mouseY) * sec
985 * WALK_SPEED_FACTOR;
986 if (mouseX < 0)
987 distX = -distX;
988 if (mouseY < 0)
989 distY = -distY;
990
991 // figure out the up and right vector
992 SbMatrix mx;
993 mx = camera->orientation.getValue();
994 SbVec3f upVector(mx[1][0], mx[1][1], mx[1][2]);
995 SbVec3f rightVector(mx[0][0], mx[0][1], mx[0][2]);
996
997 // finally move the camera
998 if (distX)
999 camera->position = camera->position.getValue() + distX * rightVector;
1000 if (distY)
1001 camera->position = camera->position.getValue() + distY * upVector;
1002 }
1003 }
1004
1005 ////////////////////////////////////////////////////////////////////////
1006 //
1007 // Description:
1008 // Redefines this to move at the eye level (default) rather than
1009 // directly forward.
1010 //
1011 // Use: virtual protected
1012
1013 void
1014 SoXtWalkViewer::rightWheelMotion(float newVal)
1015 //
1016 ////////////////////////////////////////////////////////////////////////
1017 {
1018 float dist = (newVal - rightWheelVal) * sceneSize * viewerSpeed
1019 * WHEEL_DOLLY_FACTOR;
1020 dollyCamera(dist);
1021
1022 rightWheelVal = newVal;
1023 }
1024
1025
1026 ////////////////////////////////////////////////////////////////////////
1027 //
1028 // Description:
1029 // Move the camera in the eye level plane (default
1030 // way of walking) or in the lookat direction which is an alternate way
1031 // of walking (base class)
1032 //
1033 // Use: private
1034
1035 void
1036 SoXtWalkViewer::dollyCamera(float dist)
1037 //
1038 ////////////////////////////////////////////////////////////////////////
1039 {
1040 if (camera == NULL)
1041 return;
1042
1043 // compute the eye level direction vector. This is simply
1044 // the cross produc between the up direction and the
1045 // camera right vector
1046 SbMatrix mx;
1047 mx = camera->orientation.getValue();
1048 SbVec3f eyeLevel = upDirection.cross( SbVec3f(mx[0][0], mx[0][1], mx[0][2]) );
1049 eyeLevel.normalize();
1050
1051 // move camera by distance
1052 camera->position = camera->position.getValue() + eyeLevel * dist;
1053 camera->focalDistance = camera->focalDistance.getValue() - dist;
1054 }
1055
1056 ////////////////////////////////////////////////////////////////////////
1057 //
1058 // Description:
1059 // This routine will pick a few times around the center of the
1060 // screen to get an estimate of what the camera focal distance should
1061 // be. Some viewers (like the examiner viewer) depend a resonable
1062 // focal distance, so we need to have it point at objects in front of us.
1063 //
1064 // Use: private
1065
1066 void
1067 SoXtWalkViewer::updateCameraFocalPoint()
1068 //
1069 ////////////////////////////////////////////////////////////////////////
1070 {
1071 int numPicked = 0;
1072 float distance = 0;
1073 float locations[4][2] = {{.5, .3}, {.5, .7}, {.3, .5}, {.7, .5}};
1074 SbVec3f camPos = camera->position.getValue();
1075 SoPickedPoint *pp;
1076
1077 SoRayPickAction pick = SoRayPickAction(SbViewportRegion(getGlxSize()));
1078 pick.setRadius(1.0);
1079 pick.setPickAll(FALSE); // pick only the closest object
1080
1081 //
1082 // pick around the center point, and average
1083 // all the picks to get a more resonable value.
1084 //
1085 for (int i=0; i<4; i++) {
1086 pick.setNormalizedPoint(SbVec2f(locations[i]));
1087 pick.apply(sceneRoot);
1088 pp = pick.getPickedPoint();
1089 if (pp) {
1090 distance += (pp->getPoint() - camPos).length();
1091 numPicked++;
1092
1093 // only need two good picks...(faster)
1094 if (numPicked == 2)
1095 break;
1096 }
1097 }
1098
1099 if (numPicked > 1)
1100 distance /= float(numPicked);
1101
1102 // now update the camera focal distance
1103 if (distance != 0) {
1104 camera->enableNotify(FALSE); // don't cause a redraw
1105 camera->focalDistance = distance;
1106 camera->enableNotify(TRUE);
1107 }
1108 }
1109
1110
1111
1112 //
1113 // redefine those generic virtual functions
1114 //
1115 const char *
1116 SoXtWalkViewer::getDefaultWidgetName() const
1117 { return "SoXtWalkViewer"; }
1118
1119 const char *
1120 SoXtWalkViewer::getDefaultTitle() const
1121 { return rl.walkViewer; }
1122
1123 const char *
1124 SoXtWalkViewer::getDefaultIconTitle() const
1125 { return rl.walkViewer; }
1126
1127
1128
1129
1130
1131
1132 //
1133 ////////////////////////////////////////////////////////////////////////
1134 // static callbacks stubs
1135 ////////////////////////////////////////////////////////////////////////
1136 //
1137
1138 void
1139 SoXtWalkViewer::animationSensorCB(void *p, SoSensor *)
1140 { ((SoXtWalkViewer *)p)->doCameraAnimation(); }
1141
1142 void
1143 SoXtWalkViewer::heightWheelCB(Widget, SoXtWalkViewer *p, XtPointer *d)
1144 {
1145 if (p->camera == NULL)
1146 return;
1147
1148 static SbBool firstDrag = TRUE;
1149 SgThumbWheelCallbackStruct *data = (SgThumbWheelCallbackStruct *) d;
1150
1151 if (data->reason == XmCR_DRAG) {
1152 // for the first move, invoke the start callbacks
1153 if (firstDrag) {
1154 p->interactiveCountInc();
1155 firstDrag = FALSE;
1156 }
1157
1158 // move the camera up/down
1159 float d = (p->heightWheelVal - data->value) * p->sceneHeight
1160 * p->viewerSpeed * WHEEL_HEIGHT_FACTOR;
1161 p->camera->position = p->camera->position.getValue() + d * p->upDirection;
1162 p->heightWheelVal = data->value;
1163 }
1164 else {
1165 // reason = XmCR_VALUE_CHANGED, invoke the finish callbacks
1166 p->interactiveCountDec();
1167 firstDrag = TRUE;
1168 }
1169 }
1170
1171 //
1172 // called by the viewer finish callback to update the camera focal point
1173 //
1174 void
1175 SoXtWalkViewer::setFocalPointFinishCallback(void *data, SoXtViewer *)
1176 {
1177 SoXtWalkViewer *p = (SoXtWalkViewer *) data;
1178
1179 // if we are not at the end of a seek, update the camera focal distance
1180 if (! p->isSeekMode())
1181 p->updateCameraFocalPoint();
1182 }