Debian Codegraph @

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