KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gr_offscreen.h
Go to the documentation of this file.
1 
34 /* =========================================================================== *
35 |
36 | Copyright (c) 1994-2008 by Kobus Barnard (author).
37 |
38 | Personal and educational use of this code is granted, provided that this
39 | header is kept intact, and that the authorship is not misrepresented, that
40 | its use is acknowledged in publications, and relevant papers are cited.
41 |
42 | For other use contact the author (kobus AT cs DOT arizona DOT edu).
43 |
44 | Please note that the code in this file has not necessarily been adequately
45 | tested. Naturally, there is no guarantee of performance, support, or fitness
46 | for any particular task. Nonetheless, I am interested in hearing about
47 | problems that you encounter.
48 |
49 | Authors:
50 | Joseph Schlecht, Luca Del Pero
51 |
52 * =========================================================================== */
53 
67 #ifndef KJB_GR_OFFSCREEN_H
68 #define KJB_GR_OFFSCREEN_H
69 
70 #include <l_cpp/l_exception.h>
72 
73 #ifdef KJB_HAVE_X11
74 #ifdef KJB_HAVE_OPENGL
75 #ifndef MAC_OSX
76 
77 #ifdef KJB_HAVE_OSMESA
78 #include <GL/osmesa.h>
79 #else
80 #include <X11/Xlib.h>
81 #include <GL/gl.h>
82 #include <GL/glx.h>
83 #endif
84 
85 #else
86 #include <OpenGL/gl.h>
87 #include <OpenGL/OpenGL.h>
88 #endif
89 
90 #endif
91 #endif
92 
93 
94 
95 namespace kjb {
96 
97 
101 void retrieve_offscreen_capabilities(bool *_has_gl, bool *_has_osmesa);
102 
108 bool supports_pbuffers();
109 
115 bool supports_pixmaps();
116 
123 {
124  public:
125 
127  Offscreen_buffer(int iwidth, int iheight)
128  { this->width = iwidth; this->height = iheight; active = false; }
129 
131  virtual ~Offscreen_buffer() { }
132 
134  virtual void activate() throw(kjb::Exception) = 0;
135 
137  virtual void deactivate() throw(kjb::Exception) = 0;
138 
140  virtual void reactivate() throw(kjb::Exception) = 0;
141 
142  protected:
143 
145  bool active;
146 
148  int width;
149 
151  int height;
152 };
153 
154 
155 #if defined KJB_HAVE_X11 && defined KJB_HAVE_OPENGL
156 #ifndef KJB_HAVE_OSMESA
157 #ifndef MAC_OSX
158 
163 class GLX_offscreen_buffer : public Offscreen_buffer
164 {
165  public:
166 
168  GLX_offscreen_buffer(int iwidth, int iheight)
169  : Offscreen_buffer(iwidth, iheight) { display_open = false; }
170 
172  virtual ~GLX_offscreen_buffer() { }
173 
175  virtual void activate() throw(kjb::Exception);
176 
178  virtual void deactivate() throw(kjb::Exception);
179 
181  virtual void reactivate() throw(kjb::Exception);
182 
183  protected:
184 
189  void save_old_context();
190 
192  void open_display(const char* disp_name=0)
193  throw(kjb::Result_error);
194 
196  virtual void create_drawable_and_context(int* attrs)
197  throw(kjb::Exception) = 0;
198 
200  virtual void destroy_drawable() = 0;
201 
203  bool has_pbuffer_extension() const;
204 
206  bool has_pixmap_extension() const;
207 
208  protected:
209 
211  bool display_open;
212 
214  Display* display;
215 
217  GLXDrawable drawable;
218 
220  GLXDrawable old_drawable;
221 
223  GLXContext context;
224 
226  GLXContext old_context;
227 };
228 
229 
237 class GLX_offscreen_pixmap : public GLX_offscreen_buffer
238 {
239  public:
240 
242  GLX_offscreen_pixmap(int width, int height, int* attrs=0)
243  throw(kjb::Exception);
244 
246  virtual ~GLX_offscreen_pixmap();
247 
248  protected:
249 
251  virtual void create_drawable_and_context(int* attrs)
252  throw(kjb::Result_error);
253 
255  virtual void destroy_drawable();
256 
257  protected:
258 
260  Pixmap pixmap;
261 };
262 
263 
271 class GLX_offscreen_pbuffer : public GLX_offscreen_buffer
272 {
273  public:
274 
276  GLX_offscreen_pbuffer(int width, int height)
277  throw(kjb::Exception);
278 
280  GLX_offscreen_pbuffer(int width, int heigth, int* attrs)
281  throw (kjb::Exception);
282 
284  virtual ~GLX_offscreen_pbuffer();
285 
286  protected:
287 
289  virtual void create_drawable_and_context(int* attrs)
290  throw(kjb::Exception);
291 
293  virtual void destroy_drawable();
294 };
295 
296 
297 #else
298 
303 class CGL_offscreen_pbuffer : public Offscreen_buffer
304 {
305  public:
306 
308  CGL_offscreen_pbuffer
309  (
310  GLsizei width,
311  GLsizei height,
312  GLenum format=GL_RGBA,
313  const CGLPixelFormatAttribute* attrs=0
314  )
315  throw (kjb::Exception);
316 
318  virtual ~CGL_offscreen_pbuffer();
319 
321  virtual void activate() throw(kjb::Exception);
322 
324  virtual void deactivate() throw(kjb::Exception);
325 
327  virtual void reactivate() throw(kjb::Exception);
328 
329  protected:
330 
334  virtual void save_old_context();
335 
337  virtual void create_pbuffer_and_context
338  (
339  GLenum format,
340  const CGLPixelFormatAttribute* attrs
341  )
342  throw(kjb::Exception);
343 
344  protected:
345 
347  CGLContextObj context;
348 
350  CGLContextObj old_context;
351 
353  CGLPBufferObj pbuffer;
354 };
355 #endif
356 #endif
357 #endif
358 
359 #ifdef KJB_HAVE_OSMESA
360 
365 class OSMesa32_offscreen_buffer : public Offscreen_buffer
366 {
367  public:
368 
370  OSMesa32_offscreen_buffer
371  (
372  GLsizei width,
373  GLsizei height,
374  GLenum format=OSMESA_RGBA,
375  GLint depth_bits=4,
376  GLint stencil_bits=4,
377  GLint accum_bits=4
378  )
379  throw (kjb::Exception);
380 
382  virtual ~OSMesa32_offscreen_buffer();
383 
385  virtual void activate() throw(kjb::Exception);
386 
388  virtual void deactivate() throw(kjb::Exception);
389 
391  virtual void reactivate() throw(kjb::Exception);
392 
393  protected:
394 
396  virtual void create_buffer_and_context
397  (
398  GLenum format,
399  GLint depth_bits,
400  GLint stencil_bits,
401  GLint accum_bits
402  )
403  throw(kjb::Exception);
404 
405  protected:
406 
408  OSMesaContext context;
409 
411  void* buffer;
412 };
413 #endif
414 
415 #ifdef KJB_HAVE_OPENGL
416 
431 Offscreen_buffer * create_default_offscreen_buffer
432 (
433  GLsizei iwidth,
434  GLsizei iheight,
435  bool use_pbuffers_when_available = true
436 );
437 
447 Offscreen_buffer * create_and_initialize_offscreen_buffer
448 (
449  unsigned int width,
450  unsigned int height,
451  bool use_pbuffers_when_available = true
452 );
453 
454 #endif
455 
456 }
457 
458 
459 #endif
Base class of all exceptions in the jwsc++ library.
Definition: l_exception.h:132
bool supports_pbuffers()
DEPRECATED Returns true if pbuffers are available. ONLY FOR DEBUG PURPOSES.
Definition: gr_offscreen.cpp:117
int GLenum
Definition: gr_opengl_headers.h:65
virtual void reactivate()=0
Reactivates an offscreen buffer.
Object thrown when a function cannot generate a valid result.
Definition: l_exception.h:516
Offscreen_buffer(int iwidth, int iheight)
Constructs an offscreen buffer.
Definition: gr_offscreen.h:127
virtual ~Offscreen_buffer()
Deletes an offscreen buffer.
Definition: gr_offscreen.h:131
int width
Width of the offscreen buffer.
Definition: gr_offscreen.h:148
int height
height of the offscreen buffer.
Definition: gr_offscreen.h:151
Offscreen rendering buffer.
Definition: gr_offscreen.h:122
virtual void activate()=0
Activates an offscreen buffer.
bool supports_pixmaps()
DEPRECATED Returns true if pixelmaps are available. ONLY FOR DEBUG PURPOSES.
Definition: gr_offscreen.cpp:146
void retrieve_offscreen_capabilities(bool *_has_gl, bool *_has_osmesa)
Checks what offscreen rendering capabilities are available on the machine.
Definition: gr_offscreen.cpp:99
Support for error handling exception classes in libKJB.
virtual void deactivate()=0
Deactivates an offscreen buffer.
bool active
The offscreen buffer is active.
Definition: gr_offscreen.h:145