Debian Codegraph @

apps/demos/revo/profile.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 // Some code for setting up and dealing with the profile window.
39 //
40
41 #include <cstdio>
42 #include <unistd.h>
43 #include <cmath>
44 #include <cstdlib>
45
46 #include <Inventor/Xt/SoXt.h>
47 #include <Inventor/SoDB.h>
48 #include <Inventor/nodes/SoCoordinate3.h>
49 #include <Inventor/nodes/SoLightModel.h>
50 #include <Inventor/nodes/SoMaterial.h>
51 #include <Inventor/nodes/SoOrthographicCamera.h>
52 #include <Inventor/nodes/SoSeparator.h>
53 #include <Inventor/sensors/SoNodeSensor.h>
54 #include <Inventor/sensors/SoSensor.h>
55
56 #include <Xm/Text.h>
57
58 #include "LineManip.h"
59 #include "Useful.h"
60 #include "RevClass.h"
61
62 //
63 // The line manipulator.
64 //
65 static LineManip2 *TheLineManip;
66
67 //
68 // The default coordinates for the line manipulator.
69 //
70 static float defaultCoords[2][3] =
71 {
72 { 0.5, 0.2, 0.0 },
73 { 0.5, -0.2, 0.0 },
74 };
75
76 //
77 // Grid for window:
78 //
79 static char *GridString =
80 "#Inventor V2.0 ascii\n"
81 "Separator {"
82 " PickStyle { style UNPICKABLE }"
83 " LightModel { model BASE_COLOR }"
84 " BaseColor { rgb 0.2 0.2 0.2 }"
85 " Array {"
86 " numElements1 19"
87 " separation1 .1 0 0"
88 " origin CENTER"
89 " Coordinate3 { point [ 0 -1 0, 0 1 0 ] }"
90 " LineSet { numVertices [ 2 ] }"
91 " }"
92 " Array {"
93 " numElements1 19"
94 " separation1 0 .1 0"
95 " origin CENTER"
96 " Coordinate3 { point [ -1 0 0, 1 0 0 ] }"
97 " LineSet { numVertices [ 2 ] }"
98 " }"
99 " BaseColor { rgb 0.4 0.0 0.0 }"
100 " Coordinate3 { point [ -1.0 0.0 0.0, 1.0 0.0 0.0,"
101 " 0.0 -1.0 0.0, 0.0 1.0 0.0 ]"
102 " }"
103 " LineSet { numVertices [ 2, 2 ] }"
104 "}";
105
106 //
107 // This is called when the mouse leaves the profile window. It turns
108 // off the line manip's hilights.
109 //
110 static void
111 leaveProfileWindow(Widget, XtPointer mydata, XEvent *, Boolean *cont)
112 {
113 ((LineManip2 *)mydata)->removeHilights();
114
115 *cont = TRUE; // Continue dispatching this event
116 }
117
118 //
119 // Called to get rid of all the points added so far.
120 //
121 void
122 clearPoints()
123 {
124 SoCoordinate3 *coord = TheLineManip->getCoordinate3();
125 coord->point.deleteValues(0);
126 }
127
128 //
129 // Called whenever the profile is edited. It tells the surface to
130 // recalculate itself. Note that this could be more efficient by
131 // noticing exactly what changed and just recomputing what is
132 // necessary (e.g. when adding a point to the end, only two sets of
133 // normals needs to be generated, etc).
134 //
135 static void
136 profileChangedCallback(void *data, SoSensor *)
137 {
138 RevolutionSurface *s = (RevolutionSurface *)data;
139
140 SoCoordinate3 *coord = TheLineManip->getCoordinate3();
141 s->createSurface(&coord->point);
142 }
143
144 //
145 // This creates the profile's scene graph and sets up the callback so
146 // the line manipulator turns off its hilights when the mouse leaves
147 // the profile window.
148 //
149 SoNode *
150 createProfileGraph(Widget profileWidget, RevolutionSurface *surface)
151 {
152 SoSeparator *result = new SoSeparator;
153 result->ref();
154
155 SoOrthographicCamera *c = new SoOrthographicCamera;
156 result->addChild(c);
157 c->position.setValue(0.5, 0.0, 1.0);
158 c->nearDistance = 0.5;
159 c->farDistance = 1.5;
160 c->focalDistance = 1.0;
161 c->height = 1.0;
162
163 // Axes and labels (appear underneath the line manipulator)
164 SoInput in;
165 in.setBuffer(GridString, strlen(GridString));
166 SoNode *node;
167 SoDB::read(&in, node);
168 SoSeparator *g1 = (SoSeparator *)node;
169 if (g1 != NULL)
170 {
171 result->addChild(g1);
172 g1->renderCaching = SoSeparator::ON;
173 g1->boundingBoxCaching = SoSeparator::ON;
174 }
175
176 SoSeparator *manipGroup = new SoSeparator;
177 result->addChild(manipGroup);
178
179 SoLightModel *lm = new SoLightModel;
180 manipGroup->addChild(lm);
181 lm->model = SoLightModel::BASE_COLOR;
182
183 SoMaterial *m = new SoMaterial;
184 m->diffuseColor.setValue(0.0, 0.8, 0.8);
185 manipGroup->addChild(m);
186
187 TheLineManip = new LineManip2;
188 TheLineManip->setHilightSize(0.02);
189 manipGroup->addChild(TheLineManip);
190 SoCoordinate3 *coord = TheLineManip->getCoordinate3();
191 SoNodeSensor *d = new SoNodeSensor(profileChangedCallback, surface);
192 d->attach(coord);
193 coord->point.setValues(0, 2, defaultCoords);
194
195 //
196 // Make sure surface of revolution and line manip both agree on
197 // the default coordinates
198 //
199 surface->createSurface(&coord->point);
200
201 XtAddEventHandler(profileWidget, LeaveWindowMask,
202 FALSE, leaveProfileWindow, (XtPointer)TheLineManip);
203
204 result->unrefNoDelete();
205
206 return result;
207 }
208