1 /***********************************************************************/ 2 /* Open Visualization Data Explorer */ 3 /* (C) Copyright IBM Corp. 1989,1999 */ 4 /* ALL RIGHTS RESERVED */ 5 /* This code licensed under the */ 6 /* "IBM PUBLIC LICENSE - Open Visualization Data Explorer" */ 7 /***********************************************************************/ 8 9 #include <dxconfig.h> 10 11 12 #ifndef tdmInteractor_h 13 #define tdmInteractor_h 14 /*---------------------------------------------------------------------------*\ 15 $Source: /src/master/dx/src/exec/hwrender/hwInteractor.h,v $ 16 Author: Mark Hood 17 18 This include file defines core data structures used by the direct 19 interactor implementations. 20 21 \*---------------------------------------------------------------------------*/ 22 23 #include <stdio.h> 24 #include <math.h> 25 26 /* SGI */ 27 #if defined(sgi) 28 #ifndef Int32 29 #define Int32 long 30 #endif 31 #ifndef Int16 32 #define Int16 short 33 #endif 34 #endif 35 36 #ifdef STANDALONE 37 #define tdmAllocateLocal malloc 38 #define tdmFree free 39 #endif 40 41 /* 42 * The following must match UserInteractors.h 43 */ 44 #define DX_LEFT_BUTTON_MASK 0x01 45 #define DX_MIDDLE_BUTTON_MASK 0x02 46 #define DX_RIGHT_BUTTON_MASK 0x04 47 48 #define INTERACTOR_BUTTON_UP 1 49 #define INTERACTOR_BUTTON_DOWN 2 50 51 52 typedef struct tdmInteractorS tdmInteractorT, *tdmInteractor ; 53 typedef struct tdmInteractorWinS tdmInteractorWinT, *tdmInteractorWin ; 54 typedef struct tdmInteractorCamS tdmInteractorCamT, *tdmInteractorCam ; 55 typedef void (*tdmViewEchoFunc) (tdmInteractor, void *, float[4][4], int) ; 56 typedef struct tdmInteractorEchoS tdmInteractorEchoT, *tdmInteractorEcho ; 57 58 typedef enum {tdmBackBufferDraw, tdmFrontBufferDraw, 59 tdmBothBufferDraw, tdmAuxEchoMode, tdmViewEchoMode} 60 tdmInteractorRedrawMode ; 61 62 typedef enum {tdmNullEcho, tdmGlobeEcho, tdmGnomonEcho} 63 tdmEchoType ; 64 65 typedef enum {tdmXYPlaneRoll, tdmZTwirl} 66 tdmRotateModel ; 67 68 typedef enum {tdmProbeCursor, tdmRoamCursor, tdmPickCursor} 69 tdmCursorType ; 70 71 /* 72 * Following constants must have the same values as their Picture widget 73 * counterparts (switch Xm and tdm prefixes). 74 */ 75 76 #define tdmCONSTRAIN_NONE 0 77 #define tdmCONSTRAIN_X 1 78 #define tdmCONSTRAIN_Y 2 79 #define tdmCONSTRAIN_Z 3 80 81 #define tdmSLOW_CURSOR 0 82 #define tdmMEDIUM_CURSOR 1 83 #define tdmFAST_CURSOR 2 84 85 #define tdmCURSOR_CREATE 0 86 #define tdmCURSOR_DELETE 1 87 #define tdmCURSOR_MOVE 2 88 #define tdmCURSOR_SELECT 3 89 #define tdmCURSOR_DRAG 4 90 #define tdmROTATION_UPDATE 5 91 92 #define tdmPCR_TRANSLATE_SPEED 5 93 #define tdmPCR_ROTATE_SPEED 6 94 95 #define tdmLOOK_LEFT 0 96 #define tdmLOOK_RIGHT 1 97 #define tdmLOOK_UP 2 98 #define tdmLOOK_DOWN 3 99 #define tdmLOOK_BACKWARD 4 100 #define tdmLOOK_FORWARD 5 101 #define tdmLOOK_ALIGN 6 102 103 /* 104 * Data structure returned by tdmEndStroke(). 105 */ 106 107 typedef struct tdmInteractorReturnS { 108 tdmInteractor I ; 109 int id ; 110 int reason ; 111 float x, y, z ; 112 float to[3] ; 113 float from[3] ; 114 float dist ; 115 float up[3] ; 116 float view[4][4] ; 117 float matrix[4][4] ; 118 float zoom_factor ; 119 int change ; 120 } tdmInteractorReturn, *tdmInteractorReturnP ; 121 122 /* 123 * Interactor definition. 124 */ 125 126 struct tdmInteractorS { 127 void (*DoubleClick) (tdmInteractor, int, int, tdmInteractorReturnP); 128 void (*StartStroke) (tdmInteractor, int, int, int, int) ; 129 void (*StrokePoint) (tdmInteractor, int, int, int, int) ; 130 void (*EndStroke) (tdmInteractor, tdmInteractorReturnP) ; 131 void (*KeyStruck) (tdmInteractor, int, int, char, int) ; 132 void (*ResumeEcho) (tdmInteractor, tdmInteractorRedrawMode) ; 133 void (*EchoFunc) (tdmInteractor, void *, float[4][4], int) ; 134 void (*Destroy) (tdmInteractor) ; 135 136 void (*Config)() ; /* parameters vary: beware type conversions */ 137 void (*restoreConfig)() ; /* parameters vary: beware type conversions */ 138 139 tdmInteractorWin cdata ; /* pointer to common data */ 140 void *pdata ; /* pointer to private data */ 141 void *udata ; /* pointer to user data */ 142 tdmInteractorEcho edata; /* pointer to port layer echo functions */ 143 144 /* following interactor references are used by the Group interactor */ 145 tdmInteractor btn1 ; 146 tdmInteractor btn2 ; 147 tdmInteractor btn3 ; 148 int is_group ; 149 150 /* list of associated interactor echos */ 151 tdmInteractor aux ; 152 int is_aux ; 153 154 /* allocation bookkeeping */ 155 int used ; 156 157 /* event mask */ 158 int eventMask; 159 } ; 160 161 /* 162 * Data shared among all of window's interactors. 163 */ 164 165 struct tdmInteractorCamS { 166 /* global box used for manipulating 3D cursors, supplied by application */ 167 float box[8][3] ; 168 /* global 3D cursor movement constraint */ 169 int cursor_constraint ; 170 /* global 3D cursor speed specification */ 171 int cursor_speed ; 172 173 /* view coordinate system definition, supplied by application */ 174 float from[3], to[3], up[3] ; 175 /* distance between from point and to point */ 176 float vdist ; 177 /* projection type, 1 if perspective, 0 if ortho */ 178 int projection ; 179 /* field of view, width */ 180 float fov, width ; 181 /* near, far clip planes */ 182 float Near, Far ; 183 184 /* has the view coordinate system been set? */ 185 int view_coords_set ; 186 /* has anybody changed view data? */ 187 int view_state ; 188 /* has anybody changed box data? */ 189 int box_state ; 190 191 /* width, height, depth, and center of window in screen coords */ 192 long d, w, h, cx, cy ; 193 /* lower left corner of window relative to screen and screen dimensions */ 194 int ox, oy, xmaxscreen, ymaxscreen ; 195 /* aspect ratio of image */ 196 float aspect ; 197 /* view matrix */ 198 float viewXfm[4][4] ; 199 /* projection matrix */ 200 float projXfm[4][4] ; 201 /* screen transform */ 202 float scrnXfm[4][4] ; 203 /* draw mode and Z buffer modes */ 204 int drawmode, zdraw, zbuffer ; 205 206 /* screen coordinates of last input */ 207 int xlast, ylast ; 208 209 /* copy of pointer to background image and image dimensions */ 210 void *image ; 211 int iw, ih ; 212 213 /* handle to graphics API context */ 214 /* NOTE: Do not access this through CDATA, use DEFPORT(I_PORT_HANDLE) and 215 * PORT_CTX. 216 */ 217 void *phP ; 218 219 /* handle to graphics API echo functions */ 220 void *echoFuncs; 221 222 /* handle to software transformation stack */ 223 void *stack ; 224 225 /* pointer to next camera in undo/redo stack */ 226 tdmInteractorCam next ; 227 } ; 228 229 struct tdmInteractorWinS { 230 /* camera stack and redo stack pointers */ 231 tdmInteractorCam Cam ; 232 tdmInteractorCam redo ; 233 234 /* an array of interactors */ 235 tdmInteractor *Interactors ; 236 int numUsed ; 237 int numAllocated ; 238 239 /* handle to interactor storage */ 240 void *extents ; 241 } ; 242 243 /* 244 * Interactor data is accessed through these macros. 245 */ 246 247 #define FUNC(interactor,f) ((interactor)->f) 248 #define CALLFUNC(interactor,f) (*FUNC(interactor,f)) 249 250 #define UDATA(interactor) ((interactor)->udata) 251 #define WINDOW(interactor) ((interactor)->cdata) 252 253 /* gcc gets upset on alpha at least with PRIVATE previously defined */ 254 #ifdef PRIVATE 255 #undef PRIVATE 256 #endif 257 #define PRIVATE(interactor) ((interactor)->pdata) 258 259 #define BTN1(interactor) ((interactor)->btn1) 260 #define BTN2(interactor) ((interactor)->btn2) 261 #define BTN3(interactor) ((interactor)->btn3) 262 #define IS_GROUP(interactor) ((interactor)->is_group) 263 #define IS_USED(interactor) ((interactor)->used) 264 #define AUX(interactor) ((interactor)->aux) 265 #define IS_AUX(interactor) ((interactor)->is_aux) 266 #define EVENT_MASK(interactor) ((interactor) ? (interactor)->eventMask : 0) 267 268 #define DEFDATA(interactor,ptype) \ 269 register tdmInteractorCam _cdata = ((interactor)->cdata->Cam) ; \ 270 register ptype *_pdata = (ptype *) ((interactor)->pdata) ; \ 271 double _w 272 273 #define PDATA(foo) (_pdata->foo) 274 #define CDATA(foo) (_cdata->foo) 275 #define I_PORT_HANDLE (_cdata->phP) 276 277 /* 278 * Misc. macros for interactor implementations. 279 */ 280 281 #define VCOPY(t,f) (bcopy ((char *)(f), (char *)(t), sizeof(float [3]))) 282 #define MCOPY(t,f) (bcopy ((char *)(f), (char *)(t), sizeof(float [4][4]))) 283 #define DMCOPY(t,f) (bcopy ((char *)(f), (char *)(t), sizeof(double [4][4]))) 284 #define CLIPPED(x,y) ((x)<0 || (y)<0 || (x)>(CDATA(iw)-1) || (y)>(CDATA(ih)-1)) 285 286 /* used to ensure pixel reads and writes are on physical screen */ 287 #define XSCREENCLIP(x) \ 288 (CDATA(ox)+x < 0 ? -CDATA(ox) : \ 289 (CDATA(ox)+x > CDATA(xmaxscreen) ? CDATA(xmaxscreen)-CDATA(ox) : x)) 290 #define YSCREENCLIP(y) \ 291 (CDATA(oy)+y < 0 ? -CDATA(oy) : \ 292 (CDATA(oy)+y > CDATA(ymaxscreen) ? CDATA(ymaxscreen)-CDATA(oy) : y)) 293 294 #define VSUB(r, a, b) {\ 295 r[0] = a[0] - b[0] ; \ 296 r[1] = a[1] - b[1] ; \ 297 r[2] = a[2] - b[2] ; } 298 299 #define CROSS(r, a, b) {\ 300 r[0] = a[1]*b[2] - a[2]*b[1] ; \ 301 r[1] = a[2]*b[0] - a[0]*b[2] ; \ 302 r[2] = a[0]*b[1] - a[1]*b[0] ; } 303 304 #define XFORM_VECTOR(M, in, out) {\ 305 out[0] = in[0]*M[0][0] + in[1]*M[1][0] + in[2]*M[2][0] ; \ 306 out[1] = in[0]*M[0][1] + in[1]*M[1][1] + in[2]*M[2][1] ; \ 307 out[2] = in[0]*M[0][2] + in[1]*M[1][2] + in[2]*M[2][2] ; } 308 309 #define XFORM_COORDS(M, x, y, z, nx, ny, nz) {\ 310 nx = x*M[0][0] + y*M[1][0] + z*M[2][0] + M[3][0] ; \ 311 ny = x*M[0][1] + y*M[1][1] + z*M[2][1] + M[3][1] ; \ 312 nz = x*M[0][2] + y*M[1][2] + z*M[2][2] + M[3][2] ; \ 313 if (CDATA(projection)) { \ 314 _w = x*M[0][3] + y*M[1][3] + z*M[2][3] + M[3][3] ; \ 315 if (_w) { \ 316 nx /= _w ; ny /= _w ; nz /= _w ;}}} 317 318 #define YFLIP(y) ((CDATA(h) - (y)) - 1) 319 320 321 #ifndef RAD2DEG 322 #define RAD2DEG(r) (180.0*(r)/M_PI) 323 #endif 324 #ifndef DEG2RAD 325 #define DEG2RAD(d) (M_PI*(d)/180.0) 326 #endif 327 328 #ifndef LENGTH 329 #define LENGTH(V) (sqrt((double)(V[0]*V[0] + V[1]*V[1] + V[2]*V[2]))) 330 #endif 331 332 #ifndef TRUE 333 #define TRUE 1 334 #endif 335 #ifndef FALSE 336 #define FALSE 0 337 #endif 338 339 #ifdef MJHDEBUG 340 #define DPRINT(str) {fprintf (stderr, str); fflush(stderr);} 341 #define DPRINT1(str,a) {fprintf (stderr, str, a); fflush(stderr);} 342 #define VPRINT(V) fprintf (stderr, "%12f %12f %12f", V[0], V[1], V[2]) 343 #define SPRINT(S) fprintf (stderr, "%12f %12f %12f", S.x, S.y, S.z) 344 #define CPRINT(C) fprintf (stderr, "%12f %12f %12f", C.r, C.g, C.b) ; 345 #define MPRINT(M) \ 346 fprintf \ 347 (stderr, \ 348 ": %17f %17f %17f %17f\n: %17f %17f %17f %17f\n: %17f %17f %17f %17f\n: %17f %17f %17f %17f", \ 349 M[0][0], M[0][1], M[0][2], M[0][3], \ 350 M[1][0], M[1][1], M[1][2], M[1][3], \ 351 M[2][0], M[2][1], M[2][2], M[2][3], \ 352 M[3][0], M[3][1], M[3][2], M[3][3]) ; 353 #else 354 #define DPRINT(str) 0 355 #define DPRINT1(str,a) 0 356 #define VPRINT(V) 0 357 #define SPRINT(S) 0 358 #define CPRINT(C) 0 359 #define MPRINT(M) 0 360 #endif 361 362 /* 363 * Following declarations used only by interactor implementations. 364 */ 365 366 tdmInteractor _dxfAllocateInteractor (tdmInteractorWin, int) ; 367 void _dxfDeallocateInteractor(tdmInteractor) ; 368 void _dxfNullDoubleClick (tdmInteractor, int, int, tdmInteractorReturn *) ; 369 void _dxfNullStartStroke (tdmInteractor, int, int, int, int) ; 370 void _dxfNullStrokePoint (tdmInteractor, int, int, int, int ) ; 371 void _dxfNullEndStroke (tdmInteractor, tdmInteractorReturn *) ; 372 void _dxfNullResumeEcho (tdmInteractor, tdmInteractorRedrawMode) ; 373 void _dxfNullKeyStruck(tdmInteractor, int, int, char, int); 374 375 /* 376 * Public interface. 377 */ 378 379 #define tdmDoubleClick(I,x,y,R) {if((I)) (*((I)->DoubleClick)) (I,x,y,R) ;} 380 #define tdmStartStroke(I,x,y,b,s) {if((I)) (*((I)->StartStroke)) (I,x,y,b,s) ;} 381 #define tdmStrokePoint(I,x,y,type,s) {if((I)) (*((I)->StrokePoint)) (I,x,y,type,s) ;} 382 #define tdmEndStroke(I,R) {if((I)) (*((I)->EndStroke)) (I,R) ;} 383 #define tdmKeyStruck(I,x,y,c,s) {if((I)) (*((I)->KeyStruck)) (I,x,y,c,s) ;} 384 #define tdmResumeEcho(I,m) {if((I)) (*((I)->ResumeEcho)) (I,m) ;} 385 #define tdmDestroyInteractor(I) {if((I)) (*((I)->Destroy)) (I) ;} 386 387 tdmInteractorWin _dxfInitInteractors (void *ctx, void *stack) ; 388 void _dxfRedrawInteractorEchos (tdmInteractorWin W) ; 389 void _dxfDestroyAllInteractors (tdmInteractorWin W) ; 390 void _dxfAssociateInteractorEcho (tdmInteractor T, tdmInteractor A) ; 391 void _dxfDisassociateInteractorEcho (tdmInteractorWin W, tdmInteractor A) ; 392 393 tdmInteractor _dxfCreateUserInteractor (tdmInteractorWin W, tdmViewEchoFunc E, void *udata) ; 394 tdmInteractor _dxfCreateZoomInteractor (tdmInteractorWin W) ; 395 tdmInteractor _dxfCreatePanZoomInteractor (tdmInteractorWin W) ; 396 tdmInteractor _dxfCreateCursorInteractor (tdmInteractorWin W, tdmCursorType C) ; 397 tdmInteractor _dxfCreateRotationInteractor 398 (tdmInteractorWin W, tdmEchoType E, tdmRotateModel M) ; 399 tdmInteractor _dxfCreateViewRotationInteractor 400 (tdmInteractorWin W, tdmViewEchoFunc E, tdmRotateModel M, void *udata) ; 401 tdmInteractor _dxfCreateNavigator 402 (tdmInteractorWin W, tdmViewEchoFunc func, void *data) ; 403 tdmInteractor _dxfCreateInteractorGroup 404 (tdmInteractorWin W, tdmInteractor btn1, tdmInteractor btn2, 405 tdmInteractor btn3) ; 406 407 int _dxfPushInteractorCamera (tdmInteractorWin W) ; 408 int _dxfPopInteractorCamera (tdmInteractorWin W) ; 409 int _dxfRedoInteractorCamera (tdmInteractorWin W) ; 410 411 void _dxfInteractorViewChanged (tdmInteractorWin W) ; 412 void _dxfSetInteractorBox (tdmInteractorWin W, float box[8][3]) ; 413 void _dxfSetInteractorWindowSize (tdmInteractorWin W, int width, int height) ; 414 void _dxfSetInteractorViewDepth (tdmInteractorWin W, int depth) ; 415 void _dxfSetInteractorImage (tdmInteractorWin W, int w, int h, void *image) ; 416 void _dxfSetInteractorViewInfo (tdmInteractorWin W, 417 float *f, float *t, float *u, float dist, 418 float fov, float width, float aspect, int proj, 419 float Near, float Far) ; 420 void _dxfGetInteractorViewInfo (tdmInteractorWin W, 421 float *f, float *t, float *u, float *dist, 422 float *fov, float *width, float *aspect, 423 int *projection, float *Near, float *Far, 424 int *pixwidth) ; 425 426 void _dxfSetCursorSpeed (tdmInteractorWin W, int speed) ; 427 void _dxfSetCursorConstraint (tdmInteractorWin W, int constraint) ; 428 void _dxfCursorInteractorChange (tdmInteractor I, int id, int reason, 429 float x, float y, float z) ; 430 431 void _dxfCursorInteractorVisible (tdmInteractor I) ; 432 void _dxfCursorInteractorInvisible (tdmInteractor I) ; 433 void _dxfRotateInteractorVisible (tdmInteractor I) ; 434 void _dxfRotateInteractorInvisible (tdmInteractor I) ; 435 void _dxfSetNavigateTranslateSpeed (tdmInteractor I, float speed) ; 436 void _dxfSetNavigateRotateSpeed (tdmInteractor I, float speed) ; 437 void _dxfSetNavigateLookAt (tdmInteractor I, int direction, float angle, 438 float current_view[3][3], 439 float viewDirReturn[3], float viewUpReturn[3]) ; 440 void _dxfUpdateInteractorGroup (tdmInteractor I, 441 tdmInteractor btn1, 442 tdmInteractor btn2, 443 tdmInteractor btn3) ; 444 445 void _dxfResetCursorInteractor (tdmInteractor I) ; 446 void _dxfResetNavigateInteractor (tdmInteractor I) ; 447 448 void _dxfSetUserInteractorMode(tdmInteractor I, dxObject args); 449 450 451 #endif /* tdmInteractor_h */ 452 453