| 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 |
// Description: |
| 40 |
// This is the source file for the SoSpotLightDragger. |
| 41 |
// This is a composite dragger which allows independent rotation, |
| 42 |
// translation, and beam spread editting of a spot light. |
| 43 |
// |
| 44 |
// It is composed of an SoRotateSphericalDragger (for rotation), |
| 45 |
// an SoDragPointDragger (for translation), and it creates its own projector |
| 46 |
// handles mouse events for doing it's own dragging of the beam angle. |
| 47 |
// |
| 48 |
// The beam is editted by behaving like an SoRotateDiscDragger, but the |
| 49 |
// plane of the disc is re-defined every time a drag is initiated. |
| 50 |
// The plane always passes through the z axis and the selected point. |
| 51 |
// When the rotation angle is determined, however, the beam is not rotated, |
| 52 |
// but scaled so it looks like an opening or closing umbrella. This is done |
| 53 |
// by scaling evenly in x and y, and a different amount in z, so the distance |
| 54 |
// between the selected point and the origin remains constant. |
| 55 |
// |
| 56 |
//////////////////////////////////////////////////////////////////////// |
| 57 |
|
| 58 |
|
| 59 |
#include <cstdio> |
| 60 |
#include <cmath> |
| 61 |
#include <Inventor/SbLinear.h> |
| 62 |
#include <Inventor/SoDB.h> |
| 63 |
#include <Inventor/SoPath.h> |
| 64 |
#include <Inventor/projectors/SbPlaneProjector.h> |
| 65 |
#include <Inventor/projectors/SbSphereSectionProjector.h> |
| 66 |
#include <Inventor/sensors/SoFieldSensor.h> |
| 67 |
|
| 68 |
#include <Inventor/nodes/SoMaterial.h> |
| 69 |
#include <Inventor/nodes/SoRotation.h> |
| 70 |
#include <Inventor/nodes/SoScale.h> |
| 71 |
#include <Inventor/nodes/SoSeparator.h> |
| 72 |
#include <Inventor/nodes/SoTransform.h> |
| 73 |
#include <Inventor/nodes/SoTranslation.h> |
| 74 |
#include <Inventor/nodes/SoSpotLight.h> |
| 75 |
#include <Inventor/nodes/SoSwitch.h> |
| 76 |
#include <Inventor/draggers/SoDragPointDragger.h> |
| 77 |
#include <Inventor/draggers/SoRotateSphericalDragger.h> |
| 78 |
#include <Inventor/draggers/SoSpotLightDragger.h> |
| 79 |
|
| 80 |
#include "geom/SoSpotLightDraggerGeom.h" |
| 81 |
|
| 82 |
|
| 83 |
SO_KIT_SOURCE(SoSpotLightDragger); |
| 84 |
|
| 85 |
//////////////////////////////////////////////////////////////////////// |
| 86 |
// |
| 87 |
// Description: |
| 88 |
// Constructors. Each constructor calls constructorSub(), which |
| 89 |
// does work that is common to each. |
| 90 |
// |
| 91 |
// Use: public |
| 92 |
// |
| 93 |
//////////////////////////////////////////////////////////////////////// |
| 94 |
|
| 95 |
// Default constructor. |
| 96 |
SoSpotLightDragger::SoSpotLightDragger() |
| 97 |
{ |
| 98 |
SO_KIT_CONSTRUCTOR(SoSpotLightDragger); |
| 99 |
|
| 100 |
isBuiltIn = TRUE; |
| 101 |
|
| 102 |
// This gives the dragger an overall material. It is edited by lightManips |
| 103 |
// to make its dragger match the color of the light. Any materials within |
| 104 |
// other parts will override this one. |
| 105 |
SO_KIT_ADD_CATALOG_ENTRY(material, SoMaterial, |
| 106 |
TRUE, topSeparator, geomSeparator,TRUE); |
| 107 |
|
| 108 |
// The translator is kept under a separator along with a |
| 109 |
// rotation that is maintained as the inverse to the rotation of the |
| 110 |
// light. This means that using the rotator does not rotate the |
| 111 |
// coordinate system that we translate the base of the dragger in. |
| 112 |
SO_KIT_ADD_CATALOG_ENTRY(translatorSep, SoSeparator, |
| 113 |
TRUE, topSeparator, geomSeparator,FALSE); |
| 114 |
SO_KIT_ADD_CATALOG_ENTRY(translatorRotInv, SoRotation, |
| 115 |
TRUE, translatorSep, ,FALSE); |
| 116 |
SO_KIT_ADD_CATALOG_ENTRY(translator, SoDragPointDragger, |
| 117 |
TRUE, translatorSep, ,TRUE); |
| 118 |
SO_KIT_ADD_CATALOG_ENTRY(rotator, SoRotateSphericalDragger, |
| 119 |
TRUE, topSeparator, geomSeparator,TRUE); |
| 120 |
SO_KIT_ADD_CATALOG_ENTRY(beamSep, SoSeparator, |
| 121 |
TRUE, topSeparator, geomSeparator,FALSE); |
| 122 |
SO_KIT_ADD_CATALOG_ENTRY(beamPlacement, SoTranslation, |
| 123 |
TRUE, beamSep, ,TRUE); |
| 124 |
SO_KIT_ADD_CATALOG_ENTRY(beamScale, SoScale, |
| 125 |
TRUE, beamSep, ,TRUE); |
| 126 |
SO_KIT_ADD_CATALOG_ENTRY(beamSwitch, SoSwitch, |
| 127 |
TRUE, beamSep, ,FALSE); |
| 128 |
SO_KIT_ADD_CATALOG_ENTRY(beam, SoSeparator, |
| 129 |
TRUE, beamSwitch, ,TRUE); |
| 130 |
SO_KIT_ADD_CATALOG_ENTRY(beamActive, SoSeparator, |
| 131 |
TRUE, beamSwitch, ,TRUE); |
| 132 |
|
| 133 |
// Read the default geometry for this dragger |
| 134 |
if (SO_KIT_IS_FIRST_INSTANCE()) |
| 135 |
readDefaultParts( "spotLightDragger.iv", geomBuffer, sizeof(geomBuffer) ); |
| 136 |
SO_KIT_ADD_FIELD(rotation, (0.0, 0.0, 0.0, 1.0)); |
| 137 |
SO_KIT_ADD_FIELD(translation, (0.0, 0.0, 0.0)); |
| 138 |
SO_KIT_ADD_FIELD(angle, (1.0)); |
| 139 |
|
| 140 |
SO_KIT_INIT_INSTANCE(); |
| 141 |
|
| 142 |
// Set the overall material. |
| 143 |
// We need to use a copy of the resource, since the resource is shared |
| 144 |
// by all manips but we are going to edit ours. |
| 145 |
SoNode *resourceMtl |
| 146 |
= SoNode::getByName("spotLightOverallMaterial"); |
| 147 |
setPartAsDefault("material", resourceMtl->copy() ); |
| 148 |
|
| 149 |
|
| 150 |
// Create the simple draggers that comprise this dragger. |
| 151 |
// This dragger employs 2 simple draggers: |
| 152 |
// 1 DragpointDragger |
| 153 |
// 1 RotateSphericalDragger |
| 154 |
|
| 155 |
SoDragPointDragger *trD; |
| 156 |
trD = SO_GET_ANY_PART( this,"translator", SoDragPointDragger ); |
| 157 |
|
| 158 |
SoRotateSphericalDragger *roD; |
| 159 |
roD = SO_GET_ANY_PART( this, "rotator", SoRotateSphericalDragger ); |
| 160 |
|
| 161 |
// Create everything needed for the beam spreader. |
| 162 |
// Create the parts. |
| 163 |
setPartAsDefault("beam", "spotLightBeam"); |
| 164 |
setPartAsDefault("beamActive", "spotLightBeamActive"); |
| 165 |
setPartAsDefault("beamPlacement", "spotLightBeamPlacement"); |
| 166 |
|
| 167 |
// Set the switch to 0... |
| 168 |
SoNode *bs = getAnyPart("beamSwitch", TRUE ); |
| 169 |
setSwitchValue(bs, 0); |
| 170 |
|
| 171 |
// Create a plane projector for use in dragging. |
| 172 |
planeProj = new SbPlaneProjector(); |
| 173 |
|
| 174 |
addStartCallback( &SoSpotLightDragger::startCB ); |
| 175 |
addMotionCallback( &SoSpotLightDragger::motionCB ); |
| 176 |
addFinishCallback( &SoSpotLightDragger::doneCB ); |
| 177 |
|
| 178 |
// Update the rotation and scale fields when the motionMatrix is set. |
| 179 |
addValueChangedCallback( &SoSpotLightDragger::valueChangedCB ); |
| 180 |
|
| 181 |
// Updates the motionMatrix when the scaleFactor field is set. |
| 182 |
rotFieldSensor |
| 183 |
= new SoFieldSensor(&SoSpotLightDragger::fieldSensorCB, this); |
| 184 |
rotFieldSensor->setPriority( 0 ); |
| 185 |
|
| 186 |
// Updates the motionMatrix when the translationFactor field is set. |
| 187 |
translFieldSensor |
| 188 |
= new SoFieldSensor( &SoSpotLightDragger::fieldSensorCB, this); |
| 189 |
translFieldSensor->setPriority( 0 ); |
| 190 |
|
| 191 |
// Updates the motionMatrix when the scaleFactor field is set. |
| 192 |
angleFieldSensor |
| 193 |
= new SoFieldSensor( &SoSpotLightDragger::fieldSensorCB, this); |
| 194 |
angleFieldSensor->setPriority( 0 ); |
| 195 |
|
| 196 |
setUpConnections( TRUE, TRUE ); |
| 197 |
} |
| 198 |
|
| 199 |
//////////////////////////////////////////////////////////////////////// |
| 200 |
// |
| 201 |
// Description: |
| 202 |
// Destructor |
| 203 |
// |
| 204 |
SoSpotLightDragger::~SoSpotLightDragger() |
| 205 |
// |
| 206 |
//////////////////////////////////////////////////////////////////////// |
| 207 |
{ |
| 208 |
delete planeProj; |
| 209 |
if (rotFieldSensor ) |
| 210 |
delete rotFieldSensor; |
| 211 |
if (translFieldSensor ) |
| 212 |
delete translFieldSensor; |
| 213 |
if (angleFieldSensor ) |
| 214 |
delete angleFieldSensor; |
| 215 |
} |
| 216 |
|
| 217 |
// detach/attach any sensors, callbacks, and/or field connections. |
| 218 |
// Called by: start/end of SoBaseKit::readInstance |
| 219 |
// and on new copy by: start/end of SoBaseKit::copy. |
| 220 |
// Classes that redefine must call setUpConnections(TRUE,TRUE) |
| 221 |
// at end of constructor. |
| 222 |
// Returns the state of the node when this was called. |
| 223 |
SbBool |
| 224 |
SoSpotLightDragger::setUpConnections( SbBool onOff, SbBool doItAlways ) |
| 225 |
{ |
| 226 |
if ( !doItAlways && connectionsSetUp == onOff) |
| 227 |
return onOff; |
| 228 |
|
| 229 |
if ( onOff ) { |
| 230 |
|
| 231 |
// We connect AFTER base class. |
| 232 |
SoDragger::setUpConnections( onOff, FALSE ); |
| 233 |
|
| 234 |
// Set default parts and add callbacks to child draggers. |
| 235 |
SoDragger *trD = (SoDragger *) getAnyPart( "translator", FALSE ); |
| 236 |
if (trD != NULL) { |
| 237 |
|
| 238 |
// Set up the parts in the child dragger |
| 239 |
SoNode *n; |
| 240 |
n = SoNode::getByName("spotLightTranslatorLineTranslator"); |
| 241 |
trD->setPartAsDefault("xTranslator.translator", n ); |
| 242 |
trD->setPartAsDefault("yTranslator.translator", n ); |
| 243 |
trD->setPartAsDefault("zTranslator.translator", n ); |
| 244 |
|
| 245 |
n = SoNode::getByName( |
| 246 |
"spotLightTranslatorLineTranslatorActive"); |
| 247 |
trD->setPartAsDefault("xTranslator.translatorActive", n ); |
| 248 |
trD->setPartAsDefault("yTranslator.translatorActive", n ); |
| 249 |
trD->setPartAsDefault("zTranslator.translatorActive", n ); |
| 250 |
|
| 251 |
n = SoNode::getByName("spotLightTranslatorPlaneTranslator"); |
| 252 |
trD->setPartAsDefault("yzTranslator.translator", n ); |
| 253 |
trD->setPartAsDefault("xzTranslator.translator", n ); |
| 254 |
trD->setPartAsDefault("xyTranslator.translator", n ); |
| 255 |
|
| 256 |
n = SoNode::getByName( |
| 257 |
"spotLightTranslatorPlaneTranslatorActive"); |
| 258 |
trD->setPartAsDefault("yzTranslator.translatorActive", n ); |
| 259 |
trD->setPartAsDefault("xzTranslator.translatorActive", n ); |
| 260 |
trD->setPartAsDefault("xyTranslator.translatorActive", n ); |
| 261 |
|
| 262 |
registerChildDragger( trD ); |
| 263 |
} |
| 264 |
|
| 265 |
SoRotateSphericalDragger *roD |
| 266 |
= (SoRotateSphericalDragger *) getAnyPart( "rotator", FALSE ); |
| 267 |
if (roD != NULL) { |
| 268 |
|
| 269 |
// Give it a projector that moves freely in the radial direction. |
| 270 |
// We need this because our geometry is just a little stick, not a |
| 271 |
// big ol' ball |
| 272 |
SbSphereSectionProjector *ssp = new SbSphereSectionProjector(); |
| 273 |
ssp->setRadialFactor( 1.0 ); |
| 274 |
roD->setProjector(ssp); |
| 275 |
|
| 276 |
// Set up the parts in the child dragger |
| 277 |
roD->setPartAsDefault("rotator", |
| 278 |
"spotLightRotatorRotator"); |
| 279 |
roD->setPartAsDefault("rotatorActive", |
| 280 |
"spotLightRotatorRotatorActive"); |
| 281 |
roD->setPartAsDefault("feedback", |
| 282 |
"spotLightRotatorFeedback" ); |
| 283 |
roD->setPartAsDefault("feedbackActive", |
| 284 |
"spotLightRotatorFeedbackActive"); |
| 285 |
|
| 286 |
registerChildDragger( roD ); |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
// Call the sensor CBs to make things are up-to-date. |
| 291 |
fieldSensorCB( this, NULL ); |
| 292 |
|
| 293 |
// Connect the field sensors |
| 294 |
if (rotFieldSensor->getAttachedField() != &rotation) |
| 295 |
rotFieldSensor->attach( &rotation ); |
| 296 |
if (translFieldSensor->getAttachedField() != &translation) |
| 297 |
translFieldSensor->attach( &translation ); |
| 298 |
if (angleFieldSensor->getAttachedField() != &angle) |
| 299 |
angleFieldSensor->attach( &angle ); |
| 300 |
} |
| 301 |
else { |
| 302 |
|
| 303 |
// We disconnect BEFORE base class. |
| 304 |
|
| 305 |
// remove callbacks from the child draggers |
| 306 |
SoDragger *trD = (SoDragger *) getAnyPart( "translator", FALSE ); |
| 307 |
if (trD) |
| 308 |
unregisterChildDragger( trD ); |
| 309 |
|
| 310 |
SoDragger *roD = (SoDragger *) getAnyPart( "rotator", FALSE ); |
| 311 |
if (roD) |
| 312 |
unregisterChildDragger( roD ); |
| 313 |
|
| 314 |
// Disconnect the field sensors. |
| 315 |
if (rotFieldSensor->getAttachedField()) |
| 316 |
rotFieldSensor->detach(); |
| 317 |
if (translFieldSensor->getAttachedField()) |
| 318 |
translFieldSensor->detach(); |
| 319 |
if (angleFieldSensor->getAttachedField()) |
| 320 |
angleFieldSensor->detach(); |
| 321 |
|
| 322 |
SoDragger::setUpConnections( onOff, FALSE ); |
| 323 |
} |
| 324 |
|
| 325 |
return !(connectionsSetUp = onOff); |
| 326 |
} |
| 327 |
|
| 328 |
//////////////////////////////////////////////////////////////////////// |
| 329 |
// |
| 330 |
// Description: |
| 331 |
// Calculates the fields of the beamScale, based on |
| 332 |
// a beam spread angle. |
| 333 |
// |
| 334 |
// At the angle of 45 degrees, the scale is (1,1,1) |
| 335 |
// A greater beam angle increases the scale in x and y equally, |
| 336 |
// while decreasing the angle in z. |
| 337 |
// A smaller beam angle decreases the scale in x and y equally, |
| 338 |
// while increasing the angle in z. |
| 339 |
// |
| 340 |
// The equation for the relationship is: |
| 341 |
// xScale = yScale = sin( beamAngle ); |
| 342 |
// zScale = cos( beamAngle ); |
| 343 |
// |
| 344 |
// Angles are clamped to lie between 0 and 90 degrees. |
| 345 |
// |
| 346 |
// Use: protected |
| 347 |
// |
| 348 |
void |
| 349 |
SoSpotLightDragger::setBeamScaleFromAngle( float beamAngle ) |
| 350 |
// |
| 351 |
//////////////////////////////////////////////////////////////////////// |
| 352 |
{ |
| 353 |
float myNewAngle = beamAngle; |
| 354 |
// This dragger clamps angle values to lie between 0 and PI/2 radians. |
| 355 |
// But we will be more restrictive when we display, so that users |
| 356 |
// can always (hopefully) hit the beam geometry. |
| 357 |
#define TWO_AND_A_HALF_DEGREES 0.043633 |
| 358 |
if ( beamAngle < TWO_AND_A_HALF_DEGREES ) |
| 359 |
myNewAngle = TWO_AND_A_HALF_DEGREES; |
| 360 |
#undef TWO_AND_A_HALF_DEGREES |
| 361 |
if ( beamAngle > (M_PI / 2.0) ) |
| 362 |
myNewAngle = (M_PI / 2.0); |
| 363 |
|
| 364 |
float myCos = cosf(myNewAngle); |
| 365 |
float mySin = sinf(myNewAngle); |
| 366 |
|
| 367 |
if (myCos < getMinScale()) |
| 368 |
myCos = getMinScale(); |
| 369 |
|
| 370 |
if (mySin < getMinScale()) |
| 371 |
mySin = getMinScale(); |
| 372 |
|
| 373 |
SoScale *bs = (SoScale *) getAnyPart("beamScale",TRUE); |
| 374 |
if (bs) { |
| 375 |
SbVec3f newSF(mySin, mySin, myCos); |
| 376 |
if ( bs->scaleFactor.getValue() != newSF ) |
| 377 |
bs->scaleFactor.setValue( newSF ); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
//////////////////////////////////////////////////////////////////////// |
| 382 |
// |
| 383 |
// Description: |
| 384 |
// Prepare for dragging the beam spreading piece. |
| 385 |
// |
| 386 |
// Use: private |
| 387 |
// |
| 388 |
void |
| 389 |
SoSpotLightDragger::dragStart() |
| 390 |
// |
| 391 |
//////////////////////////////////////////////////////////////////////// |
| 392 |
{ |
| 393 |
// We only perform dragging if we received the mouse-down. |
| 394 |
// If there is an active child dragger, then we should just return. |
| 395 |
if ( getActiveChildDragger() != NULL) |
| 396 |
return; |
| 397 |
|
| 398 |
// Set the switch to 1... |
| 399 |
setSwitchValue( beamSwitch.getValue(), 1 ); |
| 400 |
|
| 401 |
// Establish the working space. |
| 402 |
// Working space is space at end of "beamPlacement", (but before |
| 403 |
// "beamScale"). |
| 404 |
SbMatrix workSpaceToLocal, localToWorkSpace; |
| 405 |
getPartToLocalMatrix("beamPlacement",workSpaceToLocal,localToWorkSpace); |
| 406 |
SbMatrix worldSpaceToWork = getWorldToLocalMatrix(); |
| 407 |
worldSpaceToWork.multRight( localToWorkSpace ); |
| 408 |
|
| 409 |
// Establish the projector plane in working space. |
| 410 |
// The plane is defined by two points on the z axis and the selected point. |
| 411 |
// If they are colinear or nearly so, then make the plane face the viewer. |
| 412 |
SbVec3f p0(0,0,0), p1(0,0,1); |
| 413 |
|
| 414 |
SbVec3f startWorkSpaceHitPt; |
| 415 |
worldSpaceToWork.multVecMatrix(getWorldStartingPoint(), |
| 416 |
startWorkSpaceHitPt); |
| 417 |
SbVec3f p2 = startWorkSpaceHitPt; |
| 418 |
p2.normalize(); |
| 419 |
#define MAX_WORKABLE_ANGLE_COS .98 |
| 420 |
if (((p2 - p0).dot(p1 - p0)) > MAX_WORKABLE_ANGLE_COS) { |
| 421 |
// Find a new p2. |
| 422 |
SbVec3f worldProjDir = getViewVolume().getProjectionDirection(); |
| 423 |
SbVec3f lclProjDir; |
| 424 |
worldSpaceToWork.multDirMatrix(worldProjDir,lclProjDir); |
| 425 |
lclProjDir.normalize(); |
| 426 |
lclProjDir *= -1.0; |
| 427 |
SbVec3f otherVecInPlane = lclProjDir.cross( p1 - p0 ); |
| 428 |
p2 = (otherVecInPlane - p0); |
| 429 |
} |
| 430 |
#undef MAX_WORKABLE_ANGLE_COS |
| 431 |
|
| 432 |
SbPlane wsPlane(p0, p1, p2); |
| 433 |
|
| 434 |
planeProj->setPlane( wsPlane ); |
| 435 |
} |
| 436 |
|
| 437 |
//////////////////////////////////////////////////////////////////////// |
| 438 |
// |
| 439 |
// Description: |
| 440 |
// Set the beam angle based on the new hit to the planeProj |
| 441 |
// |
| 442 |
// Use: private |
| 443 |
// |
| 444 |
void |
| 445 |
SoSpotLightDragger::drag() |
| 446 |
// |
| 447 |
//////////////////////////////////////////////////////////////////////// |
| 448 |
{ |
| 449 |
// We only perform dragging if we received the mouse-down. |
| 450 |
// If there is an active child dragger, then we should just return. |
| 451 |
if ( getActiveChildDragger() != NULL) |
| 452 |
return; |
| 453 |
|
| 454 |
// Establish the working space. |
| 455 |
// Working space is space at end of "beamPlacement", (but before |
| 456 |
// "beamScale"). |
| 457 |
SbMatrix workSpaceToLocal, localToWorkSpace; |
| 458 |
getPartToLocalMatrix("beamPlacement",workSpaceToLocal,localToWorkSpace); |
| 459 |
SbMatrix workSpaceToWorld = getLocalToWorldMatrix(); |
| 460 |
workSpaceToWorld.multLeft( workSpaceToLocal ); |
| 461 |
SbMatrix worldSpaceToWork = getWorldToLocalMatrix(); |
| 462 |
worldSpaceToWork.multRight( localToWorkSpace ); |
| 463 |
|
| 464 |
// Set the projector space and viewVolume. |
| 465 |
planeProj->setViewVolume( getViewVolume() ); |
| 466 |
planeProj->setWorkingSpace( workSpaceToWorld ); |
| 467 |
|
| 468 |
// Get newHitPt and startHitPt in workspace. |
| 469 |
SbVec3f newHitPt |
| 470 |
= planeProj->project( getNormalizedLocaterPosition()); |
| 471 |
SbVec3f startHitPt; |
| 472 |
worldSpaceToWork.multVecMatrix( getWorldStartingPoint(), startHitPt ); |
| 473 |
|
| 474 |
// Based on the position, determine the new beamAngle. |
| 475 |
// Theta will be the angle between the minus z axis and the |
| 476 |
// vector from (0,0,0) to newHitPt. |
| 477 |
|
| 478 |
// Normalize these and they become directions from (0,0,0) to hit points |
| 479 |
newHitPt.normalize(); |
| 480 |
startHitPt.normalize(); |
| 481 |
// We also need this direction: |
| 482 |
SbVec3f minusZ(0,0,-1); |
| 483 |
|
| 484 |
// If we've gone 'out of bounds' let's figure out what to do... |
| 485 |
// If newHitPt was in positive z, the angle will be > (PI/2) |
| 486 |
SbBool isPosZ = ( newHitPt[2] > 0.0 ); |
| 487 |
|
| 488 |
// If newHitPt and startHitPt are on opposite sides of the |
| 489 |
// z axis within the projector plane, we've completely closed |
| 490 |
// the 'umbrella' and gone to the other side, so we'll want to |
| 491 |
// clamp the value at 0.0 |
| 492 |
SbVec3f newCross = newHitPt.cross(minusZ); |
| 493 |
SbVec3f startCross = startHitPt.cross(minusZ); |
| 494 |
|
| 495 |
SbBool isNewOppositeStart = ( newCross.dot(startCross) < 0.0 ); |
| 496 |
|
| 497 |
// Find theta |
| 498 |
// Do different things depending on if we've gone out of bounds. |
| 499 |
float curAngle = angle.getValue(); |
| 500 |
float theta; |
| 501 |
|
| 502 |
// If both conditions are violated, use the value closer to |
| 503 |
// the current angle. |
| 504 |
if ( isPosZ && isNewOppositeStart ) { |
| 505 |
if ( fabs(curAngle - 0.0) < fabs((M_PI/2.0) - curAngle) ) |
| 506 |
theta = 0.0; |
| 507 |
else |
| 508 |
theta = M_PI / 2.0; |
| 509 |
} |
| 510 |
else if ( isPosZ ) { |
| 511 |
theta = M_PI / 2.0; |
| 512 |
} |
| 513 |
else if ( isNewOppositeStart ) { |
| 514 |
theta = 0.0; |
| 515 |
} |
| 516 |
else { |
| 517 |
// we should be able to go unconstrained. |
| 518 |
// Find angle between minusZ vector and newHitPt |
| 519 |
|
| 520 |
// Dot product of unit vectors is cosine of angle between them. |
| 521 |
float thetaCos = minusZ.dot( newHitPt ); |
| 522 |
theta = acosf( thetaCos ); |
| 523 |
|
| 524 |
// clamp theta to lie between 0 and PI/2 |
| 525 |
theta = (theta < 0.0) ? 0.0 : theta; |
| 526 |
theta = (theta > (M_PI / 2.0)) ? (M_PI / 2.0) : theta; |
| 527 |
} |
| 528 |
|
| 529 |
// Now we've got a new angle. Set the beamScale appropriately, |
| 530 |
// set the angle field, and call the special angle callbacks. |
| 531 |
|
| 532 |
setBeamScaleFromAngle(theta); |
| 533 |
|
| 534 |
angleFieldSensor->detach(); // Disconnect sensor |
| 535 |
if ( curAngle != theta ) // Set field |
| 536 |
angle = theta; |
| 537 |
angleFieldSensor->attach( &angle ); // Reconnect sensor |
| 538 |
|
| 539 |
// Invoke the value changed callbacks. |
| 540 |
// We must specifically do this since only calling setMotionMatrix() |
| 541 |
// does it automatically. |
| 542 |
valueChanged(); |
| 543 |
} |
| 544 |
|
| 545 |
//////////////////////////////////////////////////////////////////////// |
| 546 |
// |
| 547 |
// Description: |
| 548 |
// Rotate the spoLightDragger based on mouse motion. |
| 549 |
// |
| 550 |
// Use: private |
| 551 |
// |
| 552 |
void |
| 553 |
SoSpotLightDragger::dragFinish() |
| 554 |
// |
| 555 |
//////////////////////////////////////////////////////////////////////// |
| 556 |
{ |
| 557 |
// We only perform dragging if we received the mouse-down. |
| 558 |
// If there is an active child dragger, then we should just return. |
| 559 |
if ( getActiveChildDragger() != NULL) |
| 560 |
return; |
| 561 |
|
| 562 |
// Set the switches to 0... |
| 563 |
setSwitchValue( beamSwitch.getValue(), 0 ); |
| 564 |
} |
| 565 |
|
| 566 |
//////////////////////////////////////////////////////////////////// |
| 567 |
// Stubs for callbacks |
| 568 |
//////////////////////////////////////////////////////////////////// |
| 569 |
void |
| 570 |
SoSpotLightDragger::startCB( void *, SoDragger *inDragger ) |
| 571 |
{ |
| 572 |
SoSpotLightDragger *dl = (SoSpotLightDragger *) inDragger; |
| 573 |
dl->dragStart(); |
| 574 |
} |
| 575 |
|
| 576 |
void |
| 577 |
SoSpotLightDragger::motionCB( void *, SoDragger *inDragger ) |
| 578 |
{ |
| 579 |
SoSpotLightDragger *dl = (SoSpotLightDragger *) inDragger; |
| 580 |
dl->drag(); |
| 581 |
} |
| 582 |
|
| 583 |
void |
| 584 |
SoSpotLightDragger::doneCB( void *, SoDragger *inDragger ) |
| 585 |
{ |
| 586 |
SoSpotLightDragger *dl = (SoSpotLightDragger *) inDragger; |
| 587 |
dl->dragFinish(); |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
void |
| 592 |
SoSpotLightDragger::valueChangedCB( void *, SoDragger *inDragger ) |
| 593 |
{ |
| 594 |
SoSpotLightDragger *m = (SoSpotLightDragger *) inDragger; |
| 595 |
SbMatrix motMat = m->getMotionMatrix(); |
| 596 |
|
| 597 |
SbVec3f trans, scale; |
| 598 |
SbRotation rot, scaleOrient; |
| 599 |
motMat.getTransform( trans, rot, scale, scaleOrient ); |
| 600 |
|
| 601 |
// Disconnect the field sensors |
| 602 |
m->rotFieldSensor->detach(); |
| 603 |
m->translFieldSensor->detach(); |
| 604 |
m->angleFieldSensor->detach(); |
| 605 |
|
| 606 |
if ( m->rotation.getValue() != rot ) |
| 607 |
m->rotation = rot; |
| 608 |
if ( m->translation.getValue() != trans ) |
| 609 |
m->translation = trans; |
| 610 |
|
| 611 |
// Make the rotation inside the "translaterRotInv" be the inverse |
| 612 |
// of the new rotation. |
| 613 |
SbRotation newInv = rot.inverse(); |
| 614 |
SoRotation *rotInv = (SoRotation *) m->getAnyPart("translatorRotInv",TRUE); |
| 615 |
if (rotInv->rotation.getValue() != newInv) |
| 616 |
rotInv->rotation = newInv; |
| 617 |
|
| 618 |
// Note: we do not need to do anything special for 'angle.' |
| 619 |
// When the dragging routine changes the angle, it sets both |
| 620 |
// the angle field and the internal tranform for us. |
| 621 |
|
| 622 |
// Reconnect the field sensors |
| 623 |
m->rotFieldSensor->attach( &(m->rotation) ); |
| 624 |
m->translFieldSensor->attach( &(m->translation) ); |
| 625 |
m->angleFieldSensor->attach( &(m->angle) ); |
| 626 |
} |
| 627 |
|
| 628 |
void |
| 629 |
SoSpotLightDragger::fieldSensorCB(void *inDragger, SoSensor *inSensor ) |
| 630 |
{ |
| 631 |
SoSpotLightDragger *dragger = (SoSpotLightDragger *) inDragger; |
| 632 |
|
| 633 |
SoField *trigF = NULL; |
| 634 |
if ( inSensor ) |
| 635 |
trigF = ((SoDataSensor *)inSensor)->getTriggerField(); |
| 636 |
|
| 637 |
// If inSensor is NULL or the trigger field is NULL, we |
| 638 |
// will do both the matrix and beam stuff, since we don't know what changed. |
| 639 |
SbBool doMatrix = TRUE; |
| 640 |
SbBool doBeam = TRUE; |
| 641 |
|
| 642 |
// But if this is invoked by a sensor with a trigger field, |
| 643 |
// we can do different things if it was the angle |
| 644 |
// or the rotation/translation fields that triggered. |
| 645 |
if ( trigF ) { |
| 646 |
|
| 647 |
if ( trigF == &dragger->rotation || trigF == &dragger->translation ) |
| 648 |
doBeam = FALSE; |
| 649 |
|
| 650 |
if ( trigF == &dragger->angle ) |
| 651 |
doMatrix = FALSE; |
| 652 |
} |
| 653 |
|
| 654 |
// Save the cut off angle, since it can get screwed up when the |
| 655 |
// matrix on the dragger is set: |
| 656 |
float myCutOff = dragger->angle.getValue(); |
| 657 |
|
| 658 |
if ( doMatrix ) { |
| 659 |
|
| 660 |
SbMatrix motMat = dragger->getMotionMatrix(); |
| 661 |
dragger->workFieldsIntoTransform(motMat); |
| 662 |
|
| 663 |
dragger->setMotionMatrix(motMat); |
| 664 |
} |
| 665 |
|
| 666 |
// Restore the cut off angle: |
| 667 |
if (dragger->angle.getValue() != myCutOff) |
| 668 |
dragger->angle = myCutOff; |
| 669 |
|
| 670 |
if ( doBeam ) { |
| 671 |
|
| 672 |
// This line is special work done for spotlights that is |
| 673 |
// different from most other draggers. |
| 674 |
dragger->setBeamScaleFromAngle( dragger->angle.getValue() ); |
| 675 |
|
| 676 |
// We need to specifically tell the dragger to invoke the |
| 677 |
// value changed callbacks, since only calling setMotionMatrix |
| 678 |
// automatically does this. |
| 679 |
if ( doMatrix == FALSE ) |
| 680 |
dragger->valueChanged(); |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
void |
| 685 |
SoSpotLightDragger::setDefaultOnNonWritingFields() |
| 686 |
{ |
| 687 |
// We don't write out angle if it has default value. |
| 688 |
if ( ! (angle.isConnected() && angle.isConnectionEnabled()) |
| 689 |
&& angle.getValue() == 1.0 ) |
| 690 |
angle.setDefault(TRUE); |
| 691 |
|
| 692 |
// These nodes may change after construction, but we still |
| 693 |
// don't want to write them out. |
| 694 |
translatorRotInv.setDefault(TRUE); |
| 695 |
beamScale.setDefault(TRUE); |
| 696 |
|
| 697 |
// Try not to write out the sub-draggers. |
| 698 |
translator.setDefault(TRUE); |
| 699 |
rotator.setDefault(TRUE); |
| 700 |
|
| 701 |
// Call the base class... |
| 702 |
SoDragger::setDefaultOnNonWritingFields(); |
| 703 |
} |