Debian Codegraph @

apps/examples/Mentor/CXX/03.1.Molecule.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 1.
40 *
41 * This code shows how to create a molecule out of 3 spherical
42 * atoms. The molecule illustrates how the ordering of nodes
43 * within a scene graph affects the rendered image.
44 *
45 *------------------------------------------------------------*/
46
47 #include <cstdlib>
48 #include <Inventor/Xt/SoXt.h>
49 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
50 #include <Inventor/nodes/SoGroup.h>
51 #include <Inventor/nodes/SoMaterial.h>
52 #include <Inventor/nodes/SoSeparator.h>
53 #include <Inventor/nodes/SoSphere.h>
54 #include <Inventor/nodes/SoTransform.h>
55
56 SoGroup *
57 makeWaterMolecule()
58 {
59 //////////////////////////////////////////////////////////////
60 // CODE FOR The Inventor Mentor STARTS HERE
61
62 // Construct all parts
63 SoGroup *waterMolecule = new SoGroup; // water molecule
64
65 SoGroup *oxygen = new SoGroup; // oxygen atom
66 SoMaterial *redPlastic = new SoMaterial;
67 SoSphere *sphere1 = new SoSphere;
68
69 SoGroup *hydrogen1 = new SoGroup; // hydrogen atoms
70 SoGroup *hydrogen2 = new SoGroup;
71 SoTransform *hydrogenXform1 = new SoTransform;
72 SoTransform *hydrogenXform2 = new SoTransform;
73 SoMaterial *whitePlastic = new SoMaterial;
74 SoSphere *sphere2 = new SoSphere;
75 SoSphere *sphere3 = new SoSphere;
76
77 // Set all field values for the oxygen atom
78 redPlastic->ambientColor.setValue(1.0, 0.0, 0.0);
79 redPlastic->diffuseColor.setValue(1.0, 0.0, 0.0);
80 redPlastic->specularColor.setValue(0.5, 0.5, 0.5);
81 redPlastic->shininess = 0.5;
82
83 // Set all field values for the hydrogen atoms
84 hydrogenXform1->scaleFactor.setValue(0.75, 0.75, 0.75);
85 hydrogenXform1->translation.setValue(0.0, -1.2, 0.0);
86 hydrogenXform2->translation.setValue(1.1852, 1.3877, 0.0);
87 whitePlastic->ambientColor.setValue(1.0, 1.0, 1.0);
88 whitePlastic->diffuseColor.setValue(1.0, 1.0, 1.0);
89 whitePlastic->specularColor.setValue(0.5, 0.5, 0.5);
90 whitePlastic->shininess = 0.5;
91
92 // Create a hierarchy
93 waterMolecule->addChild(oxygen);
94 waterMolecule->addChild(hydrogen1);
95 waterMolecule->addChild(hydrogen2);
96
97 oxygen->addChild(redPlastic);
98 oxygen->addChild(sphere1);
99 hydrogen1->addChild(hydrogenXform1);
100 hydrogen1->addChild(whitePlastic);
101 hydrogen1->addChild(sphere2);
102 hydrogen2->addChild(hydrogenXform2);
103 hydrogen2->addChild(sphere3);
104
105 // CODE FOR The Inventor Mentor ENDS HERE
106 //////////////////////////////////////////////////////////////
107
108 return waterMolecule;
109 }
110
111 int
112 main(int , char **argv)
113 {
114 // Initialize Inventor and Xt
115 Widget myWindow = SoXt::init(argv[0]);
116 if (myWindow == NULL) exit(1);
117
118 SoSeparator *root = new SoSeparator;
119 root->ref();
120
121 // This function contains our code fragment.
122 root->addChild(makeWaterMolecule());
123
124 SoXtExaminerViewer *myViewer =
125 new SoXtExaminerViewer(myWindow);
126 myViewer->setSceneGraph(root);
127 myViewer->setTitle("H two O");
128 myViewer->show();
129 myViewer->viewAll();
130
131 SoXt::show(myWindow);
132 SoXt::mainLoop();
133 }
134