Debian Codegraph @

apps/examples/Mentor/CXX/03.2.Robot.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 3, example 2.
40 *
41 * This code shows how to create a robot out of various nodes.
42 * It introduces shared instancing of nodes to create two legs
43 * using two instances of the same subgraph.
44 *
45 *------------------------------------------------------------*/
46
47 #include <cstdlib>
48 #include <Inventor/Xt/SoXt.h>
49 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
50 #include <Inventor/nodes/SoCube.h>
51 #include <Inventor/nodes/SoCylinder.h>
52 #include <Inventor/nodes/SoMaterial.h>
53 #include <Inventor/nodes/SoSeparator.h>
54 #include <Inventor/nodes/SoSphere.h>
55 #include <Inventor/nodes/SoTransform.h>
56
57 SoSeparator *
58 makeRobot()
59 {
60 //////////////////////////////////////////////////////////////
61 // CODE FOR The Inventor Mentor STARTS HERE
62 // Robot with legs
63
64 // Construct parts for legs (thigh, calf and foot)
65 SoCube *thigh = new SoCube;
66 thigh->width = 1.2;
67 thigh->height = 2.2;
68 thigh->depth = 1.1;
69
70 SoTransform *calfTransform = new SoTransform;
71 calfTransform->translation.setValue(0, -2.25, 0.0);
72
73 SoCube *calf = new SoCube;
74 calf->width = 1;
75 calf->height = 2.2;
76 calf->depth = 1;
77
78 SoTransform *footTransform = new SoTransform;
79 footTransform->translation.setValue(0, -1.5, .5);
80
81 SoCube *foot = new SoCube;
82 foot->width = 0.8;
83 foot->height = 0.8;
84 foot->depth = 2;
85
86 // Put leg parts together
87 SoGroup *leg = new SoGroup;
88 leg->addChild(thigh);
89 leg->addChild(calfTransform);
90 leg->addChild(calf);
91 leg->addChild(footTransform);
92 leg->addChild(foot);
93
94 SoTransform *leftTransform = new SoTransform;
95 leftTransform->translation = SbVec3f(1, -4.25, 0);
96
97 // Left leg
98 SoSeparator *leftLeg = new SoSeparator;
99 leftLeg->addChild(leftTransform);
100 leftLeg->addChild(leg);
101
102 SoTransform *rightTransform = new SoTransform;
103 rightTransform->translation.setValue(-1, -4.25, 0);
104
105 // Right leg
106 SoSeparator *rightLeg = new SoSeparator;
107 rightLeg->addChild(rightTransform);
108 rightLeg->addChild(leg);
109
110 // Parts for body
111 SoTransform *bodyTransform = new SoTransform;
112 bodyTransform->translation.setValue(0.0, 3.0, 0.0);
113
114 SoMaterial *bronze = new SoMaterial;
115 bronze->ambientColor.setValue(.33, .22, .27);
116 bronze->diffuseColor.setValue(.78, .57, .11);
117 bronze->specularColor.setValue(.99, .94, .81);
118 bronze->shininess = .28;
119
120 SoCylinder *bodyCylinder = new SoCylinder;
121 bodyCylinder->radius = 2.5;
122 bodyCylinder->height = 6;
123
124 // Construct body out of parts
125 SoSeparator *body = new SoSeparator;
126 body->addChild(bodyTransform);
127 body->addChild(bronze);
128 body->addChild(bodyCylinder);
129 body->addChild(leftLeg);
130 body->addChild(rightLeg);
131
132 // Head parts
133 SoTransform *headTransform = new SoTransform;
134 headTransform->translation.setValue(0, 7.5, 0);
135 headTransform->scaleFactor.setValue(1.5, 1.5, 1.5);
136
137 SoMaterial *silver = new SoMaterial;
138 silver->ambientColor.setValue(.2, .2, .2);
139 silver->diffuseColor.setValue(.6, .6, .6);
140 silver->specularColor.setValue(.5, .5, .5);
141 silver->shininess = .5;
142
143 SoSphere *headSphere = new SoSphere;
144
145 // Construct head
146 SoSeparator *head = new SoSeparator;
147 head->addChild(headTransform);
148 head->addChild(silver);
149 head->addChild(headSphere);
150
151 // Robot is just head and body
152 SoSeparator *robot = new SoSeparator;
153 robot->addChild(body);
154 robot->addChild(head);
155
156 // CODE FOR The Inventor Mentor ENDS HERE
157 //////////////////////////////////////////////////////////////
158
159 return robot;
160 }
161
162 int
163 main(int , char **argv)
164 {
165 // Initialize Inventor and Xt
166 Widget myWindow = SoXt::init(argv[0]);
167 if (myWindow == NULL) exit(1);
168
169 SoSeparator *root = new SoSeparator;
170 root->ref();
171
172 // This function contains our code fragment.
173 root->addChild(makeRobot());
174
175 SoXtExaminerViewer *myViewer =
176 new SoXtExaminerViewer(myWindow);
177 myViewer->setSceneGraph(root);
178 myViewer->setTitle("Robot");
179 myViewer->show();
180 myViewer->viewAll();
181
182 SoXt::show(myWindow);
183 SoXt::mainLoop();
184 }
185
186