PBuffer.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 6743 $                                                       *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-08-05 11:03:10 +0200 (Mi, 05. Aug 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 //=============================================================================
00046 //
00047 //  CLASS PBuffer - IMPLEMENTATION
00048 //
00049 //============================================================================
00050 #ifdef ARCH_LINUX
00051 //=============================================================================
00052 
00053 
00054 //== INCLUDES =================================================================
00055 
00056 #include "PBuffer.hh"
00057 #include <iostream>
00058 #include <stdlib.h>
00059 
00060 
00061 //== IMPLEMENTATION ==========================================================
00062 
00063 PBuffer::PBuffer(int _bits)
00064 {
00065   int n;
00066 
00067   sbAttrib_.clear();
00068   sbAttrib_.push_back(GLX_DOUBLEBUFFER);  sbAttrib_.push_back(true);
00069   sbAttrib_.push_back(GLX_RED_SIZE);      sbAttrib_.push_back(_bits);
00070   sbAttrib_.push_back(GLX_GREEN_SIZE);    sbAttrib_.push_back(_bits);
00071   sbAttrib_.push_back(GLX_BLUE_SIZE);     sbAttrib_.push_back(_bits);
00072   sbAttrib_.push_back(GLX_ALPHA_SIZE);    sbAttrib_.push_back(_bits);
00073   sbAttrib_.push_back(GLX_DEPTH_SIZE);    sbAttrib_.push_back(24);
00074   sbAttrib_.push_back(GLX_RENDER_TYPE);   sbAttrib_.push_back(GLX_RGBA_BIT);
00075   sbAttrib_.push_back(GLX_DRAWABLE_TYPE); sbAttrib_.push_back(GLX_PBUFFER_BIT);
00076   sbAttrib_.push_back(None);
00077 
00078 
00079   pbAttrib_.clear();
00080   pbAttrib_.push_back(GLX_PBUFFER_WIDTH);       pbAttrib_.push_back(100);
00081   pbAttrib_.push_back(GLX_PBUFFER_HEIGHT);      pbAttrib_.push_back(100);
00082   pbAttrib_.push_back(GLX_PRESERVED_CONTENTS);  pbAttrib_.push_back(true);
00083   pbAttrib_.push_back(None);
00084 
00085 
00086   // Create the pbuffer
00087   dpy_      = glXGetCurrentDisplay();
00088   currctx_  = glXGetCurrentContext();
00089   currdraw_ = glXGetCurrentDrawable();
00090 
00091   fbc_ = glXChooseFBConfig(dpy_, DefaultScreen(dpy_), &sbAttrib_[0], &n);
00092   if (!fbc_) 
00093   {
00094     std::cerr << "glXChooseFBConfig failed.\n";
00095     return;
00096   }
00097 
00098   pbuf_ = glXCreatePbuffer(dpy_, fbc_[0], &pbAttrib_[0]);
00099   if (!pbuf_) 
00100   {
00101     std::cerr << "glXCreatePbuffer failed.\n";
00102     return;
00103   }
00104 
00105   pbufctx_ = glXCreateNewContext(dpy_, fbc_[0], GLX_RGBA_TYPE, currctx_, true);
00106   if (!pbufctx_) 
00107   {
00108     std::cerr << "glXCreateNewContextWithConfigSGIX failed.\n";
00109     return;
00110   }
00111 }
00112 
00113 
00114 //-----------------------------------------------------------------------------
00115 
00116 
00117 PBuffer::PBuffer(int _w, int _h, int _bits)
00118 {
00119   int n;
00120 
00121   sbAttrib_.clear();
00122   sbAttrib_.push_back(GLX_DOUBLEBUFFER);  sbAttrib_.push_back(true);
00123   sbAttrib_.push_back(GLX_RED_SIZE);      sbAttrib_.push_back(_bits);
00124   sbAttrib_.push_back(GLX_GREEN_SIZE);    sbAttrib_.push_back(_bits);
00125   sbAttrib_.push_back(GLX_BLUE_SIZE);     sbAttrib_.push_back(_bits);
00126   sbAttrib_.push_back(GLX_ALPHA_SIZE);    sbAttrib_.push_back(_bits);
00127   sbAttrib_.push_back(GLX_DEPTH_SIZE);    sbAttrib_.push_back(24);
00128   sbAttrib_.push_back(GLX_RENDER_TYPE);   sbAttrib_.push_back(GLX_RGBA_BIT);
00129   sbAttrib_.push_back(GLX_DRAWABLE_TYPE); sbAttrib_.push_back(GLX_PBUFFER_BIT);
00130   sbAttrib_.push_back(None);
00131 
00132   pbAttrib_.clear();
00133   pbAttrib_.push_back(GLX_PBUFFER_WIDTH);       pbAttrib_.push_back(_w);
00134   pbAttrib_.push_back(GLX_PBUFFER_HEIGHT);      pbAttrib_.push_back(_h);
00135   pbAttrib_.push_back(GLX_PRESERVED_CONTENTS);  pbAttrib_.push_back(true);
00136   pbAttrib_.push_back(None);
00137 
00138 
00139   // Create the pbuffer
00140   dpy_      = glXGetCurrentDisplay();
00141   currctx_  = glXGetCurrentContext();
00142   currdraw_ = glXGetCurrentDrawable();
00143 
00144   fbc_ = glXChooseFBConfig(dpy_, DefaultScreen(dpy_), &sbAttrib_[0], &n);
00145   if (!fbc_) 
00146   {
00147     std::cerr << "glXChooseFBConfig failed.\n";
00148     return;
00149   }
00150 
00151   pbuf_ = glXCreatePbuffer(dpy_, fbc_[0], &pbAttrib_[0]);
00152   if (!pbuf_) 
00153   {
00154     std::cerr << "glXCreatePbuffer failed.\n";
00155     return;
00156   }
00157 
00158   pbufctx_ = glXCreateNewContext(dpy_, fbc_[0], GLX_RGBA_TYPE, currctx_, true);
00159   if (!pbufctx_) 
00160   {
00161     std::cerr << "glXCreateNewContextWithConfigSGIX failed.\n";
00162     return;
00163   }
00164 }
00165 
00166 
00167 //-----------------------------------------------------------------------------
00168 
00169 
00170 PBuffer::~PBuffer()
00171 {
00172   glXDestroyContext(dpy_, currctx_);
00173   glXDestroyPbuffer(dpy_, pbuf_);
00174 }
00175 
00176 
00177 //-----------------------------------------------------------------------------
00178 
00179 
00180 void PBuffer::resize(int _w, int _h)
00181 {
00182   pbAttrib_[1] = _w;
00183   pbAttrib_[3] = _h;
00184   glXDestroyPbuffer(dpy_, pbuf_);
00185   pbuf_ = glXCreatePbuffer(dpy_, fbc_[0], &pbAttrib_[0]);
00186   if (!pbuf_) std::cerr << "Resizing pbuffer failed.\n";
00187 }
00188 
00189 
00190 //-----------------------------------------------------------------------------
00191 
00192 
00193 int PBuffer::bits()
00194 {
00195   return sbAttrib_[3];
00196 }
00197 
00198 
00199 //-----------------------------------------------------------------------------
00200 
00201 
00202 void PBuffer::activate()
00203 {
00204   if (!glXMakeCurrent(dpy_, pbuf_, pbufctx_))
00205     std::cerr << "PBuffer:activate() failed.\n";
00206 }
00207 
00208 
00209 //-----------------------------------------------------------------------------
00210 
00211 
00212 void PBuffer::deactivate()
00213 {
00214   if (!glXMakeCurrent(dpy_, currdraw_, currctx_))
00215     std::cerr << "PBuffer:deactivate() failed.\n";
00216 }
00217 
00218 
00219 //-----------------------------------------------------------------------------
00220 
00221 
00222 const int PBuffer::width() const
00223 {
00224   return pbAttrib_[1];
00225 }
00226 
00227 
00228 //-----------------------------------------------------------------------------
00229 
00230 
00231 const int PBuffer::height() const
00232 {
00233   return pbAttrib_[3];
00234 }
00235 
00236 
00237 //=============================================================================
00238 #endif // Linx only
00239 //=============================================================================

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .