Debian Codegraph @

lib/interaction/src/SoSceneMgr.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 * Copyright (C) 1990,91,92 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.1.1.1 $
44 |
45 | Classes:
46 | SoSceneManager
47 |
48 | Author(s): David Mott, Alain Dumesny
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include <Inventor/SoDB.h>
55 #include <Inventor/SbTime.h>
56 #include <Inventor/SbLinear.h>
57 #include <Inventor/SbViewportRegion.h>
58 #include <Inventor/errors/SoDebugError.h>
59 #include <Inventor/nodes/SoNode.h>
60 #include <Inventor/SoSceneManager.h>
61 #include <Inventor/fields/SoSFTime.h>
62 #include <Inventor/sensors/SoSensor.h>
63 #include <Inventor/sensors/SoNodeSensor.h>
64 #include <Inventor/sensors/SoOneShotSensor.h>
65 #include <Inventor/actions/SoGLRenderAction.h>
66 #include <Inventor/actions/SoHandleEventAction.h>
67
68 #include <GL/gl.h>
69 #include <GL/glu.h>
70 #include <GL/glx.h>
71
72 #include <SoDebug.h>
73
74 // Sensor which touches realTime as soon as possible after a redraw
75 SoSFTime *SoSceneManager::realTime = NULL;
76 SoOneShotSensor *SoSceneManager::realTimeSensor = NULL;
77 SbBool SoSceneManager::updateRealTime = TRUE;
78
79
80 ////////////////////////////////////////////////////////////////////////
81 //
82 // Constructor
83 //
84 SoSceneManager::SoSceneManager()
85 //
86 ////////////////////////////////////////////////////////////////////////
87 {
88 bkgColor.setValue(0,0,0);
89 bkgIndex = 0;
90 rgbMode = TRUE;
91 graphicsInitNeeded = TRUE;
92
93 // inventor specific variables
94 scene = NULL;
95 raCreatedHere = TRUE;
96 renderAction = new SoGLRenderAction(SbVec2s(1,1));
97 heaCreatedHere = TRUE;
98 handleEventAction = new SoHandleEventAction(SbVec2s(1,1));
99 renderCB = NULL;
100 renderCBData = NULL;
101 active = FALSE;
102 needToSendVP = TRUE;
103
104 // create the scene sensor (used for automatic rendering)
105 // do not attach the sensor here. That's done when we
106 // become active, and the user supplies a redraw callback.
107 sceneSensor = new SoNodeSensor;
108 sceneSensor->setData((void *) this);
109 #ifdef DEBUG
110 if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) {
111 SoDebug::NamePtr("sceneChangeSensor", sceneSensor);
112 }
113 #endif
114
115 // Assume default priority now. We'll set the sensor priority
116 // before we attach, just in case the user changes the priority
117 // before that occurs.
118 setRedrawPriority(getDefaultRedrawPriority());
119
120 // setup the sensor to touch real time after a redraw
121 if (! realTimeSensor) {
122 realTime = (SoSFTime *) SoDB::getGlobalField("realTime");
123 realTimeSensor = new SoOneShotSensor;
124 realTimeSensor->setFunction((SoSensorCB *)
125 &SoSceneManager::realTimeSensorCB);
126 #ifdef DEBUG
127 if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) {
128 SoDebug::NamePtr("realTimeSensor", realTimeSensor);
129 }
130 #endif
131 }
132 }
133
134 ////////////////////////////////////////////////////////////////////////
135 //
136 // Destructor
137 //
138 SoSceneManager::~SoSceneManager()
139 //
140 ////////////////////////////////////////////////////////////////////////
141 {
142 // delete actions
143 if (raCreatedHere)
144 delete renderAction;
145
146 if (heaCreatedHere)
147 delete handleEventAction;
148
149 // detach the scene
150 setSceneGraph(NULL);
151 delete sceneSensor;
152 }
153
154 ////////////////////////////////////////////////////////////////////////
155 //
156 // Set a new render action, for instance for highlighting.
157 //
158 // public
159 //
160 void
161 SoSceneManager::setGLRenderAction(SoGLRenderAction *ra)
162 //
163 ////////////////////////////////////////////////////////////////////////
164 {
165 #ifdef DEBUG
166 if (ra == NULL)
167 SoDebugError::post("SoSceneManager::setGLRenderAction",
168 "a NULL render action was passed. This is bad!");
169 #endif
170
171 // Make sure the viewport region is set
172 // (be paranoid and check for NULL)
173 if (renderAction != NULL) {
174 SbViewportRegion rgn(renderAction->getViewportRegion());
175 ra->setViewportRegion(rgn);
176 }
177
178 // Get rid of the old render action if it was created here.
179 if (raCreatedHere) {
180 delete renderAction;
181 raCreatedHere = FALSE;
182 }
183
184 // Set to the new render action.
185 renderAction = ra;
186 }
187
188 ////////////////////////////////////////////////////////////////////////
189 //
190 // Set a new handle event action. Passing NULL will shut off event
191 // handling.
192 //
193 // public
194 //
195 void
196 SoSceneManager::setHandleEventAction(SoHandleEventAction *hea)
197 //
198 ////////////////////////////////////////////////////////////////////////
199 {
200 // Make sure the viewport region is set
201 if ((handleEventAction != NULL) && (hea != NULL)) {
202 SbViewportRegion rgn(handleEventAction->getViewportRegion());
203 hea->setViewportRegion(rgn);
204 }
205
206 // Get rid of the old render action if it was created here.
207 if (heaCreatedHere) {
208 delete handleEventAction;
209 heaCreatedHere = FALSE;
210 }
211
212 // Set to the new render action.
213 handleEventAction = hea;
214 }
215
216 ////////////////////////////////////////////////////////////////////////
217 //
218 // Description:
219 // Make the new user supplied scene graph the rendering root.
220 //
221 // use: virtual public
222 //
223 void
224 SoSceneManager::setSceneGraph(SoNode *newScene)
225 //
226 ////////////////////////////////////////////////////////////////////////
227 {
228 SbBool currentlyActive = isActive();
229 deactivate();
230
231 // ref the new scene
232 if (newScene != NULL)
233 newScene->ref();
234
235 // check if there already is a scene graph
236 if (scene != NULL)
237 scene->unref();
238
239 // now set the new scene graph
240 scene = newScene;
241
242 if (currentlyActive)
243 activate();
244 }
245
246 ////////////////////////////////////////////////////////////////////////
247 //
248 // Description:
249 // Return the user supplied scene graph.
250 //
251 // use: virtual public
252 //
253 SoNode *
254 SoSceneManager::getSceneGraph() const
255 //
256 ////////////////////////////////////////////////////////////////////////
257 { return scene; }
258
259
260 ////////////////////////////////////////////////////////////////////////
261 //
262 // Description:
263 // Process the passed X event.
264 //
265 // use: virtual protected
266 //
267 SbBool
268 SoSceneManager::processEvent(const SoEvent *event)
269 //
270 ////////////////////////////////////////////////////////////////////////
271 {
272 if ((scene != NULL) && (handleEventAction != NULL)) {
273 handleEventAction->setEvent(event);
274 handleEventAction->apply(scene);
275
276 return handleEventAction->isHandled();
277 }
278 else
279 return FALSE;
280 }
281
282 ////////////////////////////////////////////////////////////////////////
283 //
284 // Initialize the window for rendering.
285 // A window MUST be set before this is called.
286 //
287 // use: public
288 //
289 void
290 SoSceneManager::reinitialize()
291 //
292 ////////////////////////////////////////////////////////////////////////
293 {
294 graphicsInitNeeded = TRUE;
295
296 // we have a new window, so re-init the render action
297 renderAction->invalidateState();
298 }
299
300
301 ////////////////////////////////////////////////////////////////////////
302 //
303 // Set the size of the window.
304 //
305 // use: public
306 //
307 void
308 SoSceneManager::setWindowSize(const SbVec2s &newSize)
309 //
310 ////////////////////////////////////////////////////////////////////////
311 {
312 SbViewportRegion rgn(renderAction->getViewportRegion());
313 rgn.setWindowSize(newSize);
314
315 if (renderAction != NULL)
316 renderAction->setViewportRegion(rgn);
317
318 if (handleEventAction != NULL)
319 handleEventAction->setViewportRegion(rgn);
320
321 // make sure to call glViewport() with the new size
322 needToSendVP = TRUE;
323 }
324
325 ////////////////////////////////////////////////////////////////////////
326 //
327 // Set the size of the window.
328 //
329 // use: public
330 //
331 const SbVec2s &
332 SoSceneManager::getWindowSize() const
333 //
334 ////////////////////////////////////////////////////////////////////////
335 {
336 return renderAction->getViewportRegion().getWindowSize();
337 }
338
339 ////////////////////////////////////////////////////////////////////////
340 //
341 // Set the size of the viewport within the window.
342 //
343 // use: public
344 //
345 void
346 SoSceneManager::setSize(const SbVec2s &newSize)
347 //
348 ////////////////////////////////////////////////////////////////////////
349 {
350 SbViewportRegion rgn(renderAction->getViewportRegion());
351 const SbVec2s &origin = rgn.getViewportOriginPixels();
352 rgn.setViewportPixels(origin, newSize);
353
354 if (renderAction != NULL)
355 renderAction->setViewportRegion(rgn);
356
357 if (handleEventAction != NULL)
358 handleEventAction->setViewportRegion(rgn);
359 }
360
361 ////////////////////////////////////////////////////////////////////////
362 //
363 // Set origin relative to the window.
364 //
365 // use: public
366 //
367 void
368 SoSceneManager::setOrigin(const SbVec2s &newOrigin)
369 //
370 ////////////////////////////////////////////////////////////////////////
371 {
372 SbViewportRegion rgn(renderAction->getViewportRegion());
373 const SbVec2s &size = rgn.getViewportSizePixels();
374 rgn.setViewportPixels(newOrigin, size);
375
376 if (renderAction != NULL)
377 renderAction->setViewportRegion(rgn);
378
379 if (handleEventAction != NULL)
380 handleEventAction->setViewportRegion(rgn);
381 }
382
383 ////////////////////////////////////////////////////////////////////////
384 //
385 // Get viewport size.
386 //
387 // use: public
388 //
389 const SbVec2s &
390 SoSceneManager::getSize() const
391 //
392 ////////////////////////////////////////////////////////////////////////
393 {
394 return renderAction->getViewportRegion().getViewportSizePixels();
395 }
396
397 ////////////////////////////////////////////////////////////////////////
398 //
399 // Get origin relative to the window.
400 //
401 // use: public
402 //
403 const SbVec2s &
404 SoSceneManager::getOrigin() const
405 //
406 ////////////////////////////////////////////////////////////////////////
407 {
408 return renderAction->getViewportRegion().getViewportOriginPixels();
409 }
410
411 ////////////////////////////////////////////////////////////////////////
412 //
413 // Set the viewport region instead of setting size and origin separately.
414 //
415 // use: public
416 //
417 void
418 SoSceneManager::setViewportRegion(const SbViewportRegion &rgn)
419 //
420 ////////////////////////////////////////////////////////////////////////
421 {
422 if (renderAction != NULL)
423 renderAction->setViewportRegion(rgn);
424
425 if (handleEventAction != NULL)
426 handleEventAction->setViewportRegion(rgn);
427 }
428
429 ////////////////////////////////////////////////////////////////////////
430 //
431 // Get the viewport region.
432 //
433 // use: public
434 //
435 const SbViewportRegion &
436 SoSceneManager::getViewportRegion() const
437 //
438 ////////////////////////////////////////////////////////////////////////
439 {
440 return renderAction->getViewportRegion();
441 }
442
443 ////////////////////////////////////////////////////////////////////////
444 //
445 // Set to RGB mode or color map mode.
446 //
447 // use: public
448 //
449 void
450 SoSceneManager::setRGBMode(SbBool onOrOff)
451 //
452 ////////////////////////////////////////////////////////////////////////
453 {
454 rgbMode = onOrOff;
455
456 // the render action determines color map/rgb at initialization
457 //???pauli renderAction->reinitialize();
458 }
459
460 ////////////////////////////////////////////////////////////////////////
461 //
462 // Description:
463 // Sets antialiasing on GL rendering action.
464 //
465 // use: public
466 //
467 void
468 SoSceneManager::setAntialiasing(SbBool smoothing, int numPasses)
469 //
470 ////////////////////////////////////////////////////////////////////////
471 {
472 renderAction->setSmoothing( smoothing );
473 renderAction->setNumPasses( numPasses );
474
475 // Set render action callback for multiple pass rendering. This
476 // makes sure to clear background to correct color between frames.
477 if ( numPasses > 1 )
478 renderAction->setPassCallback(antialiasingCallback, this);
479 else
480 renderAction->setPassCallback(NULL, NULL);
481 }
482
483 ////////////////////////////////////////////////////////////////////////
484 //
485 // Description:
486 // Gets antialiasing on GL rendering action.
487 //
488 // use: public
489 //
490 void
491 SoSceneManager::getAntialiasing(SbBool &smoothing, int &numPasses) const
492 //
493 ////////////////////////////////////////////////////////////////////////
494 {
495 smoothing = renderAction->isSmoothing();
496 numPasses = renderAction->getNumPasses();
497 }
498
499 ////////////////////////////////////////////////////////////////////////
500 //
501 // This routine is called to render the scene graph.
502 // A window MUST be set before this is called.
503 //
504 // use: public
505 //
506 void
507 SoSceneManager::render(SbBool clearWindow, SbBool clearZbuffer)
508 //
509 ////////////////////////////////////////////////////////////////////////
510 {
511 // reinitialize if necessary
512 if (graphicsInitNeeded) {
513 int numBits[1];
514 glGetIntegerv(GL_DEPTH_BITS, numBits);
515 needZbuffer = (numBits[0] != 0); // FALSE for overlay windows !
516 if (needZbuffer)
517 glDepthFunc(GL_LEQUAL); // needed for hidden line rendering
518 graphicsInitNeeded = FALSE;
519 }
520
521 //
522 // when the window changes size, we need to call glViewport() before
523 // we can do a color clear.
524 //
525 if (needToSendVP) {
526 const SbViewportRegion &theRegion = renderAction->getViewportRegion();
527 SbVec2s size = theRegion.getViewportSizePixels();
528 SbVec2s origin = theRegion.getViewportOriginPixels();
529 glViewport(origin[0], origin[1], size[0], size[1]);
530 needToSendVP = FALSE;
531 }
532
533 //
534 // clear to the background color and clear the zbuffer
535 //
536 if (clearWindow) {
537 if (rgbMode)
538 glClearColor(bkgColor[0], bkgColor[1], bkgColor[2], 0);
539 else glClearIndex(bkgIndex);
540
541 // clear the color+zbuffer at the same time if we can
542 if (needZbuffer && clearZbuffer)
543 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
544 else
545 glClear(GL_COLOR_BUFFER_BIT);
546 }
547 // check to see if only the zbuffer is needed...
548 else if (needZbuffer && clearZbuffer)
549 glClear(GL_DEPTH_BUFFER_BIT);
550
551 // render the scene graph!
552 if (scene != NULL)
553 renderAction->apply(scene);
554
555 // sensor doesn't need to fire again if it's still scheduled
556 sceneSensor->unschedule();
557
558 // schedule the realTime One shot sensor to update the real time
559 // as soon as we can now that we have rendered the scene. This will
560 // enable us to render things that are animating with a consistent
561 // time across mutiple renderAreas, while providing the maximum
562 // optainable frame rate (much better than a hard coded 30 or 60
563 // times/sec timer sensor).
564 if (updateRealTime)
565 realTimeSensor->schedule();
566 }
567
568 ////////////////////////////////////////////////////////////////////////
569 //
570 // Description:
571 // Callback to update the realTime global field.
572 //
573 // Use: static private
574
575 void
576 SoSceneManager::realTimeSensorCB()
577 //
578 ////////////////////////////////////////////////////////////////////////
579 {
580 realTime->setValue(SbTime::getTimeOfDay());
581 }
582
583 ////////////////////////////////////////////////////////////////////////
584 //
585 // Description:
586 // Enables/Disable the realTime update after each redraw
587 //
588 // Use: static public
589
590 void
591 SoSceneManager::enableRealTimeUpdate(SbBool flag)
592 //
593 ////////////////////////////////////////////////////////////////////////
594 {
595 if (updateRealTime == flag)
596 return;
597
598 updateRealTime = flag;
599
600 if (updateRealTime)
601 realTimeSensor->schedule();
602 else
603 realTimeSensor->unschedule();
604 }
605
606 ////////////////////////////////////////////////////////////////////////
607 //
608 // The scene manager will only employ sensors while it is active.
609 //
610 // use: public, virtual
611 //
612 void
613 SoSceneManager::activate()
614 {
615 // attach sceneSensor to top node for redrawing purpose
616 // only if the user has specified a redraw callback (i.e.
617 // auto-redraw is enabled)
618 if (renderCB != NULL) {
619 if (scene != NULL && sceneSensor->getAttachedNode() == NULL) {
620 sceneSensor->setFunction((SoSensorCB *)
621 &SoSceneManager::sceneSensorCallback);
622 sceneSensor->attach(scene);
623 }
624 }
625 active = TRUE;
626 }
627 void
628 SoSceneManager::deactivate()
629 {
630 sceneSensor->detach();
631 sceneSensor->setFunction(NULL);
632 active = FALSE;
633 }
634
635 ////////////////////////////////////////////////////////////////////////
636 //
637 // Set the rendering callback. The app can supply this to enable
638 // automatic redrawing of the scene. This callback should perform
639 // window specific graphics initialization, then call the scene manager
640 // render() method.
641 //
642 // use: public
643 //
644 void
645 SoSceneManager::setRenderCallback(
646 SoSceneManagerRenderCB *f,
647 void *userData)
648 //
649 ////////////////////////////////////////////////////////////////////////
650 {
651 renderCB = f;
652 renderCBData = userData;
653
654 if (f != NULL) {
655 // if we are active, attach the scene sensor
656 if (active) {
657 if (scene != NULL && sceneSensor->getAttachedNode() == NULL) {
658 sceneSensor->setFunction((SoSensorCB *)
659 &SoSceneManager::sceneSensorCallback);
660 sceneSensor->attach(scene);
661 }
662 }
663 }
664 else {
665 // detach the scene sensor (whether active or not)
666 sceneSensor->detach();
667 sceneSensor->setFunction(NULL);
668 }
669 }
670
671 ////////////////////////////////////////////////////////////////////////
672 //
673 // Priority of the redraw sensor
674 //
675 // public
676 //
677 void
678 SoSceneManager::setRedrawPriority(uint32_t priority)
679 //
680 ////////////////////////////////////////////////////////////////////////
681 {
682 sceneSensor->setPriority(priority);
683 }
684
685 ////////////////////////////////////////////////////////////////////////
686 //
687 // Priority of the redraw sensor
688 //
689 // public
690 //
691 uint32_t
692 SoSceneManager::getRedrawPriority() const
693 //
694 ////////////////////////////////////////////////////////////////////////
695 {
696 return sceneSensor->getPriority();
697 }
698
699
700 //
701 ////////////////////////////////////////////////////////////////////////
702 // static callbacks stubs
703 ////////////////////////////////////////////////////////////////////////
704 //
705
706 void
707 SoSceneManager::antialiasingCallback( void *r )
708 {
709 SoSceneManager *mgr = (SoSceneManager *) r;
710
711 if (mgr->rgbMode)
712 glClearColor(mgr->bkgColor[0], mgr->bkgColor[1], mgr->bkgColor[2], 0);
713 else glClearIndex( mgr->bkgIndex );
714
715 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
716 }
717
718 void
719 SoSceneManager::sceneSensorCallback(SoSceneManager *mgr, SoSensor *)
720 {
721 #ifdef DEBUG
722 if (mgr->renderCB == NULL) {
723 SoDebugError::post("SoSceneManager::sceneSensorCallback",
724 "Callback was fired, but auto-redraw is turned off.");
725 return;
726 }
727 if (! mgr->isActive()) {
728 SoDebugError::post("SoSceneManager::sceneSensorCallback",
729 "Callback was fired, but scene manager not active.");
730 return;
731 }
732 #endif
733
734 // redraw that scene graph!
735 mgr->redraw();
736 }
737
738 //
739 ////////////////////////////////////////////////////////////////////////
740 // real short methods
741 ////////////////////////////////////////////////////////////////////////
742 //
743
744 void
745 SoSceneManager::redraw()
746 {
747 if (renderCB != NULL)
748 (*renderCB)(renderCBData, this);
749 }
750
751 void
752 SoSceneManager::scheduleRedraw()
753 {
754 // The sceneSensor will only schedule itself if a CB func is set.
755 // We only set the sensor CB func if autoRedraw is on and we are active.
756 // Thus, there are no flags to check here. Just call schedule, and
757 // the sensor will do the right thing.
758 sceneSensor->schedule();
759 }
760
761 void
762 SoSceneManager::setBackgroundColor(const SbColor &c)
763 {
764 bkgColor = c;
765 if (isRGBMode())
766 scheduleRedraw();
767 }
768
769 void
770 SoSceneManager::setBackgroundIndex(int index)
771 {
772 bkgIndex = index;
773 if (! isRGBMode())
774 scheduleRedraw();
775 }