Debian Codegraph @

apps/demos/SceneViewer/SceneViewer.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 // Sample SceneViewer program.
39 // This program reads in a Inventor datafile, and creates a SceneViewer
40 // to allow interaction with it.
41 //
42
43 #include <cstdlib>
44 #include <X11/Intrinsic.h>
45 #include <X11/keysym.h>
46 #include <locale.h>
47 #include <Inventor/SoDB.h>
48 #include <Inventor/SoInteraction.h>
49 #include <Inventor/SoPickedPoint.h>
50 #include <Inventor/Xt/SoXt.h>
51 #include <Inventor/nodekits/SoBaseKit.h>
52 #include <Inventor/nodekits/SoNodeKit.h>
53 #include <Inventor/nodes/SoSelection.h>
54
55 #include "SoSceneViewer.h"
56 #include "../../samples/widgets/MyFileRead.h"
57
58 // #define TEST_PICK_CB
59 // #define DISABLE_SEL_PICK_MATCH
60
61 #ifdef TEST_PICK_CB
62 // Test the selection pick callback mechanism
63 static SoPath *
64 selectionPickCB(void *userData, const SoPickedPoint *pick)
65 {
66 // Pick the topmost group beneath the selection node
67 SoPath *p = pick->getPath();
68 SoSelection *sel = (SoSelection *) userData;
69
70 // See which child of selection got picked
71 int selIndex = -1;
72 if (p->getHead() == sel)
73 selIndex = 0;
74 else {
75 for (int i = 1; (selIndex == -1) && (i < p->getLength() - 1); i++)
76 if (p->getNode(i) == sel)
77 selIndex = i;
78 }
79
80 // Return a path truncated to this point
81 if (selIndex == -1) {
82 fprintf(stderr, "SceneViewer selectionPickCB - did not find sel node in pick path!\n");
83 return NULL;
84 }
85
86 return p->copy(selIndex, 2); // 2 nodes, starting at selection node
87 }
88 #endif
89
90 static SbBool
91 getArgs(int argc, char **argv, char *&envFile, char *&filename)
92 {
93 SbBool ok = TRUE;
94
95 envFile = NULL;
96 filename = NULL;
97
98 if (argc == 2) {
99 filename = argv[1];
100 }
101 else if (argc > 2) {
102 // see if there is a -e before the file name
103 if (strcmp(argv[1], "-e") == 0) {
104 envFile = argv[2];
105 if (argc > 3)
106 filename = argv[3];
107 }
108 else if (argc == 4) {
109 // maybe the -e is after the file name
110 if (strcmp(argv[2], "-e") == 0) {
111 envFile = argv[3];
112 filename = argv[1];
113 }
114 }
115 else ok = FALSE;
116 }
117
118 return ok;
119 }
120
121 // Stolen from Annotator.
122 //
123 static String _myXtDefaultLanguageProc(Display *, String xnl, XtPointer)
124 {
125 if (! setlocale(LC_ALL, xnl))
126 XtWarning("locale not supported by C library, locale unchanged");
127
128 if (! XSupportsLocale()) {
129 XtWarning("locale not supported by Xlib, locale set to C");
130 setlocale(LC_ALL, "C");
131 }
132 if (! XSetLocaleModifiers(""))
133 XtWarning("X locale modifiers not supported, using default");
134
135 setlocale(LC_NUMERIC, "C");
136
137 return setlocale(LC_CTYPE, NULL);
138 }
139
140 int
141 main(int argc, char **argv)
142 {
143 Widget mainWindow;
144 SoSceneViewer *sv;
145 SoInput in;
146 char *envFile, *filename;
147
148 XtSetLanguageProc( NULL, _myXtDefaultLanguageProc, NULL );
149
150 // check usage
151 if (! getArgs(argc, argv, envFile, filename)) {
152 fprintf(stderr, "usage: SceneViewer [-e environ.iv] [file.iv]\n");
153 exit( 1 );
154 }
155
156 // workaround for bug 200909 - this will force OpenGL to create
157 // a connection with the X server to receive delete window events
158 // (so that OpenGL can delete Accumulation and other software
159 // buffers when the window is destroyed).
160 if (putenv("GL_CHECK_WINDOW_DESTROY=y"))
161 fprintf(stderr, "Could not set the GL_CHECK_WINDOW_DESTROY env.\n");
162
163 SbBool useStdin = (filename && strcmp(filename,"-")==0);
164
165 // init Inventor
166 mainWindow = SoXt::init(argv[0], "SceneViewer");
167
168 // read the file in, creating a selection node as the root
169 // of the scene graph.
170 SoSelection *selRoot = new SoSelection;
171 selRoot->ref();
172 #ifdef TEST_PICK_CB
173 selRoot->setPickFilterCallback(selectionPickCB, selRoot);
174 #endif
175
176 #ifdef DISABLE_SEL_PICK_MATCH
177 selRoot->setPickMatching(FALSE);
178 #endif
179
180 SbBool doRead = FALSE;
181 SbBool readFromFile = FALSE;
182 if (useStdin) {
183 doRead = TRUE;
184 in.setFilePointer(stdin);
185 }
186 else if (filename) {
187 doRead = TRUE;
188 readFromFile = TRUE;
189 }
190
191 if (doRead) {
192 fprintf( stderr, "Reading input file...");
193 SoSeparator *readRoot;
194
195 // If reading from a file, call MyFileRead in libInventorWidgets sample code.
196 // It will do an SoDB::readAll on Inventor files.
197 // On non-Inventor files, it will use FTR rules to attempt file conversion
198 // to the Inventor format before reading.
199 SbString errmsg;
200 if (readFromFile)
201 readRoot = MyFileRead(filename, errmsg);
202 else readRoot = SoDB::readAll(&in);
203
204 if (readRoot != NULL) {
205 selRoot->addChild(readRoot);
206 }
207
208 if ( selRoot->getNumChildren() == 0 ) {
209 if (readFromFile)
210 fprintf(stderr, "%s\n", errmsg.getString());
211 fprintf(stderr, "No data read; creating empty scene.\n" );
212 }
213 else
214 fprintf(stderr, "done.\n");
215 }
216
217 //
218 // Create the SceneViewer
219 //
220 sv = new SoSceneViewer(mainWindow, NULL, TRUE, selRoot, envFile);
221 selRoot->unref();
222
223 //
224 // Build and show the SceneViewer
225 //
226 sv->show();
227 XtRealizeWidget(mainWindow);
228
229 //
230 // Loop forever
231 //
232 SoXt::mainLoop();
233 return 0;
234 }