Debian Codegraph @

apps/demos/qmorf/qmorf.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 // Program to transform one quad mesh into another. Linear
39 // interpolation of the vertices is done. Transparent blending is
40 // also done to interpolate colors or textures on the objects (again,
41 // linearly).
42 //
43 // Written by Gavin Bell for Silicon Graphics
44 //
45 #include <cstdio>
46 #include <cmath>
47 #include <cstdlib>
48 #include <unistd.h>
49 #include <cstring>
50
51
52 #include <Inventor/Xt/SoXt.h>
53 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
54 #include <Inventor/Xt/SoXtRenderArea.h>
55 #include <Inventor/SoDB.h>
56 #include <Inventor/actions/SoGLRenderAction.h>
57 #include <Inventor/elements/SoCacheElement.h>
58 #include <Inventor/nodes/SoCallback.h>
59 #include <Inventor/nodes/SoCoordinate3.h>
60 #include <Inventor/nodes/SoMaterial.h>
61 #include <Inventor/nodes/SoMaterialBinding.h>
62 #include <Inventor/nodes/SoPerspectiveCamera.h>
63 #include <Inventor/nodes/SoSeparator.h>
64 #include <Inventor/nodes/SoShapeHints.h>
65 #include <Inventor/nodes/SoSwitch.h>
66 #include <Inventor/nodes/SoTexture2.h>
67 #include <Inventor/nodes/SoTextureCoordinate2.h>
68 #include <Inventor/nodes/SoTextureCoordinateBinding.h>
69 #include <Inventor/nodes/SoTextureCoordinatePlane.h>
70 #include <Inventor/sensors/SoIdleSensor.h>
71
72 #include "../../samples/common/InventorLogo.h"
73 #include "QuadThing.h"
74 #include "Background.h"
75
76 #include <Xm/Xm.h>
77 #include <Xm/Text.h>
78 #include <Xm/Form.h>
79 #include <Xm/ScrollBar.h>
80 #include <Xm/LabelG.h>
81 #include <Xm/PushBG.h>
82 #include <Xm/ToggleBG.h>
83
84 //
85 // The list of things we'll morph between
86 //
87
88 static QuadThingList objectList;
89
90 //
91 // The interpolated shape we'll constantly modify
92 //
93 static QuadThing *TheShape;
94
95 //
96 // Scale for slider bars
97 //
98 static const int SLIDER_MAX = 1000;
99
100 //
101 // Sensor to do automatic animation
102 //
103 static SoIdleSensor *animationSensor;
104
105 FlashBackground *background;
106 static int nobackground = 0;
107 static int notextures = 0;
108
109 //
110 // Create a scene graph that looks something like this:
111 //
112 // Separator root
113 // / \
114 // / \
115 // Separator ToStuff Separator FromStuff
116 // / | | | |
117 // ToTexSwitch | FromTexSwitch TransparentMaterial
118 // / \ | / \ |
119 // Texture Material | Texture Material /
120 // \ /
121 // \ /
122 // ...Separator for quadmesh shape...
123 //
124 // ... So the quadMesh gets drawn twice, to blend the two sets of
125 // colors. The transparency field in the TransparentMaterial is used
126 // to control how much of the 'From' object is blended into the 'To'
127 // object.
128 //
129
130 static SoSeparator *ToStuff, *FromStuff;
131 static SoGroup *placeHolder;
132 static SoGroup *ToTexGroup, *FromTexGroup;
133 static SoGroup *ToMatGroup, *FromMatGroup;
134 static SoMaterial *TransparentMaterial;
135 static SoSwitch *ToTexSwitch, *FromTexSwitch;
136 static SoTexture2 *noopTexture;
137 static SoTextureCoordinatePlane *noopTextureCoord;
138
139 //
140 // Setup the scene graph when we change the shapes we are
141 // interpolating. The topology of the scene graph is always the same,
142 // but we may replace nodes like materials or textures on the fly (if
143 // an object has no material or texture a placeholder node that does
144 // not affect anything is used).
145 //
146 void
147 setToFromStuff(int fromObject)
148 {
149 int toObject = (fromObject+1) % objectList.getLength();
150
151 SoNode *t;
152
153 // Texture image:
154 if ((t = objectList[fromObject]->getTexture2()) != NULL) {
155 FromTexGroup->replaceChild(0, t);
156 if (FromTexSwitch->whichChild.getValue() != 1)
157 FromTexSwitch->whichChild = 0;
158 } else {
159 FromTexGroup->replaceChild(0, noopTexture);
160 if (FromTexSwitch->whichChild.getValue() != 1)
161 FromTexSwitch->whichChild = SO_SWITCH_ALL;
162 }
163
164 // Texture coordinates:
165 if ((t = objectList[fromObject]->getTexCoord()) != NULL)
166 FromTexGroup->replaceChild(1, t);
167 else
168 FromTexGroup->replaceChild(1, noopTextureCoord);
169
170 // Texture coordinate binding:
171 if ((t = objectList[fromObject]->getTexBinding()) != NULL)
172 FromTexGroup->replaceChild(2, t);
173 else
174 FromTexGroup->replaceChild(2, placeHolder);
175
176 // No texture, has materials (maybe)
177 if ((t = objectList[fromObject]->getMaterial()) != NULL)
178 FromMatGroup->replaceChild(0, t);
179 else
180 FromMatGroup->replaceChild(0, placeHolder);
181 if ((t = objectList[fromObject]->getMatBinding()) != NULL)
182 FromMatGroup->replaceChild(1, t);
183 else
184 FromMatGroup->replaceChild(1, placeHolder);
185
186 if ((t = objectList[toObject]->getTexture2()) != NULL) {
187 ToTexGroup->replaceChild(0, t);
188 if (ToTexSwitch->whichChild.getValue() != 1)
189 ToTexSwitch->whichChild = 0;
190 } else {
191 ToTexGroup->replaceChild(0, noopTexture);
192 if (ToTexSwitch->whichChild.getValue() != 1)
193 ToTexSwitch->whichChild = SO_SWITCH_ALL;
194 }
195 if ((t = objectList[toObject]->getTexCoord()) != NULL)
196 ToTexGroup->replaceChild(1, t);
197 else
198 ToTexGroup->replaceChild(1, noopTextureCoord);
199 if ((t = objectList[toObject]->getTexBinding()) != NULL)
200 ToTexGroup->replaceChild(2, t);
201 else
202 ToTexGroup->replaceChild(2, placeHolder);
203 if ((t = objectList[toObject]->getMaterial()) != NULL)
204 ToMatGroup->replaceChild(0, t);
205 else
206 ToMatGroup->replaceChild(0, placeHolder);
207 if ((t = objectList[toObject]->getMatBinding()) != NULL)
208 ToMatGroup->replaceChild(1, t);
209 else
210 ToMatGroup->replaceChild(1, placeHolder);
211 }
212
213 //
214 // A bunch of stuff used to control the animation
215 //
216 static const int COLOR = 0;
217 static const int SHAPE = 1;
218 static int lastColor = (-1);
219 static double animationTime[2] = { 0.0, 0.0 };
220 static int whatIsAnimating[2] = { 1, 1 };
221 static Widget sliders[2];
222 static SbTime lastTime;
223
224 //
225 // Stop animation. what is either COLOR or SHAPE. Called by the UI
226 // widgets (the animation buttons or to stop the animation when a
227 // slider is moved).
228 //
229 void
230 stopAnimating(int what)
231 {
232 whatIsAnimating[what] = 0;
233 if (!(whatIsAnimating[0] || whatIsAnimating[1]))
234 animationSensor->unschedule();
235 }
236 void
237 startAnimating(int what)
238 {
239 whatIsAnimating[what] = 1;
240 animationSensor->schedule();
241 lastTime = SbTime::getTimeOfDay();
242 }
243
244 //
245 // Change the COLOR or SHAPE to the given value. Called by the slider
246 // widgets.
247 //
248 void
249 setValue(int what, double value)
250 {
251 animationTime[what] = fmod(value, objectList.getLength());
252
253 double t = fmod(animationTime[what], 1.0);
254
255 if (what == SHAPE)
256 {
257 int object1 = int(animationTime[SHAPE]);
258 int object2 = (object1+1) % objectList.getLength();
259 TheShape->interp(objectList[object1], objectList[object2], t);
260 }
261 else
262 {
263 // If we'll be using the colors of another shape, must change
264 // scene graph:
265 if (int(animationTime[COLOR]) != lastColor)
266 {
267 lastColor = int(animationTime[COLOR]);
268 setToFromStuff(lastColor);
269 }
270 TransparentMaterial->transparency.setValue(t);
271 }
272 }
273
274 //
275 // Called by a sensor to automatically change the objects into one
276 // another. This also updates the slider widgets.
277 //
278 void
279 morfCallback(void *, SoSensor *sensor)
280 {
281 static const double transition_time = 4.0; // Seconds
282
283 SbTime now = SbTime::getTimeOfDay();
284 SbTime dt = (now - lastTime) / transition_time;
285 static SbTime bgShape, bgColor;
286
287 if (whatIsAnimating[COLOR])
288 {
289 setValue(COLOR, animationTime[COLOR] + dt.getValue());
290 double t = animationTime[COLOR] / objectList.getLength();
291 Arg resources[1];
292 XtSetArg(resources[0], XmNvalue, t * SLIDER_MAX);
293 XtSetValues(sliders[COLOR], resources, 1);
294 bgColor += dt;
295 if (!nobackground)
296 background->animateColor(bgColor.getValue());
297 }
298 if (whatIsAnimating[SHAPE])
299 {
300 setValue(SHAPE, animationTime[SHAPE] + dt.getValue());
301 double t = animationTime[SHAPE] / objectList.getLength();
302 Arg resources[1];
303 XtSetArg(resources[0], XmNvalue, t * SLIDER_MAX);
304 XtSetValues(sliders[SHAPE], resources, 1);
305 bgShape += dt / 3.0;
306 if (!nobackground)
307 background->animateShape(bgShape.getValue());
308 }
309
310 lastTime = now;
311 sensor->schedule();
312 }
313
314 //
315 // These two callbacks cooperate to figure out if rendering with
316 // textures turned on is too slow. The first is called from a
317 // callback node at the top of the scene graph, and notes the time
318 // when rendering starts. The second is a render abort callback, and
319 // will turn off texturing if it is determined to be too slow.
320 // They both only check the first few frames so they don't slow
321 // traversal down excessively.
322 //
323 static int TimeSet = 0;
324 static SbTime renderTime;
325
326 static void
327 setTimeCallback(void *, SoAction *action)
328 {
329 if (action->isOfType(SoGLRenderAction::getClassTypeId()) &&
330 TimeSet < 4) {
331
332 renderTime.setToTimeOfDay();
333
334 if (TimeSet == 0) {
335 animationSensor->schedule();
336 lastTime = renderTime;
337 }
338
339 // This node must not be cached!
340 SoCacheElement::invalidate(action->getState());
341
342 ++TimeSet;
343 }
344 }
345 static SoGLRenderAction::AbortCode
346 checkTimeCallback(void *data)
347 {
348 static int turnedOff = 0;
349
350 // If textures aren't turned off already...
351 if ((TimeSet > 1) && (TimeSet < 4) && !turnedOff) {
352 SbTime delta = SbTime::getTimeOfDay() - renderTime;
353 if (delta > SbTime(.5)) {
354 fprintf(stderr, "Disabling textures, "
355 "they are too slow\n");
356
357 // Turn off textures...
358 FromTexSwitch->whichChild = 1;
359 ToTexSwitch->whichChild = 1;
360 turnedOff = TRUE;
361 // And turn off render abort callback
362 SoGLRenderAction *ra = (SoGLRenderAction *)data;
363 ra->setAbortCallback(NULL, NULL);
364 return SoGLRenderAction::ABORT;
365 }
366 }
367 return SoGLRenderAction::CONTINUE;
368 }
369
370 void
371 parse_args(int argc, char **argv)
372 {
373 int err = 0; // Flag: error in options?
374 int c;
375
376 // Note: optind and optarg are declared in getopt.h
377
378 while ((c = getopt(argc, argv, "bth")) != -1)
379 {
380 switch(c)
381 {
382 case 'b':
383 nobackground = 1;
384 break;
385 case 't':
386 notextures = 1;
387 break;
388 case 'h': // Help
389 default:
390 err = 1;
391 break;
392 }
393 }
394 // Handle optional filenames
395 for (; optind < argc; optind++)
396 {
397 QuadThing *q = new QuadThing(argv[optind]);
398 if (q->getSceneGraph() != NULL)
399 objectList.append(q);
400 else delete q;
401 }
402 if (objectList.getLength() < 2)
403 {
404 err = 1;
405 }
406
407 if (err)
408 {
409 fprintf(stderr, "Usage: %s [-bth] file file [file ...]\n", argv[0]);
410 fprintf(stderr, "-b : No background\n");
411 fprintf(stderr, "-t : No textures\n");
412 fprintf(stderr, "-h : This message (help)\n");
413 fprintf(stderr, "At least two files must be given, and "
414 "they must contain QuadMeshes (qmorf can\n");
415 fprintf(stderr, "only morph QuadMesh nodes).\n");
416 fprintf(stderr, "The directory " IVDATADIR "/CyberHeads "
417 "contains good data to morph.\n");
418
419 exit(7);
420 }
421 }
422
423 //
424 // Callback for quit button
425 //
426 void
427 quitCallback(Widget, XtPointer, XtPointer)
428 {
429 exit(0);
430 }
431
432 //
433 // Callback for 'About...' button
434 //
435 void
436 showAboutDialog(Widget, XtPointer, XtPointer)
437 {
438 if (access(IVDEMOBINDIR"/qmorf.about.pdf", R_OK) != 0)
439 {
440 system("xmessage 'Sorry, could not find "
441 IVDEMOBINDIR"/qmorf.about.pdf' > /dev/null");
442 return;
443 }
444 char command[2*PATH_MAX+100];
445 sprintf(command, "which " PDFVIEWER " > /dev/null");
446
447 int err = system(command);
448 if (err)
449 {
450 system("xmessage 'You must install " PDFVIEWER
451 " for this function to work' > /dev/null");
452 return;
453 }
454
455 sprintf(command, PDFVIEWER " " IVDEMOBINDIR "/qmorf.about.pdf &");
456 system(command);
457 }
458
459 //
460 // Callback for the animation buttons
461 //
462 static void
463 toggleAnimation(Widget, XtPointer mydata, XtPointer)
464 {
465 if ((long)mydata == COLOR)
466 {
467 if (whatIsAnimating[COLOR])
468 stopAnimating(COLOR);
469 else startAnimating(COLOR);
470 }
471 else
472 {
473 if (whatIsAnimating[SHAPE])
474 stopAnimating(SHAPE);
475 else startAnimating(SHAPE);
476 }
477 }
478
479 //
480 // Callbacks for color and shape sliders
481 //
482 static void
483 dragColorSlider(Widget, XtPointer mydata, XtPointer cbstruct)
484 {
485 if (whatIsAnimating[COLOR])
486 {
487 stopAnimating(COLOR);
488 Arg resources[1];
489 XtSetArg(resources[0], XmNset, 0);
490 XtSetValues((Widget)mydata, resources, 1);
491 }
492 int value = ((XmScrollBarCallbackStruct *)cbstruct)->value;
493 double fv = value * objectList.getLength() / (double)SLIDER_MAX;
494 setValue(COLOR, fv);
495 }
496 static void
497 dragShapeSlider(Widget, XtPointer mydata, XtPointer cbstruct)
498 {
499 if (whatIsAnimating[SHAPE])
500 {
501 stopAnimating(SHAPE);
502 Arg resources[1];
503 XtSetArg(resources[0], XmNset, 0);
504 XtSetValues((Widget)mydata, resources, 1);
505 }
506 int value = ((XmScrollBarCallbackStruct *)cbstruct)->value;
507 double fv = value * objectList.getLength() / (double)SLIDER_MAX;
508 setValue(SHAPE, fv);
509 }
510
511
512 ////////////////////////////////////////////////////////////////////////
513 //
514 // Draw the Inventor logo in the overlays.
515 //
516 ////////////////////////////////////////////////////////////////////////
517
518 static void
519 logoCB(void *, SoAction *action)
520 {
521 if (action->isOfType(SoGLRenderAction::getClassTypeId()))
522 glViewport(0, 0, 80, 80); // See Dave Mott for details!
523 }
524 static void
525 setOverlayLogo(SoXtRenderArea *ra)
526 {
527 static SoSeparator *logo = NULL;
528
529 if (logo == NULL) {
530 SoInput in;
531 in.setBuffer((void *)ivLogo, ivLogoSize); // see common directory
532 logo = SoDB::readAll(&in);
533 logo->ref();
534 SoCallback *cb = new SoCallback; // sets GL viewport
535 cb->setCallback(logoCB);
536 logo->insertChild(cb, 0);
537 }
538 SbColor col(1, 1, 1);
539 ra->setOverlayColorMap(1, 1, &col);
540 ra->setOverlaySceneGraph(logo);
541 }
542
543
544 //////////////////////////////////////////////////////////////////////
545 //
546 // Create and layout all of the UI stuff (I'm really not very fond of
547 // Motif).
548 //
549 static SoXtExaminerViewer *
550 buildUI( Widget appWindow,
551 SoNode *root,
552 SoPerspectiveCamera *c)
553 //////////////////////////////////////////////////////////////////////
554 {
555 //
556 // Form that everything is part of...
557 //
558 Arg resources[20]; int n = 0;
559 XtSetArg(resources[n], "width", 600); n++;
560 XtSetArg(resources[n], "height", 600); n++;
561 Widget form = XmCreateForm(appWindow, "form", resources, n); n = 0;
562
563 //
564 // 8 widgets at bottom of window:
565 // 2 buttons, 2 labels, 2 sliders, and 2 toggle buttons
566 //
567 Widget w[8];
568
569 #define STRING(a) XmStringCreateSimple(a)
570 XtSetArg(resources[n], XmNlabelString, STRING("Animate")); ++n;
571 XtSetArg(resources[n], XmNrightAttachment, XmATTACH_FORM); ++n;
572 XtSetArg(resources[n], XmNbottomAttachment, XmATTACH_FORM); ++n;
573 XtSetArg(resources[n], XmNset, 1); ++n;
574 w[2] = XmCreateToggleButtonGadget(form, "colorAnimate", resources,
575 n);
576 XtAddCallback(w[2], XmNvalueChangedCallback,
577 toggleAnimation, (XtPointer)(unsigned long)COLOR);
578 n = 0;
579 XtSetArg(resources[n], XmNlabelString, STRING("Animate")); ++n;
580 XtSetArg(resources[n], XmNrightAttachment, XmATTACH_FORM); ++n;
581 XtSetArg(resources[n], XmNbottomAttachment, XmATTACH_WIDGET); ++n;
582 XtSetArg(resources[n], XmNbottomWidget, w[2]); n++;
583 XtSetArg(resources[n], XmNset, 1); ++n;
584 w[5] = XmCreateToggleButtonGadget(form, "shapeAnimate", resources,
585 n);
586 XtAddCallback(w[5], XmNvalueChangedCallback,
587 toggleAnimation, (XtPointer)(unsigned long)SHAPE);
588 n = 0;
589
590 XtSetArg(resources[n], XmNlabelString, STRING("Quit")); ++n;
591 XtSetArg(resources[n], XmNleftAttachment, XmATTACH_FORM); ++n;
592 XtSetArg(resources[n], XmNbottomAttachment, XmATTACH_FORM); ++n;
593 w[7] = XmCreatePushButtonGadget(form, "quit", resources, n);
594 XtAddCallback(w[7], XmNactivateCallback,
595 quitCallback, NULL);
596 n = 0;
597 XtSetArg(resources[n], XmNlabelString, STRING("About...")); ++n;
598 XtSetArg(resources[n], XmNleftAttachment, XmATTACH_FORM); ++n;
599 XtSetArg(resources[n], XmNbottomAttachment, XmATTACH_WIDGET); ++n;
600 XtSetArg(resources[n], XmNbottomWidget, w[7]); ++n;
601 w[6] = XmCreatePushButtonGadget(form, "about", resources, n);
602 XtAddCallback(w[6], XmNactivateCallback,
603 showAboutDialog, NULL);
604 n = 0;
605
606 XtSetArg(resources[n], XmNlabelString, STRING("Color")); ++n;
607 XtSetArg(resources[n], XmNleftAttachment, XmATTACH_WIDGET); ++n;
608 XtSetArg(resources[n], XmNleftWidget, w[6]); n++;
609 XtSetArg(resources[n], XmNbottomAttachment, XmATTACH_FORM); ++n;
610 w[0] = XmCreateLabelGadget(form, "colorLabel", resources, n);
611 n = 0;
612 XtSetArg(resources[n], XmNlabelString, STRING("Shape")); ++n;
613 XtSetArg(resources[n], XmNleftAttachment, XmATTACH_WIDGET); ++n;
614 XtSetArg(resources[n], XmNleftWidget, w[6]); n++;
615 XtSetArg(resources[n], XmNbottomAttachment, XmATTACH_WIDGET); ++n;
616 XtSetArg(resources[n], XmNbottomWidget, w[2]); n++;
617 XtSetArg(resources[n], XmNbottomOffset, 4); ++n;
618 w[3] = XmCreateLabelGadget(form, "shapeLabel", resources, n);
619 n = 0;
620
621 XtSetArg(resources[n], XmNmaximum, SLIDER_MAX + SLIDER_MAX/20); ++n;
622 XtSetArg(resources[n], XmNsliderSize, SLIDER_MAX/20); ++n;
623 XtSetArg(resources[n], XmNincrement, SLIDER_MAX/40); ++n;
624 XtSetArg(resources[n], XmNpageIncrement, SLIDER_MAX/20); ++n;
625 XtSetArg(resources[n], XmNshowArrows, FALSE); ++n;
626 XtSetArg(resources[n], XmNorientation, XmHORIZONTAL); ++n;
627 int tn = n;
628 XtSetArg(resources[n], XmNleftAttachment, XmATTACH_WIDGET); ++n;
629 XtSetArg(resources[n], XmNleftWidget, w[3]); n++;
630 XtSetArg(resources[n], XmNrightAttachment, XmATTACH_WIDGET); ++n;
631 XtSetArg(resources[n], XmNrightWidget, w[2]); n++;
632 XtSetArg(resources[n], XmNbottomAttachment, XmATTACH_FORM); ++n;
633 XtSetArg(resources[n], XmNbottomOffset, 4); ++n;
634 w[1] = XmCreateScrollBar(form, "colorScrollbar", resources, n);
635 sliders[COLOR] = w[1];
636 XtAddCallback(w[1], XmNdragCallback,
637 dragColorSlider, (XtPointer)w[2]);
638 XtAddCallback(w[1], XmNvalueChangedCallback,
639 dragColorSlider, (XtPointer)w[2]);
640 n = tn;
641 XtSetArg(resources[n], XmNleftAttachment, XmATTACH_WIDGET); ++n;
642 XtSetArg(resources[n], XmNleftWidget, w[3]); n++;
643 XtSetArg(resources[n], XmNrightAttachment, XmATTACH_WIDGET); ++n;
644 XtSetArg(resources[n], XmNrightWidget, w[5]); n++;
645 XtSetArg(resources[n], XmNbottomAttachment, XmATTACH_WIDGET); ++n;
646 XtSetArg(resources[n], XmNbottomWidget, w[1]); n++;
647 XtSetArg(resources[n], XmNbottomOffset, 10); ++n;
648 w[4] = XmCreateScrollBar(form, "shapeScrollbar", resources, n);
649 sliders[SHAPE] = w[4];
650 XtAddCallback(w[4], XmNdragCallback,
651 dragShapeSlider, (XtPointer)w[5]);
652 XtAddCallback(w[4], XmNvalueChangedCallback,
653 dragShapeSlider, (XtPointer)w[5]);
654 n = 0;
655
656 //
657 // Create viewer:
658 //
659 SoXtExaminerViewer *viewer = new SoXtExaminerViewer(form);
660 viewer->setSceneGraph(root);
661 viewer->setCamera(c);
662 viewer->setTransparencyType(SoGLRenderAction::BLEND);
663 viewer->setDecoration(FALSE);
664 viewer->setPopupMenuEnabled(FALSE);
665 viewer->setBorder(TRUE);
666 viewer->setFeedbackVisibility(FALSE);
667 viewer->setAutoClipping(FALSE);
668 // Set the draw style to as is, otherwise we'll never see
669 // any texture mapping on systems without graphics hardware
670 viewer->setDrawStyle(SoXtViewer::STILL, SoXtViewer::VIEW_AS_IS);
671
672 // Add Inventor logo to viewer
673 setOverlayLogo( viewer );
674
675 XtSetArg(resources[n], XmNtopAttachment, XmATTACH_FORM); n++;
676 XtSetArg(resources[n], XmNleftAttachment, XmATTACH_FORM); n++;
677 XtSetArg(resources[n], XmNrightAttachment, XmATTACH_FORM); n++;
678 XtSetArg(resources[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
679 XtSetArg(resources[n], XmNbottomWidget, w[6]); n++;
680 XtSetValues(viewer->getWidget(), resources, n); n = 0;
681
682 viewer->show();
683
684 XtManageChildren(w, 8);
685
686 XtManageChild(form);
687
688 return viewer;
689 }
690
691 int
692 main(int argc, char **argv)
693 {
694 Widget appWindow = SoXt::init(argv[0]);
695 if ( appWindow == NULL ) exit( 1 );
696
697 parse_args(argc, argv);
698
699 TheShape = new QuadThing(objectList[0]);
700 TheShape->interp(objectList[0], objectList[1], 0.0);
701
702 SoSeparator *root = new SoSeparator;
703 root->ref();
704
705 // Add a shapeHints node
706 SoShapeHints *hints = new SoShapeHints;
707 hints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
708 hints->faceType = SoShapeHints::CONVEX;
709 root->insertChild(hints, 0);
710
711 // Set up a callback node at the beginning of the scene graph that
712 // will set renderTime, which is used to figure out if texturing
713 // is too slow and should be turned off.
714 SoCallback *setTime = new SoCallback;
715 root->addChild(setTime);
716 setTime->setCallback(setTimeCallback, NULL);
717
718 SoPerspectiveCamera *camera = new SoPerspectiveCamera;
719 root->addChild(camera);
720
721 // And set up a no-op texture node. The GL doesn't guarantee
722 // pixel exactness if textured and non-textured polygons are
723 // blended together, so we'll make sure all shapes are textured by
724 // default (with a 1 by 1 all-white modulated texture, which won't
725 // affect the shape's color at all!).
726 noopTexture = new SoTexture2;
727 noopTexture->ref();
728 unsigned char whitePix[1]; whitePix[0] = 0xff;
729 noopTexture->image.setValue(SbVec2s(1,1), 1, whitePix);
730 // And an aribrary function for coords:
731 noopTextureCoord = new SoTextureCoordinatePlane;
732 noopTextureCoord->ref();
733
734 ToStuff = new SoSeparator;
735 root->addChild(ToStuff);
736 FromStuff = new SoSeparator;
737 root->addChild(FromStuff);
738 placeHolder = new SoGroup;
739 placeHolder->ref();
740 ToTexSwitch = new SoSwitch;
741 if (notextures)
742 ToTexSwitch->whichChild = 1;
743 else
744 ToTexSwitch->whichChild = 0;
745 ToStuff->addChild(ToTexSwitch);
746 ToTexGroup = new SoGroup;
747 ToTexSwitch->addChild(ToTexGroup);
748 ToTexGroup->addChild(noopTexture);
749 ToTexGroup->addChild(noopTextureCoord);
750 ToTexGroup->addChild(placeHolder); // Room for texCoordBinding
751 ToMatGroup = new SoGroup;
752 ToTexSwitch->addChild(ToMatGroup);
753 ToMatGroup->addChild(placeHolder); // Room for material
754 ToMatGroup->addChild(placeHolder); // Room for MaterialBinding
755
756 ToStuff->addChild(TheShape->getSceneGraph());
757
758 FromTexSwitch = new SoSwitch;
759 FromStuff->addChild(FromTexSwitch);
760 FromTexGroup = new SoGroup;
761 FromTexSwitch->addChild(FromTexGroup);
762 if (notextures)
763 FromTexSwitch->whichChild = 1;
764 else
765 FromTexSwitch->whichChild = 0;
766 FromTexGroup->addChild(noopTexture);
767 FromTexGroup->addChild(noopTextureCoord);
768 FromTexGroup->addChild(placeHolder); // Room for texCoordBinding
769 FromMatGroup = new SoGroup;
770 FromTexSwitch->addChild(FromMatGroup);
771 FromMatGroup->addChild(placeHolder); // Room for material
772 FromMatGroup->addChild(placeHolder); // Room for materialBinding
773
774 TransparentMaterial = new SoMaterial;
775 TransparentMaterial->diffuseColor.setIgnored(TRUE);
776 TransparentMaterial->ambientColor.setIgnored(TRUE);
777 TransparentMaterial->specularColor.setIgnored(TRUE);
778 TransparentMaterial->emissiveColor.setIgnored(TRUE);
779 TransparentMaterial->shininess.setIgnored(TRUE);
780 TransparentMaterial->transparency.setValue(0.0);
781 FromStuff->addChild(TransparentMaterial);
782 FromStuff->addChild(TheShape->getSceneGraph());
783
784 setToFromStuff(0);
785
786 camera->viewAll(root, SbVec2s(1,1));
787 camera->nearDistance = 0.1;
788 camera->farDistance = 500.0;
789
790 if (!nobackground)
791 {
792 background = new FlashBackground(20);
793 root->addChild(background->getSceneGraph());
794 }
795
796 SoXtExaminerViewer *viewer = buildUI(appWindow, root, camera);
797
798 // Setup a render abort to turn off texturing if it is too slow:
799 viewer->getGLRenderAction()->setAbortCallback(checkTimeCallback,
800 viewer->getGLRenderAction());
801
802 animationSensor = new SoIdleSensor(morfCallback, NULL);
803 // The animation sensor is scheduled the first time the scene is
804 // rendered.
805
806 SoXt::show( appWindow );
807 SoXt::mainLoop();
808
809 return 0;
810 }