forked from coin3d/sogui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoGuiRenderArea.cpp.in
2262 lines (1901 loc) · 67.9 KB
/
SoGuiRenderArea.cpp.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @configure_input@
/**************************************************************************\
* Copyright (c) Kongsberg Oil & Gas Technologies AS
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/
/**************************************************************************\
*
* A WORD OF ADVICE
*
* It is fruitless to modify the contents of the So@[email protected] file
* because it is autogenerated by configure from the SoGuiRenderArea.cpp.in
* file which you will find in the src/Inventor/@Gui@/common/ directory.
* Do your modifications to that file instead.
*
\**************************************************************************/
// *************************************************************************
/*!
\class So@Gui@RenderArea Inventor/@Gui@/So@[email protected]
\brief The So@Gui@RenderArea class adds scene graph handling and event management.
\ingroup components viewers
The So@Gui@RenderArea class is a component that adds scene graph
management and input device event handling to the So@Gui@GLWidget
component.
The class has many convenient methods for controlling aspects of the
rendering, like for instance transparency, aliasing and for
scheduling of redraws.
Native toolkit events are caught by So@Gui@RenderArea components,
translated to Coin SoEvent instances and passed on to the
scene graph, in case the user is doing interactive operations on for
instance Coin geometry draggers.
So@Gui@RenderArea is the first non-abstract component in its
inheritance hierarchy that you can use directly from client
application code to set up a scene graph viewer canvas.
For an So@Gui@RenderArea component to properly display your
scene graph, it must contain an SoCamera-derived node and at least
one SoLight-derived light source node.
Here's a complete, stand-alone example on how to set up an
So@Gui@RenderArea with a scene graph:
\code
#include <Inventor/@Gui@/So@[email protected]>
#include <Inventor/@Gui@/So@[email protected]>
#include <Inventor/nodes/SoCube.h>
#include <Inventor/nodes/SoRotor.h>
#include <Inventor/nodes/SoArray.h>
#include <Inventor/nodes/SoDirectionalLight.h>
#include <Inventor/nodes/SoPerspectiveCamera.h>
#include <Inventor/nodes/SoSeparator.h>
// Set up a simple scene graph, just for demonstration purposes.
static SoSeparator *
get_scene_graph(void)
{
SoSeparator * root = new SoSeparator;
SoGroup * group = new SoGroup;
SoRotor * rotor = new SoRotor;
rotor->rotation = SbRotation(SbVec3f(0.2, 0.5, 0.9), M_PI/4.0);
group->addChild(rotor);
SoCube * cube = new SoCube;
group->addChild(cube);
SoArray * array = new SoArray;
array->origin = SoArray::CENTER;
array->addChild(group);
array->numElements1 = 2;
array->numElements2 = 2;
array->separation1 = SbVec3f(4, 0, 0);
array->separation2 = SbVec3f(0, 4, 0);
root->addChild(array);
return root;
}
int
main(int argc, char ** argv)
{
@WIDGET@ window = So@Gui@::init(argv[0]);
SoSeparator * root = new SoSeparator;
root->ref();
SoPerspectiveCamera * camera;
root->addChild(camera = new SoPerspectiveCamera);
root->addChild(new SoDirectionalLight);
SoSeparator * userroot = get_scene_graph();
root->addChild(userroot);
So@Gui@RenderArea * renderarea = new So@Gui@RenderArea(window);
camera->viewAll(userroot, renderarea->getViewportRegion());
renderarea->setSceneGraph(root);
renderarea->setBackgroundColor(SbColor(0.0f, 0.2f, 0.3f));
if (argc > 1) {
renderarea->setTitle(argv[1]);
renderarea->setIconTitle(argv[1]);
}
renderarea->show();
So@Gui@::show(window);
So@Gui@::mainLoop();
delete renderarea;
root->unref();
return 0;
}
\endcode
*/
// *************************************************************************
#include <Inventor/@Gui@/So@[email protected]>
#include <cstring> // strchr()
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H
#if SOQT_DEBUG // For the "soinfo" debugging backdoor.
#include <qapplication.h>
#endif // SOQT_DEBUG
#include <Inventor/@Gui@/common/gl.h> // glDrawBuffer()
#include <Inventor/actions/SoSearchAction.h>
#include <Inventor/actions/SoWriteAction.h>
#include <Inventor/errors/SoDebugError.h>
#include <Inventor/events/SoKeyboardEvent.h>
#include <Inventor/misc/SoBasic.h>
#include <Inventor/nodekits/SoBaseKit.h>
#include <Inventor/nodes/SoCamera.h>
#include <Inventor/nodes/SoSelection.h>
#include <Inventor/SoOffscreenRenderer.h>
#include <so@[email protected]>
#include <Inventor/@Gui@/So@[email protected]>
#include <Inventor/@Gui@/So@[email protected]>
#include <Inventor/@Gui@/devices/So@[email protected]>
#include <Inventor/@Gui@/devices/So@[email protected]>
#ifdef HAVE_JOYSTICK_LINUX
#include <Inventor/@Gui@/devices/So@[email protected]>
#endif // HAVE_JOYSTICK_LINUX
#ifdef HAVE_SPACENAV_SUPPORT
#include <Inventor/@Gui@/devices/So@[email protected]>
#endif // HAVE_SPACENAV_SUPPORT
#include <Inventor/@Gui@/So@[email protected]>
#include <Inventor/@Gui@/SoAny.h>
#define RENDERAREA_DEBUG_REDRAWS 0
#define PRIVATE(obj) ((obj)->pimpl)
#define PUBLIC(obj) ((obj)->pub)
// *************************************************************************
SO@GUI@_OBJECT_SOURCE(So@Gui@RenderArea);
// *************************************************************************
#ifndef DOXYGEN_SKIP_THIS
class So@Gui@RenderAreaP {
public:
So@Gui@RenderAreaP(class So@Gui@RenderArea * pub);
~So@Gui@RenderAreaP(void);
SbBool clear;
SbBool clearZBuffer;
SbBool clearOverlay;
SoSceneManager * normalManager;
SoSceneManager * overlayManager;
SbColor * normalColormap;
int normalColormapSize;
int normalColormapStart;
SbColor * overlayColormap;
int overlayColormapSize;
int overlayColormapStart;
SbPList * devicelist;
struct {
So@Gui@Keyboard * keyboard;
So@Gui@Mouse * mouse;
#ifdef HAVE_SPACENAV_SUPPORT
So@Gui@SpacenavDevice * spacenav;
#endif // HAVE_SPACENAV_SUPPORT
} devices;
SbBool autoRedraw;
void replaceSoSelectionMonitor(SoSelection * newsel, SoSelection * oldsel) const;
SoSelection * normalselection;
SoSelection * overlayselection;
static const int GL_DEFAULT_MODE;
void constructor(SbBool mouseInput, SbBool keyboardInput, SbBool build);
static void renderCB(void * user, SoSceneManager * manager);
static void selection_redraw_cb(void * data, SoSelection * sel);
void setDevicesWindowSize(const SbVec2s size);
// OpenGL info-window hack.
enum { NONE, OPENGL, INVENTOR, TOOLKIT, DUMPSCENEGRAPH, DUMPCAMERAS, OFFSCREENGRAB };
int checkMagicSequences(const char c);
void showOpenGLDriverInformation(void);
void showInventorInformation(void);
void showToolkitInformation(void);
void dumpScenegraph(void);
void dumpCameras(void);
void offScreenGrab(void);
SbBool invokeAppCB(@EVENT@ event);
const SoEvent * getSoEvent(@EVENT@ event);
So@Gui@RenderAreaEventCB * appeventhandler;
void * appeventhandlerdata;
private:
So@Gui@RenderArea * pub; // public interface class
SbString currentinput; // For the OpenGL info-window hack.
};
const int So@Gui@RenderAreaP::GL_DEFAULT_MODE = (SO_GL_RGB |
SO_GL_ZBUFFER |
SO_GL_DOUBLE );
#if SO@GUI@_DEBUG && defined(__COIN__)
// Disabled when compiling against SGI / TGS Inventor, as we're using
// our Coin-specific extension SbString::sprintf() a lot.
#define DEBUGGING_EGGS 1
#endif // SO@GUI@_DEBUG && __COIN__
// Note: assumes a valid current OpenGL context.
void
So@Gui@RenderAreaP::showOpenGLDriverInformation(void)
{
#if DEBUGGING_EGGS
const GLubyte * vendor = glGetString(GL_VENDOR);
const GLubyte * renderer = glGetString(GL_RENDERER);
const GLubyte * version = glGetString(GL_VERSION);
const GLubyte * extensions = glGetString(GL_EXTENSIONS);
SbString info = "GL_VENDOR: \""; info += (const char *)vendor; info += "\"\n";
info += "GL_RENDERER: \""; info += (const char *)renderer; info += "\"\n";
info += "GL_VERSION: \""; info += (const char *)version; info += "\"\n";
info += "GL_EXTENSIONS: \"\n ";
SbString exts = (const char *)extensions;
const char * p;
int count = 0;
// (the extra parentheses in the while-expression kills a gcc warning)
while ((p = strchr(exts.getString(), ' '))) {
const char * start = exts.getString();
info += exts.getSubString(0, int(p - start));
exts.deleteSubString(0, int(p - start));
count++;
if (count == 4) { // number of extensions listed on each line
info += "\n ";
count = 0;
}
}
if (exts.getLength() > 0) { info += "\n "; info += exts; }
info += "\"\n";
// FIXME: should also show available GLX / WGL / AGL
// extensions. 20020802 mortene.
// Misc implementation info
{
SbVec2f range;
float granularity;
PUBLIC(this)->getPointSizeLimits(range, granularity);
SbString s;
s.sprintf("glPointSize(): range=[%f, %f], granularity=%f\n",
range[0], range[1], granularity);
info += s;
PUBLIC(this)->getLineWidthLimits(range, granularity);
s.sprintf("glLineWidth(): range=[%f, %f], granularity=%f\n",
range[0], range[1], granularity);
info += s;
GLint depthbits[1];
glGetIntegerv(GL_DEPTH_BITS, depthbits);
s.sprintf("GL_DEPTH_BITS==%d\n", depthbits[0]);
info += s;
GLint colbits[4];
glGetIntegerv(GL_RED_BITS, &colbits[0]);
glGetIntegerv(GL_GREEN_BITS, &colbits[1]);
glGetIntegerv(GL_BLUE_BITS, &colbits[2]);
glGetIntegerv(GL_ALPHA_BITS, &colbits[3]);
s.sprintf("GL_[RED|GREEN|BLUE|ALPHA]_BITS==[%d, %d, %d, %d]\n",
colbits[0], colbits[1], colbits[2], colbits[3]);
info += s;
GLint accumbits[4];
glGetIntegerv(GL_ACCUM_RED_BITS, &accumbits[0]);
glGetIntegerv(GL_ACCUM_GREEN_BITS, &accumbits[1]);
glGetIntegerv(GL_ACCUM_BLUE_BITS, &accumbits[2]);
glGetIntegerv(GL_ACCUM_ALPHA_BITS, &accumbits[3]);
s.sprintf("GL_ACCUM_[RED|GREEN|BLUE|ALPHA]_BITS==[%d, %d, %d, %d]\n",
accumbits[0], accumbits[1], accumbits[2], accumbits[3]);
info += s;
GLint stencilbits;
glGetIntegerv(GL_STENCIL_BITS, &stencilbits);
s.sprintf("GL_STENCIL_BITS==%d\n", stencilbits);
info += s;
GLint maxdims[2];
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, maxdims);
s.sprintf("GL_MAX_VIEWPORT_DIMS==<%d, %d>\n", maxdims[0], maxdims[1]);
info += s;
GLint texdim;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texdim);
s.sprintf("GL_MAX_TEXTURE_SIZE==%d\n", texdim);
info += s;
GLint maxlights;
glGetIntegerv(GL_MAX_LIGHTS, &maxlights);
s.sprintf("GL_MAX_LIGHTS==%d\n", maxlights);
info += s;
GLint maxplanes;
glGetIntegerv(GL_MAX_CLIP_PLANES, &maxplanes);
s.sprintf("GL_MAX_CLIP_PLANES==%d\n", maxplanes);
info += s;
// FIXME: other implementation specifics to print are
//
// * maximum stack depths (attribute, modelview matrix, name,
// projection matrix, texture matrix)
//
// * max display list nesting
//
// * max 3D texture size (needs specific extension?)
//
// 20020802 mortene.
}
SbString s;
s.sprintf("\n"
"Rendering is %sdirect.\n",
SoGuiGLWidgetP::isDirectRendering(PUBLIC(this)) ? "" : "in");
info += s;
So@Gui@::createSimpleErrorDialog(NULL, "OpenGL driver information",
info.getString());
#endif // DEBUGGING_EGGS
}
void
So@Gui@RenderAreaP::showInventorInformation(void)
{
#if DEBUGGING_EGGS
SbString info;
info.sprintf("%s\n", SoDB::getVersion());
// Display calculated maximum resolution of SbTime::getTimeOfDay().
{
const double DURATION = 0.2; // in seconds
SbTime current = SbTime::getTimeOfDay();
SbTime end(current + DURATION);
SbTime last = current;
unsigned int ticks = 0;
do {
current = SbTime::getTimeOfDay();
if (current.getValue() != last.getValue()) { ticks++; last = current; }
} while (current < end);
SbString s;
s.sprintf("\nSbTime::getTimeOfDay() resolution: ~ %d Hz\n",
(int)(((double)ticks) / DURATION));
info += s;
}
// FIXME: dump list of available node classes? 20010927 mortene.
So@Gui@::createSimpleErrorDialog(NULL, "Inventor implementation info",
info.getString());
#endif // DEBUGGING_EGGS
}
void
So@Gui@RenderAreaP::showToolkitInformation(void)
{
#if DEBUGGING_EGGS
SbString info = "So@Gui@ version "; info += SO@GUI@_VERSION; info += "\n";
#if SO@GUI@_MAKE_DLL
info += "Built as Microsoft Windows DLL.\n";
#endif // !SO@GUI@_MAKE_DLL
// FIXME: include information about the underlying toolkit library,
// if possible, like we do for Qt below (i.e. Gtk version, Motif
// implementation, Microsoft Windows version, ...). 20010927 mortene.
#ifdef SOQT_INTERNAL
// Qt implementation info.
{
SbString s;
s.sprintf("\nQt version: %s\n", qVersion());
info += s;
}
#endif // SOQT_INTERNAL
// FIXME: information about DLL path(s) (both for the So@Gui@ and
// Coin/Inventor library) would be _extremely_ useful for debugging
// "remote" applications, as application programmers (including
// ourselves) tend to spread those files around misc disk drive
// directories -- especially on Microsoft Windows platforms. Mismatches for
// runtime binding and link-time binding then causes bugs which are
// impossible to make sense of.
//
// I don't know if any platforms have enough introspection
// functionality to enable us to do this, though. Should
// investigate. (update: GetModuleHandle() looks like the place to
// start looking in the Win32 API.)
//
// 20010927 mortene.
// OpenGL canvas settings.
{
SbString s;
s.sprintf("\nCurrent OpenGL canvas:\n"
" %sbuffer\n"
" drawing to %sbuffer\n"
" %s rendering%s\n"
" %s mode\n"
" with%s overlay planes\n",
PUBLIC(this)->isDoubleBuffer() ? "double" : "single",
PUBLIC(this)->isDrawToFrontBufferEnable() ? "front" : "back",
PUBLIC(this)->isStereoBuffer() ? "stereo" : "mono",
PUBLIC(this)->isQuadBufferStereo() ? " (OpenGL quadbuffer)" : "",
PUBLIC(this)->isRGBMode() ? "RGB" : "colorindex",
PUBLIC(this)->isOverlayRender() ? "" : "out");
// FIXME: information about the native OpenGL widget format?
// 20010927 mortene.
info += s;
}
// Underlying Inventor implementation.
{
SbString s;
s.sprintf("\nInventor implementation: %s\n", SoDB::getVersion());
info += s;
}
So@Gui@::createSimpleErrorDialog(NULL, "So@Gui@ implementation info",
info.getString());
#endif // DEBUGGING_EGGS
}
void
So@Gui@RenderAreaP::dumpScenegraph(void)
{
#ifdef DEBUGGING_EGGS
SoOutput out;
SbString filename = SbTime::getTimeOfDay().format();
filename += "-dump.iv";
SbBool ok = out.openFile(filename.getString());
if (!ok) {
SoDebugError::post("So@Gui@RenderAreaP::dumpScenegraph",
"couldn't open file '%s'", filename.getString());
return;
}
SoWriteAction wa(&out);
wa.apply(this->normalManager->getSceneGraph());
SoDebugError::postInfo("So@Gui@RenderAreaP::dumpScenegraph",
"dumped scene graph to '%s'", filename.getString());
#endif // DEBUGGING_EGGS
}
void
So@Gui@RenderAreaP::dumpCameras(void)
{
#ifdef DEBUGGING_EGGS
const SbBool kitsearch = SoBaseKit::isSearchingChildren();
SoBaseKit::setSearchingChildren(TRUE);
SoSearchAction search;
search.setType(SoCamera::getClassTypeId());
search.setInterest(SoSearchAction::ALL);
search.setSearchingAll(TRUE);
search.apply(this->normalManager->getSceneGraph());
SoBaseKit::setSearchingChildren(kitsearch);
const SoPathList & pl = search.getPaths();
const unsigned int numcams = pl.getLength();
SoDebugError::postInfo("So@Gui@RenderAreaP::dumpCameras",
"Number of cameras in scene graph: %d",
numcams);
for (unsigned int i = 0; i < numcams; i++) {
const SoPath * p = pl[i];
SoNode * n = p->getTail();
assert(n->isOfType(SoCamera::getClassTypeId()));
SoCamera * cam = (SoCamera *)n;
const SbVec3f pos = cam->position.getValue();
const SbRotation rot = cam->orientation.getValue();
SbVec3f axis;
float angle;
rot.getValue(axis, angle);
SoDebugError::postInfo("So@Gui@RenderAreaP::dumpCameras",
"type==%s, name=='%s', position==<%f, %f, %f>, "
"orientation-rotation==<%f, %f, %f>--%f",
cam->getTypeId().getName().getString(),
cam->getName().getString(),
pos[0], pos[1], pos[2],
axis[0], axis[1], axis[2], angle);
}
#endif // DEBUGGING_EGGS
}
/*
Behaviour controlled by environment variables
COIN_SOGRAB_GEOMETRY (maximum geometry - on-screen aspect is preserved)
COIN_SOGRAB_FILENAME (filename template - can use %d to insert counter)
Examples:
export COIN_SOGRAB_GEOMETRY=1024x768
export COIN_SOGRAB_FILENAME=c:\\grab%03d.png
*/
void
So@Gui@RenderAreaP::offScreenGrab(void)
{
#ifdef DEBUGGING_EGGS
static int maxwidth = -1;
static int maxheight = -1;
static int counter = 0;
static const char fallback_ext[] = ".rgb";
static const char fallback_name[] = "coingrab%03d.rgb";
/*
FIXME:
- schedule a regular render-pass and hook into the render
pipe-line to check all the interesting GL context features that
might need to be enabled for the offscreen renderer context.
Then set up the offscreen renderer context to match those
features, so the rendering becomes the same.
- create a clone of the on-screen renderaction to get the same
kind of custom rendering.
- check if we might be able to hook up the on-screen pre-render
callback to the offscreen renderer as well, if any point.
- disable accidentally enabled seek mode again (from typing 'osgrab').
20050606 larsa.
*/
counter++;
if ( maxwidth <= 0 ) {
const char * env =
SoAny::si()->getenv("COIN_SOGRAB_GEOMETRY");
if ( env ) {
sscanf(env, "%dx%d", &maxwidth, &maxheight);
}
if ( (maxwidth <= 0) || !env ) {
SbVec2s vp = PUBLIC(this)->getViewportRegion().getWindowSize();
maxwidth = vp[0];
maxheight = vp[1];
}
}
if ( maxwidth <= 0 || maxheight <= 0 ) {
SoDebugError::post("So@Gui@RenderAreaP::offScreenGrab",
"invalid geometry: %dx%d", maxwidth, maxheight);
return;
}
SbVec2s vp = PUBLIC(this)->getViewportRegion().getWindowSize();
const char * filenametpl =
SoAny::si()->getenv("COIN_SOGRAB_FILENAME");
if ( !filenametpl ) filenametpl = fallback_name;
SbString filename;
filename.sprintf(filenametpl, counter);
const char * ext = strrchr(filename.getString(), '.');
if ( !ext ) ext = fallback_ext;
ext++;
SbVec2s osvp(maxwidth, maxheight);
if ( vp[0] > maxwidth || vp[1] > maxheight ||
(vp[0] < maxwidth && vp[1] < maxheight) ) {
float onscaspect = float(vp[0]) / float(vp[1]);
float offscaspect = float(maxwidth) / float(maxheight);
osvp[1] = maxheight;
osvp[0] = short(maxheight * onscaspect);
if ( osvp[0] > maxwidth ) {
osvp[0] = maxwidth;
osvp[1] = short(maxwidth * (1.0f / onscaspect));
}
}
SoOffscreenRenderer os(osvp);
if ( !os.render(PUBLIC(this)->getSceneManager()->getSceneGraph()) ) {
return;
}
SbBool written = FALSE;
if (strcmp(ext, "rgb") == 0) {
written = os.writeToRGB(filename.getString());
}
else {
written = os.writeToFile(filename, ext);
}
if (written) {
SoDebugError::postInfo("So@Gui@RenderAreaP::offScreenGrab",
"wrote image #%d, %dx%d as '%s'",
counter, osvp[0], osvp[1],
filename.getString());
}
else {
SoDebugError::post("So@Gui@RenderAreaP::offScreenGrab",
"tried to write image '%s', but failed for unknown "
"reason",
filename.getString());
}
#endif // DEBUGGING_EGGS
}
int
So@Gui@RenderAreaP::checkMagicSequences(const char c)
{
#if DEBUGGING_EGGS
this->currentinput += c;
if (0) { // handy for debugging keyboard handling
SoDebugError::postInfo("So@Gui@RenderAreaP::checkMagicSequences",
"'%s'", this->currentinput.getString());
}
const int cl = this->currentinput.getLength();
static const char * keyseq[] = {
"glinfo", "ivinfo", "soinfo", "dumpiv", "cameras", "osgrab"
};
static const int id[] = {
So@Gui@RenderAreaP::OPENGL,
So@Gui@RenderAreaP::INVENTOR,
So@Gui@RenderAreaP::TOOLKIT,
So@Gui@RenderAreaP::DUMPSCENEGRAPH,
So@Gui@RenderAreaP::DUMPCAMERAS,
So@Gui@RenderAreaP::OFFSCREENGRAB
};
for (unsigned int i = 0; i < (sizeof(keyseq) / sizeof(keyseq[0])); i++) {
const int ml = (int)strlen(keyseq[i]);
if (cl >= ml && this->currentinput.getSubString(cl - ml) == keyseq[i]) {
return id[i];
}
}
// Limit memory usage.
if (cl > 1024) { this->currentinput = ""; }
#endif // DEBUGGING_EGGS
return So@Gui@RenderAreaP::NONE;
}
// This method sets the window size data in all the connected device
// classes.
void
So@Gui@RenderAreaP::setDevicesWindowSize(const SbVec2s size)
{
if (!this->devicelist) return;
const int num = this->devicelist->getLength();
for (int i = 0; i < num; i++)
((So@Gui@Device *)(*this->devicelist)[i])->setWindowSize(size);
}
// *************************************************************************
void
So@Gui@RenderAreaP::renderCB(void * closure, SoSceneManager * manager)
{
assert(closure && manager);
So@Gui@RenderArea * thisptr = (So@Gui@RenderArea *) closure;
if (manager == PRIVATE(thisptr)->normalManager) {
thisptr->render();
} else if (manager == PRIVATE(thisptr)->overlayManager) {
thisptr->renderOverlay();
} else {
#if SO@GUI@_DEBUG
SoDebugError::post("So@Gui@RenderAreaP::renderCB",
"invoked for unknown SoSceneManager (%p)", manager);
#endif // SO@GUI@_DEBUG
return;
}
}
// Callback for automatic redraw on SoSelection changes.
void
So@Gui@RenderAreaP::selection_redraw_cb(void * closure, SoSelection * sel)
{
So@Gui@RenderArea * ra = (So@Gui@RenderArea *) closure;
if (sel == PRIVATE(ra)->normalselection)
ra->scheduleRedraw();
else if (sel == PRIVATE(ra)->overlayselection)
ra->scheduleOverlayRedraw();
else
assert(0 && "callback on unknown SoSelection node");
}
// Private class constructor.
So@Gui@RenderAreaP::So@Gui@RenderAreaP(So@Gui@RenderArea * api)
{
PUBLIC(this) = api;
this->normalManager = new SoSceneManager;
this->overlayManager = new SoSceneManager;
this->normalColormap = NULL;
this->normalColormapSize = 0;
this->overlayColormap = NULL;
this->overlayColormapSize = 0;
this->clear = TRUE;
this->clearZBuffer = TRUE;
this->clearOverlay = TRUE;
this->autoRedraw = TRUE;
this->normalselection = NULL;
this->overlayselection = NULL;
this->devices.mouse = NULL;
this->devices.keyboard = NULL;
#ifdef HAVE_SPACENAV_SUPPORT
this->devices.spacenav = NULL;
#endif // HAVE_SPACENAV_SUPPORT
}
// Private class destructor.
So@Gui@RenderAreaP::~So@Gui@RenderAreaP()
{
delete this->normalManager;
delete this->overlayManager;
delete [] this->normalColormap;
delete [] this->overlayColormap;
}
// Common code for all constructors.
void
So@Gui@RenderAreaP::constructor(SbBool mouseInput,
SbBool keyboardInput,
SbBool build)
{
this->normalManager->setRenderCallback(So@Gui@RenderAreaP::renderCB, PUBLIC(this));
this->normalManager->activate();
this->overlayManager->setRenderCallback(So@Gui@RenderAreaP::renderCB, PUBLIC(this));
this->overlayManager->activate();
// FIXME: what is this magic number doing here - shouldn't we use
// SoGLCacheContextElement::getUniqueCacheContext() for Coin, and
// magic numbers just for SGI / TGS Inventor?
//
// On a side note: won't this code fail if we construct several
// So@Gui@RenderArea instances with overlays? They will all use
// cachecontext==1 for their SoGLRenderAction instances -- is that
// kosher?
//
// 20010831 mortene.
this->overlayManager->getGLRenderAction()->setCacheContext(1);
this->appeventhandler = NULL;
this->appeventhandlerdata = NULL;
this->devicelist = new SbPList;
if (mouseInput) {
this->devices.mouse = new So@Gui@Mouse;
PUBLIC(this)->registerDevice(this->devices.mouse);
}
if (keyboardInput) {
this->devices.keyboard = new So@Gui@Keyboard;
PUBLIC(this)->registerDevice(this->devices.keyboard);
}
if (! build) return;
PUBLIC(this)->setClassName("So@Gui@RenderArea");
@WIDGET@ glarea = PUBLIC(this)->buildWidget(PUBLIC(this)->getParentWidget());
PUBLIC(this)->setBaseWidget(glarea);
PUBLIC(this)->setSize(SbVec2s(400, 400));
}
// This method invokes the application event handler, if one is set.
SbBool
So@Gui@RenderAreaP::invokeAppCB(@EVENT@ event)
{
if (this->appeventhandler != NULL)
return this->appeventhandler(this->appeventhandlerdata, event);
return FALSE;
}
// This method returns an SoEvent * corresponding to the given \a
// event, or \c NULL if there are none.
const SoEvent *
So@Gui@RenderAreaP::getSoEvent(@EVENT@ event)
{
if (!this->devicelist)
return (SoEvent *) NULL;
const SoEvent * soevent = NULL;
const int num = this->devicelist->getLength();
for (int i = 0; (i < num) && (soevent == NULL); i++)
soevent = ((So@Gui@Device *)(*this->devicelist)[i])->translateEvent(event);
return soevent;
}
#endif // DOXYGEN_SKIP_THIS
// *************************************************************************
/*!
Public constructor.
*/
So@Gui@RenderArea::So@Gui@RenderArea(@WIDGET@ parent,
const char * name,
SbBool embed,
SbBool mouseInput,
SbBool keyboardInput)
: inherited(parent, name, embed, So@Gui@RenderAreaP::GL_DEFAULT_MODE, FALSE)
{
PRIVATE(this) = new So@Gui@RenderAreaP(this);
PRIVATE(this)->constructor(mouseInput, keyboardInput, TRUE);
}
/*!
Protected constructor used by derived classes.
*/
So@Gui@RenderArea::So@Gui@RenderArea(@WIDGET@ parent,
const char * name,
SbBool embed,
SbBool mouseInput,
SbBool keyboardInput,
SbBool build)
: inherited(parent, name, embed, So@Gui@RenderAreaP::GL_DEFAULT_MODE, FALSE)
{
PRIVATE(this) = new So@Gui@RenderAreaP(this);
PRIVATE(this)->constructor(mouseInput, keyboardInput, build);
}
/*!
Destructor.
*/
So@Gui@RenderArea::~So@Gui@RenderArea()
{
// Clean out any callbacks we may have registered with SoSelection
// nodes.
this->redrawOverlayOnSelectionChange(NULL);
this->redrawOnSelectionChange(NULL);
for (int i = PRIVATE(this)->devicelist->getLength() - 1; i >= 0; i--) {
So@Gui@Device * device = (So@Gui@Device *) ((*PRIVATE(this)->devicelist)[i]);
this->unregisterDevice(device);
delete device;
}
delete PRIVATE(this)->devicelist;
delete PRIVATE(this);
}
// *************************************************************************
/*!
This method adds \a device to the list of devices handling events
for this component.
*/
void
So@Gui@RenderArea::registerDevice(So@Gui@Device * device)
{
int idx = PRIVATE(this)->devicelist->find(device);
if (idx != -1) {
#if SO@GUI@_DEBUG
SoDebugError::postWarning("So@Gui@RenderArea::registerDevice",
"device already registered");
#endif // SO@GUI@_DEBUG
return;
}
PRIVATE(this)->devicelist->append(device);
@WIDGET@ w = this->getGLWidget();
if (w != NULL) {
#ifdef __COIN_SOXT__
device->enable(w, (SoXtEventHandler *) &So@Gui@GLWidget::eventHandler, (void *)this);
#else
device->enable(w, &So@Gui@GLWidgetP::eventHandler, (void *)this);
#endif
device->setWindowSize(this->getGLSize());
}
}
/*!
This method removes \a device from the list of devices handling
events for this component.
*/
void
So@Gui@RenderArea::unregisterDevice(So@Gui@Device * device)
{
assert(PRIVATE(this)->devicelist != NULL);
const int idx = PRIVATE(this)->devicelist->find(device);
if (idx == -1) {
#if SO@GUI@_DEBUG
SoDebugError::post("So@Gui@RenderArea::unregisterDevice",
"tried to remove nonexisting device");
#endif // SO@GUI@_DEBUG
return;
}
PRIVATE(this)->devicelist->remove(idx);
@WIDGET@ w = this->getGLWidget();
if (w != NULL) { device->disable(w, NULL, NULL); }
}
// *************************************************************************
// Documented in superclass.
void
So@Gui@RenderArea::afterRealizeHook(void)
{
inherited::afterRealizeHook();
#ifdef HAVE_JOYSTICK_LINUX
if (So@Gui@LinuxJoystick::exists())
this->registerDevice(new So@Gui@LinuxJoystick);
#endif // HAVE_JOYSTICK_LINUX
#ifdef HAVE_SPACENAV_SUPPORT
PRIVATE(this)->devices.spacenav = new So@Gui@SpacenavDevice;
this->registerDevice(PRIVATE(this)->devices.spacenav);
#endif // HAVE_SPACENAV_SUPPORT
}
/*!
This method sets the scene graph to be rendered in the normal bitmap
planes.
\sa getSceneGraph(), setOverlaySceneGraph()