| 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 |
_______________________________________________________________________ |
| 39 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 40 |
| |
| 41 |
| $Revision: 1.1.1.1 $ |
| 42 |
| |
| 43 |
| Classes : TsViewer |
| 44 |
| |
| 45 |
| Author(s) : Dave Immel |
| 46 |
| |
| 47 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 48 |
_______________________________________________________________________ |
| 49 |
*/ |
| 50 |
|
| 51 |
#include <cmath> |
| 52 |
|
| 53 |
#include <X11/Intrinsic.h> |
| 54 |
#include <X11/Xlib.h> |
| 55 |
#include <X11/keysym.h> |
| 56 |
|
| 57 |
#include <Inventor/nodes/SoOrthographicCamera.h> |
| 58 |
#include <Inventor/nodes/SoPerspectiveCamera.h> |
| 59 |
#include <Inventor/Xt/SoXtCursors.h> |
| 60 |
#include "TsViewer.h" |
| 61 |
|
| 62 |
|
| 63 |
enum ViewerModes { |
| 64 |
IDLE_MODE, |
| 65 |
TRANS_MODE, |
| 66 |
SEEK_MODE, |
| 67 |
ROT_MODE, |
| 68 |
}; |
| 69 |
|
| 70 |
#define TS_XROT_FACTOR 3.14 |
| 71 |
#define TS_YROT_FACTOR 3.14 |
| 72 |
#define TS_YTRANS_FACTOR 16.0 |
| 73 |
|
| 74 |
//////////////////////////////////////////////////////////////////////// |
| 75 |
// |
| 76 |
// Public constructor - build the widget right now |
| 77 |
// |
| 78 |
TsViewer::TsViewer( |
| 79 |
Widget parent, |
| 80 |
const char *name) |
| 81 |
: SoXtViewer( |
| 82 |
parent, |
| 83 |
name, |
| 84 |
TRUE, |
| 85 |
SoXtViewer::BROWSER, |
| 86 |
TRUE) |
| 87 |
// |
| 88 |
//////////////////////////////////////////////////////////////////////// |
| 89 |
{ |
| 90 |
// init local vars |
| 91 |
mode = IDLE_MODE; |
| 92 |
createdCursors = FALSE; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
//////////////////////////////////////////////////////////////////////// |
| 97 |
// |
| 98 |
// Destructor |
| 99 |
// |
| 100 |
TsViewer::~TsViewer() |
| 101 |
// |
| 102 |
//////////////////////////////////////////////////////////////////////// |
| 103 |
{} |
| 104 |
|
| 105 |
//////////////////////////////////////////////////////////////////////// |
| 106 |
// |
| 107 |
// Description: |
| 108 |
// Process the given event to change the camera |
| 109 |
// |
| 110 |
// Use: protected |
| 111 |
|
| 112 |
void |
| 113 |
TsViewer::processEvent(XAnyEvent *xe) |
| 114 |
|
| 115 |
// |
| 116 |
//////////////////////////////////////////////////////////////////////// |
| 117 |
{ |
| 118 |
// check if cursors need to be defined (they can only |
| 119 |
// be defined after the window has been mapped. |
| 120 |
// Recieving events guarantees that the window has |
| 121 |
// been mapped. |
| 122 |
if (!createdCursors) { |
| 123 |
defineCursors(); |
| 124 |
Widget w = SoXtRenderArea::getWidget(); |
| 125 |
XDefineCursor(XtDisplay(w), XtWindow(w), vwrCursor); |
| 126 |
} |
| 127 |
|
| 128 |
XButtonEvent *be; |
| 129 |
XMotionEvent *me; |
| 130 |
|
| 131 |
switch(xe->type) { |
| 132 |
case KeyPress: |
| 133 |
KeySym key; |
| 134 |
key = XLookupKeysym((XKeyEvent *)xe, 0); |
| 135 |
switch ( key ) { |
| 136 |
case XK_Home: |
| 137 |
resetToHomePosition(); |
| 138 |
return; |
| 139 |
} |
| 140 |
break; |
| 141 |
case ButtonPress: |
| 142 |
be = (XButtonEvent *)xe; |
| 143 |
locator[0] = windowSize[0] - be->x; |
| 144 |
locator[1] = windowSize[1] - be->y; |
| 145 |
if (be->button == Button1) { |
| 146 |
switch (mode) { |
| 147 |
case IDLE_MODE: |
| 148 |
interactiveCountInc(); |
| 149 |
break; |
| 150 |
} |
| 151 |
switchMode(ROT_MODE); |
| 152 |
} |
| 153 |
else if (be->button == Button2) { |
| 154 |
switch (mode) { |
| 155 |
case IDLE_MODE: |
| 156 |
interactiveCountInc(); |
| 157 |
break; |
| 158 |
} |
| 159 |
switchMode(TRANS_MODE); |
| 160 |
} |
| 161 |
return; |
| 162 |
|
| 163 |
case ButtonRelease: |
| 164 |
be = (XButtonEvent *)xe; |
| 165 |
if (be->button == Button1) { |
| 166 |
if (mode == ROT_MODE) |
| 167 |
{ |
| 168 |
switchMode(IDLE_MODE); |
| 169 |
interactiveCountDec(); |
| 170 |
} |
| 171 |
else |
| 172 |
switchMode(TRANS_MODE); |
| 173 |
} |
| 174 |
else if (be->button == Button2) { |
| 175 |
if (mode == TRANS_MODE) |
| 176 |
{ |
| 177 |
switchMode(IDLE_MODE); |
| 178 |
interactiveCountDec(); |
| 179 |
} |
| 180 |
else |
| 181 |
switchMode(ROT_MODE); |
| 182 |
} |
| 183 |
return; |
| 184 |
|
| 185 |
case MotionNotify: |
| 186 |
me = (XMotionEvent *)xe; |
| 187 |
movement[0] = locator[0]; |
| 188 |
movement[1] = locator[1]; |
| 189 |
locator[0] = windowSize[0] - me->x; |
| 190 |
locator[1] = windowSize[1] - me->y; |
| 191 |
if (mode == TRANS_MODE) |
| 192 |
translateCamera(); |
| 193 |
else if (mode == ROT_MODE) |
| 194 |
{ |
| 195 |
rotateCamera(); |
| 196 |
} |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
// Pass all other events to the render area so they will be given |
| 201 |
// to the scene graph. |
| 202 |
SoXtRenderArea::processEvent(xe); |
| 203 |
} |
| 204 |
|
| 205 |
//////////////////////////////////////////////////////////////////////// |
| 206 |
// |
| 207 |
// Description: |
| 208 |
// Switches to the specified viewer mode. The correct |
| 209 |
// cursor is also set on the window. |
| 210 |
// |
| 211 |
// Use: private |
| 212 |
|
| 213 |
void |
| 214 |
TsViewer::switchMode(int newMode) |
| 215 |
|
| 216 |
// |
| 217 |
//////////////////////////////////////////////////////////////////////// |
| 218 |
{ |
| 219 |
// needed to define new cursors |
| 220 |
Widget w = SoXtRenderArea::getWidget(); |
| 221 |
Display *display = (w != NULL) ? XtDisplay(w) : NULL; |
| 222 |
Window window = (w != NULL) ? XtWindow(w) : 0; |
| 223 |
if (!createdCursors && window != 0) |
| 224 |
defineCursors(); |
| 225 |
|
| 226 |
// switch to new viewer mode |
| 227 |
mode = newMode; |
| 228 |
switch (mode) { |
| 229 |
case IDLE_MODE: |
| 230 |
if (window != 0) |
| 231 |
XDefineCursor(display, window, vwrCursor); |
| 232 |
break; |
| 233 |
|
| 234 |
case TRANS_MODE: |
| 235 |
{ |
| 236 |
// Figure out the focal plane |
| 237 |
SbMatrix mx; |
| 238 |
mx = camera->orientation.getValue(); |
| 239 |
SbVec3f forward(-mx[2][0], -mx[2][1], -mx[2][2]); |
| 240 |
SbVec3f fp = camera->position.getValue() + |
| 241 |
forward * camera->focalDistance.getValue(); |
| 242 |
focalplane = SbPlane(forward, fp); |
| 243 |
|
| 244 |
// map mouse position onto the viewing plane |
| 245 |
SbViewVolume cameraVolume; |
| 246 |
SbLine line; |
| 247 |
cameraVolume = camera->getViewVolume(); |
| 248 |
cameraVolume.projectPointToLine( |
| 249 |
SbVec2f( locator[0] / float(windowSize[0]), |
| 250 |
locator[1] / float(windowSize[1])), line); |
| 251 |
focalplane.intersect(line, locator3D); |
| 252 |
} |
| 253 |
if (window != 0) |
| 254 |
XDefineCursor(display, window, vwrCursor); |
| 255 |
break; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
//////////////////////////////////////////////////////////////////////// |
| 260 |
// |
| 261 |
// Description: |
| 262 |
// Called by the base class (SoXtGLWidget) when the |
| 263 |
// render Area widget changes size. Redefine it here to |
| 264 |
// cache the RA window size. |
| 265 |
// |
| 266 |
// Use: protected |
| 267 |
|
| 268 |
void |
| 269 |
TsViewer::sizeChanged(const SbVec2s &newSize) |
| 270 |
|
| 271 |
// |
| 272 |
//////////////////////////////////////////////////////////////////////// |
| 273 |
{ |
| 274 |
// cache the renderArea size |
| 275 |
windowSize = newSize; |
| 276 |
|
| 277 |
// call parent class |
| 278 |
SoXtViewer::sizeChanged(newSize); |
| 279 |
} |
| 280 |
|
| 281 |
//////////////////////////////////////////////////////////////////////// |
| 282 |
// |
| 283 |
// Description: |
| 284 |
// This routine is used to define cursors, which can |
| 285 |
// only be called after the window has been realized. |
| 286 |
// |
| 287 |
// Use: private |
| 288 |
|
| 289 |
void |
| 290 |
TsViewer::defineCursors() |
| 291 |
|
| 292 |
// |
| 293 |
//////////////////////////////////////////////////////////////////////// |
| 294 |
{ |
| 295 |
XColor foreground; |
| 296 |
Pixmap source; |
| 297 |
Display *display = getDisplay(); |
| 298 |
Drawable d = DefaultRootWindow(display); |
| 299 |
|
| 300 |
// set a red color |
| 301 |
foreground.red = 65535; |
| 302 |
foreground.green = foreground.blue = 0; |
| 303 |
|
| 304 |
// cursor |
| 305 |
source = XCreateBitmapFromData(display, d, |
| 306 |
so_xt_curved_hand_bits, |
| 307 |
so_xt_curved_hand_width, |
| 308 |
so_xt_curved_hand_height); |
| 309 |
vwrCursor = XCreatePixmapCursor(display, source, source, |
| 310 |
&foreground, &foreground, |
| 311 |
so_xt_curved_hand_x_hot, |
| 312 |
so_xt_curved_hand_y_hot); |
| 313 |
XFreePixmap(display, source); |
| 314 |
|
| 315 |
createdCursors = TRUE; |
| 316 |
} |
| 317 |
|
| 318 |
//////////////////////////////////////////////////////////////////////// |
| 319 |
// |
| 320 |
// Description: |
| 321 |
// Moves the camera along the vertical Y axis as the user moves |
| 322 |
// the mouse up and down. |
| 323 |
// |
| 324 |
// Use: private |
| 325 |
|
| 326 |
void |
| 327 |
TsViewer::translateCamera() |
| 328 |
|
| 329 |
// |
| 330 |
//////////////////////////////////////////////////////////////////////// |
| 331 |
{ |
| 332 |
if (camera == NULL) |
| 333 |
return; |
| 334 |
|
| 335 |
SbVec2f delta2D( (movement[0]-locator[0]) / float(windowSize[0]), |
| 336 |
(movement[1]-locator[1]) / float(windowSize[1]) ); |
| 337 |
|
| 338 |
// map vertical mouse motion into a Y translation in world space. |
| 339 |
SbVec3f delta3D; |
| 340 |
delta3D[0] = 0.0; |
| 341 |
delta3D[1] = (delta2D[0] + delta2D[1]) * TS_YTRANS_FACTOR; |
| 342 |
delta3D[2] = 0.0; |
| 343 |
|
| 344 |
camera->position = camera->position.getValue() + delta3D; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
//////////////////////////////////////////////////////////////////////// |
| 349 |
// |
| 350 |
// Description: |
| 351 |
// Revolves the camera around its focal point |
| 352 |
// to follow the new mouse location. Rotations are about |
| 353 |
// the X and Y axes only. |
| 354 |
// |
| 355 |
// Use: private |
| 356 |
|
| 357 |
void |
| 358 |
TsViewer::rotateCamera() |
| 359 |
// |
| 360 |
//////////////////////////////////////////////////////////////////////// |
| 361 |
{ |
| 362 |
if (camera == NULL) |
| 363 |
return; |
| 364 |
|
| 365 |
SbVec2f newLocator( (movement[0]-locator[0]) / float(windowSize[0]), |
| 366 |
(movement[1]-locator[1]) / float(windowSize[1]) ); |
| 367 |
|
| 368 |
// figure out the new rotation. |
| 369 |
SbRotation xRot( SbVec3f(-1.0, 0.0, 0.0), newLocator[1] * TS_XROT_FACTOR ); |
| 370 |
SbRotation yRot( SbVec3f(0.0, -1.0, 0.0), newLocator[0] * TS_YROT_FACTOR ); |
| 371 |
SbRotation rot; |
| 372 |
|
| 373 |
// get center of rotation |
| 374 |
SbRotation camRot = camera->orientation.getValue(); |
| 375 |
float radius = camera->focalDistance.getValue(); |
| 376 |
SbMatrix mx; |
| 377 |
mx = camRot; |
| 378 |
SbVec3f forward( -mx[2][0], -mx[2][1], -mx[2][2]); |
| 379 |
SbVec3f center = camera->position.getValue() |
| 380 |
+ radius * forward; |
| 381 |
|
| 382 |
// apply new rotation to the camera |
| 383 |
camRot = xRot * camRot * yRot; |
| 384 |
camera->orientation = camRot; |
| 385 |
|
| 386 |
// reposition camera to look at pt of interest |
| 387 |
mx = camRot; |
| 388 |
forward.setValue( -mx[2][0], -mx[2][1], -mx[2][2]); |
| 389 |
camera->position = center - radius * forward; |
| 390 |
|
| 391 |
movement[0] = locator[0]; |
| 392 |
movement[1] = locator[1]; |
| 393 |
} |