Debian Codegraph @

apps/examples/Mentor/CXX/10.5.SelectionCB.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 * chapter 10, example 5.
40 *
41 * The scene graph has a sphere and a text 3D object.
42 * A selection node is placed at the top of the scene graph.
43 * When an object is selected, a selection callback is called
44 * to change the material color of that object.
45 *------------------------------------------------------------*/
46
47 #include <cstdlib>
48 #include <X11/Intrinsic.h>
49 #include <Inventor/Sb.h>
50 #include <Inventor/SoInput.h>
51 #include <Inventor/Xt/SoXt.h>
52 #include <Inventor/Xt/SoXtRenderArea.h>
53 #include <Inventor/nodes/SoDirectionalLight.h>
54 #include <Inventor/nodes/SoMaterial.h>
55 #include <Inventor/nodes/SoPerspectiveCamera.h>
56 #include <Inventor/nodes/SoPickStyle.h>
57 #include <Inventor/nodes/SoSelection.h>
58 #include <Inventor/nodes/SoSphere.h>
59 #include <Inventor/nodes/SoText3.h>
60 #include <Inventor/nodes/SoTransform.h>
61
62 // global data
63 SoMaterial *textMaterial, *sphereMaterial;
64 static float reddish[] = {1.0, 0.2, 0.2}; // Color when selected
65 static float white[] = {0.8, 0.8, 0.8}; // Color when not selected
66
67 // This routine is called when an object gets selected.
68 // We determine which object was selected, and change
69 // that objects material color.
70 void
71 mySelectionCB(void *, SoPath *selectionPath)
72 {
73 if (selectionPath->getTail()->
74 isOfType(SoText3::getClassTypeId())) {
75 textMaterial->diffuseColor.setValue(reddish);
76 } else if (selectionPath->getTail()->
77 isOfType(SoSphere::getClassTypeId())) {
78 sphereMaterial->diffuseColor.setValue(reddish);
79 }
80 }
81
82 // This routine is called whenever an object gets deselected.
83 // We determine which object was deselected, and reset
84 // that objects material color.
85 void
86 myDeselectionCB(void *, SoPath *deselectionPath)
87 {
88 if (deselectionPath->getTail()->
89 isOfType(SoText3::getClassTypeId())) {
90 textMaterial->diffuseColor.setValue(white);
91 } else if (deselectionPath->getTail()->
92 isOfType(SoSphere::getClassTypeId())) {
93 sphereMaterial->diffuseColor.setValue(white);
94 }
95 }
96
97
98 int
99 main(int argc, 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->policy = SoSelection::SINGLE;
109 selectionRoot-> addSelectionCallback(mySelectionCB);
110 selectionRoot-> addDeselectionCallback(myDeselectionCB);
111
112 // Create the scene graph
113 SoSeparator *root = new SoSeparator;
114 selectionRoot->addChild(root);
115
116 SoPerspectiveCamera *myCamera = new SoPerspectiveCamera;
117 root->addChild(myCamera);
118 root->addChild(new SoDirectionalLight);
119
120 // Add a sphere node
121 SoSeparator *sphereRoot = new SoSeparator;
122 SoTransform *sphereTransform = new SoTransform;
123 sphereTransform->translation.setValue(17., 17., 0.);
124 sphereTransform->scaleFactor.setValue(8., 8., 8.);
125 sphereRoot->addChild(sphereTransform);
126
127 sphereMaterial = new SoMaterial;
128 sphereMaterial->diffuseColor.setValue(.8, .8, .8);
129 sphereRoot->addChild(sphereMaterial);
130 sphereRoot->addChild(new SoSphere);
131 root->addChild(sphereRoot);
132
133 // Add a text node
134 SoSeparator *textRoot = new SoSeparator;
135 SoTransform *textTransform = new SoTransform;
136 textTransform->translation.setValue(0., -1., 0.);
137 textRoot->addChild(textTransform);
138
139 textMaterial = new SoMaterial;
140 textMaterial->diffuseColor.setValue(.8, .8, .8);
141 textRoot->addChild(textMaterial);
142 SoPickStyle *textPickStyle = new SoPickStyle;
143 textPickStyle->style.setValue(SoPickStyle::BOUNDING_BOX);
144 textRoot->addChild(textPickStyle);
145 SoText3 *myText = new SoText3;
146 myText->string = "rhubarb";
147 textRoot->addChild(myText);
148 root->addChild(textRoot);
149
150 SoXtRenderArea *myRenderArea = new SoXtRenderArea(myWindow);
151 myRenderArea->setSceneGraph(selectionRoot);
152 myRenderArea->setTitle("My Selection Callback");
153 myRenderArea->show();
154
155 // Make the camera see the whole scene
156 const SbViewportRegion myViewport =
157 myRenderArea->getViewportRegion();
158 myCamera->viewAll(root, myViewport, 2.0);
159
160 SoXt::show(myWindow);
161 SoXt::mainLoop();
162 }
163