Debian Codegraph @

apps/examples/Mentor/CXX/08.2.UniCurve.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 8, example 2.
40 *
41 * This example creates and displays a Uniform B-Spline curve
42 * that passes through the end control points.
43 * The curve is order 4 with 13 control points and a knot
44 * vector of length 17. The end knots have multiplicity
45 * 4 to illustrate the curve passing through the endpoints.
46 *--------------------------------------------------------*/
47
48 #include <cstdlib>
49 #include <Inventor/SoDB.h>
50 #include <Inventor/SoInput.h>
51 #include <Inventor/Xt/SoXt.h>
52 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
53 #include <Inventor/nodes/SoBaseColor.h>
54 #include <Inventor/nodes/SoCamera.h>
55 #include <Inventor/nodes/SoComplexity.h>
56 #include <Inventor/nodes/SoCoordinate3.h>
57 #include <Inventor/nodes/SoDrawStyle.h>
58 #include <Inventor/nodes/SoLightModel.h>
59 #include <Inventor/nodes/SoMaterial.h>
60 #include <Inventor/nodes/SoNurbsCurve.h>
61 #include <Inventor/nodes/SoRotation.h>
62 #include <Inventor/nodes/SoScale.h>
63 #include <Inventor/nodes/SoSeparator.h>
64 #include <Inventor/nodes/SoTranslation.h>
65
66 static char *floorData =
67 "#Inventor V2.0 ascii\n"
68 "Separator {\n"
69 " SpotLight {\n"
70 " cutOffAngle 0.9\n"
71 " dropOffRate 0.2\n"
72 " location 6 12 2\n"
73 " direction 0 -1 0\n"
74 " }\n"
75 " ShapeHints {\n"
76 " faceType UNKNOWN_FACE_TYPE\n"
77 " }\n"
78 " Texture2Transform {\n"
79 " #rotation 1.57\n"
80 " scaleFactor 8 8\n"
81 " }\n"
82 " Texture2 {\n"
83 " filename /usr/share/src/Inventor/examples/data/oak.rgb\n"
84 " }\n"
85 " NormalBinding {\n"
86 " value PER_PART\n"
87 " }\n"
88 " Material { diffuseColor 1 1 1 specularColor 1 1 1 shininess 0.4 }\n"
89 " DEF FloorPanel Separator {\n"
90 " DEF FloorStrip Separator {\n"
91 " DEF FloorBoard Separator {\n"
92 " Normal { vector 0 1 0 }\n"
93 " TextureCoordinate2 {\n"
94 " point [ 0 0, 0.5 0, 0.5 2, 0.5 4, 0.5 6,\n"
95 " 0.5 8, 0 8, 0 6, 0 4, 0 2 ] }\n"
96 " Coordinate3 {\n"
97 " point [ 0 0 0, .5 0 0, .5 0 -2, .5 0 -4, .5 0 -6,\n"
98 " .5 0 -8, 0 0 -8, 0 0 -6, 0 0 -4, 0 0 -2, ]\n"
99 " }\n"
100 " FaceSet { numVertices 10 }\n"
101 " BaseColor { rgb 0.3 0.1 0.0 }\n"
102 " Translation { translation 0.125 0 -0.333 }\n"
103 " Cylinder { parts TOP radius 0.04167 height 0.002 }\n"
104 " Translation { translation 0.25 0 0 }\n"
105 " Cylinder { parts TOP radius 0.04167 height 0.002 }\n"
106 " Translation { translation 0 0 -7.333 }\n"
107 " Cylinder { parts TOP radius 0.04167 height 0.002 }\n"
108 " Translation { translation -0.25 0 0 }\n"
109 " Cylinder { parts TOP radius 0.04167 height 0.002 }\n"
110 " }\n"
111 " Translation { translation 0 0 8.03 }\n"
112 " USE FloorBoard\n"
113 " Translation { translation 0 0 8.04 }\n"
114 " USE FloorBoard\n"
115 " }\n"
116 " Translation { translation 0.53 0 -0.87 }\n"
117 " USE FloorStrip\n"
118 " Translation { translation 0.53 0 -2.3 }\n"
119 " USE FloorStrip\n"
120 " Translation { translation 0.53 0 1.3 }\n"
121 " USE FloorStrip\n"
122 " Translation { translation 0.53 0 1.1 }\n"
123 " USE FloorStrip\n"
124 " Translation { translation 0.53 0 -0.87 }\n"
125 " USE FloorStrip\n"
126 " Translation { translation 0.53 0 1.7 }\n"
127 " USE FloorStrip\n"
128 " Translation { translation 0.53 0 -0.5 }\n"
129 " USE FloorStrip\n"
130 " }\n"
131 " Translation { translation 4.24 0 0 }\n"
132 " USE FloorPanel\n"
133 " Translation { translation 4.24 0 0 }\n"
134 " USE FloorPanel\n"
135 "}";
136
137 ////////////////////////////////////////////////////////////
138 // CODE FOR The Inventor Mentor STARTS HERE
139
140 // The control points for this curve
141 float pts[13][3] = {
142 { 6.0, 0.0, 6.0},
143 {-5.5, 0.5, 5.5},
144 {-5.0, 1.0, -5.0},
145 { 4.5, 1.5, -4.5},
146 { 4.0, 2.0, 4.0},
147 {-3.5, 2.5, 3.5},
148 {-3.0, 3.0, -3.0},
149 { 2.5, 3.5, -2.5},
150 { 2.0, 4.0, 2.0},
151 {-1.5, 4.5, 1.5},
152 {-1.0, 5.0, -1.0},
153 { 0.5, 5.5, -0.5},
154 { 0.0, 6.0, 0.0}};
155
156 // The knot vector
157 float knots[17] = {
158 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};
159
160 // Create the nodes needed for the B-Spline curve.
161 SoSeparator *
162 makeCurve()
163 {
164 SoSeparator *curveSep = new SoSeparator();
165 curveSep->ref();
166
167 // Set the draw style of the curve.
168 SoDrawStyle *drawStyle = new SoDrawStyle;
169 drawStyle->lineWidth = 4;
170 curveSep->addChild(drawStyle);
171
172 // Define the NURBS curve including the control points
173 // and a complexity.
174 SoComplexity *complexity = new SoComplexity;
175 SoCoordinate3 *controlPts = new SoCoordinate3;
176 SoNurbsCurve *curve = new SoNurbsCurve;
177 complexity->value = 0.8;
178 controlPts->point.setValues(0, 13, pts);
179 curve->numControlPoints = 13;
180 curve->knotVector.setValues(0, 17, knots);
181 curveSep->addChild(complexity);
182 curveSep->addChild(controlPts);
183 curveSep->addChild(curve);
184
185 curveSep->unrefNoDelete();
186 return curveSep;
187 }
188
189 // CODE FOR The Inventor Mentor ENDS HERE
190 ////////////////////////////////////////////////////////////
191
192 int
193 main(int, char **argv)
194 {
195 // Initialize Inventor and Xt
196 Widget appWindow = SoXt::init(argv[0]);
197 if (appWindow == NULL)
198 exit(1);
199
200 SoSeparator *root = new SoSeparator;
201 root->ref();
202
203 // Create the scene graph for the spiral
204 SoSeparator *spiral = new SoSeparator;
205 SoSeparator *curveSep = makeCurve();
206 SoLightModel *lmodel = new SoLightModel;
207 SoBaseColor *clr = new SoBaseColor;
208
209 lmodel->model = SoLightModel::BASE_COLOR;
210 clr->rgb.setValue(SbColor(1.0, 0.0, 0.1));
211 spiral->addChild(lmodel);
212 spiral->addChild(clr);
213 spiral->addChild(curveSep);
214 root->addChild(spiral);
215
216 // Create the scene graph for the floor
217 SoSeparator *floor = new SoSeparator;
218 SoTranslation *xlate = new SoTranslation;
219 SoRotation *rot = new SoRotation;
220 SoScale *scale = new SoScale;
221 SoInput in;
222 SoNode *result;
223
224 in.setBuffer(floorData, strlen(floorData));
225 SoDB::read(&in, result);
226 xlate->translation.setValue(SbVec3f(-12.0, -5.0, -5.0));
227 scale->scaleFactor.setValue(SbVec3f(2.0, 1.0, 2.0));
228 rot->rotation.setValue(SbRotation(SbVec3f(0.0, 1.0, 0.0), M_PI/2.0));
229 floor->addChild(rot);
230 floor->addChild(xlate);
231 floor->addChild(scale);
232 floor->addChild(result);
233 root->addChild(floor);
234
235 // Create the scene graph for the spiral's shadow
236 SoSeparator *shadow = new SoSeparator;
237 SoLightModel *shmdl = new SoLightModel;
238 SoMaterial *shmtl = new SoMaterial;
239 SoBaseColor *shclr = new SoBaseColor;
240 SoTranslation *shxl = new SoTranslation;
241 SoScale *shscl = new SoScale;
242
243 shmdl->model = SoLightModel::BASE_COLOR;
244 shclr->rgb.setValue(SbColor(0.21, 0.15, 0.09));
245 shmtl->transparency = 0.5;
246 shxl->translation.setValue(SbVec3f(0.0, -4.9, 0.0));
247 shscl->scaleFactor.setValue(SbVec3f(1.0, 0.0, 1.0));
248 shadow->addChild(shmtl);
249 shadow->addChild(shmdl);
250 shadow->addChild(shclr);
251 shadow->addChild(shxl);
252 shadow->addChild(shscl);
253 shadow->addChild(curveSep);
254 root->addChild(shadow);
255
256 // Initialize an Examiner Viewer
257 SoXtExaminerViewer *viewer =
258 new SoXtExaminerViewer(appWindow);
259 viewer->setSceneGraph(root);
260 viewer->setTitle("B-Spline Curve");
261 SoCamera *cam = viewer->getCamera();
262 cam->position.setValue(SbVec3f(-6.0, 8.0, 20.0));
263 cam->pointAt(SbVec3f(0.0, -2.0, -4.0));
264 viewer->show();
265
266 SoXt::show(appWindow);
267 SoXt::mainLoop();
268 }
269