1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
46 #include "OSGConfig.h"
48 #define OSG_COMPILEWINDOWXINST
52 #include "OSGGLFuncProtos.h"
53 #include "OSGGLXFuncProtos.h"
55 #include "OSGXWindow.h"
60 // Documentation for this class is emited in the
61 // OSGXWindowBase.cpp file.
62 // To modify it, please change the .fcd file (OSGXWindow.fcd) and
63 // regenerate the base file.
65 /*----------------------- constructors & destructors ----------------------*/
68 XWindow::XWindow(void) :
74 XWindow::XWindow(const XWindow
&source
) :
80 XWindow::~XWindow(void)
84 /*----------------------------- class specific ----------------------------*/
86 //! initialize the static features of the class, e.g. action callbacks
87 void XWindow::initMethod(InitPhase ePhase
)
89 Inherited::initMethod(ePhase
);
92 //! react to field changes
93 void XWindow::changed(ConstFieldMaskArg whichField
,
97 Inherited::changed(whichField
, origin
, details
);
100 //! output the instance for debug purposes
101 void XWindow::dump( UInt32
,
102 const BitVector
) const
104 SLOG
<< "Dump XWindow NI" << std::endl
;
107 #ifdef OSG_DEBUG_OLD_C_CASTS
111 #define DisplayString(dpy)((reinterpret_cast<_XPrivDisplay>(dpy))->display_name)
112 #ifdef ScreenOfDisplay
113 #undef ScreenOfDisplay
118 #define ScreenOfDisplay(dpy, scr)(&(_XPrivDisplay(dpy))->screens[scr])
119 #define DefaultScreen(dpy) ((_XPrivDisplay(dpy))->default_screen)
122 void XWindow::classicInit(void)
124 XVisualInfo
*vi
, visInfo
;
125 XWindowAttributes winAttr
;
127 XGetWindowAttributes(getDisplay(), getWindow(), &winAttr
);
129 // get the existing glWidget's visual-id
130 memset(&visInfo
, 0, sizeof(XVisualInfo
));
132 visInfo
.visualid
= XVisualIDFromVisual(winAttr
.visual
);
134 // get new display-variable
135 if(getDisplay() == NULL
)
137 setDisplay(XOpenDisplay(DisplayString(getDisplay())));
140 // get a visual for the glx context
143 vi
= XGetVisualInfo(getDisplay(), VisualIDMask
, &visInfo
, &nvis
);
145 // is the visual GL-capable ?
147 glXGetConfig( getDisplay(),
154 SFATAL
<< "Visual is not OpenGL-capable!" << std::endl
;
158 // create the new context
159 this->setContext(glXCreateContext(getDisplay(), vi
, None
, GL_TRUE
));
164 /*! Init the window: create the context and setup the OpenGL.
166 void XWindow::init(GLInitFunctor oFunc
)
168 if(_sfFbConfigId
.getValue() == -1)
174 OSGGETGLFUNCBYNAME_EXT(glxChooseFBConfig
,
175 osgGlxChooseFBConfig
,
179 OSG_ASSERT(osgGlxChooseFBConfig
!= NULL
);
185 GLX_FBCONFIG_ID
, _sfFbConfigId
.getValue(),
189 GLXFBConfig
*fbConfigs
=
190 osgGlxChooseFBConfig( getDisplay(),
191 DefaultScreen(getDisplay()),
197 fprintf(stderr
, "no valid fbconfig %d\n",
198 _sfFbConfigId
.getValue());
204 OSGGETGLFUNCBYNAME_EXT(glxCreateContextAttribsARB
,
205 osgGlxCreateContextAttribsARB
,
206 "glXCreateContextAttribsARB",
209 if(osgGlxCreateContextAttribsARB
!= NULL
)
211 std::vector
<int> ctxAttr
;
213 if(getRequestMajor() > 0)
215 ctxAttr
.push_back(GLX_CONTEXT_MAJOR_VERSION_ARB
);
216 ctxAttr
.push_back(getRequestMajor());
217 ctxAttr
.push_back(GLX_CONTEXT_MINOR_VERSION_ARB
);
218 ctxAttr
.push_back(getRequestMinor());
221 if(getContextFlags() != 0)
223 ctxAttr
.push_back(GLX_CONTEXT_FLAGS_ARB
);
224 ctxAttr
.push_back(getContextFlags() );
227 ctxAttr
.push_back(None
);
229 this->setContext(osgGlxCreateContextAttribsARB( getDisplay(),
233 &(ctxAttr
.front())));
235 if(getContext() == NULL
)
237 FWARNING(("Could not create context, requested version "
238 "%d.%d might not be supported (guessing)\n",
253 Inherited::init(oFunc
);
256 void XWindow::terminate(void)
258 Inherited::doTerminate();
260 if(getDisplay() != NULL
&& getContext() != NULL
)
262 this->doDeactivate();
264 glXDestroyContext(getDisplay(), getContext());
270 // activate the window: bind the OGL context
271 void XWindow::doActivate(void)
276 if(getDisplay() == NULL
)
278 SWARNING
<< "XWindow::doActivate: Display is NULL, can not activate context."
284 SWARNING
<< "XWindow::doActivate: Window is NULL, can not activate "
289 if(getContext() == NULL
)
291 SWARNING
<< "XWindow::doActivate: Context is NULL, can not activate "
297 res
= glXMakeCurrent(getDisplay(), getWindow(), getContext());
301 FWARNING(("XWindow::activate: makeCurrent failed!\n"));
302 glErr("XWindow::activate");
303 glErr("XWindow::activate");
304 glErr("XWindow::activate");
308 // activate the window: bind the OGL context
309 void XWindow::doDeactivate(void)
311 glXMakeCurrent(getDisplay(), None
, NULL
);
314 // swap front and back buffers
315 bool XWindow::doSwap(void)
317 glXSwapBuffers(getDisplay(), getWindow());
321 bool XWindow::hasContext(void)
323 return (this->getContext() != NULL
);
326 void XWindow::onDestroy(UInt32 uiContainerId
)
328 Inherited::onDestroy(uiContainerId
);
332 #include "OSGSField.ins"
333 #include "OSGMField.ins"
335 #if defined(OSG_TMPL_STATIC_MEMBER_NEEDS_FUNCTION_INSTANTIATION) || \
336 defined(OSG_TMPL_STATIC_MEMBER_NEEDS_CLASS_INSTANTIATION )
338 #include "OSGSFieldFuncs.ins"
339 #include "OSGMFieldFuncs.ins"
344 DataType FieldTraits
<DisplayP
, 2>::_type("DisplayP", NULL
);
345 #if ( !defined(__GNUC__) || !defined(__linux) || ( !defined(__ia64) && !defined(__x86_64) && !defined(_ARCH_PPC64)) ) && (!defined(_MIPS_SZPTR) || _MIPS_SZPTR != 64)
346 DataType FieldTraits
<X11Window
, 2>::_type("X11Window", NULL
);
348 DataType FieldTraits
<GLXContext
, 2>::_type("GLXContext", NULL
);
350 OSG_FIELD_DLLEXPORT_DEF2(SField
, DisplayP
, 2)
351 OSG_FIELD_DLLEXPORT_DEF2(MField
, DisplayP
, 2)
352 #if ( !defined(__GNUC__) || !defined(__linux) || ( !defined(__ia64) && !defined(__x86_64) && !defined(_ARCH_PPC64)) ) && (!defined(_MIPS_SZPTR) || _MIPS_SZPTR != 64)
353 OSG_FIELD_DLLEXPORT_DEF2(SField
, X11Window
, 2)
354 OSG_FIELD_DLLEXPORT_DEF2(MField
, X11Window
, 2)
356 OSG_FIELD_DLLEXPORT_DEF2(SField
, GLXContext
, 2)
357 OSG_FIELD_DLLEXPORT_DEF2(MField
, GLXContext
, 2)