| 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 10, example 1. |
| 40 |
* |
| 41 |
* The scene graph has 4 objects which may be |
| 42 |
* selected by picking with the left mouse button |
| 43 |
* (use shift key to extend the selection to more |
| 44 |
* than one object). |
| 45 |
* |
| 46 |
* Hitting the up arrow key will increase the size of |
| 47 |
* each selected object; hitting down arrow will decrease |
| 48 |
* the size of each selected object. |
| 49 |
*-----------------------------------------------------------*/ |
| 50 |
|
| 51 |
#include <cstdlib> |
| 52 |
#include <Inventor/Sb.h> |
| 53 |
#include <Inventor/Xt/SoXt.h> |
| 54 |
#include <Inventor/Xt/SoXtRenderArea.h> |
| 55 |
#include <Inventor/actions/SoBoxHighlightRenderAction.h> |
| 56 |
#include <Inventor/actions/SoSearchAction.h> |
| 57 |
#include <Inventor/events/SoKeyboardEvent.h> |
| 58 |
#include <Inventor/nodes/SoCone.h> |
| 59 |
#include <Inventor/nodes/SoCube.h> |
| 60 |
#include <Inventor/nodes/SoCylinder.h> |
| 61 |
#include <Inventor/nodes/SoDirectionalLight.h> |
| 62 |
#include <Inventor/nodes/SoEventCallback.h> |
| 63 |
#include <Inventor/nodes/SoMaterial.h> |
| 64 |
#include <Inventor/nodes/SoNode.h> |
| 65 |
#include <Inventor/nodes/SoPerspectiveCamera.h> |
| 66 |
#include <Inventor/nodes/SoSphere.h> |
| 67 |
#include <Inventor/nodes/SoSelection.h> |
| 68 |
#include <Inventor/nodes/SoSeparator.h> |
| 69 |
#include <Inventor/nodes/SoTransform.h> |
| 70 |
|
| 71 |
// Function prototypes |
| 72 |
void myKeyPressCB(void *, SoEventCallback *); |
| 73 |
void myScaleSelection(SoSelection *, float); |
| 74 |
|
| 75 |
// Global data |
| 76 |
SbViewportRegion viewportRegion; |
| 77 |
SoTransform *cubeTransform, *sphereTransform, |
| 78 |
*coneTransform, *cylTransform; |
| 79 |
|
| 80 |
int |
| 81 |
main(int , char **argv) |
| 82 |
{ |
| 83 |
// Print out usage message |
| 84 |
printf("Left mouse button - selects object\n"); |
| 85 |
printf("<shift>Left mouse button - selects multiple objects\n"); |
| 86 |
printf("Up and Down arrows - scale selected objects\n"); |
| 87 |
|
| 88 |
// Initialize Inventor and Xt |
| 89 |
Widget myWindow = SoXt::init(argv[0]); |
| 90 |
if (myWindow == NULL) exit(1); |
| 91 |
|
| 92 |
// Create and set up the selection node |
| 93 |
SoSelection *selectionRoot = new SoSelection; |
| 94 |
selectionRoot->ref(); |
| 95 |
selectionRoot->policy = SoSelection::SHIFT; |
| 96 |
|
| 97 |
// Add a camera and some light |
| 98 |
SoPerspectiveCamera *myCamera = new SoPerspectiveCamera; |
| 99 |
selectionRoot->addChild(myCamera); |
| 100 |
selectionRoot->addChild(new SoDirectionalLight); |
| 101 |
|
| 102 |
/////////////////////////////////////////////////////////////// |
| 103 |
// CODE FOR The Inventor Mentor STARTS HERE (part 1) |
| 104 |
|
| 105 |
// An event callback node so we can receive key press events |
| 106 |
SoEventCallback *myEventCB = new SoEventCallback; |
| 107 |
myEventCB->addEventCallback( |
| 108 |
SoKeyboardEvent::getClassTypeId(), |
| 109 |
myKeyPressCB, selectionRoot); |
| 110 |
selectionRoot->addChild(myEventCB); |
| 111 |
|
| 112 |
// CODE FOR The Inventor Mentor ENDS HERE |
| 113 |
/////////////////////////////////////////////////////////////// |
| 114 |
|
| 115 |
// Add some geometry to the scene |
| 116 |
|
| 117 |
// a red cube |
| 118 |
SoSeparator *cubeRoot = new SoSeparator; |
| 119 |
SoMaterial *cubeMaterial = new SoMaterial; |
| 120 |
cubeTransform = new SoTransform; |
| 121 |
cubeRoot->addChild(cubeTransform); |
| 122 |
cubeRoot->addChild(cubeMaterial); |
| 123 |
cubeRoot->addChild(new SoCube); |
| 124 |
cubeTransform->translation.setValue(-2, 2, 0); |
| 125 |
cubeMaterial->diffuseColor.setValue(.8, 0, 0); |
| 126 |
selectionRoot->addChild(cubeRoot); |
| 127 |
|
| 128 |
// a blue sphere |
| 129 |
SoSeparator *sphereRoot = new SoSeparator; |
| 130 |
SoMaterial *sphereMaterial = new SoMaterial; |
| 131 |
sphereTransform = new SoTransform; |
| 132 |
sphereRoot->addChild(sphereTransform); |
| 133 |
sphereRoot->addChild(sphereMaterial); |
| 134 |
sphereRoot->addChild(new SoSphere); |
| 135 |
sphereTransform->translation.setValue(2, 2, 0); |
| 136 |
sphereMaterial->diffuseColor.setValue(0, 0, .8); |
| 137 |
selectionRoot->addChild(sphereRoot); |
| 138 |
|
| 139 |
// a green cone |
| 140 |
SoSeparator *coneRoot = new SoSeparator; |
| 141 |
SoMaterial *coneMaterial = new SoMaterial; |
| 142 |
coneTransform = new SoTransform; |
| 143 |
coneRoot->addChild(coneTransform); |
| 144 |
coneRoot->addChild(coneMaterial); |
| 145 |
coneRoot->addChild(new SoCone); |
| 146 |
coneTransform->translation.setValue(2, -2, 0); |
| 147 |
coneMaterial->diffuseColor.setValue(0, .8, 0); |
| 148 |
selectionRoot->addChild(coneRoot); |
| 149 |
|
| 150 |
// a magenta cylinder |
| 151 |
SoSeparator *cylRoot = new SoSeparator; |
| 152 |
SoMaterial *cylMaterial = new SoMaterial; |
| 153 |
cylTransform = new SoTransform; |
| 154 |
cylRoot->addChild(cylTransform); |
| 155 |
cylRoot->addChild(cylMaterial); |
| 156 |
cylRoot->addChild(new SoCylinder); |
| 157 |
cylTransform->translation.setValue(-2, -2, 0); |
| 158 |
cylMaterial->diffuseColor.setValue(.8, 0, .8); |
| 159 |
selectionRoot->addChild(cylRoot); |
| 160 |
|
| 161 |
// Create a render area for viewing the scene |
| 162 |
SoXtRenderArea *myRenderArea = new SoXtRenderArea(myWindow); |
| 163 |
myRenderArea->setSceneGraph(selectionRoot); |
| 164 |
myRenderArea->setGLRenderAction(new SoBoxHighlightRenderAction()); |
| 165 |
myRenderArea->redrawOnSelectionChange(selectionRoot); |
| 166 |
myRenderArea->setTitle("Adding Event Callbacks"); |
| 167 |
|
| 168 |
// Make the camera see the whole scene |
| 169 |
viewportRegion = myRenderArea->getViewportRegion(); |
| 170 |
myCamera->viewAll(selectionRoot, viewportRegion, 2.0); |
| 171 |
|
| 172 |
// Show our application window, and loop forever... |
| 173 |
myRenderArea->show(); |
| 174 |
SoXt::show(myWindow); |
| 175 |
SoXt::mainLoop(); |
| 176 |
} |
| 177 |
|
| 178 |
/////////////////////////////////////////////////////////////// |
| 179 |
// CODE FOR The Inventor Mentor STARTS HERE (part 2) |
| 180 |
|
| 181 |
// If the event is down arrow, then scale down every object |
| 182 |
// in the selection list; if the event is up arrow, scale up. |
| 183 |
// The userData is the selectionRoot from main(). |
| 184 |
void |
| 185 |
myKeyPressCB(void *userData, SoEventCallback *eventCB) |
| 186 |
{ |
| 187 |
SoSelection *selection = (SoSelection *) userData; |
| 188 |
const SoEvent *event = eventCB->getEvent(); |
| 189 |
|
| 190 |
// check for the Up and Down arrow keys being pressed |
| 191 |
if (SO_KEY_PRESS_EVENT(event, UP_ARROW)) { |
| 192 |
myScaleSelection(selection, 1.1); |
| 193 |
eventCB->setHandled(); |
| 194 |
} else if (SO_KEY_PRESS_EVENT(event, DOWN_ARROW)) { |
| 195 |
myScaleSelection(selection, 1.0/1.1); |
| 196 |
eventCB->setHandled(); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// CODE FOR The Inventor Mentor ENDS HERE |
| 201 |
/////////////////////////////////////////////////////////////// |
| 202 |
|
| 203 |
// Scale each object in the selection list |
| 204 |
void |
| 205 |
myScaleSelection(SoSelection *selection, float sf) |
| 206 |
{ |
| 207 |
SoPath *selectedPath; |
| 208 |
SoTransform *xform; |
| 209 |
SbVec3f scaleFactor; |
| 210 |
int i,j; |
| 211 |
|
| 212 |
// Scale each object in the selection list |
| 213 |
|
| 214 |
for (i = 0; i < selection->getNumSelected(); i++) { |
| 215 |
selectedPath = selection->getPath(i); |
| 216 |
xform = NULL; |
| 217 |
|
| 218 |
// Look for the shape node, starting from the tail of the |
| 219 |
// path. Once we know the type of shape, we know which |
| 220 |
// transform to modify |
| 221 |
for (j=0; j < selectedPath->getLength() && |
| 222 |
(xform == NULL); j++) { |
| 223 |
SoNode *n = (SoNode *)selectedPath->getNodeFromTail(j); if (n->isOfType(SoCube::getClassTypeId())) { |
| 224 |
xform = cubeTransform; |
| 225 |
} else if (n->isOfType(SoCone::getClassTypeId())) { |
| 226 |
xform = coneTransform; |
| 227 |
} else if (n->isOfType(SoSphere::getClassTypeId())) { |
| 228 |
xform = sphereTransform; |
| 229 |
} else if (n->isOfType(SoCylinder::getClassTypeId())) { xform = cylTransform; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
// Apply the scale |
| 234 |
scaleFactor = xform->scaleFactor.getValue(); |
| 235 |
scaleFactor *= sf; |
| 236 |
xform->scaleFactor.setValue(scaleFactor); |
| 237 |
} |
| 238 |
|
| 239 |
} |
| 240 |
|