Debian Codegraph @

apps/examples/Mentor/CXX/13.4.Gate.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 5.
40 *
41 * Gate engine.
42 * Mouse button presses enable and disable a gate engine.
43 * The gate engine controls an elapsed time engine that
44 * controls the motion of the duck.
45 *------------------------------------------------------------*/
46
47 #include <cstdlib>
48 #include <Inventor/SoDB.h>
49 #include <Inventor/Xt/SoXt.h>
50 #include <Inventor/Xt/SoXtRenderArea.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/SoTransform.h>
63 #include <Inventor/nodes/SoTranslation.h>
64
65 void myMousePressCB(void *, SoEventCallback *);
66
67 int
68 main(int , char **argv)
69 {
70 // Print out usage message
71 printf("Click the left mouse button to enable/disable the duck motion\n");
72
73 // Initialize Inventor and Xt
74 Widget myWindow = SoXt::init(argv[0]);
75 if (myWindow == NULL) exit(1);
76
77 SoSeparator *root = new SoSeparator;
78 root->ref();
79
80 // Add a camera and light
81 SoPerspectiveCamera *myCamera = new SoPerspectiveCamera;
82 myCamera->position.setValue(0., -4., 8.0);
83 myCamera->heightAngle = M_PI/2.5;
84 myCamera->nearDistance = 1.0;
85 myCamera->farDistance = 15.0;
86 root->addChild(myCamera);
87 root->addChild(new SoDirectionalLight);
88
89 // Rotate scene slightly to get better view
90 SoRotationXYZ *globalRotXYZ = new SoRotationXYZ;
91 globalRotXYZ->axis = SoRotationXYZ::X;
92 globalRotXYZ->angle = M_PI/9;
93 root->addChild(globalRotXYZ);
94
95 // Pond group
96 SoSeparator *pond = new SoSeparator;
97 root->addChild(pond);
98 SoMaterial *cylMaterial = new SoMaterial;
99 cylMaterial->diffuseColor.setValue(0., 0.3, 0.8);
100 pond->addChild(cylMaterial);
101 SoTranslation *cylTranslation = new SoTranslation;
102 cylTranslation->translation.setValue(0., -6.725, 0.);
103 pond->addChild(cylTranslation);
104 SoCylinder *myCylinder = new SoCylinder;
105 myCylinder->radius.setValue(4.0);
106 myCylinder->height.setValue(0.5);
107 pond->addChild(myCylinder);
108
109 /////////////////////////////////////////////////////////////
110 // CODE FOR The Inventor Mentor STARTS HERE (part 1)
111
112 // Duck group
113 SoSeparator *duck = new SoSeparator;
114 root->addChild(duck);
115
116 // Read the duck object from a file and add to the group
117 SoInput myInput;
118 if (!myInput.openFile("/usr/share/src/Inventor/examples/data/duck.iv"))
119 exit (1);
120 SoSeparator *duckObject = SoDB::readAll(&myInput);
121 if (duckObject == NULL)
122 exit (1);
123
124 // Set up the duck transformations
125 SoRotationXYZ *duckRotXYZ = new SoRotationXYZ;
126 duck->addChild(duckRotXYZ);
127 SoTransform *initialTransform = new SoTransform;
128 initialTransform->translation.setValue(0., 0., 3.);
129 initialTransform->scaleFactor.setValue(6., 6., 6.);
130 duck->addChild(initialTransform);
131
132 duck->addChild(duckObject);
133
134 // Update the rotation value if the gate is enabled.
135 SoGate *myGate = new SoGate(SoMFFloat::getClassTypeId());
136 SoElapsedTime *myCounter = new SoElapsedTime;
137 myGate->input->connectFrom(&myCounter->timeOut);
138 duckRotXYZ->axis = SoRotationXYZ::Y; // rotate about Y axis
139 duckRotXYZ->angle.connectFrom(myGate->output);
140
141 // Add an event callback to catch mouse button presses.
142 // Each button press will enable or disable the duck motion.
143 SoEventCallback *myEventCB = new SoEventCallback;
144 myEventCB->addEventCallback(
145 SoMouseButtonEvent::getClassTypeId(),
146 myMousePressCB, myGate);
147 root->addChild(myEventCB);
148
149 // CODE FOR The Inventor Mentor ENDS HERE
150 /////////////////////////////////////////////////////////////
151
152 SoXtRenderArea *myRenderArea = new SoXtRenderArea(myWindow);
153 myRenderArea->setSceneGraph(root);
154 myRenderArea->setTitle("Duck Pond");
155 myRenderArea->show();
156
157 SoXt::show(myWindow);
158 SoXt::mainLoop();
159 }
160
161 /////////////////////////////////////////////////////////////
162 // CODE FOR The Inventor Mentor STARTS HERE (part 2)
163
164 // This routine is called for every mouse button event.
165 void
166 myMousePressCB(void *userData, SoEventCallback *eventCB)
167 {
168 SoGate *gate = (SoGate *) userData;
169 const SoEvent *event = eventCB->getEvent();
170
171 // Check for mouse button being pressed
172 if (SO_MOUSE_PRESS_EVENT(event, ANY)) {
173
174 // Toggle the gate that controls the duck motion
175 if (gate->enable.getValue())
176 gate->enable.setValue(FALSE);
177 else
178 gate->enable.setValue(TRUE);
179
180 eventCB->setHandled();
181 }
182 }
183
184 // CODE FOR The Inventor Mentor ENDS HERE
185 /////////////////////////////////////////////////////////////