Developer Documentation
PBuffer.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 //=============================================================================
53 //
54 // CLASS PBuffer - IMPLEMENTATION
55 //
56 //============================================================================
57 #ifdef ARCH_LINUX
58 //=============================================================================
59 
60 
61 //== INCLUDES =================================================================
62 
63 #include "PBuffer.hh"
64 #include <iostream>
65 #include <cstdlib>
66 
67 
68 //== IMPLEMENTATION ==========================================================
69 
70 PBuffer::PBuffer(int _bits)
71 {
72  int n;
73 
74  sbAttrib_.clear();
75  sbAttrib_.push_back(GLX_DOUBLEBUFFER); sbAttrib_.push_back(true);
76  sbAttrib_.push_back(GLX_RED_SIZE); sbAttrib_.push_back(_bits);
77  sbAttrib_.push_back(GLX_GREEN_SIZE); sbAttrib_.push_back(_bits);
78  sbAttrib_.push_back(GLX_BLUE_SIZE); sbAttrib_.push_back(_bits);
79  sbAttrib_.push_back(GLX_ALPHA_SIZE); sbAttrib_.push_back(_bits);
80  sbAttrib_.push_back(GLX_DEPTH_SIZE); sbAttrib_.push_back(24);
81  sbAttrib_.push_back(GLX_RENDER_TYPE); sbAttrib_.push_back(GLX_RGBA_BIT);
82  sbAttrib_.push_back(GLX_DRAWABLE_TYPE); sbAttrib_.push_back(GLX_PBUFFER_BIT);
83  sbAttrib_.push_back(None);
84 
85 
86  pbAttrib_.clear();
87  pbAttrib_.push_back(GLX_PBUFFER_WIDTH); pbAttrib_.push_back(100);
88  pbAttrib_.push_back(GLX_PBUFFER_HEIGHT); pbAttrib_.push_back(100);
89  pbAttrib_.push_back(GLX_PRESERVED_CONTENTS); pbAttrib_.push_back(true);
90  pbAttrib_.push_back(None);
91 
92 
93  // Create the pbuffer
94  dpy_ = glXGetCurrentDisplay();
95  currctx_ = glXGetCurrentContext();
96  currdraw_ = glXGetCurrentDrawable();
97 
98  fbc_ = glXChooseFBConfig(dpy_, DefaultScreen(dpy_), &sbAttrib_[0], &n);
99  if (!fbc_)
100  {
101  std::cerr << "glXChooseFBConfig failed.\n";
102  return;
103  }
104 
105  pbuf_ = glXCreatePbuffer(dpy_, fbc_[0], &pbAttrib_[0]);
106  if (!pbuf_)
107  {
108  std::cerr << "glXCreatePbuffer failed.\n";
109  return;
110  }
111 
112  pbufctx_ = glXCreateNewContext(dpy_, fbc_[0], GLX_RGBA_TYPE, currctx_, true);
113  if (!pbufctx_)
114  {
115  std::cerr << "glXCreateNewContextWithConfigSGIX failed.\n";
116  return;
117  }
118 }
119 
120 
121 //-----------------------------------------------------------------------------
122 
123 
124 PBuffer::PBuffer(int _w, int _h, int _bits)
125 {
126  int n;
127 
128  sbAttrib_.clear();
129  sbAttrib_.push_back(GLX_DOUBLEBUFFER); sbAttrib_.push_back(true);
130  sbAttrib_.push_back(GLX_RED_SIZE); sbAttrib_.push_back(_bits);
131  sbAttrib_.push_back(GLX_GREEN_SIZE); sbAttrib_.push_back(_bits);
132  sbAttrib_.push_back(GLX_BLUE_SIZE); sbAttrib_.push_back(_bits);
133  sbAttrib_.push_back(GLX_ALPHA_SIZE); sbAttrib_.push_back(_bits);
134  sbAttrib_.push_back(GLX_DEPTH_SIZE); sbAttrib_.push_back(24);
135  sbAttrib_.push_back(GLX_RENDER_TYPE); sbAttrib_.push_back(GLX_RGBA_BIT);
136  sbAttrib_.push_back(GLX_DRAWABLE_TYPE); sbAttrib_.push_back(GLX_PBUFFER_BIT);
137  sbAttrib_.push_back(None);
138 
139  pbAttrib_.clear();
140  pbAttrib_.push_back(GLX_PBUFFER_WIDTH); pbAttrib_.push_back(_w);
141  pbAttrib_.push_back(GLX_PBUFFER_HEIGHT); pbAttrib_.push_back(_h);
142  pbAttrib_.push_back(GLX_PRESERVED_CONTENTS); pbAttrib_.push_back(true);
143  pbAttrib_.push_back(None);
144 
145 
146  // Create the pbuffer
147  dpy_ = glXGetCurrentDisplay();
148  currctx_ = glXGetCurrentContext();
149  currdraw_ = glXGetCurrentDrawable();
150 
151  fbc_ = glXChooseFBConfig(dpy_, DefaultScreen(dpy_), &sbAttrib_[0], &n);
152  if (!fbc_)
153  {
154  std::cerr << "glXChooseFBConfig failed.\n";
155  return;
156  }
157 
158  pbuf_ = glXCreatePbuffer(dpy_, fbc_[0], &pbAttrib_[0]);
159  if (!pbuf_)
160  {
161  std::cerr << "glXCreatePbuffer failed.\n";
162  return;
163  }
164 
165  pbufctx_ = glXCreateNewContext(dpy_, fbc_[0], GLX_RGBA_TYPE, currctx_, true);
166  if (!pbufctx_)
167  {
168  std::cerr << "glXCreateNewContextWithConfigSGIX failed.\n";
169  return;
170  }
171 }
172 
173 
174 //-----------------------------------------------------------------------------
175 
176 
177 PBuffer::~PBuffer()
178 {
179  glXDestroyContext(dpy_, currctx_);
180  glXDestroyPbuffer(dpy_, pbuf_);
181 }
182 
183 
184 //-----------------------------------------------------------------------------
185 
186 
187 void PBuffer::resize(int _w, int _h)
188 {
189  pbAttrib_[1] = _w;
190  pbAttrib_[3] = _h;
191  glXDestroyPbuffer(dpy_, pbuf_);
192  pbuf_ = glXCreatePbuffer(dpy_, fbc_[0], &pbAttrib_[0]);
193  if (!pbuf_) std::cerr << "Resizing pbuffer failed.\n";
194 }
195 
196 
197 //-----------------------------------------------------------------------------
198 
199 
200 int PBuffer::bits()
201 {
202  return sbAttrib_[3];
203 }
204 
205 
206 //-----------------------------------------------------------------------------
207 
208 
209 void PBuffer::activate()
210 {
211  if (!glXMakeCurrent(dpy_, pbuf_, pbufctx_))
212  std::cerr << "PBuffer:activate() failed.\n";
213 }
214 
215 
216 //-----------------------------------------------------------------------------
217 
218 
219 void PBuffer::deactivate()
220 {
221  if (!glXMakeCurrent(dpy_, currdraw_, currctx_))
222  std::cerr << "PBuffer:deactivate() failed.\n";
223 }
224 
225 
226 //-----------------------------------------------------------------------------
227 
228 
229 const int PBuffer::width() const
230 {
231  return pbAttrib_[1];
232 }
233 
234 
235 //-----------------------------------------------------------------------------
236 
237 
238 const int PBuffer::height() const
239 {
240  return pbAttrib_[3];
241 }
242 
243 
244 //=============================================================================
245 #endif // Linx only
246 //=============================================================================
Clear all attribute bits.
Definition: Attributes.hh:86