Debian Codegraph @

apps/examples/Mentor/CXX/15.3.AttachManip.c++

raw
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 * volume 1, chapter 15, example 3.
40 *
41 * Manipulator attachment example.
42 *
43 * The scene graph has an SoWrapperKit, a cube and a sphere.
44 * A file containing a luxo lamp is read in as the 'contents'
45 * part of the SoWrapperKit.
46 * When the user picks on the SoWrapperKit (lamp), the kit's
47 * "transform" part is replaced with an SoTransformBoxManip.
48 * Alternatively, when the user picks the sphere, the
49 * sphere's associated transform is replaced by an
50 * SoHandleBoxManip. Picking the cube causes an
51 * SoTrackballManip to replace the cube's transform.
52 *
53 * Manipulator callbacks are used to change
54 * the color of the object being manipulated.
55 *
56 * Note that for illustration purposes, the
57 * cube and SoWrapperKit already have transform nodes
58 * associated with them; the sphere does not. In all cases,
59 * the routine createTransformPath() is used to find the
60 * transform node that affects the picked object.
61 *-----------------------------------------------------------*/
62
63
64 #include <cstdlib>
65 #include <Inventor/SoDB.h>
66 #include <Inventor/SoInput.h>
67 #include <Inventor/manips/SoHandleBoxManip.h>
68 #include <Inventor/manips/SoTrackballManip.h>
69 #include <Inventor/manips/SoTransformBoxManip.h>
70 #include <Inventor/nodekits/SoWrapperKit.h>
71 #include <Inventor/nodes/SoCamera.h>
72 #include <Inventor/nodes/SoCube.h>
73 #include <Inventor/nodes/SoGroup.h>
74 #include <Inventor/nodes/SoLight.h>
75 #include <Inventor/nodes/SoMaterial.h>
76 #include <Inventor/nodes/SoSelection.h>
77 #include <Inventor/nodes/SoSphere.h>
78 #include <Inventor/nodes/SoTransform.h>
79
80 #include <Inventor/Xt/SoXt.h>
81 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
82
83 // function prototypes
84 void selectionCallback(void *, SoPath *);
85 void deselectionCallback(void *, SoPath *);
86 void dragStartCallback(void *, SoDragger *);
87 void dragFinishCallback(void *, SoDragger *);
88
89 // global data
90 SoSeparator *root;
91 SoHandleBoxManip *myHandleBox;
92 SoTrackballManip *myTrackball;
93 SoTransformBoxManip *myTransformBox;
94 SoPath *handleBoxPath = NULL;
95 SoPath *trackballPath = NULL;
96 SoPath *transformBoxPath = NULL;
97
98 int
99 main(int, char **argv)
100 {
101 // Initialize Inventor and Xt
102 Widget myWindow = SoXt::init(argv[0]);
103 if (myWindow == NULL) exit(1);
104
105 // create and set up the selection node
106 SoSelection *selectionRoot = new SoSelection;
107 selectionRoot->ref();
108 selectionRoot->
109 addSelectionCallback(selectionCallback, NULL);
110 selectionRoot->
111 addDeselectionCallback(deselectionCallback, NULL);
112
113 // create the scene graph
114 root = new SoSeparator;
115 selectionRoot->addChild(root);
116
117 // Read a file into contents of SoWrapperKit
118 // Translate it to the right.
119 SoWrapperKit *myWrapperKit = new SoWrapperKit;
120 root->addChild(myWrapperKit);
121 SoInput myInput;
122 if (!myInput.openFile("/usr/share/src/Inventor/examples/data/luxo.iv"))
123 exit (1);
124 SoSeparator *objectFromFile = SoDB::readAll(&myInput);
125 if (objectFromFile == NULL)
126 exit (1);
127 myWrapperKit->setPart("contents",objectFromFile);
128 myWrapperKit->set("transform { translation 3 -1 0 }");
129 SoMaterial *wrapperMat
130 = (SoMaterial *) myWrapperKit->getPart("material",TRUE);
131 wrapperMat->diffuseColor.setValue(.8, .8, .8);
132
133 // Create a cube with its own transform.
134 SoSeparator *cubeRoot = new SoSeparator;
135 SoTransform *cubeXform = new SoTransform;
136 cubeXform->translation.setValue(-4, 0, 0);
137 root->addChild(cubeRoot);
138 cubeRoot->addChild(cubeXform);
139
140 SoMaterial *cubeMat = new SoMaterial;
141 cubeMat->diffuseColor.setValue(.8, .8, .8);
142 cubeRoot->addChild(cubeMat);
143 cubeRoot->addChild(new SoCube);
144
145 // add a sphere node without a transform
146 // (one will be added when we attach the manipulator)
147 SoSeparator *sphereRoot = new SoSeparator;
148 SoMaterial *sphereMat = new SoMaterial;
149 root->addChild(sphereRoot);
150 sphereRoot->addChild(sphereMat);
151 sphereRoot->addChild(new SoSphere);
152 sphereMat->diffuseColor.setValue(.8, .8, .8);
153
154 // create the manipulators
155 myHandleBox = new SoHandleBoxManip;
156 myHandleBox->ref();
157 myTrackball = new SoTrackballManip;
158 myTrackball->ref();
159 myTransformBox = new SoTransformBoxManip;
160 myTransformBox->ref();
161
162 // Get the draggers and add callbacks to them. Note
163 // that you don't put callbacks on manipulators. You put
164 // them on the draggers which handle events for them.
165 SoDragger *myDragger;
166 myDragger = myTrackball->getDragger();
167 myDragger->addStartCallback(dragStartCallback,cubeMat);
168 myDragger->addFinishCallback(dragFinishCallback,cubeMat);
169
170 myDragger = myHandleBox->getDragger();
171 myDragger->addStartCallback(dragStartCallback,sphereMat);
172 myDragger->addFinishCallback(dragFinishCallback,sphereMat);
173
174 myDragger = myTransformBox->getDragger();
175 myDragger->addStartCallback(dragStartCallback,wrapperMat);
176 myDragger->addFinishCallback(dragFinishCallback,wrapperMat);
177
178 SoXtExaminerViewer *myViewer
179 = new SoXtExaminerViewer(myWindow);
180 myViewer->setSceneGraph(selectionRoot);
181 myViewer->setTitle("Attaching Manipulators");
182 myViewer->show();
183 myViewer->viewAll();
184
185 SoXt::show(myWindow);
186 SoXt::mainLoop();
187 }
188
189 // Is this node of a type that is influenced by transforms?
190 SbBool
191 isTransformable(SoNode *myNode)
192 {
193 if (myNode->isOfType(SoGroup::getClassTypeId())
194 || myNode->isOfType(SoShape::getClassTypeId())
195 || myNode->isOfType(SoCamera::getClassTypeId())
196 || myNode->isOfType(SoLight::getClassTypeId()))
197 return TRUE;
198 else
199 return FALSE;
200 }
201
202 // Create a path to the transform node that affects the tail
203 // of the input path. Three possible cases:
204 // [1] The path-tail is a node kit. Just ask the node kit for
205 // a path to the part called "transform"
206 // [2] The path-tail is NOT a group. Search siblings of path
207 // tail from right to left until you find a transform. If
208 // none is found, or if another transformable object is
209 // found (shape,group,light,or camera), then insert a
210 // transform just to the left of the tail. This way, the
211 // manipulator only effects the selected object.
212 // [3] The path-tail IS a group. Search its children left to
213 // right until a transform is found. If a transformable
214 // node is found first, insert a transform just left of
215 // that node. This way the manip will affect all nodes
216 // in the group.
217 SoPath *
218 createTransformPath(SoPath *inputPath)
219 {
220 int pathLength = inputPath->getLength();
221 if (pathLength < 2) // Won't be able to get parent of tail
222 return NULL;
223
224 SoNode *tail = inputPath->getTail();
225
226 // CASE 1: The tail is a node kit.
227 // Nodekits have built in policy for creating parts.
228 // The kit copies inputPath, then extends it past the
229 // kit all the way down to the transform. It creates the
230 // transform if necessary.
231 if (tail->isOfType(SoBaseKit::getClassTypeId())) {
232 SoBaseKit *kit = (SoBaseKit *) tail;
233 return kit->createPathToPart("transform",TRUE,inputPath);
234 }
235
236 SoTransform *editXf = NULL;
237 SoGroup *parent;
238 SbBool existedBefore = FALSE;
239
240 // CASE 2: The tail is not a group.
241 SbBool isTailGroup;
242 isTailGroup = tail->isOfType(SoGroup::getClassTypeId());
243 if (!isTailGroup) {
244 // 'parent' is node above tail. Search under parent right
245 // to left for a transform. If we find a 'movable' node
246 // insert a transform just left of tail.
247 parent = (SoGroup *) inputPath->getNode(pathLength - 2);
248 int tailIndx = parent->findChild(tail);
249
250 for (int i = tailIndx; (i >= 0) && (editXf == NULL);i--){
251 SoNode *myNode = parent->getChild(i);
252 if (myNode->isOfType(SoTransform::getClassTypeId()))
253 editXf = (SoTransform *) myNode;
254 else if (i != tailIndx && (isTransformable(myNode)))
255 break;
256 }
257 if (editXf == NULL) {
258 existedBefore = FALSE;
259 editXf = new SoTransform;
260 parent->insertChild(editXf, tailIndx);
261 }
262 else
263 existedBefore = TRUE;
264 }
265 // CASE 3: The tail is a group.
266 else {
267 // Search the children from left to right for transform
268 // nodes. Stop the search if we come to a movable node.
269 // and insert a transform before it.
270 parent = (SoGroup *) tail;
271 int i;
272 for (i = 0;
273 (i < parent->getNumChildren()) && (editXf == NULL);
274 i++) {
275 SoNode *myNode = parent->getChild(i);
276 if (myNode->isOfType(SoTransform::getClassTypeId()))
277 editXf = (SoTransform *) myNode;
278 else if (isTransformable(myNode))
279 break;
280 }
281 if (editXf == NULL) {
282 existedBefore = FALSE;
283 editXf = new SoTransform;
284 parent->insertChild(editXf, i);
285 }
286 else
287 existedBefore = TRUE;
288 }
289
290 // Create 'pathToXform.' Copy inputPath, then make last
291 // node be editXf.
292 SoPath *pathToXform = NULL;
293 pathToXform = inputPath->copy();
294 pathToXform->ref();
295 if (!isTailGroup) // pop off the last entry.
296 pathToXform->pop();
297 // add editXf to the end
298 int xfIndex = parent->findChild(editXf);
299 pathToXform->append(xfIndex);
300 pathToXform->unrefNoDelete();
301
302 return(pathToXform);
303 }
304
305 // This routine is called when an object
306 // gets selected. We determine which object
307 // was selected, then call replaceNode()
308 // to replace the object's transform with
309 // a manipulator.
310 void
311 selectionCallback(
312 void *, // user data is not used
313 SoPath *selectionPath)
314 {
315 // Attach the manipulator.
316 // Use the convenience routine to get a path to
317 // the transform that effects the selected object.
318 SoPath *xformPath = createTransformPath(selectionPath);
319 if (xformPath == NULL) return;
320 xformPath->ref();
321
322 // Attach the handle box to the sphere,
323 // the trackball to the cube
324 // or the transformBox to the wrapperKit
325 if (selectionPath->getTail()->isOfType(
326 SoSphere::getClassTypeId())) {
327 handleBoxPath = xformPath;
328 myHandleBox->replaceNode(xformPath);
329 }
330 else if (selectionPath->getTail()->
331 isOfType(SoCube::getClassTypeId())) {
332 trackballPath = xformPath;
333 myTrackball->replaceNode(xformPath);
334 }
335 else if (selectionPath->getTail()->
336 isOfType(SoWrapperKit::getClassTypeId())) {
337 transformBoxPath = xformPath;
338 myTransformBox->replaceNode(xformPath);
339 }
340 }
341
342 // This routine is called whenever an object gets
343 // deselected. It detaches the manipulator from
344 // the transform node, and removes it from the
345 // scene graph that will not be visible.
346 void
347 deselectionCallback(
348 void *, // user data is not used
349 SoPath *deselectionPath)
350 {
351 if (deselectionPath->getTail()->
352 isOfType(SoSphere::getClassTypeId())) {
353 myHandleBox->replaceManip(handleBoxPath,NULL);
354 handleBoxPath->unref();
355 }
356 else if (deselectionPath->getTail()->
357 isOfType(SoCube::getClassTypeId())) {
358 myTrackball->replaceManip(trackballPath,NULL);
359 trackballPath->unref();
360 }
361 else if (deselectionPath->getTail()->
362 isOfType(SoWrapperKit::getClassTypeId())) {
363 myTransformBox->replaceManip(transformBoxPath,NULL);
364 transformBoxPath->unref();
365 }
366 }
367
368 // This is called when a manipulator is
369 // about to begin manipulation.
370 void
371 dragStartCallback(
372 void *myMaterial, // user data
373 SoDragger *) // callback data not used
374 {
375 ((SoMaterial *) myMaterial)->diffuseColor=SbColor(1,.2,.2);
376 }
377
378 // This is called when a manipulator is
379 // done manipulating.
380 void
381 dragFinishCallback(
382 void *myMaterial, // user data
383 SoDragger *) // callback data not used
384 {
385 ((SoMaterial *) myMaterial)->diffuseColor=SbColor(.8,.8,.8);
386 }