Debian Codegraph @

libSoXt/src/viewers/SoXtFlyVwr.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 : SoXtFlyViewer
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 <X11/Intrinsic.h>
55 #include <X11/Xlib.h>
56 #include <X11/keysym.h>
57
58 #include <Xm/LabelG.h>
59 #include <Xm/Form.h>
60 #include <Xm/PushBG.h>
61
62 #include <Inventor/SoDB.h>
63 #include <Inventor/SoPickedPoint.h>
64 #include <Inventor/sensors/SoFieldSensor.h>
65 #include <Inventor/fields/SoSFTime.h>
66 #include <Inventor/nodes/SoCamera.h>
67 #include <Inventor/nodes/SoOrthographicCamera.h>
68 #include <Inventor/nodes/SoPerspectiveCamera.h>
69 #include <Inventor/Xt/SoXtCursors.h>
70 #include <Inventor/Xt/viewers/SoXtFlyViewer.h>
71 #include <Inventor/Xt/SoXtResource.h>
72 #include <GL/gl.h>
73
74
75 /*
76 * Defines
77 */
78 enum ViewerModes {
79 STILL_MODE,
80 FLY_MODE,
81 TILT_MODE,
82 SEEK_MODE,
83 SET_UP_MODE
84 };
85
86 // color used in feedback
87 #define DARK_COLOR glColor3ub(90, 90, 90)
88 #define TEXT_COLOR glColor3ub(255, 255, 90)
89 #define MAX_SPEED_COL glColor3ub(0, 200, 200)
90 #define CUR_SPEED_COL glColor3ub(255, 0, 0)
91
92 #define CROSS 12 // cross feedback size
93 #define CIRCLE_RAD 15 // center circle radius
94 #define TEXT_DX 0.05
95 #define TEXT_DY 0.03
96 #define DECOR_DX 0.15
97
98 #define MIN_SPEED 0.02
99 #define MAX_INC 1.5
100 #define TURN_SPEED .8*M_PI // radians/second
101
102 #define WHEEL_DOLLY_FACTOR 0.5
103
104
105 // Resources for labels.
106 typedef struct {
107 char *flyViewer;
108 char *fvPrefSheet;
109 char *flyingSpeed;
110 char *increase;
111 char *decrease;
112 } RES_LABELS;
113 static RES_LABELS rl;
114 static char *defaultLabel[]={
115 "Fly Viewer",
116 "Fly Viewer Preference Sheet",
117 "Flying speed:",
118 " increase ",
119 " decrease "
120 };
121
122
123 ////////////////////////////////////////////////////////////////////////
124 //
125 // Public constructor - build the widget right now
126 //
127 SoXtFlyViewer::SoXtFlyViewer(
128 Widget parent,
129 const char *name,
130 SbBool buildInsideParent,
131 SoXtFullViewer::BuildFlag b,
132 SoXtViewer::Type t)
133 : SoXtConstrainedViewer(
134 parent,
135 name,
136 buildInsideParent,
137 b,
138 t,
139 FALSE) // tell base class not to build just yet
140 //
141 ////////////////////////////////////////////////////////////////////////
142 {
143 // In this case, render area is what the app wants, so buildNow = TRUE
144 constructorCommon(TRUE);
145 }
146
147 ////////////////////////////////////////////////////////////////////////
148 //
149 // SoEXTENDER constructor - the subclass tells us whether to build or not
150 //
151 SoXtFlyViewer::SoXtFlyViewer(
152 Widget parent,
153 const char *name,
154 SbBool buildInsideParent,
155 SoXtFullViewer::BuildFlag b,
156 SoXtViewer::Type t,
157 SbBool buildNow)
158 : SoXtConstrainedViewer(
159 parent,
160 name,
161 buildInsideParent,
162 b,
163 t,
164 FALSE) // tell base class not to build just yet
165 //
166 ////////////////////////////////////////////////////////////////////////
167 {
168 // In this case, render area may be what the app wants,
169 // or it may want a subclass of render area. Pass along buildNow
170 // as it was passed to us.
171 constructorCommon(buildNow);
172 }
173
174 ////////////////////////////////////////////////////////////////////////
175 //
176 // Called by the constructors
177 //
178 // private
179 //
180 void
181 SoXtFlyViewer::constructorCommon(SbBool buildNow)
182 //
183 ////////////////////////////////////////////////////////////////////////
184 {
185 // init vars
186 mode = STILL_MODE;
187 createdCursors = FALSE;
188 viewerCursor = seekCursor = upCursor = 0;
189 speedLimitFactor = 0.5;
190 setClassName("SoXtFlyViewer");
191
192 // init animation variables
193 animationSensor = new SoFieldSensor(SoXtFlyViewer::animationSensorCB, this);
194
195
196 // Build the widget tree, and let SoXtComponent know about our base widget.
197 if (buildNow) {
198
199 // get resources...
200
201 SoXtResource xr(getParentWidget());
202 if (!xr.getResource( "flyViewer", "FlyViewer", rl.flyViewer ))
203 rl.flyViewer = defaultLabel[0];
204 if (!xr.getResource( "flyViewerPreferenceSheet","FlyViewerPreferenceSheet",rl.fvPrefSheet ))
205 rl.fvPrefSheet = defaultLabel[1];
206 if (!xr.getResource( "flyingSpeed", "FlyingSpeed", rl.flyingSpeed ))
207 rl.flyingSpeed = defaultLabel[2];
208 if (!xr.getResource( "increase", "Increase", rl.increase ))
209 rl.increase = defaultLabel[3];
210 if (!xr.getResource( "decrease", "Decrease", rl.decrease ))
211 rl.decrease = defaultLabel[4];
212
213 // assign decoration names
214 setPopupMenuString( rl.flyViewer );
215 setPrefSheetString( rl.fvPrefSheet );
216
217 Widget w = buildWidget(getParentWidget());
218 setBaseWidget(w);
219 }
220 }
221
222 ////////////////////////////////////////////////////////////////////////
223 //
224 // Destructor
225 //
226 // Use: public
227
228 SoXtFlyViewer::~SoXtFlyViewer()
229 //
230 ////////////////////////////////////////////////////////////////////////
231 {
232 // free the viewer cursors
233 if (getDisplay()) {
234 Display *display = getDisplay();
235 if (viewerCursor) XFreeCursor(display, viewerCursor);
236 if (seekCursor) XFreeCursor(display, seekCursor);
237 if (upCursor) XFreeCursor(display, upCursor);
238 }
239 }
240
241 ////////////////////////////////////////////////////////////////////////
242 //
243 // Description:
244 // Call the base class and sets the correct cursors on the window
245 //
246 // Use: virtual public
247 void
248 SoXtFlyViewer::setViewing(SbBool flag)
249 //
250 ////////////////////////////////////////////////////////////////////////
251 {
252 if (flag == viewingFlag)
253 return;
254
255 // call the base class
256 SoXtConstrainedViewer::setViewing(flag);
257
258 // set the right cursor
259 Widget w = getRenderAreaWidget();
260 Window window = (w != NULL) ? XtWindow(w) : 0;
261 if (window != 0) {
262 if (!createdCursors)
263 defineCursors();
264 if (isViewing())
265 XDefineCursor(XtDisplay(w), window, viewerCursor);
266 else
267 XUndefineCursor(XtDisplay(w), window);
268 }
269
270 // erase viewer feedback
271 if (mode != STILL_MODE)
272 switchMode(STILL_MODE);
273 else
274 scheduleRedraw();
275 }
276
277 ////////////////////////////////////////////////////////////////////////
278 //
279 // Description:
280 // Enables/Disable the viewer cursor on the window.
281 //
282 // Use: virtual public
283 void
284 SoXtFlyViewer::setCursorEnabled(SbBool flag)
285 //
286 ////////////////////////////////////////////////////////////////////////
287 {
288 if (flag == cursorEnabledFlag)
289 return;
290
291 cursorEnabledFlag = flag;
292
293 if (! isViewing())
294 return;
295
296 Display *display = getDisplay();
297 Widget w = getRenderAreaWidget();
298 Window window = (w != NULL) ? XtWindow(w) : 0;
299 if (! window)
300 return;
301
302 // now set the right cursor on the window
303 if (flag) {
304
305 if (! createdCursors)
306 defineCursors();
307
308 switch(mode) {
309 case STILL_MODE:
310 case FLY_MODE:
311 case TILT_MODE:
312 XDefineCursor(display, window, viewerCursor);
313 break;
314 case SEEK_MODE:
315 XDefineCursor(display, window, seekCursor);
316 break;
317 case SET_UP_MODE:
318 XDefineCursor(display, window, upCursor);
319 break;
320 }
321 }
322 else
323 XUndefineCursor(display, window);
324 }
325
326 ////////////////////////////////////////////////////////////////////////
327 //
328 // Description:
329 // redefines this to force the camera to be perspective (since
330 // orthographic camera don't any sense in this viewer).
331 //
332 // Use: virtual public
333 void
334 SoXtFlyViewer::setCamera(SoCamera *newCamera)
335 //
336 ////////////////////////////////////////////////////////////////////////
337 {
338 if (camera == newCamera)
339 return;
340
341 // call parent class
342 SoXtConstrainedViewer::setCamera(newCamera);
343
344 // now make sure the camera is not orthographic, else switch to
345 // perspective.
346 if (camera != NULL &&
347 camera->isOfType(SoOrthographicCamera::getClassTypeId()))
348 toggleCameraType();
349 }
350
351 ////////////////////////////////////////////////////////////////////////
352 //
353 // Description:
354 // This is redefined to prevent the camera type from being changed.
355 //
356 // Use: virtual public
357 void
358 SoXtFlyViewer::setCameraType(SoType type)
359 //
360 ////////////////////////////////////////////////////////////////////////
361 {
362 #ifdef DEBUG
363 if (! type.isDerivedFrom(SoPerspectiveCamera::getClassTypeId()))
364 SoDebugError::post("SoXtWalkViewer::setCameraType()",
365 "ignored - must be perspective camera");
366 #endif
367 }
368
369 ////////////////////////////////////////////////////////////////////////
370 //
371 // Description:
372 // Call the base class and sets the correct cursors on the window
373 //
374 // Use: virtual public
375 void
376 SoXtFlyViewer::setSeekMode(SbBool flag)
377 //
378 ////////////////////////////////////////////////////////////////////////
379 {
380 if (!isViewing())
381 return;
382
383 // call the base class
384 SoXtConstrainedViewer::setSeekMode(flag);
385
386 // set the new viewer mode
387 if (isSeekMode())
388 switchMode(SEEK_MODE);
389 else
390 switchMode(STILL_MODE);
391 }
392
393 ////////////////////////////////////////////////////////////////////////
394 //
395 // Description:
396 // Call the base class and stops any flying if any.
397 //
398 // Use: virtual public
399 void
400 SoXtFlyViewer::resetToHomePosition()
401 //
402 ////////////////////////////////////////////////////////////////////////
403 {
404 // call the base class
405 SoXtConstrainedViewer::resetToHomePosition();
406
407 // set the new viewer mode
408 switchMode(STILL_MODE);
409 }
410
411 ////////////////////////////////////////////////////////////////////////
412 //
413 // Description:
414 // Redefine this routine to add some viewer specific stuff.
415 //
416 // Use: virtual protected
417 void
418 SoXtFlyViewer::createPrefSheet()
419 //
420 ////////////////////////////////////////////////////////////////////////
421 {
422 // create the preference sheet shell and form widget
423 Widget shell, form;
424 createPrefSheetShellAndForm(shell, form);
425
426 // create all of the parts
427 Widget widgetList[10];
428 int num = 0;
429 createDefaultPrefSheetParts(widgetList, num, form);
430 widgetList[num++] = createFlyPrefSheetGuts(form);
431
432 layoutPartsAndMapPrefSheet(widgetList, num, form, shell);
433 }
434
435 ////////////////////////////////////////////////////////////////////////
436 //
437 // Description:
438 // Brings the viewer help card (called by "?" push button)
439 //
440 // Use: virtual protected
441 void
442 SoXtFlyViewer::openViewerHelpCard()
443 //
444 ////////////////////////////////////////////////////////////////////////
445 {
446 // tell the base class to open the file for us
447 openHelpCard("SoXtFlyViewer.help");
448 }
449
450 ////////////////////////////////////////////////////////////////////////
451 //
452 // Description:
453 // Process the given event to do viewing stuff
454 //
455 // Use: virtual protected
456 void
457 SoXtFlyViewer::processEvent(XAnyEvent *xe)
458 //
459 ////////////////////////////////////////////////////////////////////////
460 {
461 // check for leave and enter notify, in which case the viewer
462 // temporaly stops flying and resume flying if it was flying.
463 if (isViewing() && mode == FLY_MODE) {
464 if (xe->type == LeaveNotify) {
465 animationSensor->detach();
466 animationSensor->unschedule();
467 interactiveCountDec();
468 }
469 else if (xe->type == EnterNotify) {
470 animationSensor->attach(viewerRealTime);
471 animationSensor->schedule();
472 prevAnimTime = viewerRealTime->getValue();
473 interactiveCountInc();
474 }
475 }
476
477 if ( processCommonEvents(xe) )
478 return;
479
480 if (!createdCursors) {
481 defineCursors();
482 Widget w = getRenderAreaWidget();
483 XDefineCursor(XtDisplay(w), XtWindow(w), viewerCursor);
484 }
485
486 SbVec2s raSize = getGlxSize();
487 XButtonEvent *be;
488 XKeyEvent *ke;
489
490 switch(xe->type) {
491 case ButtonPress:
492 be = (XButtonEvent *)xe;
493 if (be->button != Button1 && be->button != Button2)
494 break;
495 locator[0] = be->x;
496 locator[1] = raSize[1] - be->y;
497 switch(mode) {
498 case STILL_MODE:
499 // check if both buttons are down
500 if ((be->button == Button1 && be->state & Button2Mask) ||
501 (be->button == Button2 && be->state & Button1Mask))
502 break;
503 switchMode(FLY_MODE);
504 changeMaxStraightSpeed(be->button == Button1);
505 speed = maxSpeed;
506 break;
507 case FLY_MODE:
508 // check if both buttons are down
509 if ((be->button == Button1 && be->state & Button2Mask) ||
510 (be->button == Button2 && be->state & Button1Mask))
511 switchMode(STILL_MODE);
512 else
513 changeMaxStraightSpeed(be->button == Button1);
514 break;
515 case SEEK_MODE:
516 if (be->button == Button1)
517 seekToPoint(locator);
518 break;
519 case SET_UP_MODE:
520 if (be->button == Button1) {
521 findUpDirection(locator);
522 switchMode(STILL_MODE);
523 }
524 break;
525 }
526 break;
527
528 case MotionNotify:
529 locator[0] = ((XMotionEvent *)xe)->x;
530 locator[1] = raSize[1] - ((XMotionEvent *)xe)->y;
531 switch(mode) {
532 case FLY_MODE:
533 // find new max speed based on curvature
534 calculateMaxSpeed();
535 break;
536 case TILT_MODE:
537 // reset the camera orientation
538 camera->orientation = camStartOrientation;
539
540 // rotate right/left
541 if (locator[0] != startPos[0]) {
542 float angle = (startPos[0] - locator[0]) / float(raSize[0]);
543 SbRotation rot(upDirection, angle * 2 * M_PI);
544 camera->orientation = camera->orientation.getValue() * rot;
545 }
546
547 // tilt up/down
548 if (locator[1] != startPos[1]) {
549 float angle = (locator[1] - startPos[1]) / float(raSize[1]);
550 tiltCamera(angle * 2 * M_PI);
551 }
552 break;
553 }
554 break;
555
556 case KeyPress:
557 ke = (XKeyEvent *)xe;
558 locator[0] = ke->x;
559 locator[1] = raSize[1] - ke->y;
560 switch ( XLookupKeysym(ke, 0) ) {
561 case XK_u:
562 if (isSeekMode())
563 setSeekMode(FALSE);
564 switchMode( (mode == SET_UP_MODE) ? STILL_MODE : SET_UP_MODE );
565 break;
566 case XK_Control_L:
567 case XK_Control_R:
568 if (mode == STILL_MODE || mode == FLY_MODE) {
569 interactiveCountInc();
570 switchMode(TILT_MODE);
571 }
572 break;
573 }
574 break;
575
576 case KeyRelease:
577 ke = (XKeyEvent *)xe;
578 switch ( XLookupKeysym(ke, 0) ) {
579 case XK_Control_L:
580 case XK_Control_R:
581 if (mode == TILT_MODE) {
582 switchMode(STILL_MODE);
583 interactiveCountDec();
584 }
585 break;
586 }
587 break;
588 }
589 }
590
591 ////////////////////////////////////////////////////////////////////////
592 //
593 // Description:
594 // switches to the specified viewer mode
595 //
596 // Use: private
597 void
598 SoXtFlyViewer::switchMode(int newMode)
599 //
600 ////////////////////////////////////////////////////////////////////////
601 {
602 if (mode == newMode)
603 return;
604
605 // assing new mode
606 SbBool redrawNeeded = TRUE;
607 int prevMode = mode;
608 mode = newMode;
609
610 // needed to define new cursors
611 Widget w = getRenderAreaWidget();
612 Display *display = (w != NULL) ? XtDisplay(w) : NULL;
613 Window window = (w != NULL) ? XtWindow(w) : 0;
614 if (!createdCursors && window != 0)
615 defineCursors();
616
617 // check the old viewer mode
618 switch(prevMode) {
619 case FLY_MODE:
620 animationSensor->detach();
621 animationSensor->unschedule();
622 interactiveCountDec();
623 break;
624 }
625
626 // switch to new viewer mode
627 switch(newMode) {
628 case STILL_MODE:
629 if (window != 0)
630 XDefineCursor(display, window, viewerCursor);
631 break;
632 case FLY_MODE:
633 animationSensor->attach(viewerRealTime);
634 animationSensor->schedule();
635 prevAnimTime = viewerRealTime->getValue();
636 interactiveCountInc();
637 speed = maxSpeed = maxStraightSpeed = 0;
638 speedLimit = sceneSize * speedLimitFactor;
639 redrawNeeded = FALSE; // wait for sensor to fire
640 if (window != 0)
641 XDefineCursor(display, window, viewerCursor);
642 break;
643 case TILT_MODE:
644 // save mouse and camera starting values
645 startPos = locator;
646 camStartOrientation = camera->orientation.getValue();
647 if (window != 0)
648 XDefineCursor(display, window, viewerCursor);
649 break;
650 case SEEK_MODE:
651 if (window != 0)
652 XDefineCursor(display, window, seekCursor);
653 break;
654 case SET_UP_MODE:
655 if (window != 0)
656 XDefineCursor(display, window, upCursor);
657 break;
658 }
659
660 if (redrawNeeded)
661 scheduleRedraw();
662 }
663
664 ////////////////////////////////////////////////////////////////////////
665 //
666 // Description:
667 // draws viewer feedback during a render area redraw of the scene.
668 //
669 // Use: virtual protected
670 void
671 SoXtFlyViewer::actualRedraw()
672 //
673 ////////////////////////////////////////////////////////////////////////
674 {
675 // have the base class draw the scene
676 SoXtConstrainedViewer::actualRedraw();
677
678 // now draw the viewer feedback
679 if (isViewing() && camera != NULL) {
680
681 setFeedbackOrthoProjection(getGlxSize());
682
683 drawViewerFeedback();
684
685 // now restore state
686 restoreGLStateAfterFeedback();
687 }
688 }
689
690 ////////////////////////////////////////////////////////////////////////
691 //
692 // Description:
693 // Draws the viewer feedback once the projection and state are set.
694 //
695 // Use: private
696 void
697 SoXtFlyViewer::drawViewerFeedback()
698 //
699 ////////////////////////////////////////////////////////////////////////
700 {
701 //
702 // draw the current action text
703 //
704 SbVec2s raSize = getGlxSize();
705 short tx = short(raSize[0] * TEXT_DX);
706 short ty = short(raSize[1] * TEXT_DY);
707 glRasterPos2s(tx, ty);
708 TEXT_COLOR;
709 // ???alain
710 #if 0
711 char str[10];
712 switch(mode) {
713 case STILL_MODE: charstr("Still"); break;
714 case TILT_MODE: charstr("Tilting"); break;
715 case SEEK_MODE:
716 charstr("Seeking");
717 break;
718 case SET_UP_MODE: charstr("Set Up Direction"); break;
719 case FLY_MODE:
720 charstr("Flying ");
721 sprintf(str, "%f", speed);
722 charstr(str);
723 break;
724 }
725 #endif
726
727 //
728 // draw the current speed feedback stuff
729 //
730 short x0 = short(raSize[0] * DECOR_DX);
731 short x1 = raSize[0] / 2;
732 short x2 = raSize[0] - x0;
733 short y = ty + 30;
734 glLineWidth(1);
735 DARK_COLOR;
736 glBegin(GL_LINES);
737 glVertex2s(x0, y); glVertex2s(x2, y);
738 glVertex2s(x0, y-5); glVertex2s(x0, y+5);
739 glVertex2s(x1, y-5); glVertex2s(x1, y+5);
740 glVertex2s(x2, y-5); glVertex2s(x2, y+5);
741 glEnd();
742 #if 0
743 cmov2s(x0-15, y-5); charstr("-");
744 cmov2s(x2+10, y-5); charstr("+");
745 #endif
746 if (mode == FLY_MODE) {
747 // draw the maximum speed bar
748 short l = short((x2-x1) * maxStraightSpeed / speedLimit);
749 MAX_SPEED_COL;
750 glLineWidth(5);
751 glBegin(GL_LINES);
752 glVertex2s(x1, y); glVertex2s(x1+l, y);
753 glEnd();
754
755 // draw the current speed bar
756 l = short((x2-x1) * speed / speedLimit);
757 CUR_SPEED_COL;
758 glLineWidth(3);
759 glBegin(GL_LINES);
760 glVertex2s(x1, y); glVertex2s(x1+l, y);
761 glEnd();
762 }
763
764 //
765 // draw mode specific stuff
766 //
767 glLineWidth(1);
768 if (mode == TILT_MODE) {
769 // draw cross at starting point
770 DARK_COLOR;
771 glBegin(GL_LINES);
772 glVertex2s(startPos[0] - CROSS, startPos[1]);
773 glVertex2s(startPos[0] + CROSS, startPos[1]);
774 glVertex2s(startPos[0], startPos[1] - CROSS);
775 glVertex2s(startPos[0], startPos[1] + CROSS);
776 glEnd();
777 }
778 else {
779 #if 0
780 static GLUquadricObj *quad = NULL;
781 if (! quad) quad = gluNewQuadric();
782
783 // draw small circle at screen center
784 DARK_COLOR;
785 circi(raSize[0]/2, raSize[1]/2, CIRCLE_RAD);
786 #endif
787 }
788
789 //
790 // finally draw the small directional axis
791 //
792 #if 0
793 #define DRAW_AXIS(color, x, y, z) \
794 cpack(color); \
795 axis.setValue(x, y, z); \
796 bgnline(); \
797 v3f(center.getValue()); \
798 v3f(axis.getValue()); \
799 endline();
800
801 SbVec3f axis, center(0, 0, 0);
802 SbVec2s pos(raSize[0]/2, raSize[1]/3);
803 short min = (raSize[0] < raSize[1]) ? raSize[0] : raSize[1];
804 short size = short(min * 0.08);
805 SbMatrix mx;
806 mx = camera->orientation.getValue().inverse();
807 SbVec3f bboxDir(sceneBbox.getCenter() - camera->position.getValue());
808 bboxDir.normalize();
809 SbVec3f viewVector(-mx[2][0], -mx[2][1], -mx[2][2]);
810
811 ortho(-1, 1, -1, 1, -1, 1);
812 viewport(pos[0]-size, pos[0]+size, pos[1]-size, pos[1]+size);
813 zbuffer(TRUE);
814 zclear();
815 pushmatrix();
816 multmatrix((Matrix) (float *) mx);
817 DRAW_AXIS(0x6666ff, 1, 0, 0)
818 DRAW_AXIS(0x66ff66, 0, 1, 0)
819 DRAW_AXIS(0xff6666, 0, 0, 1)
820 (viewVector.dot(bboxDir) < 0) ? cpack(0x66ffff) : cpack(0xff66ff);
821 bgnline(); v3f(center.getValue()); v3f(bboxDir.getValue()); endline();
822 popmatrix();
823 zbuffer(FALSE);
824 #undef DRAW_AXIS
825 #endif
826 }
827
828 ////////////////////////////////////////////////////////////////////////
829 //
830 // Description:
831 // This routine is used to define cursors (can only be called after
832 // window has been realized).
833 //
834 // Use: private
835
836 void
837 SoXtFlyViewer::defineCursors()
838 //
839 ////////////////////////////////////////////////////////////////////////
840 {
841 XColor foreground;
842 Pixmap source;
843 Display *display = getDisplay();
844 Drawable d = DefaultRootWindow(display);
845
846 // set color
847 foreground.red = 65535;
848 foreground.green = foreground.blue = 0;
849
850 // viewing cursor
851 source = XCreateBitmapFromData(display, d,
852 so_xt_viewing_bits, so_xt_viewing_width, so_xt_viewing_height);
853 viewerCursor = XCreatePixmapCursor(display, source, source,
854 &foreground, &foreground, so_xt_viewing_x_hot, so_xt_viewing_y_hot);
855 XFreePixmap(display, source);
856
857 // seek cursor
858 source = XCreateBitmapFromData(display, d,
859 so_xt_target_bits, so_xt_target_width, so_xt_target_height);
860 seekCursor = XCreatePixmapCursor(display, source, source,
861 &foreground, &foreground, so_xt_target_x_hot, so_xt_target_y_hot);
862 XFreePixmap(display, source);
863
864 // up direction cursor
865 source = XCreateBitmapFromData(display, d,
866 so_xt_normal_vec_bits, so_xt_normal_vec_width, so_xt_normal_vec_height);
867 upCursor = XCreatePixmapCursor(display, source, source,
868 &foreground, &foreground, so_xt_normal_vec_x_hot, so_xt_normal_vec_y_hot);
869 XFreePixmap(display, source);
870
871 createdCursors = TRUE;
872 }
873
874 ////////////////////////////////////////////////////////////////////////
875 //
876 // Description:
877 // Called by the animation sensor to change the camera position
878 //
879 // Use: private
880
881 void
882 SoXtFlyViewer::doCameraAnimation()
883 //
884 ////////////////////////////////////////////////////////////////////////
885 {
886 if (camera == NULL)
887 return;
888
889 //
890 // get time interval since last call
891 //
892 SbTime time = viewerRealTime->getValue();
893 float sec = float((time - prevAnimTime).getValue());
894 prevAnimTime = time;
895
896 // make sure to have at least a delta time for the first call.
897 if (sec == 0.0)
898 sec = 1.0/72.0;
899
900 //
901 // turn the camera left/right using the distance^2 which gives a nice
902 // gradual speedup.
903 //
904 SbVec2s raSize = getGlxSize();
905 float dist = (locator[0] - raSize[0]/2) / float(raSize[0]);
906 float angle = TURN_SPEED * (dist * dist) * sec;
907 if (angle != 0.0) {
908 if (dist < 0)
909 angle = -angle;
910 SbRotation rot(upDirection, -angle);
911 camera->orientation = camera->orientation.getValue() * rot;
912 }
913
914 //
915 // tilt camera up/down using the distance^2 which gives a nice
916 // gradual speedup.
917 //
918 dist = (locator[1] - raSize[1]/2) / float(raSize[1]);
919 angle = TURN_SPEED * (dist * dist) * sec;
920 if (dist < 0)
921 angle = -angle;
922 if (angle != 0.0)
923 tiltCamera(angle);
924
925 //
926 // move the camera forward
927 //
928 float dollyDist = speed * sec;
929 if (dollyDist > 0.0) {
930 // get camera forward direction
931 SbMatrix mx;
932 mx = camera->orientation.getValue();
933 SbVec3f forward(-mx[2][0], -mx[2][1], -mx[2][2]);
934
935 // now move camera foward by distance
936 camera->position = camera->position.getValue() + forward * dollyDist;
937 }
938
939 //
940 // increase the current speed if we havn't reach max speed yet
941 //
942 if ((speed > 0 && speed < maxSpeed) || (speed < 0 && speed > maxSpeed)) {
943 speed *= powf(3.0, sec);
944
945 // clip the value to the maxSpeed
946 if ((speed > 0 && speed > maxSpeed) || (speed < 0 && speed < maxSpeed))
947 speed = maxSpeed;
948 }
949 }
950
951 ////////////////////////////////////////////////////////////////////////
952 //
953 // Description:
954 // Moves the camera forward/backward (called by thumb wheel).
955 //
956 // Use: virtual protected
957
958 void
959 SoXtFlyViewer::rightWheelMotion(float newVal)
960 //
961 ////////////////////////////////////////////////////////////////////////
962 {
963 float dist = (newVal - rightWheelVal) * sceneSize * viewerSpeed
964 * WHEEL_DOLLY_FACTOR;
965
966 // get camera forward direction
967 SbMatrix mx;
968 mx = camera->orientation.getValue();
969 SbVec3f forward(-mx[2][0], -mx[2][1], -mx[2][2]);
970
971 // now move camera foward by distance
972 camera->position = camera->position.getValue() + forward * dist;
973 camera->focalDistance = camera->focalDistance.getValue() - dist;
974
975 rightWheelVal = newVal;
976 }
977
978
979 ////////////////////////////////////////////////////////////////////////
980 //
981 // Description:
982 // Increases/decreases the maximum straight speed
983 //
984 // Use: private
985 void
986 SoXtFlyViewer::changeMaxStraightSpeed(SbBool increase)
987 //
988 ////////////////////////////////////////////////////////////////////////
989 {
990 // check if we are just starting
991 if (maxStraightSpeed == 0)
992 maxStraightSpeed = increase ? MIN_SPEED * speedLimit : - MIN_SPEED * speedLimit;
993 else {
994
995 // find the new maxStraightSpeed
996 if ((maxStraightSpeed > 0 && increase) ||
997 (maxStraightSpeed < 0 && !increase))
998 maxStraightSpeed *= MAX_INC;
999 else
1000 maxStraightSpeed /= MAX_INC;
1001
1002 // clip to speedLimit boundaries
1003 if (maxStraightSpeed > speedLimit)
1004 maxStraightSpeed = speedLimit;
1005 else if (maxStraightSpeed < -speedLimit)
1006 maxStraightSpeed = -speedLimit;
1007
1008 // check if we are less than the minimum speed, in which
1009 // case we just stop flying
1010 float min = MIN_SPEED * speedLimit;
1011 if (maxStraightSpeed > -min && maxStraightSpeed < min) {
1012 switchMode(STILL_MODE);
1013 return;
1014 }
1015 }
1016
1017 // given the new maxStraightSpeed, calculate the maxSpeed
1018 calculateMaxSpeed();
1019 }
1020
1021 ////////////////////////////////////////////////////////////////////////
1022 //
1023 // Description:
1024 // Calculates the maximum speed based on the mouse location relative
1025 // to the screen center and the maximum forward speed.
1026 //
1027 // Use: private
1028 void
1029 SoXtFlyViewer::calculateMaxSpeed()
1030 //
1031 ////////////////////////////////////////////////////////////////////////
1032 {
1033 // get the x and y normalized distances from screen center
1034 SbVec2s raSize = getGlxSize();
1035 float dx = 2.0 * (locator[0] - raSize[0]/2) / float(raSize[0]);
1036 if (dx < 0.0) dx = -dx;
1037 if (dx > 1.0) dx = 1.0;
1038 float dy = 2.0 * (locator[1] - raSize[1]/2) / float(raSize[1]);
1039 if (dy < 0.0) dy = -dy;
1040 if (dy > 1.0) dy = 1.0;
1041
1042 // assign new maxSpeed and clip the current speed if needed
1043 maxSpeed = (dx > dy) ? (1.0 - dx) * maxStraightSpeed :
1044 (1.0 - dy) * maxStraightSpeed;
1045 if ((speed > 0 && speed > maxSpeed) || (speed < 0 && speed < maxSpeed))
1046 speed = maxSpeed;
1047 }
1048
1049 ////////////////////////////////////////////////////////////////////////
1050 //
1051 // Description:
1052 // Creates the viewer extra pref sheet stuff
1053 //
1054 // Use: private
1055 Widget
1056 SoXtFlyViewer::createFlyPrefSheetGuts(Widget parent)
1057 //
1058 ////////////////////////////////////////////////////////////////////////
1059 {
1060 Widget widgetList[3];
1061 Arg args[12];
1062 int n;
1063
1064 // create a form to hold everything together
1065 Widget form = XtCreateWidget("", xmFormWidgetClass,
1066 parent, NULL, 0);
1067
1068 // create all the parts
1069 widgetList[0] = XtCreateWidget( rl.flyingSpeed,
1070 xmLabelGadgetClass, form, NULL, 0);
1071
1072 n = 0;
1073 XtSetArg(args[n], XmNhighlightThickness, 0); n++;
1074 widgetList[1] = XtCreateWidget( rl.increase, xmPushButtonGadgetClass,
1075 form, args, n);
1076 widgetList[2] = XtCreateWidget( rl.decrease, xmPushButtonGadgetClass,
1077 form, args, n);
1078 XtAddCallback(widgetList[1], XmNactivateCallback,
1079 (XtCallbackProc) SoXtFlyViewer::incPrefSheetButtonCB,
1080 (XtPointer) this);
1081 XtAddCallback(widgetList[2], XmNactivateCallback,
1082 (XtCallbackProc) SoXtFlyViewer::decPrefSheetButtonCB,
1083 (XtPointer) this);
1084
1085 // layout
1086 n = 0;
1087 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
1088 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
1089 XtSetValues(widgetList[0], args, n);
1090
1091 n = 0;
1092 XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
1093 XtSetArg(args[1], XmNleftWidget, widgetList[0]); n++;
1094 XtSetArg(args[n], XmNleftOffset, 10); n++;
1095 XtSetValues(widgetList[1], args, n);
1096 XtSetArg(args[1], XmNleftWidget, widgetList[1]);
1097 XtSetValues(widgetList[2], args, n);
1098
1099 XtManageChildren(widgetList, 3);
1100
1101 return form;
1102 }
1103
1104 //
1105 // redefine those generic virtual functions
1106 //
1107 const char *
1108 SoXtFlyViewer::getDefaultWidgetName() const
1109 { return "SoXtFlyViewer"; }
1110
1111 const char *
1112 SoXtFlyViewer::getDefaultTitle() const
1113 { return rl.flyViewer; }
1114
1115 const char *
1116 SoXtFlyViewer::getDefaultIconTitle() const
1117 { return rl.flyViewer; }
1118
1119
1120
1121
1122 //
1123 ////////////////////////////////////////////////////////////////////////
1124 // static callbacks stubs
1125 ////////////////////////////////////////////////////////////////////////
1126 //
1127
1128 void
1129 SoXtFlyViewer::animationSensorCB(void *v, SoSensor *)
1130 { ((SoXtFlyViewer *) v)->doCameraAnimation(); }
1131
1132 void
1133 SoXtFlyViewer::incPrefSheetButtonCB(Widget, SoXtFlyViewer *v, void *)
1134 {
1135 v->speedLimitFactor *= 2.0;
1136 v->speedLimit *= 2.0;
1137 v->maxStraightSpeed *= 2.0;
1138 }
1139
1140 void
1141 SoXtFlyViewer::decPrefSheetButtonCB(Widget, SoXtFlyViewer *v, void *)
1142 {
1143 v->speedLimitFactor /= 2.0;
1144 v->speedLimit /= 2.0;
1145 v->maxStraightSpeed /= 2.0;
1146 }