| 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 |
* This is an example from The Inventor Mentor, |
| 39 |
* chapter 9, example 4. |
| 40 |
* |
| 41 |
* Example of setting up pick actions and using the pick path. |
| 42 |
* A couple of objects are displayed. The program catches |
| 43 |
* mouse button events and determines the mouse position. |
| 44 |
* A pick action is applied and if an object is picked the |
| 45 |
* pick path is printed to stdout. |
| 46 |
*-----------------------------------------------------------*/ |
| 47 |
|
| 48 |
#include <Inventor/SbViewportRegion.h> |
| 49 |
#include <Inventor/SoDB.h> |
| 50 |
#include <Inventor/SoInput.h> |
| 51 |
#include <Inventor/SoPickedPoint.h> |
| 52 |
#include <Inventor/Xt/SoXt.h> |
| 53 |
#include <Inventor/Xt/viewers/SoXtExaminerViewer.h> |
| 54 |
#include <Inventor/actions/SoRayPickAction.h> |
| 55 |
#include <Inventor/actions/SoWriteAction.h> |
| 56 |
#include <Inventor/events/SoMouseButtonEvent.h> |
| 57 |
#include <Inventor/nodes/SoEventCallback.h> |
| 58 |
#include <Inventor/nodes/SoMaterial.h> |
| 59 |
#include <Inventor/nodes/SoRotationXYZ.h> |
| 60 |
#include <Inventor/nodes/SoSeparator.h> |
| 61 |
#include <Inventor/nodes/SoTranslation.h> |
| 62 |
|
| 63 |
#include <cstdlib> |
| 64 |
|
| 65 |
/////////////////////////////////////////////////////////////// |
| 66 |
// CODE FOR The Inventor Mentor STARTS HERE |
| 67 |
|
| 68 |
SbBool |
| 69 |
writePickedPath (SoNode *root, |
| 70 |
const SbViewportRegion &viewport, |
| 71 |
const SbVec2s &cursorPosition) |
| 72 |
{ |
| 73 |
SoRayPickAction myPickAction(viewport); |
| 74 |
|
| 75 |
// Set an 8-pixel wide region around the pixel |
| 76 |
myPickAction.setPoint(cursorPosition); |
| 77 |
myPickAction.setRadius(8.0); |
| 78 |
|
| 79 |
// Start a pick traversal |
| 80 |
myPickAction.apply(root); |
| 81 |
const SoPickedPoint *myPickedPoint = |
| 82 |
myPickAction.getPickedPoint(); |
| 83 |
if (myPickedPoint == NULL) return FALSE; |
| 84 |
|
| 85 |
// Write out the path to the picked object |
| 86 |
SoWriteAction myWriteAction; |
| 87 |
myWriteAction.apply(myPickedPoint->getPath()); |
| 88 |
|
| 89 |
return TRUE; |
| 90 |
} |
| 91 |
|
| 92 |
// CODE FOR The Inventor Mentor ENDS HERE |
| 93 |
/////////////////////////////////////////////////////////////// |
| 94 |
|
| 95 |
// This routine is called for every mouse button event. |
| 96 |
void |
| 97 |
myMousePressCB(void *userData, SoEventCallback *eventCB) |
| 98 |
{ |
| 99 |
SoSeparator *root = (SoSeparator *) userData; |
| 100 |
const SoEvent *event = eventCB->getEvent(); |
| 101 |
|
| 102 |
// Check for mouse button being pressed |
| 103 |
if (SO_MOUSE_PRESS_EVENT(event, ANY)) { |
| 104 |
const SbViewportRegion &myRegion = |
| 105 |
eventCB->getAction()->getViewportRegion(); |
| 106 |
writePickedPath(root, myRegion, |
| 107 |
event->getPosition(myRegion)); |
| 108 |
eventCB->setHandled(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
int |
| 113 |
main(int, char **argv) |
| 114 |
{ |
| 115 |
SoMouseButtonEvent myMouseEvent; |
| 116 |
|
| 117 |
// Initialize Inventor and Xt |
| 118 |
Widget myWindow = SoXt::init(argv[0]); |
| 119 |
if (myWindow == NULL) exit(1); |
| 120 |
|
| 121 |
SoSeparator *root = new SoSeparator; |
| 122 |
root->ref(); |
| 123 |
|
| 124 |
// Add an event callback to catch mouse button presses. |
| 125 |
// The callback is set up later on. |
| 126 |
SoEventCallback *myEventCB = new SoEventCallback; |
| 127 |
root->addChild(myEventCB); |
| 128 |
|
| 129 |
// Read object data from a file |
| 130 |
SoInput mySceneInput; |
| 131 |
if (!mySceneInput.openFile("/usr/share/src/Inventor/examples/data/star.iv")) |
| 132 |
exit (1); |
| 133 |
SoSeparator *starObject = SoDB::readAll(&mySceneInput); |
| 134 |
if (starObject == NULL) exit (1); |
| 135 |
mySceneInput.closeFile(); |
| 136 |
|
| 137 |
// Add two copies of the star object, one white and one red |
| 138 |
SoRotationXYZ *myRotation = new SoRotationXYZ; |
| 139 |
myRotation->axis.setValue(SoRotationXYZ::X); |
| 140 |
myRotation->angle.setValue(M_PI/2.2); // almost 90 degrees |
| 141 |
root->addChild(myRotation); |
| 142 |
|
| 143 |
root->addChild(starObject); // first star object |
| 144 |
|
| 145 |
SoMaterial *myMaterial = new SoMaterial; |
| 146 |
myMaterial->diffuseColor.setValue(1.0, 0.0, 0.0); // red |
| 147 |
root->addChild(myMaterial); |
| 148 |
SoTranslation *myTranslation = new SoTranslation; |
| 149 |
myTranslation->translation.setValue(1., 0., 1.); |
| 150 |
root->addChild(myTranslation); |
| 151 |
root->addChild(starObject); // second star object |
| 152 |
|
| 153 |
// Create a render area in which to see our scene graph. |
| 154 |
SoXtExaminerViewer *myViewer = new SoXtExaminerViewer(myWindow); |
| 155 |
|
| 156 |
// Turn off viewing to allow picking |
| 157 |
myViewer->setViewing(FALSE); |
| 158 |
|
| 159 |
myViewer->setSceneGraph(root); |
| 160 |
myViewer->setTitle("Pick Actions & Paths"); |
| 161 |
myViewer->show(); |
| 162 |
|
| 163 |
// Set up the event callback. We want to pass the root of the |
| 164 |
// entire scene graph (including the camera) as the userData, |
| 165 |
// so we get the scene manager's version of the scene graph |
| 166 |
// root. |
| 167 |
myEventCB->addEventCallback( |
| 168 |
SoMouseButtonEvent::getClassTypeId(), |
| 169 |
myMousePressCB, |
| 170 |
myViewer->getSceneManager()->getSceneGraph()); |
| 171 |
|
| 172 |
SoXt::show(myWindow); |
| 173 |
SoXt::mainLoop(); |
| 174 |
} |
| 175 |
|