Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Framebuffer.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
33 #include <vlCore/Log.hpp>
34 #include <vlCore/Say.hpp>
35 
36 #include <map>
37 #include <set>
38 
39 using namespace vl;
40 
41 //-----------------------------------------------------------------------------
42 // Framebuffer
43 //-----------------------------------------------------------------------------
45 {
46  std::map<int, const char*> fbo_attachments;
47 
48  fbo_attachments[GL_NONE] = "GL_NONE";
49  fbo_attachments[GL_BACK_LEFT] = "GL_BACK_LEFT";
50  fbo_attachments[GL_BACK_RIGHT] = "GL_BACK_RIGHT";
51  fbo_attachments[GL_FRONT_LEFT] = "GL_FRONT_LEFT";
52  fbo_attachments[GL_FRONT_RIGHT] = "GL_FRONT_RIGHT";
53  fbo_attachments[GL_AUX0] = "GL_AUX0";
54  fbo_attachments[GL_AUX1] = "GL_AUX1";
55  fbo_attachments[GL_AUX2] = "GL_AUX2";
56  fbo_attachments[GL_AUX3] = "GL_AUX3";
57  fbo_attachments[GL_COLOR_ATTACHMENT0] = "GL_COLOR_ATTACHMENT0";
58  fbo_attachments[GL_COLOR_ATTACHMENT1] = "GL_COLOR_ATTACHMENT1";
59  fbo_attachments[GL_COLOR_ATTACHMENT2] = "GL_COLOR_ATTACHMENT2";
60  fbo_attachments[GL_COLOR_ATTACHMENT3] = "GL_COLOR_ATTACHMENT3";
61  fbo_attachments[GL_COLOR_ATTACHMENT4] = "GL_COLOR_ATTACHMENT4";
62  fbo_attachments[GL_COLOR_ATTACHMENT5] = "GL_COLOR_ATTACHMENT5";
63  fbo_attachments[GL_COLOR_ATTACHMENT6] = "GL_COLOR_ATTACHMENT6";
64  fbo_attachments[GL_COLOR_ATTACHMENT7] = "GL_COLOR_ATTACHMENT7";
65  fbo_attachments[GL_COLOR_ATTACHMENT8] = "GL_COLOR_ATTACHMENT8";
66  fbo_attachments[GL_COLOR_ATTACHMENT9] = "GL_COLOR_ATTACHMENT9";
67  fbo_attachments[GL_COLOR_ATTACHMENT10] = "GL_COLOR_ATTACHMENT10";
68  fbo_attachments[GL_COLOR_ATTACHMENT11] = "GL_COLOR_ATTACHMENT11";
69  fbo_attachments[GL_COLOR_ATTACHMENT12] = "GL_COLOR_ATTACHMENT12";
70  fbo_attachments[GL_COLOR_ATTACHMENT13] = "GL_COLOR_ATTACHMENT13";
71  fbo_attachments[GL_COLOR_ATTACHMENT14] = "GL_COLOR_ATTACHMENT14";
72  fbo_attachments[GL_COLOR_ATTACHMENT15] = "GL_COLOR_ATTACHMENT15";
73 
74  int fbo = 0;
75  if (Has_GL_EXT_framebuffer_object||Has_GL_ARB_framebuffer_object)
76  glGetIntegerv(GL_FRAMEBUFFER_BINDING, &fbo);
77 
78  if (fbo)
79  {
80  std::set<GLenum> legal;
81  legal.insert(GL_NONE);
82  legal.insert(GL_COLOR_ATTACHMENT0);
83  legal.insert(GL_COLOR_ATTACHMENT1);
84  legal.insert(GL_COLOR_ATTACHMENT2);
85  legal.insert(GL_COLOR_ATTACHMENT3);
86  legal.insert(GL_COLOR_ATTACHMENT4);
87  legal.insert(GL_COLOR_ATTACHMENT5);
88  legal.insert(GL_COLOR_ATTACHMENT6);
89  legal.insert(GL_COLOR_ATTACHMENT7);
90  legal.insert(GL_COLOR_ATTACHMENT8);
91  legal.insert(GL_COLOR_ATTACHMENT9);
92  legal.insert(GL_COLOR_ATTACHMENT10);
93  legal.insert(GL_COLOR_ATTACHMENT11);
94  legal.insert(GL_COLOR_ATTACHMENT12);
95  legal.insert(GL_COLOR_ATTACHMENT13);
96  legal.insert(GL_COLOR_ATTACHMENT14);
97  legal.insert(GL_COLOR_ATTACHMENT15);
98  for(unsigned i=0; i<mDrawBuffers.size(); ++i)
99  {
100  if(legal.find(mDrawBuffers[i]) == legal.end())
101  {
102  Log::error(Say("FBO bound but Framebuffer::setDrawBuffers() called with non FBO compatible draw buffer '%s'.\n") << fbo_attachments[mDrawBuffers[i]]);
103  return false;
104  }
105  }
106  }
107  else
108  {
109  std::set<GLenum> legal;
110  legal.insert(GL_NONE);
111  legal.insert(GL_BACK_LEFT);
112  legal.insert(GL_BACK_RIGHT);
113  legal.insert(GL_FRONT_LEFT);
114  legal.insert(GL_FRONT_RIGHT);
115  legal.insert(GL_AUX0);
116  legal.insert(GL_AUX1);
117  legal.insert(GL_AUX2);
118  legal.insert(GL_AUX3);
119  for(unsigned i=0; i<mDrawBuffers.size(); ++i)
120  {
121  if(legal.find(mDrawBuffers[i]) == legal.end())
122  {
123  Log::error(Say("FBO not bound or not supported but Framebuffer::setDrawBuffers() called with FBO specific draw buffer '%s'.\n") << fbo_attachments[mDrawBuffers[i]]);
124  return false;
125  }
126  }
127  }
128  return true;
129 }
130 //-----------------------------------------------------------------------------
132 {
133  VL_CHECK_OGL()
134 
135  VL_CHECK(!mDrawBuffers.empty())
136 
137  #ifndef NDEBUG
139  #endif
140 
141  if (mDrawBuffers.size() > 1 && (Has_GL_ARB_draw_buffers||Has_GL_Version_2_0))
142  {
143  glDrawBuffers( (GLsizei)mDrawBuffers.size(), (const GLenum*)&mDrawBuffers[0] );
144  VL_CHECK_OGL() // If you are using RDB_BACK_LEFT/RIGHT make sure sure you have a double buffered gl context.
145  // Otherwise use Framebuffer::setDrawBuffer(RDB_FRONT_LEFT).
146  }
147  else
148  {
149  glDrawBuffer( mDrawBuffers[0] );
150  VL_CHECK_OGL() // If you are using RDB_BACK_LEFT/RIGHT make sure sure you have a double buffered gl context.
151  // Otherwise use Framebuffer::setDrawBuffer(RDB_FRONT_LEFT).
152  if ( mDrawBuffers.size() > 1 )
153  {
154  Log::error( "Framebuffer::bindDrawBuffers() error:\nglDrawBuffers() not supported by the current OpenGL driver. GL_ARB_draw_buffers or OpenGL 2.0 required.\n" );
155  }
156  }
157 }
158 //-----------------------------------------------------------------------------
160 {
161  VL_CHECK_OGL();
162 
163  glReadBuffer( readBuffer() );
164 
165  VL_CHECK_OGL();
166 }
167 //-----------------------------------------------------------------------------
168 
void bindReadBuffer()
Binds to the currently active framebuffer object (including the 0 one) the read buffer specified by s...
A simple String formatting class.
Definition: Say.hpp:124
EReadDrawBuffer readBuffer() const
The read-buffer bound when the render target is activated.
void bindDrawBuffers() const
Binds to the currently active framebuffer object (including the 0 one) the draw buffers specified by ...
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
Definition: Log.cpp:165
Visualization Library main namespace.
bool checkDrawBuffers() const
Returns true if the draw buffers bound to this render target are legal for this render target type...
Definition: Framebuffer.cpp:44
#define VL_CHECK_OGL()
Definition: OpenGL.hpp:156
bool Has_GL_Version_2_0
Definition: OpenGL.cpp:54
#define VL_CHECK(expr)
Definition: checks.hpp:73