Debian Codegraph @

apps/examples/Mentor/CXX/13.5.Boolean.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 13, example 6.
40 *
41 * Boolean engine. Derived from example 13.5.
42 * The smaller duck stays still while the bigger duck moves,
43 * and starts moving as soon as the bigger duck stops.
44 *------------------------------------------------------------*/
45
46 #include <cstdlib>
47 #include <Inventor/SoDB.h>
48 #include <Inventor/Xt/SoXt.h>
49 #include <Inventor/Xt/SoXtRenderArea.h>
50 #include <Inventor/engines/SoBoolOperation.h>
51 #include <Inventor/engines/SoCompose.h>
52 #include <Inventor/engines/SoElapsedTime.h>
53 #include <Inventor/engines/SoGate.h>
54 #include <Inventor/events/SoMouseButtonEvent.h>
55 #include <Inventor/nodes/SoCylinder.h>
56 #include <Inventor/nodes/SoDirectionalLight.h>
57 #include <Inventor/nodes/SoEventCallback.h>
58 #include <Inventor/nodes/SoMaterial.h>
59 #include <Inventor/nodes/SoPerspectiveCamera.h>
60 #include <Inventor/nodes/SoRotationXYZ.h>
61 #include <Inventor/nodes/SoSeparator.h>
62 #include <Inventor/nodes/SoSphere.h>
63 #include <Inventor/nodes/SoTransform.h>
64 #include <Inventor/nodes/SoTranslation.h>
65
66 void myMousePressCB(void *, SoEventCallback *);
67
68 int
69 main(int , char **argv)
70 {
71 // Print out usage message
72 printf("Only one duck can move at a time.\n");
73 printf("Click the left mouse button to toggle between the two ducks.\n");
74
75 // Initialize Inventor and Xt
76 Widget myWindow = SoXt::init(argv[0]);
77 if (myWindow == NULL) exit(1);
78
79 SoSeparator *root = new SoSeparator;
80 root->ref();
81
82 // Add a camera and light
83 SoPerspectiveCamera *myCamera = new SoPerspectiveCamera;
84 myCamera->position.setValue(0., -4., 8.0);
85 myCamera->heightAngle = M_PI/2.5;
86 myCamera->nearDistance = 1.0;
87 myCamera->farDistance = 15.0;
88 root->addChild(myCamera);
89 root->addChild(new SoDirectionalLight);
90
91 // Rotate scene slightly to get better view
92 SoRotationXYZ *globalRotXYZ = new SoRotationXYZ;
93 globalRotXYZ->axis = SoRotationXYZ::X;
94 globalRotXYZ->angle = M_PI/9;
95 root->addChild(globalRotXYZ);
96
97 // Pond group
98 SoSeparator *pond = new SoSeparator;
99 root->addChild(pond);
100 SoTranslation *pondTranslation = new SoTranslation;
101 pondTranslation->translation.setValue(0., -6.725, 0.);
102 pond->addChild(pondTranslation);
103 // water
104 SoMaterial *waterMaterial = new SoMaterial;
105 waterMaterial->diffuseColor.setValue(0., 0.3, 0.8);
106 pond->addChild(waterMaterial);
107 SoCylinder *waterCylinder = new SoCylinder;
108 waterCylinder->radius.setValue(4.0);
109 waterCylinder->height.setValue(0.5);
110 pond->addChild(waterCylinder);
111 // rock
112 SoMaterial *rockMaterial = new SoMaterial;
113 rockMaterial->diffuseColor.setValue(0.8, 0.23, 0.03);
114 pond->addChild(rockMaterial);
115 SoSphere *rockSphere = new SoSphere;
116 rockSphere->radius.setValue(0.9);
117 pond->addChild(rockSphere);
118
119 // Read the duck object from a file and add to the group
120 SoInput myInput;
121 if (!myInput.openFile("/usr/share/src/Inventor/examples/data/duck.iv"))
122 exit (1);
123 SoSeparator *duckObject = SoDB::readAll(&myInput);
124 if (duckObject == NULL)
125 exit (1);
126
127 /////////////////////////////////////////////////////////////
128 // CODE FOR The Inventor Mentor STARTS HERE
129
130 // Bigger duck group
131 SoSeparator *bigDuck = new SoSeparator;
132 root->addChild(bigDuck);
133 SoRotationXYZ *bigDuckRotXYZ = new SoRotationXYZ;
134 bigDuck->addChild(bigDuckRotXYZ);
135 SoTransform *bigInitialTransform = new SoTransform;
136 bigInitialTransform->translation.setValue(0., 0., 3.5);
137 bigInitialTransform->scaleFactor.setValue(6., 6., 6.);
138 bigDuck->addChild(bigInitialTransform);
139 bigDuck->addChild(duckObject);
140
141 // Smaller duck group
142 SoSeparator *smallDuck = new SoSeparator;
143 root->addChild(smallDuck);
144 SoRotationXYZ *smallDuckRotXYZ = new SoRotationXYZ;
145 smallDuck->addChild(smallDuckRotXYZ);
146 SoTransform *smallInitialTransform = new SoTransform;
147 smallInitialTransform->translation.setValue(0., -2.24, 1.5);
148 smallInitialTransform->scaleFactor.setValue(4., 4., 4.);
149 smallDuck->addChild(smallInitialTransform);
150 smallDuck->addChild(duckObject);
151
152 // Use a gate engine to start/stop the rotation of
153 // the bigger duck.
154 SoGate *bigDuckGate = new SoGate(SoMFFloat::getClassTypeId());
155 SoElapsedTime *bigDuckTime = new SoElapsedTime;
156 bigDuckGate->input->connectFrom(&bigDuckTime->timeOut);
157 bigDuckRotXYZ->axis = SoRotationXYZ::Y; // Y axis
158 bigDuckRotXYZ->angle.connectFrom(bigDuckGate->output);
159
160 // Each mouse button press will enable/disable the gate
161 // controlling the bigger duck.
162 SoEventCallback *myEventCB = new SoEventCallback;
163 myEventCB->addEventCallback(
164 SoMouseButtonEvent::getClassTypeId(),
165 myMousePressCB, bigDuckGate);
166 root->addChild(myEventCB);
167
168 // Use a Boolean engine to make the rotation of the smaller
169 // duck depend on the bigger duck. The smaller duck moves
170 // only when the bigger duck is still.
171 SoBoolOperation *myBoolean = new SoBoolOperation;
172 myBoolean->a.connectFrom(&bigDuckGate->enable);
173 myBoolean->operation = SoBoolOperation::NOT_A;
174
175 SoGate *smallDuckGate = new SoGate(SoMFFloat::getClassTypeId());
176 SoElapsedTime *smallDuckTime = new SoElapsedTime;
177 smallDuckGate->input->connectFrom(&smallDuckTime->timeOut);
178 smallDuckGate->enable.connectFrom(&myBoolean->output);
179 smallDuckRotXYZ->axis = SoRotationXYZ::Y; // Y axis
180 smallDuckRotXYZ->angle.connectFrom(smallDuckGate->output);
181
182 // CODE FOR The Inventor Mentor ENDS HERE
183 /////////////////////////////////////////////////////////////
184
185 SoXtRenderArea *myRenderArea = new SoXtRenderArea(myWindow);
186 myRenderArea->setSceneGraph(root);
187 myRenderArea->setTitle("Duck and Duckling");
188 myRenderArea->show();
189
190 SoXt::show(myWindow);
191 SoXt::mainLoop();
192 }
193
194 // This routine is called for every mouse button event.
195 void
196 myMousePressCB(void *userData, SoEventCallback *eventCB)
197 {
198 SoGate *gate = (SoGate *) userData;
199 const SoEvent *event = eventCB->getEvent();
200
201 // Check for mouse button being pressed
202 if (SO_MOUSE_PRESS_EVENT(event, ANY)) {
203
204 // Toggle the gate that controls the duck motion
205 if (gate->enable.getValue())
206 gate->enable.setValue(FALSE);
207 else
208 gate->enable.setValue(TRUE);
209
210 eventCB->setHandled();
211 }
212 }
213