root/OpenSceneGraph/trunk/src/osgPlugins/cfg/RenderSurface.h @ 13041

Revision 13041, 22.9 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- Producer - Copyright (C) 2001-2004  Don Burns
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12 */
13
14
15#ifndef OSGPRODUCER_RENDER_SURFACE
16#define OSGPRODUCER_RENDER_SURFACE 1
17
18#include <map>
19#include <string>
20#include <iostream>
21
22#include <osg/Referenced>
23#include <osg/ref_ptr>
24#include "VisualChooser.h"
25
26#ifdef _WIN32_IMPLEMENTATION
27  #include <vector>
28#endif
29
30namespace osgProducer {
31
32/**
33    \class RenderSurface
34    \brief A RenderSurface provides a rendering surface for 3D graphics applications.
35
36    A RenderSurface creates a window in a windowing system for the purpose of 3D
37    rendering.  The focus of a RenderSurface differs from a windowing system window
38    in that it is not a user input/output device, but rather a context and screen area
39    specifically designed for 3D applications.  Consequently, a RenderSurface does not
40    provide or impose a requirement on the caller to structure the application around
41    the capturing or handling of events.  Further, RenderSurface provides increased
42    control over the quality of pixel formats.
43 */
44
45class RenderSurface : public osg::Referenced
46{
47    public :
48
49        static const unsigned int UnknownDimension;
50        static const unsigned int UnknownAmount;
51        static unsigned int _numScreens;
52
53        class Callback : public osg::Referenced
54        {
55            public:
56                Callback() {}
57                virtual void operator()( const RenderSurface & ) = 0;
58
59            protected:
60                virtual ~Callback() {}
61        };
62
63        struct InputRectangle
64        {
65            public:
66                InputRectangle(): _left(-1.0), _bottom(-1.0), _width(2.0), _height(2.0) {}
67                InputRectangle( float left, float right, float bottom, float top ):
68                    _left(left), _bottom(bottom), _width(right-left), _height(top-bottom) {}
69                InputRectangle(const InputRectangle &ir)
70                {
71                     _left = ir._left;
72                     _bottom = ir._bottom;
73                     _width = ir._width;
74                     _height = ir._height;
75                }
76                virtual ~InputRectangle() {}
77
78                void set( float left, float right, float bottom, float top )
79                {
80                    _left = left;
81                    _bottom = bottom;
82                    _width = right - left;
83                    _height = top - bottom;
84                }
85                float left() const { return _left; }
86                float bottom() const { return _bottom; }
87                float width() const { return _width; }
88                float height() const { return _height; }
89
90            protected:
91
92            private:
93                float _left, _bottom, _width, _height;
94        };
95
96        enum DrawableType {
97            DrawableType_Window,
98            DrawableType_PBuffer
99        };
100
101
102        RenderSurface( void );
103
104        static unsigned int getNumberOfScreens(void);
105
106        void setDrawableType( DrawableType );
107        DrawableType getDrawableType();
108        void setReadDrawable( RenderSurface *);
109        RenderSurface* getReadDrawable() { return _readDrawableRenderSurface; }
110
111        void setInputRectangle( const InputRectangle &ir );
112        const InputRectangle &getInputRectangle() const;
113        void bindInputRectangleToWindowSize(bool);
114
115
116        /** Set the name of the Host the window is to be created on.
117            Ignored on Win32*/
118        void setHostName( const std::string & );
119
120        /**
121          * Get the name of the Host the window is to be created on.
122          * Ignored on Win32 */
123        const std::string& getHostName( void ) const;
124
125        /**
126          * Set the number of the display the render surface is to
127          * be created on.  In XWindows, this is the number of the
128          * XServer.  Ignored on Win32   */
129        void setDisplayNum( int );
130
131        /** Get the number of the display the render surface is to
132          * be created on.  In XWindows, this is the number of the
133          * XServer.  Ignored on Win32   */
134        int getDisplayNum( void ) const;
135
136        /** Set the number of the screen the render surface is to
137          * be created on.  In XWindows, this is the number of the
138          * XServer.  Ignored on Win32   */
139        void setScreenNum( int );
140
141        /** Get the number of the screen the render surface is to
142          * be created on.  In XWindows, this is the number of the
143          * XServer.  Ignored on Win32   */
144        int getScreenNum( void ) const;
145
146        /** Get the size of the screen in pixels the render surface
147          * is to be created on.  */
148        void getScreenSize( unsigned int &width, unsigned int &height ) const;
149
150
151        /** Default window name */
152        static const std::string defaultWindowName;
153
154        static const std::string &getDefaultWindowName();
155
156        /** Set the Window system Window name of the Render Surface */
157        void setWindowName( const std::string & );
158        /** Get the Window system Window name of the Render Surface */
159        const std::string& getWindowName( void ) const;
160
161        /** Set the windowing system rectangle the RenderSurface will
162          * occupy on the screen.  The parameters are given as integers
163          * in screen space.  x and y determine the lower left hand corner
164          * of the RenderSurface.  Width and height are given in screen
165          * coordinates */
166        void  setWindowRectangle( int x, int y, unsigned int width, unsigned int height,
167                                    bool resize=true );
168        /** Get the windowing system rectangle the RenderSurface will
169          * occupy on the screen.  The parameters are given as integers
170          * in screen space.  x and y determine the lower left hand corner
171          * of the RenderSurface.  Width and height are given in screen
172          * coordinates */
173        void getWindowRectangle( int &x, int &y, unsigned int &width, unsigned int &height ) const;
174        /** Get the X coordinate of the origin of the RenderSurface's window */
175        int getWindowOriginX() const;
176
177        /** Get the Y coordinate of the origin of the RenderSurface's window */
178        int getWindowOriginY() const;
179
180        /** Get the width of the RenderSurface in windowing system screen coordinates */
181        unsigned int getWindowWidth() const;
182
183        /** Get the height of the RenderSurface in windowing system screen coordinates */
184        unsigned int getWindowHeight() const;
185
186#if 0
187        /** Explicitely set the Display variable before realization. (X11 only). */
188        void               setDisplay( Display *dpy );
189        /** Get the Display. (X11 only). */
190        Display*           getDisplay( void );
191        /** Get the const Display. (X11 only). */
192        const Display*     getDisplay( void ) const;
193
194        /** Explicitely set the Windowing system window before realization. */
195        void               setWindow( const Window win );
196        /** Returns the Windowing system handle to the window */
197        Window getWindow( void ) const;
198
199        void setGLContext( GLContext );
200        /** Returns the OpenGL context */
201        GLContext getGLContext( void ) const;
202
203        /** Set the Windowing system's parent window */
204        void setParentWindow( Window parent );
205        /** Get the Windowing system's parent window */
206        Window getParentWindow( void ) const;
207
208#endif
209
210        void setVisualChooser( VisualChooser *vc );
211        VisualChooser *getVisualChooser( void );
212        const VisualChooser *getVisualChooser( void ) const;
213
214#if 0
215        void setVisualInfo( VisualInfo *vi );
216        VisualInfo *getVisualInfo( void );
217        const VisualInfo *getVisualInfo( void ) const;
218
219        /** Returns true if the RenderSurface has been realized, false if not. */
220        bool isRealized( void ) const;
221#endif
222
223        /** Request the use of a window border.  If flag is false, no border will
224          * appear after realization.  If flag is true, the windowing system window
225          * will be created in default state. */
226        void useBorder( bool flag );
227        bool usesBorder();
228
229        /** Use overrideRedirect (X11 only).  This bypasses the window manager
230          * control over the Window.  Ignored on Win32 and Mac CGL versions.
231          * This call will only have effect if called before realize().  Calling
232          * it subsequent to realize() will issue a warning. */
233         void useOverrideRedirect(bool);
234         bool usesOverrideRedirect();
235
236       /** Request whether the window should have a visible cursor.  If true, the
237         * windowing system's default cursor will be assigned to the window.  If false
238         * the window will not have a visible cursor. */
239        void useCursor( bool flag );
240
241#if 0
242        /** Set the current window cursor.  Producer provides no functionality to create
243          * cursors.  It is the application's responsiblity to create a Cursor using the
244          * windowing system of choice.  setCursor() will simply set a predefined cursor
245          * as the current Cursor */
246        void setCursor( Cursor );
247#endif
248        void setCursorToDefault();
249
250        /** Specify whether the RenderSurface should use a separate thread for
251          * window configuration events.  If flag is set to true, then the
252          * RenderSurface will spawn a new thread to manage events caused by
253          * resizing the window, mapping or destroying the window. */
254        void useConfigEventThread(bool flag);
255
256        /** shareAllGLContexts will share all OpenGL Contexts between render surfaces
257          * upon realize.  This must be called before any call to renderSurface::realize().
258          */
259        static void shareAllGLContexts(bool);
260
261        /** Returns true or false indicating the the state flag for sharing contexts
262          * between RenderSurfaces
263          */
264        static bool allGLContextsAreShared();
265
266#if 0
267        /** Realize the RenderSurface.  When realized, all components of the RenderSurface
268          * not already configured are configured, a window and a graphics context are
269          * created and made current.  If an already existing graphics context is passed
270          * through "sharedGLContext", then the graphics context created will share certain
271          * graphics constructs (such as display lists) with "sharedGLContext". */
272        bool realize( VisualChooser *vc=NULL, GLContext sharedGLContext=0 );
273
274
275        void addRealizeCallback( Callback *realizeCB );
276        void setRealizeCallback( Callback *realizeCB ) { addRealizeCallback(realizeCB); }
277
278        /** Swaps buffers if RenderSurface quality attribute is DoubleBuffered */
279        virtual void swapBuffers(void);
280
281        /** Makes the graphics context and RenderSurface current for rendering */
282        bool makeCurrent(void) const;
283
284        /** Where supported, sync() will synchronize with the vertical retrace signal of the
285          * graphics display device.  \a divisor specifies the number of vertical retace signals
286          * to allow before returning. */
287        virtual void sync( int divisor=1 );
288
289        /** Where supported, getRefreshRate() will return the frequency in hz of the vertical
290          * retrace signal of the graphics display device.  If getRefreshRate() returns 0, then
291          * the underlying support to get the graphics display device's vertical retrace signal
292          * is not present. */
293        unsigned int getRefreshRate() const;
294
295        /** Where supported, initThreads will initialize all graphics components for thread
296          * safety.  InitThreads() should be called before any other calls to Xlib, or OpenGL
297          * are made, and should always be called when multi-threaded environments are intended.          */
298        static void initThreads();
299
300        /**
301         * Puts the calling thread to sleep until the RenderSurface is realized.  Returns true
302         * if for success and false for failure.
303         * */
304        bool waitForRealize();
305
306        /** fullScreen(flag).  If flag is true, RenderSurface resizes its window to fill the
307         * entire screen and turns off the border.  If false, the window is returned to a size
308         * previously specified.  If previous state specified a border around the window, a
309         * the border is replaced
310         * */
311
312        /** positionPointer(x,y) places the pointer at window coordinates x, y.
313          */
314        void positionPointer( int x, int y );
315
316        /** set/getUseDefaultEsc is deprecated */
317        void setUseDefaultEsc( bool flag ) {_useDefaultEsc = flag; }
318        bool getUseDefaultEsc() { return _useDefaultEsc; }
319
320        /** map and unmap the window */
321        void mapWindow();
322        void unmapWindow();
323#endif
324        void fullScreen( bool flag );
325        /** setCustomFullScreenRencangle(x,y,width,height).  Programmer may set a customized
326          * rectangle to be interpreted as "fullscreen" when fullscreen(true) is called.  This
327          * allows configurations that have large virtual screens that span more than one monitor
328          * to define a "local" full screen for each monitor.
329          */
330        void setCustomFullScreenRectangle( int x, int y, unsigned int width, unsigned int height );
331        /** useDefaultFullScreenRetangle().  Sets the application back to using the default
332          * screen size as fullscreen rather than the custom full screen rectangle
333          */
334        void useDefaultFullScreenRectangle();
335
336        /** isFullScreen() returns true if the RenderSurface's window fills the entire screen
337         * and has no border. */
338        bool isFullScreen() const { return _isFullScreen; }
339
340        // TEMPORARY PBUFFER RENDER TO TEXTURE SUPPORT
341        // Temporary PBuffer support for Windows.  Somebody got really
342        // confused and mixed up PBuffers with a renderable texture and
343        // caused this messy headache.
344        // These have no meaning in GLX world.....
345        enum BufferType {
346            FrontBuffer,
347            BackBuffer
348        };
349
350        enum RenderToTextureMode {
351            RenderToTextureMode_None,
352            RenderToRGBTexture,
353            RenderToRGBATexture
354        };
355
356        enum RenderToTextureTarget {
357            Texture1D,
358            Texture2D,
359            TextureCUBE
360        };
361
362        enum RenderToTextureOptions {
363            RenderToTextureOptions_Default = 0,
364            RequestSpaceForMipMaps  = 1,
365            RequestLargestPBuffer   = 2
366        };
367
368        enum CubeMapFace {
369            PositiveX  = 0,
370            NegativeX  = 1,
371            PositiveY  = 2,
372            NegativeY  = 3,
373            PositiveZ  = 4,
374            NegativeZ  = 5
375        };
376
377        /**
378          * Bind PBuffer content to the currently selected texture.
379          * This method affects PBuffer surfaces only. */
380        void bindPBufferToTexture(BufferType buffer = FrontBuffer) const;
381
382        /**
383          * Get the render-to-texture mode (PBuffer drawables only).
384          * This method has no effect if it is called after realize() */
385        RenderToTextureMode getRenderToTextureMode() const;
386
387        /**
388          * Set the render-to-texture mode (PBuffer drawables only). You
389          * can pass int values different from the constants defined by
390          * RenderToTextureMode, in which case they will be applied
391          * directly as parameters to the WGL_TEXTURE_FORMAT attribute.
392          * This method has no effect if it is called after realize(). */
393        void setRenderToTextureMode(RenderToTextureMode mode);
394
395        /**
396          * Get the render-to-texture target (PBuffer drawables only).
397          * This method has no effect if it is called after realize(). */
398        RenderToTextureTarget getRenderToTextureTarget() const;
399
400        /**
401          * Set the render-to-texture target (PBuffer drawables only). You
402          * can pass int values different from the constants defined by
403          * RenderToTextureTarget, in which case they will be applied
404          * directly as parameters to the WGL_TEXTURE_TARGET attribute.
405          * This method has no effect if it is called after realize(). */
406        void setRenderToTextureTarget(RenderToTextureTarget target);
407
408        /**
409          * Get the render-to-texture options (PBuffer drawables only).
410          * This method has no effect if it is called after realize(). */
411        RenderToTextureOptions getRenderToTextureOptions() const;
412
413        /**
414          * Set the render-to-texture options (PBuffer drawables only).
415          * You can pass any combination of the constants defined in
416          * enum RenderToTextureOptions.
417          * This method has no effect if it is called after realize(). */
418        void setRenderToTextureOptions(RenderToTextureOptions options);
419
420        /**
421          * Get which mipmap level on the target texture will be
422          * affected by rendering. */
423        int getRenderToTextureMipMapLevel() const;
424
425        /**
426          * Select which mipmap level on the target texture will be
427          * affected by rendering. This method can be called after the
428          * PBuffer has been realized. */
429        void setRenderToTextureMipMapLevel(int level);
430
431        /**
432          * Get which face on the target cube map texture will be
433          * affected by rendering. */
434        CubeMapFace getRenderToTextureFace() const;
435
436        /**
437          * Select which face on the target cube map texture will be
438          * affected by rendering. This method can be called after the
439          * PBuffer has been realized. */
440        void setRenderToTextureFace(CubeMapFace face);
441
442        // END TEMPORARY PBUFFER RENDER TO TEXTURE SUPPORT
443
444        /**
445          * Get the (const) vector of user-defined PBuffer attributes. */
446        const std::vector<int> &getPBufferUserAttributes() const;
447
448        /**
449          * Get the vector of user-defined PBuffer attributes. This
450          * vector will be used to initialize the PBuffer's attribute list.*/
451        std::vector<int> &getPBufferUserAttributes();
452
453    private :
454        unsigned int _refreshRate;
455#ifdef _X11_IMPLEMENTATION
456        int (*__glxGetRefreshRateSGI)(unsigned int *);
457        int (*__glxGetVideoSyncSGI)(unsigned int *);
458        int (*__glxWaitVideoSyncSGI)(int,int,unsigned int *);
459        void testVSync( void );
460#endif
461
462#if 0
463        bool _checkEvents( Display *);
464        void _setBorder( bool flag );
465        void _setWindowName( const std::string & );
466        void _useCursor(bool);
467        void _setCursor(Cursor);
468        void _setCursorToDefault();
469        void _positionPointer(int x, int y);
470        void _resizeWindow();
471#endif
472        bool _overrideRedirectFlag;
473    protected :
474
475        virtual ~RenderSurface( void );
476        //        void _useOverrideRedirect( bool );
477
478        DrawableType       _drawableType;
479        std::string        _hostname;
480        int                _displayNum;
481        float              _windowLeft, _windowRight,
482                           _windowBottom, _windowTop;
483        int                _windowX, _windowY;
484        unsigned int       _windowWidth, _windowHeight;
485        unsigned int       _screenWidth, _screenHeight;
486        bool               _useCustomFullScreen;
487        int                _customFullScreenOriginX;
488        int                _customFullScreenOriginY;
489        unsigned int       _customFullScreenWidth;
490        unsigned int       _customFullScreenHeight;
491
492        int                _screen;
493#if 0
494        Display *          _dpy;
495        Window             _win;
496        Window             _parent;
497#endif
498        RenderSurface      *_readDrawableRenderSurface;
499        unsigned int       _parentWindowHeight;
500        bool               _realized;
501        osg::ref_ptr< VisualChooser >  _visualChooser;
502#if 0
503        VisualInfo *       _visualInfo;
504        unsigned int       _visualID;
505        GLContext          _glcontext;
506        GLContext          _sharedGLContext;
507        Cursor             _currentCursor;
508        Cursor             _nullCursor;
509        Cursor             _defaultCursor;
510#endif
511        bool               _decorations;
512        bool               _useCursorFlag;
513        std::string        _windowName;
514        unsigned int       _frameCount;
515        bool               _mayFullScreen;
516        bool               _isFullScreen;
517        bool               _bindInputRectangleToWindowSize;
518
519
520        // Temporary "Pbuffer support for Win32
521        RenderToTextureMode     _rtt_mode;
522        RenderToTextureTarget   _rtt_target;
523        RenderToTextureOptions  _rtt_options;
524        int                     _rtt_mipmap;
525        CubeMapFace             _rtt_face;
526        bool                    _rtt_dirty_mipmap;
527        bool                    _rtt_dirty_face;
528
529#ifdef _OSX_AGL_IMPLEMENTATION
530        GLContext          _windowGlcontext;
531        GLContext          _fullscreenGlcontext;
532        CFDictionaryRef    _oldDisplayMode;
533
534        bool _captureDisplayOrWindow();
535        void _releaseDisplayOrWindow();
536#endif
537
538        // user-defined PBuffer attributes
539        std::vector<int>        _user_pbattr;
540
541
542        bool _useConfigEventThread;
543        bool _checkOwnEvents;
544        bool _useDefaultEsc;
545
546        InputRectangle _inputRectangle;
547
548        //        void _computeScreenSize( unsigned int &width, unsigned int &height ) const;
549
550#if 0
551        bool  _createVisualInfo();
552
553        virtual bool  _init();
554        virtual void  _fini();
555        virtual void  run();
556
557        static  void  _initThreads();
558#endif
559
560#ifdef _WIN32_IMPLEMENTATION
561
562        class Client : public Producer::Referenced
563        {
564            public:
565                Client(unsigned long mask);
566                unsigned int mask() { return _mask; }
567                EventQueue *getQueue() { return q.get(); }
568                void queue( ref_ptr<Event> ev );
569
570            private :
571                unsigned int _mask;
572                Producer::ref_ptr<EventQueue> q;
573        };
574        std::vector < Producer::ref_ptr<Client> >clients;
575        void dispatch( ref_ptr<Event> );
576
577        int _ownWindow;
578        int _ownVisualChooser;
579        int _ownVisualInfo;
580
581        HDC _hdc;
582        HINSTANCE _hinstance;
583
584        BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag);
585        void KillGLWindow();
586
587        LONG WINAPI proc( Window, UINT, WPARAM, LPARAM );
588        static LONG WINAPI s_proc( Window, UINT, WPARAM, LPARAM );
589        static std::map <Window, RenderSurface *>registry;
590
591        /* mouse things */
592        int _mx, _my;
593        unsigned int _mbutton;
594        WNDPROC _oldWndProc;
595
596
597public:
598        EventQueue * selectInput( unsigned int mask );
599
600        // if _parent is set, resize the window to
601        // fill the client area of the parent
602        void resizeToParent();
603#endif
604
605        static bool _shareAllGLContexts;
606
607};
608
609}
610
611
612#endif
Note: See TracBrowser for help on using the browser.