Debian Codegraph @

apps/examples/Mentor/CXX/10.2.setEventCB.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 2.
40 *
41 * This demonstrates using SoXtRenderArea::setEventCallback().
42 * which causes events to be sent directly to the application
43 * without being sent into the scene graph.
44 *
45 * Clicking the left mouse button and dragging will draw
46 * points in the xy plane beneath the mouse cursor.
47 * Clicking middle mouse and holding causes the point set
48 * to rotate about the Y axis.
49 * Clicking right mouse clears all points drawn so far out
50 * of the point set.
51 *-----------------------------------------------------------*/
52
53 #include <cstdlib>
54 #include <X11/Intrinsic.h>
55
56 #include <Inventor/Sb.h>
57 #include <Inventor/Xt/SoXtRenderArea.h>
58 #include <Inventor/Xt/SoXt.h>
59 #include <Inventor/nodes/SoCamera.h>
60 #include <Inventor/nodes/SoCoordinate3.h>
61 #include <Inventor/nodes/SoGroup.h>
62 #include <Inventor/nodes/SoLightModel.h>
63 #include <Inventor/nodes/SoPerspectiveCamera.h>
64 #include <Inventor/nodes/SoPointSet.h>
65 #include <Inventor/nodes/SoSeparator.h>
66 #include <Inventor/sensors/SoTimerSensor.h>
67
68 // Timer sensor
69 // Rotate 90 degrees every second, update 30 times a second
70 SoTimerSensor *myTicker;
71 #define UPDATE_RATE 1.0/30.0
72 #define ROTATION_ANGLE M_PI/60.0
73
74 void
75 myProjectPoint(SoXtRenderArea *myRenderArea,
76 int mousex, int mousey, SbVec3f &intersection)
77 {
78 // Take the x,y position of mouse, and normalize to [0,1].
79 // X windows have 0,0 at the upper left,
80 // Inventor expects 0,0 to be the lower left.
81 SbVec2s size = myRenderArea->getSize();
82 float x = float(mousex) / size[0];
83 float y = float(size[1] - mousey) / size[1];
84
85 // Get the camera and view volume
86 SoGroup *root = (SoGroup *) myRenderArea->getSceneGraph();
87 SoCamera *myCamera = (SoCamera *) root->getChild(0);
88 SbViewVolume myViewVolume;
89 myViewVolume = myCamera->getViewVolume();
90
91 // Project the mouse point to a line
92 SbVec3f p0, p1;
93 myViewVolume.projectPointToLine(SbVec2f(x,y), p0, p1);
94
95 // Midpoint of the line intersects a plane thru the origin
96 intersection = (p0 + p1) / 2.0;
97 }
98
99 void
100 myAddPoint(SoXtRenderArea *myRenderArea, const SbVec3f point)
101 {
102 SoGroup *root = (SoGroup *) myRenderArea->getSceneGraph();
103 SoCoordinate3 *coord = (SoCoordinate3 *) root->getChild(2);
104 SoPointSet *myPointSet = (SoPointSet *) root->getChild(3);
105
106 coord->point.set1Value(coord->point.getNum(), point);
107 myPointSet->numPoints.setValue(coord->point.getNum());
108 }
109
110 void
111 myClearPoints(SoXtRenderArea *myRenderArea)
112 {
113 SoGroup *root = (SoGroup *) myRenderArea->getSceneGraph();
114 SoCoordinate3 *coord = (SoCoordinate3 *) root->getChild(2);
115 SoPointSet *myPointSet = (SoPointSet *) root->getChild(3);
116
117 // Delete all values starting from 0
118 coord->point.deleteValues(0);
119 myPointSet->numPoints.setValue(0);
120 }
121
122 void
123 tickerCallback(void *userData, SoSensor *)
124 {
125 SoCamera *myCamera = (SoCamera *) userData;
126 SbRotation rot;
127 SbMatrix mtx;
128 SbVec3f pos;
129
130 // Adjust the position
131 pos = myCamera->position.getValue();
132 rot = SbRotation(SbVec3f(0,1,0), ROTATION_ANGLE);
133 mtx.setRotate(rot);
134 mtx.multVecMatrix(pos, pos);
135 myCamera->position.setValue(pos);
136
137 // Adjust the orientation
138 myCamera->orientation.setValue(
139 myCamera->orientation.getValue() * rot);
140 }
141
142 ///////////////////////////////////////////////////////////////
143 // CODE FOR The Inventor Mentor STARTS HERE (part 1)
144
145 SbBool
146 myAppEventHandler(void *userData, XAnyEvent *anyevent)
147 {
148 SoXtRenderArea *myRenderArea = (SoXtRenderArea *) userData;
149 XButtonEvent *myButtonEvent;
150 XMotionEvent *myMotionEvent;
151 SbVec3f vec;
152 SbBool handled = TRUE;
153
154 switch (anyevent->type) {
155
156 case ButtonPress:
157 myButtonEvent = (XButtonEvent *) anyevent;
158 if (myButtonEvent->button == Button1) {
159 myProjectPoint(myRenderArea,
160 myButtonEvent->x, myButtonEvent->y, vec);
161 myAddPoint(myRenderArea, vec);
162 } else if (myButtonEvent->button == Button2) {
163 myTicker->schedule(); // start spinning the camera
164 } else if (myButtonEvent->button == Button3) {
165 myClearPoints(myRenderArea); // clear the point set
166 }
167 break;
168
169 case ButtonRelease:
170 myButtonEvent = (XButtonEvent *) anyevent;
171 if (myButtonEvent->button == Button2) {
172 myTicker->unschedule(); // stop spinning the camera
173 }
174 break;
175
176 case MotionNotify:
177 myMotionEvent = (XMotionEvent *) anyevent;
178 if (myMotionEvent->state & Button1Mask) {
179 myProjectPoint(myRenderArea,
180 myMotionEvent->x, myMotionEvent->y, vec);
181 myAddPoint(myRenderArea, vec);
182 }
183 break;
184
185 default:
186 handled = FALSE;
187 break;
188 }
189
190 return handled;
191 }
192
193 // CODE FOR The Inventor Mentor ENDS HERE
194 ////////////////////////////////////////////////////////////
195
196 int
197 main(int argc, char **argv)
198 {
199 // Print out usage instructions
200 printf("Mouse buttons:\n");
201 printf("\tLeft (with mouse motion): adds points\n");
202 printf("\tMiddle: rotates points about the Y axis\n");
203 printf("\tRight: deletes all the points\n");
204
205 // Initialize Inventor and Xt
206 Widget appWindow = SoXt::init(argv[0]);
207 if (appWindow == NULL) exit(1);
208
209 // Create and set up the root node
210 SoSeparator *root = new SoSeparator;
211 root->ref();
212
213 // Add a camera
214 SoPerspectiveCamera *myCamera = new SoPerspectiveCamera;
215 root->addChild(myCamera); // child 0
216
217 // Use the base color light model so we don't need to
218 // specify normals
219 SoLightModel *myLightModel = new SoLightModel;
220 myLightModel->model = SoLightModel::BASE_COLOR;
221 root->addChild(myLightModel); // child 1
222
223 // Set up the camera view volume
224 myCamera->position.setValue(0, 0, 4);
225 myCamera->nearDistance.setValue(1.0);
226 myCamera->farDistance.setValue(7.0);
227 myCamera->heightAngle.setValue(M_PI/3.0);
228
229 // Add a coordinate and point set
230 SoCoordinate3 *myCoord = new SoCoordinate3;
231 SoPointSet *myPointSet = new SoPointSet;
232 root->addChild(myCoord); // child 2
233 root->addChild(myPointSet); // child 3
234
235 // Timer sensor to tick off time while middle mouse is down
236 myTicker = new SoTimerSensor(tickerCallback, myCamera);
237 myTicker->setInterval(UPDATE_RATE);
238
239 // Create a render area for viewing the scene
240 SoXtRenderArea *myRenderArea = new SoXtRenderArea(appWindow);
241 myRenderArea->setSceneGraph(root);
242 myRenderArea->setTitle("My Event Handler");
243
244 //////////////////////////////////////////////////////////////
245 // CODE FOR The Inventor Mentor STARTS HERE (part 2)
246
247 // Have render area send events to us instead of the scene
248 // graph. We pass the render area as user data.
249 myRenderArea->setEventCallback(
250 myAppEventHandler, myRenderArea);
251
252 // CODE FOR The Inventor Mentor ENDS HERE
253 //////////////////////////////////////////////////////////////
254
255 // Show our application window, and loop forever...
256 myRenderArea->show();
257 SoXt::show(appWindow);
258 SoXt::mainLoop();
259 }
260