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]
CopyTexSubImage.hpp
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 
32 #ifndef CopyTexSubImage_INCLUDE_ONCE
33 #define CopyTexSubImage_INCLUDE_ONCE
34 
35 #include <vlGraphics/Camera.hpp>
36 #include <vlGraphics/Texture.hpp>
38 
39 namespace vl
40 {
41  //-----------------------------------------------------------------------------
42  // CopyTexSubImage
43  //-----------------------------------------------------------------------------
57  {
59 
60  public:
62  {
63  VL_DEBUG_SET_OBJECT_NAME()
64  }
65 
68 
70  void setReadBuffer(EReadDrawBuffer render_buffer) { mReadBuffer = render_buffer; }
71 
73  {
74  copyPixels();
75  return true;
76  }
77 
79  {
80  copyPixels();
81  return true;
82  }
83 
84  virtual bool onRendererStarted(const RendererAbstract*)
85  {
86  copyPixels();
87  return true;
88  }
89 
90  virtual bool onRendererFinished(const RendererAbstract*)
91  {
92  copyPixels();
93  return true;
94  }
95 
99  virtual void copyPixels() = 0;
100 
101  protected:
103  };
104  //-----------------------------------------------------------------------------
107  {
109 
110  public:
111  CopyTexSubImage1D(int level, int xoffset, int x, int y, int width, Texture* texture=NULL, EReadDrawBuffer read_buffer=RDB_BACK_LEFT)
112  {
113  VL_DEBUG_SET_OBJECT_NAME()
114  mLevel = level;
115  mXOffset = xoffset;
116  mX = x;
117  mY = y;
118  mWidth = width;
119  mTexture = texture;
120  setReadBuffer(read_buffer);
121  }
122 
123  void setTexture(Texture* tex) { mTexture = tex; }
124  void setLevel(int level) { mLevel = level; }
125  void setXOffset(int xoffset) { mXOffset = xoffset; }
126  void setX(int x) { mX = x; }
127  void setY(int y) { mY = y; }
128  void setWidth(int width) { mWidth = width; }
129 
130  Texture* texture() { return mTexture.get(); }
131  const Texture* texture() const { return mTexture.get(); }
132  int level() const { return mLevel; }
133  int xoffset() const { return mXOffset; }
134  int x() const { return mX; }
135  int y() const { return mY; }
136  int width() const { return mWidth; }
137 
138  virtual void copyPixels()
139  {
140  VL_CHECK_OGL()
141  VL_CHECK(texture()->dimension() == TD_TEXTURE_1D)
142 
143  VL_CHECK(texture()->handle())
144  VL_CHECK(xoffset() >= 0)
145  VL_CHECK(x() >= 0)
146  VL_CHECK(y() >= 0)
147  VL_CHECK(xoffset()+width() <= texture()->width())
148 
149 #if defined(VL_OPENGL)
150  int read_buffer = 0;
151  if (!texture()->isDepthTexture())
152  {
153  glGetIntegerv(GL_READ_BUFFER, &read_buffer); VL_CHECK_OGL()
154  glReadBuffer(readBuffer()); VL_CHECK_OGL()
155  }
156 #endif
157 
158  glBindTexture(TD_TEXTURE_1D, texture()->handle() ); VL_CHECK_OGL()
159  glCopyTexSubImage1D(TD_TEXTURE_1D, level(), xoffset(), x(), y(), width()); VL_CHECK_OGL()
160  glBindTexture(TD_TEXTURE_1D, 0 ); VL_CHECK_OGL()
161 
162 #if defined(VL_OPENGL)
163  if (read_buffer)
164  {
165  glReadBuffer( read_buffer ); VL_CHECK_OGL()
166  }
167 #endif
168  }
169 
170  protected:
172  int mLevel;
173  int mXOffset;
174  int mX;
175  int mY;
176  int mWidth;
177  };
178  //-----------------------------------------------------------------------------
181  {
183 
184  public:
185  CopyTexSubImage2D(int level, int xoffset, int yoffset, int x, int y, int width, int height, Texture* texture=NULL, ETex2DTarget target=T2DT_TEXTURE_2D, EReadDrawBuffer read_buffer=RDB_BACK_LEFT)
186  {
187  VL_DEBUG_SET_OBJECT_NAME()
188 
189  mLevel = level;
190  mXOffset = xoffset;
191  mYOffset = yoffset;
192  mX = x;
193  mY = y;
194  mWidth = width;
195  mHeight = height;
196  mTexture = texture;
197  mTarget = target;
198  setReadBuffer(read_buffer);
199  }
200 
201  void setTexture(Texture* tex) { mTexture = tex; }
202  void setLevel(int level) { mLevel = level; }
203  void setXOffset(int xoffset) { mXOffset = xoffset; }
204  void setYOffset(int yoffset) { mYOffset = yoffset; }
205  void setX(int x) { mX = x; }
206  void setY(int y) { mY = y; }
207  void setWidth(int width) { mWidth = width; }
208  void setHeight(int height) { mHeight = height; }
209  void setTarget(ETex2DTarget target) { mTarget = target; }
210 
211  Texture* texture() { return mTexture.get(); }
212  const Texture* texture() const { return mTexture.get(); }
213  int level() const { return mLevel; }
214  int xoffset() const { return mXOffset; }
215  int yoffset() const { return mYOffset; }
216  int x() const { return mX; }
217  int y() const { return mY; }
218  int width() const { return mWidth; }
219  int height() const { return mHeight; }
220  ETex2DTarget target() const { return mTarget; }
221 
222  virtual void copyPixels()
223  {
224  VL_CHECK_OGL()
225 
226  VL_CHECK(texture()->handle())
227  VL_CHECK(xoffset() >= 0)
228  VL_CHECK(yoffset() >= 0)
229  VL_CHECK(x() >= 0)
230  VL_CHECK(y() >= 0)
231  VL_CHECK(xoffset()+width() <= texture()->width())
232  VL_CHECK(yoffset()+height() <= texture()->height())
233 
234 #if defined(VL_OPENGL)
235  int read_buffer = 0;
236  if (!texture()->isDepthTexture())
237  {
238  glGetIntegerv(GL_READ_BUFFER, &read_buffer); VL_CHECK_OGL()
239  glReadBuffer(readBuffer()); VL_CHECK_OGL()
240  }
241 #endif
242 
243  int bind_target = 0;
244  switch( target() )
245  {
252  bind_target = GL_TEXTURE_CUBE_MAP;
253  break;
254  default:
255  bind_target = target();
256  };
257 
258  glBindTexture( bind_target, texture()->handle() ); VL_CHECK_OGL()
259  glCopyTexSubImage2D( target(), level(), xoffset(), yoffset(), x(), y(), width(), height() ); VL_CHECK_OGL()
260  glBindTexture( bind_target, 0 ); VL_CHECK_OGL()
261 
262 #if defined(VL_OPENGL)
263  if (read_buffer)
264  {
265  glReadBuffer( read_buffer ); VL_CHECK_OGL()
266  }
267 #endif
268  }
269 
270  protected:
272  int mLevel;
273  int mXOffset;
274  int mYOffset;
275  int mX;
276  int mY;
277  int mWidth;
278  int mHeight;
280  };
281  //-----------------------------------------------------------------------------
284  {
286 
287  public:
288  CopyTexSubImage3D(int level, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height, Texture* texture, EReadDrawBuffer read_buffer=RDB_BACK_LEFT)
289  {
290  VL_DEBUG_SET_OBJECT_NAME()
291 
292  mLevel = level;
293  mXOffset = xoffset;
294  mYOffset = yoffset;
295  mZOffset = zoffset;
296  mX = x;
297  mY = y;
298  mWidth = width;
299  mHeight = height;
300  mTexture = texture;
301  setReadBuffer(read_buffer);
302  }
303 
304  void setTexture(Texture* tex) { mTexture = tex; }
305  void setLevel(int level) { mLevel = level; }
306  void setXOffset(int xoffset) { mXOffset = xoffset; }
307  void setYOffset(int yoffset) { mYOffset = yoffset; }
308  void setZOffset(int zoffset) { mZOffset = zoffset; }
309  void setX(int x) { mX = x; }
310  void setY(int y) { mY = y; }
311  void setWidth(int width) { mWidth = width; }
312  void setHeight(int height) { mHeight = height; }
313 
314  Texture* texture() { return mTexture.get(); }
315  const Texture* texture() const { return mTexture.get(); }
316  int level() const { return mLevel; }
317  int xoffset() const { return mXOffset; }
318  int yoffset() const { return mYOffset; }
319  int zoffset() const { return mZOffset; }
320  int x() const { return mX; }
321  int y() const { return mY; }
322  int width() const { return mWidth; }
323  int height() const { return mHeight; }
324 
325  virtual void copyPixels()
326  {
327  if (Has_Texture_3D)
328  {
329  VL_CHECK_OGL()
330  VL_CHECK( texture()->dimension() == TD_TEXTURE_3D )
331 
332  VL_CHECK(texture()->handle())
333  VL_CHECK(xoffset() >= 0)
334  VL_CHECK(yoffset() >= 0)
335  VL_CHECK(zoffset() >= 0)
336  VL_CHECK(x() >= 0)
337  VL_CHECK(y() >= 0)
338  VL_CHECK(xoffset()+width() <= texture()->width())
339  VL_CHECK(yoffset()+height() <= texture()->height())
340  VL_CHECK(zoffset() < texture()->depth())
341 
342 #if defined(VL_OPENGL)
343  int read_buffer = 0;
344  if (!texture()->isDepthTexture())
345  {
346  glGetIntegerv(GL_READ_BUFFER, &read_buffer); VL_CHECK_OGL()
347  glReadBuffer(readBuffer()); VL_CHECK_OGL()
348  }
349 #endif
350 
351  glBindTexture(texture()->dimension(), texture()->handle() ); VL_CHECK_OGL()
352  VL_glCopyTexSubImage3D(texture()->dimension(), level(), xoffset(), yoffset(), zoffset(), x(), y(), width(), height()); VL_CHECK_OGL()
353  glBindTexture(texture()->dimension(), 0 );
354 
355 #if defined(VL_OPENGL)
356  if (read_buffer)
357  {
358  glReadBuffer( read_buffer ); VL_CHECK_OGL()
359  }
360 #endif
361  }
362  else
363  Log::error("CopyTexSubImage3D requires OpenGL 1.2!\n");
364  }
365 
366  protected:
368  int mLevel;
369  int mXOffset;
370  int mYOffset;
371  int mZOffset;
372  int mX;
373  int mY;
374  int mWidth;
375  int mHeight;
376  };
377  //-----------------------------------------------------------------------------
378 }
379 
380 #endif
void setYOffset(int yoffset)
virtual void copyPixels()
Copies the pixels from the specified read buffer to the specified texture.
void setTarget(ETex2DTarget target)
const Texture * texture() const
Wrapper class of the OpenGL function glCopyTexSubImage.
virtual bool onRendererStarted(const RendererAbstract *)
Reimplement to react to this event.
The RenderingAbstract class is the base of all the rendering related sub-classes. ...
void setXOffset(int xoffset)
void setXOffset(int xoffset)
CopyTexSubImage1D(int level, int xoffset, int x, int y, int width, Texture *texture=NULL, EReadDrawBuffer read_buffer=RDB_BACK_LEFT)
void setTexture(Texture *tex)
EReadDrawBuffer mReadBuffer
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
const Texture * texture() const
Wraps an OpenGL texture object representing and managing all the supported texture types...
Definition: Texture.hpp:143
virtual void copyPixels()=0
Copies the pixels from the specified read buffer to the specified texture.
void setLevel(int level)
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
virtual void copyPixels()
Copies the pixels from the specified read buffer to the specified texture.
void setYOffset(int yoffset)
virtual bool onRendererFinished(const RendererAbstract *)
Reimplement to react to this event.
virtual bool onRenderingFinished(const RenderingAbstract *)
Reimplement to react to this event.
ETex2DTarget
EReadDrawBuffer
Visualization Library main namespace.
void setLevel(int level)
An abstract class used to react to rendering events.
virtual bool onRenderingStarted(const RenderingAbstract *)
Reimplement to react to this event.
void setReadBuffer(EReadDrawBuffer render_buffer)
The source buffer used when copying color buffers, ignored when using depth textures.
void setLevel(int level)
void setHeight(int height)
void setTexture(Texture *tex)
const Texture * texture() const
CopyTexSubImage2D(int level, int xoffset, int yoffset, int x, int y, int width, int height, Texture *texture=NULL, ETex2DTarget target=T2DT_TEXTURE_2D, EReadDrawBuffer read_buffer=RDB_BACK_LEFT)
Base class providing all the basic funtionalities of a Renderer.
ETex2DTarget target() const
void setZOffset(int zoffset)
#define NULL
Definition: OpenGLDefs.hpp:81
CopyTexSubImage3D(int level, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height, Texture *texture, EReadDrawBuffer read_buffer=RDB_BACK_LEFT)
void setHeight(int height)
void setWidth(int width)
Wraps glCopyTexSubImage1D, see also CopyTexSubImage.
Wraps glCopyTexSubImage2D, see also CopyTexSubImage. To be used also for 1D array textures...
Wraps glCopyTexSubImage3D, see also CopyTexSubImage. To be used also for 2D array textures...
#define VL_CHECK_OGL()
Definition: OpenGL.hpp:156
#define VL_INSTRUMENT_ABSTRACT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:145
void setWidth(int width)
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
void setXOffset(int xoffset)
virtual void copyPixels()
Copies the pixels from the specified read buffer to the specified texture.
#define VL_CHECK(expr)
Definition: checks.hpp:73
void setWidth(int width)
bool Has_Texture_3D
Definition: OpenGL.cpp:91
void setTexture(Texture *tex)
EReadDrawBuffer readBuffer() const
The source buffer used when copying color buffers, ignored when using depth textures.