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
45
46 //=============================================================================
47 //
48 // CLASS PBuffer - IMPLEMENTATION
49 //
50 //============================================================================
51 #ifdef ARCH_LINUX
52 //=============================================================================
53
54
55 //== INCLUDES =================================================================
56
57 #include "PBuffer.hh"
58 #include <iostream>
59 #include <cstdlib>
60
61
62 //== IMPLEMENTATION ==========================================================
63
64 PBuffer::PBuffer(int _bits)
65 {
66 int n;
67
68 sbAttrib_.clear();
69 sbAttrib_.push_back(GLX_DOUBLEBUFFER); sbAttrib_.push_back(true);
70 sbAttrib_.push_back(GLX_RED_SIZE); sbAttrib_.push_back(_bits);
71 sbAttrib_.push_back(GLX_GREEN_SIZE); sbAttrib_.push_back(_bits);
72 sbAttrib_.push_back(GLX_BLUE_SIZE); sbAttrib_.push_back(_bits);
73 sbAttrib_.push_back(GLX_ALPHA_SIZE); sbAttrib_.push_back(_bits);
74 sbAttrib_.push_back(GLX_DEPTH_SIZE); sbAttrib_.push_back(24);
75 sbAttrib_.push_back(GLX_RENDER_TYPE); sbAttrib_.push_back(GLX_RGBA_BIT);
76 sbAttrib_.push_back(GLX_DRAWABLE_TYPE); sbAttrib_.push_back(GLX_PBUFFER_BIT);
77 sbAttrib_.push_back(None);
78
79
80 pbAttrib_.clear();
81 pbAttrib_.push_back(GLX_PBUFFER_WIDTH); pbAttrib_.push_back(100);
82 pbAttrib_.push_back(GLX_PBUFFER_HEIGHT); pbAttrib_.push_back(100);
83 pbAttrib_.push_back(GLX_PRESERVED_CONTENTS); pbAttrib_.push_back(true);
84 pbAttrib_.push_back(None);
85
86
87 // Create the pbuffer
88 dpy_ = glXGetCurrentDisplay();
89 currctx_ = glXGetCurrentContext();
90 currdraw_ = glXGetCurrentDrawable();
91
92 fbc_ = glXChooseFBConfig(dpy_, DefaultScreen(dpy_), &sbAttrib_[0], &n);
93 if (!fbc_)
94 {
95 std::cerr << "glXChooseFBConfig failed.\n";
96 return;
97 }
98
99 pbuf_ = glXCreatePbuffer(dpy_, fbc_[0], &pbAttrib_[0]);
100 if (!pbuf_)
101 {
102 std::cerr << "glXCreatePbuffer failed.\n";
103 return;
104 }
105
106 pbufctx_ = glXCreateNewContext(dpy_, fbc_[0], GLX_RGBA_TYPE, currctx_, true);
107 if (!pbufctx_)
108 {
109 std::cerr << "glXCreateNewContextWithConfigSGIX failed.\n";
110 return;
111 }
112 }
113
114
115 //-----------------------------------------------------------------------------
116
117
118 PBuffer::PBuffer(int _w, int _h, int _bits)
119 {
120 int n;
121
122 sbAttrib_.clear();
123 sbAttrib_.push_back(GLX_DOUBLEBUFFER); sbAttrib_.push_back(true);
124 sbAttrib_.push_back(GLX_RED_SIZE); sbAttrib_.push_back(_bits);
125 sbAttrib_.push_back(GLX_GREEN_SIZE); sbAttrib_.push_back(_bits);
126 sbAttrib_.push_back(GLX_BLUE_SIZE); sbAttrib_.push_back(_bits);
127 sbAttrib_.push_back(GLX_ALPHA_SIZE); sbAttrib_.push_back(_bits);
128 sbAttrib_.push_back(GLX_DEPTH_SIZE); sbAttrib_.push_back(24);
129 sbAttrib_.push_back(GLX_RENDER_TYPE); sbAttrib_.push_back(GLX_RGBA_BIT);
130 sbAttrib_.push_back(GLX_DRAWABLE_TYPE); sbAttrib_.push_back(GLX_PBUFFER_BIT);
131 sbAttrib_.push_back(None);
132
133 pbAttrib_.clear();
134 pbAttrib_.push_back(GLX_PBUFFER_WIDTH); pbAttrib_.push_back(_w);
135 pbAttrib_.push_back(GLX_PBUFFER_HEIGHT); pbAttrib_.push_back(_h);
136 pbAttrib_.push_back(GLX_PRESERVED_CONTENTS); pbAttrib_.push_back(true);
137 pbAttrib_.push_back(None);
138
139
140 // Create the pbuffer
141 dpy_ = glXGetCurrentDisplay();
142 currctx_ = glXGetCurrentContext();
143 currdraw_ = glXGetCurrentDrawable();
144
145 fbc_ = glXChooseFBConfig(dpy_, DefaultScreen(dpy_), &sbAttrib_[0], &n);
146 if (!fbc_)
147 {
148 std::cerr << "glXChooseFBConfig failed.\n";
149 return;
150 }
151
152 pbuf_ = glXCreatePbuffer(dpy_, fbc_[0], &pbAttrib_[0]);
153 if (!pbuf_)
154 {
155 std::cerr << "glXCreatePbuffer failed.\n";
156 return;
157 }
158
159 pbufctx_ = glXCreateNewContext(dpy_, fbc_[0], GLX_RGBA_TYPE, currctx_, true);
160 if (!pbufctx_)
161 {
162 std::cerr << "glXCreateNewContextWithConfigSGIX failed.\n";
163 return;
164 }
165 }
166
167
168 //-----------------------------------------------------------------------------
169
170
171 PBuffer::~PBuffer()
172 {
173 glXDestroyContext(dpy_, currctx_);
174 glXDestroyPbuffer(dpy_, pbuf_);
175 }
176
177
178 //-----------------------------------------------------------------------------
179
180
181 void PBuffer::resize(int _w, int _h)
182 {
183 pbAttrib_[1] = _w;
184 pbAttrib_[3] = _h;
185 glXDestroyPbuffer(dpy_, pbuf_);
186 pbuf_ = glXCreatePbuffer(dpy_, fbc_[0], &pbAttrib_[0]);
187 if (!pbuf_) std::cerr << "Resizing pbuffer failed.\n";
188 }
189
190
191 //-----------------------------------------------------------------------------
192
193
194 int PBuffer::bits()
195 {
196 return sbAttrib_[3];
197 }
198
199
200 //-----------------------------------------------------------------------------
201
202
203 void PBuffer::activate()
204 {
205 if (!glXMakeCurrent(dpy_, pbuf_, pbufctx_))
206 std::cerr << "PBuffer:activate() failed.\n";
207 }
208
209
210 //-----------------------------------------------------------------------------
211
212
213 void PBuffer::deactivate()
214 {
215 if (!glXMakeCurrent(dpy_, currdraw_, currctx_))
216 std::cerr << "PBuffer:deactivate() failed.\n";
217 }
218
219
220 //-----------------------------------------------------------------------------
221
222
223 const int PBuffer::width() const
224 {
225 return pbAttrib_[1];
226 }
227
228
229 //-----------------------------------------------------------------------------
230
231
232 const int PBuffer::height() const
233 {
234 return pbAttrib_[3];
235 }
236
237
238 //=============================================================================
239 #endif // Linx only
240 //=============================================================================
OpenMesh::Attributes::None
Clear all attribute bits.
Definition: Attributes.hh:81