Debian Codegraph @

apps/examples/Mentor/CXX/14.3.Balance.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 14, example 3.
40 *
41 * This example illustrates the creation of motion hierarchies
42 * using nodekits by creating a model of a balance-style scale.
43 *
44 * It adds an SoEventCallback to the "callback" list in the
45 * nodekit called 'support.'
46 * The callback will have the following response to events:
47 * Pressing right arrow key == lower the right pan
48 * Pressing left arrow key == lower the left pan
49 * The pans are lowered by animating three rotations in the
50 * motion hierarchy.
51 * Use an SoText2Kit to print instructions to the user as part
52 * of the scene.
53 *----------------------------------------------------------------*/
54
55 #include <cstdlib>
56 #include <Inventor/events/SoKeyboardEvent.h>
57 #include <Inventor/nodekits/SoCameraKit.h>
58 #include <Inventor/nodekits/SoLightKit.h>
59 #include <Inventor/nodekits/SoSceneKit.h>
60 #include <Inventor/nodekits/SoShapeKit.h>
61 #include <Inventor/nodes/SoCone.h>
62 #include <Inventor/nodes/SoCube.h>
63 #include <Inventor/nodes/SoCylinder.h>
64 #include <Inventor/nodes/SoEventCallback.h>
65 #include <Inventor/nodes/SoText2.h>
66 #include <Inventor/nodes/SoTransform.h>
67 #include <Inventor/nodes/SoPerspectiveCamera.h>
68
69 #include <Inventor/Xt/SoXt.h>
70 #include <Inventor/Xt/SoXtRenderArea.h>
71
72 #include <Inventor/actions/SoWriteAction.h>
73
74 // Callback Function for Animating the Balance Scale.
75 // -- used to make the balance tip back and forth
76 // -- Note: this routine is only called in response to KeyPress
77 // events since the call 'setEventInterest(KeyPressMask)' is
78 // made on the SoEventCallback node that uses it.
79 // -- The routine checks if the key pressed was left arrow (which
80 // is XK_Left in X-windows talk), or right arrow (which is
81 // XK_Right)
82 // -- The balance is made to tip by rotating the beam part of the
83 // scale (to tip it) and then compensating (making the strings
84 // vertical again) by rotating the string parts in the opposite
85 // direction.
86 void
87 tipTheBalance(
88 void *userData, // The nodekit representing 'support', the
89 // fulcrum of the balance. Passed in during
90 // main routine, below.
91 SoEventCallback *eventCB)
92 {
93 const SoEvent *ev = eventCB->getEvent();
94
95 // Which Key was pressed?
96 // If Right or Left Arrow key, then continue...
97 if (SO_KEY_PRESS_EVENT(ev, RIGHT_ARROW) ||
98 SO_KEY_PRESS_EVENT(ev, LEFT_ARROW)) {
99 SoShapeKit *support, *beam1, *string1, *string2;
100 SbRotation startRot, beamIncrement, stringIncrement;
101
102 // Get the different nodekits from the userData.
103 support = (SoShapeKit *) userData;
104
105 // These three parts are extracted based on knowledge of the
106 // motion hierarchy (see the diagram in the main routine.
107 beam1 = (SoShapeKit *) support->getPart("childList[0]",TRUE);
108 string1 = (SoShapeKit *) beam1->getPart("childList[0]",TRUE);
109 string2 = (SoShapeKit *) beam1->getPart("childList[1]",TRUE);
110
111 // Set angular increments to be .1 Radians about the Z-Axis
112 // The strings rotate opposite the beam, and the two types
113 // of key press produce opposite effects.
114 if (SO_KEY_PRESS_EVENT(ev, RIGHT_ARROW)) {
115 beamIncrement.setValue(SbVec3f(0, 0, 1), -.1);
116 stringIncrement.setValue(SbVec3f(0, 0, 1), .1);
117 }
118 else {
119 beamIncrement.setValue(SbVec3f(0, 0, 1), .1);
120 stringIncrement.setValue(SbVec3f(0, 0, 1), -.1);
121 }
122
123 // Use SO_GET_PART to find the transform for each of the
124 // rotating parts and modify their rotations.
125
126 SoTransform *xf;
127 xf = SO_GET_PART(beam1, "transform", SoTransform);
128 startRot = xf->rotation.getValue();
129 xf->rotation.setValue(startRot * beamIncrement);
130
131 xf = SO_GET_PART(string1, "transform", SoTransform);
132 startRot = xf->rotation.getValue();
133 xf->rotation.setValue(startRot * stringIncrement);
134
135 xf = SO_GET_PART(string2, "transform", SoTransform);
136 startRot = xf->rotation.getValue();
137 xf->rotation.setValue(startRot * stringIncrement);
138
139 eventCB->setHandled();
140 }
141 }
142
143 int
144 main(int , char **argv)
145 {
146 Widget myWindow = SoXt::init(argv[0]);
147 if (myWindow == NULL) exit(1);
148
149 SoSceneKit *myScene = new SoSceneKit;
150 myScene->ref();
151
152 myScene->setPart("lightList[0]", new SoLightKit);
153 myScene->setPart("cameraList[0]", new SoCameraKit);
154 myScene->setCameraNumber(0);
155
156 // Create the Balance Scale -- put each part in the
157 // childList of its parent, to build up this hierarchy:
158 //
159 // myScene
160 // |
161 // support
162 // |
163 // beam
164 // |
165 // --------
166 // | |
167 // string1 string2
168 // | |
169 // tray1 tray2
170
171 SoShapeKit *support = new SoShapeKit();
172 support->setPart("shape", new SoCone);
173 support->set("shape { height 3 bottomRadius .3 }");
174 myScene->setPart("childList[0]", support);
175
176 SoShapeKit *beam = new SoShapeKit();
177 beam->setPart("shape", new SoCube);
178 beam->set("shape { width 3 height .2 depth .2 }");
179 beam->set("transform { translation 0 1.5 0 } ");
180 support->setPart("childList[0]", beam);
181
182 SoShapeKit *string1 = new SoShapeKit;
183 string1->setPart("shape", new SoCylinder);
184 string1->set("shape { radius .05 height 2}");
185 string1->set("transform { translation -1.5 -1 0 }");
186 string1->set("transform { center 0 1 0 }");
187 beam->setPart("childList[0]", string1);
188
189 SoShapeKit *string2 = new SoShapeKit;
190 string2->setPart("shape", new SoCylinder);
191 string2->set("shape { radius .05 height 2}");
192 string2->set("transform { translation 1.5 -1 0 } ");
193 string2->set("transform { center 0 1 0 } ");
194 beam->setPart("childList[1]", string2);
195
196 SoShapeKit *tray1 = new SoShapeKit;
197 tray1->setPart("shape", new SoCylinder);
198 tray1->set("shape { radius .75 height .1 }");
199 tray1->set("transform { translation 0 -1 0 } ");
200 string1->setPart("childList[0]", tray1);
201
202 SoShapeKit *tray2 = new SoShapeKit;
203 tray2->setPart("shape", new SoCylinder);
204 tray2->set("shape { radius .75 height .1 }");
205 tray2->set("transform { translation 0 -1 0 } ");
206 string2->setPart("childList[0]", tray2);
207
208 // Add EventCallback so Balance Responds to Events
209 SoEventCallback *myCallbackNode = new SoEventCallback;
210 myCallbackNode->addEventCallback(
211 SoKeyboardEvent::getClassTypeId(),
212 tipTheBalance, support);
213 support->setPart("callbackList[0]", myCallbackNode);
214
215 // Add Instructions as Text in the Scene...
216 SoShapeKit *myText = new SoShapeKit;
217 myText->setPart("shape", new SoText2);
218 myText->set("shape { string \"Press Left or Right Arrow Key\" }");
219 myText->set("shape { justification CENTER }");
220 myText->set("font { name \"Helvetica-Bold\" }");
221 myText->set("font { size 16.0 }");
222 myText->set("transform { translation 0 -2 0 }");
223 myScene->setPart("childList[1]", myText);
224
225 SoXtRenderArea *myRenderArea = new SoXtRenderArea(myWindow);
226
227 // Get camera from scene and tell it to viewAll...
228 SoPerspectiveCamera *myCamera = SO_GET_PART(myScene,
229 "cameraList[0].camera", SoPerspectiveCamera);
230 myCamera->viewAll(myScene, myRenderArea->getSize());
231
232 myRenderArea->setSceneGraph(myScene);
233 myRenderArea->setTitle("Balance Scale Made of Nodekits");
234 myRenderArea->show();
235
236 SoXt::show(myWindow);
237 SoXt::mainLoop();
238 }