OSGXWindow.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 //---------------------------------------------------------------------------
00040 //  Includes
00041 //---------------------------------------------------------------------------
00042 
00043 #include <cstdlib>
00044 #include <cstdio>
00045 
00046 #include <OSGConfig.h>
00047 
00048 #define OSG_COMPILEWINDOWXINST
00049 
00050 #include <OSGGLU.h>
00051 #include "OSGXWindow.h"
00052 
00053 OSG_USING_NAMESPACE
00054 
00055 // Documentation for this class is emited in the
00056 // OSGXWindowBase.cpp file.
00057 // To modify it, please change the .fcd file (OSGXWindow.fcd) and
00058 // regenerate the base file.
00059 
00060 /*----------------------- constructors & destructors ----------------------*/
00061 
00063 XWindow::XWindow(void) :
00064     Inherited()
00065 {
00066 }
00067 
00069 XWindow::XWindow(const XWindow &source) :
00070     Inherited(source)
00071 {
00072 }
00073 
00075 XWindow::~XWindow(void)
00076 {
00077 }
00078 
00079 /*----------------------------- class specific ----------------------------*/
00080 
00082 void XWindow::initMethod(InitPhase ePhase)
00083 {
00084     Inherited::initMethod(ePhase);
00085 }
00086 
00088 void XWindow::changed(ConstFieldMaskArg whichField, 
00089                       UInt32            origin,
00090                       BitVector         details)
00091 {
00092     Inherited::changed(whichField, origin, details);
00093 }
00094 
00096 void XWindow::dump(      UInt32    , 
00097                    const BitVector ) const
00098 {
00099     SLOG << "Dump XWindow NI" << std::endl;
00100 }
00101 
00102 
00103 
00106 void XWindow::init(void)
00107 {    
00108     XVisualInfo       *vi, visInfo;
00109     XWindowAttributes winAttr;
00110 
00111     XGetWindowAttributes(getDisplay(), getWindow(), &winAttr);
00112 
00113     // get the existing glWidget's visual-id
00114     memset(&visInfo, 0, sizeof(XVisualInfo));
00115 
00116     visInfo.visualid = XVisualIDFromVisual(winAttr.visual);
00117 
00118     // get new display-variable
00119     if(getDisplay() == NULL)
00120     {
00121         setDisplay(XOpenDisplay(DisplayString(getDisplay())));  
00122     }
00123 
00124     // get a visual for the glx context
00125     int nvis;
00126 
00127     vi = XGetVisualInfo(getDisplay(), VisualIDMask, &visInfo, &nvis);
00128 
00129     // is the visual GL-capable ?
00130     int useGL;
00131     glXGetConfig(getDisplay(), 
00132                  vi, 
00133                  GLX_USE_GL, 
00134                  &useGL );
00135     if (!useGL)
00136     {
00137         SFATAL << "Visual is not OpenGL-capable!" << std::endl;
00138     }    
00139   
00140     // create the new context
00141     this->setContext(glXCreateContext(getDisplay(), vi, None, GL_TRUE));
00142 
00143     XFree(vi);
00144 
00145     glXMakeCurrent(getDisplay(), getWindow(), getContext());
00146 
00147     setupGL();
00148 }
00149     
00150 void XWindow::terminate(void)
00151 {
00152     if(getDisplay() != NULL && getContext() != NULL)
00153     {
00154         deactivate();
00155         
00156         glXDestroyContext(getDisplay(), getContext());
00157     }
00158 }
00159 
00160 // activate the window: bind the OGL context    
00161 void XWindow::activate(void)
00162 {
00163     Bool res;
00164     res = glXMakeCurrent(getDisplay(), getWindow(), getContext());
00165     
00166     if(res != True)
00167     {
00168         FWARNING(("XWindow::activate: makeCurrent failed!\n"));
00169         glErr("XWindow::activate");
00170         glErr("XWindow::activate");
00171         glErr("XWindow::activate");
00172     }
00173 }
00174     
00175 // activate the window: bind the OGL context    
00176 void XWindow::deactivate(void)
00177 {
00178     glXMakeCurrent(getDisplay(), None, NULL);
00179 }
00180     
00181 // swap front and back buffers  
00182 bool XWindow::swap(void)
00183 {
00184     glXSwapBuffers(getDisplay(), getWindow());
00185     return true;
00186 }
00187 
00188 #include <OSGSField.ins>
00189 #include <OSGMField.ins>
00190 
00191 #if defined(OSG_TMPL_STATIC_MEMBER_NEEDS_FUNCTION_INSTANTIATION) || \
00192     defined(OSG_TMPL_STATIC_MEMBER_NEEDS_CLASS_INSTANTIATION   )
00193 
00194 #include "OSGSFieldFuncs.ins"
00195 #include "OSGMFieldFuncs.ins"
00196 #endif
00197 
00198 OSG_BEGIN_NAMESPACE
00199 
00200 // doxygen can't find these
00201 #ifndef OSG_DO_DOC 
00202 
00203 DataType FieldTraits<DisplayP     >::_type("DisplayP",   NULL);
00204 DataType FieldTraits<X11Window    >::_type("X11Window",  NULL);
00205 DataType FieldTraits<GLXContext, 1>::_type("GLXContext", NULL);
00206 
00207 #endif
00208 
00209 OSG_FIELD_DLLEXPORT_DEF1(SField, DisplayP     );
00210 OSG_FIELD_DLLEXPORT_DEF1(MField, DisplayP     );
00211 OSG_FIELD_DLLEXPORT_DEF1(SField, X11Window    );
00212 OSG_FIELD_DLLEXPORT_DEF1(MField, X11Window    );
00213 OSG_FIELD_DLLEXPORT_DEF2(SField, GLXContext, 1);
00214 OSG_FIELD_DLLEXPORT_DEF2(MField, GLXContext, 1);
00215 
00216 OSG_END_NAMESPACE