Debian Codegraph @

apps/examples/Mentor/CXX/09.5.GenSph.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 9, example 5.
40 *
41 * Using a callback for generated primitives.
42 * A simple scene with a sphere is created.
43 * A callback is used to write out the triangles that
44 * form the sphere in the scene.
45 *----------------------------------------------------------*/
46
47 #include <Inventor/SoDB.h>
48 #include <Inventor/SoPrimitiveVertex.h>
49 #include <Inventor/actions/SoCallbackAction.h>
50 #include <Inventor/actions/SoWriteAction.h>
51 #include <Inventor/nodes/SoDirectionalLight.h>
52 #include <Inventor/nodes/SoMaterial.h>
53 #include <Inventor/nodes/SoPerspectiveCamera.h>
54 #include <Inventor/nodes/SoSeparator.h>
55 #include <Inventor/nodes/SoSphere.h>
56
57 // Function prototypes
58 void printSpheres(SoNode *);
59 SoCallbackAction::Response printHeaderCallback(void *,
60 SoCallbackAction *, const SoNode *);
61 void printTriangleCallback(void *, SoCallbackAction *,
62 const SoPrimitiveVertex *, const SoPrimitiveVertex *,
63 const SoPrimitiveVertex *);
64 void printVertex(const SoPrimitiveVertex *);
65
66 //////////////////////////////////////////////////////////////
67 // CODE FOR The Inventor Mentor STARTS HERE
68
69 void
70 printSpheres(SoNode *root)
71 {
72 SoCallbackAction myAction;
73
74 myAction.addPreCallback(SoSphere::getClassTypeId(),
75 printHeaderCallback, NULL);
76 myAction.addTriangleCallback(SoSphere::getClassTypeId(),
77 printTriangleCallback, NULL);
78
79 myAction.apply(root);
80 }
81
82 SoCallbackAction::Response
83 printHeaderCallback(void *, SoCallbackAction *,
84 const SoNode *node)
85 {
86 printf("\n Sphere ");
87 // Print the node name (if it exists) and address
88 if (! !node->getName())
89 printf("named \"%s\" ", node->getName().getString());
90 printf("at address %p\n", node);
91
92 return SoCallbackAction::CONTINUE;
93 }
94
95 void
96 printTriangleCallback(void *, SoCallbackAction *,
97 const SoPrimitiveVertex *vertex1,
98 const SoPrimitiveVertex *vertex2,
99 const SoPrimitiveVertex *vertex3)
100 {
101 printf("Triangle:\n");
102 printVertex(vertex1);
103 printVertex(vertex2);
104 printVertex(vertex3);
105 }
106
107 void
108 printVertex(const SoPrimitiveVertex *vertex)
109 {
110 const SbVec3f &point = vertex->getPoint();
111 printf("\tCoords = (%g, %g, %g)\n",
112 point[0], point[1], point[2]);
113
114 const SbVec3f &normal = vertex->getNormal();
115 printf("\tNormal = (%g, %g, %g)\n",
116 normal[0], normal[1], normal[2]);
117 }
118
119 // CODE FOR The Inventor Mentor ENDS HERE
120 ///////////////////////////////////////////////////////////////
121
122 int
123 main(int, char **)
124 {
125 // Initialize Inventor
126 SoDB::init();
127
128 // Make a scene containing a red sphere
129 SoSeparator *root = new SoSeparator;
130 SoPerspectiveCamera *myCamera = new SoPerspectiveCamera;
131 SoMaterial *myMaterial = new SoMaterial;
132 root->ref();
133 root->addChild(myCamera);
134 root->addChild(new SoDirectionalLight);
135 myMaterial->diffuseColor.setValue(1.0, 0.0, 0.0); // Red
136 root->addChild(myMaterial);
137 root->addChild(new SoSphere);
138 root->ref();
139
140 // Write out the triangles that form the sphere in the scene
141 printSpheres(root);
142
143 root->unref();
144 return 0;
145 }