GLS  1.0.0
GL Stuff - A library aimed at reducing the boilerplate OpenGL code you always have to write.
texture.hpp
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 #include <gls/headercheck.hpp>
8 #include <gls/errorcheck.hpp>
9 #include <gls/objects/object.hpp>
10 #include <cassert>
11 
12 namespace gls {
13 
14 namespace priv {
15  void gen_textures( GLsizei size, GLuint* name ) { glGenTextures( size, name ); }
16  void delete_textures( GLsizei size, const GLuint* name ) { glDeleteTextures( size, name ); }
17 }
18 
25 template<GLenum Target = GL_TEXTURE_2D>
26 class texture {
27 public:
36  texture() {
37  parameter( GL_TEXTURE_MIN_FILTER, GL_LINEAR );
38  parameter( GL_TEXTURE_MAG_FILTER, GL_LINEAR );
39  }
40 
47  GLuint name() const {
48  return m_object.name();
49  }
50 
57  GLsizei width() const {
58  return m_width;
59  }
60 
67  GLsizei height() const {
68  return m_height;
69  }
70 
77  GLsizei depth() const {
78  return m_depth;
79  }
80 
88  void bind() {
89  check_gl_error( glBindTexture( Target, m_object.name() ) );
90  }
91 
99  static void unbind() {
100  check_gl_error( glBindTexture( Target, 0 ) );
101  }
102 
112  void parameter( GLenum pname, GLfloat param ) {
113  bind();
114  check_gl_error( glTexParameterf( Target, pname, param ) );
115  unbind();
116  }
117 
127  void parameter( GLenum pname, GLint param ) {
128  bind();
129  check_gl_error( glTexParameteri( Target, pname, param ) );
130  unbind();
131  }
132 
142  void parameter( GLenum pname, const GLfloat* param ) {
143  bind();
144  check_gl_error( glTexParameterfv( Target, pname, param ) );
145  unbind();
146  }
147 
157  void parameter( GLenum pname, const GLint* param ) {
158  bind();
159  check_gl_error( glTexParameteriv( Target, pname, param ) );
160  unbind();
161  }
162 
176  template<typename T>
177  void image_1d( GLint level, GLint internal_format, T the_width, GLenum format, GLenum type, const GLvoid* data ) {
178  m_width = static_cast<GLsizei>( the_width );
179  bind();
180  check_gl_error( glTexImage1D( Target, level, internal_format, m_width, 0, format, type, data ) );
181  unbind();
182  }
183 
198  template<typename T, typename U>
199  void image_2d( GLint level, GLint internal_format, T the_width, U the_height, GLenum format, GLenum type, const GLvoid* data ) {
200  m_width = static_cast<GLsizei>( the_width );
201  m_height = static_cast<GLsizei>( the_height );
202  bind();
203  check_gl_error( glTexImage2D( Target, level, internal_format, m_width, m_height, 0, format, type, data ) );
204  unbind();
205  }
206 
222  template<typename T, typename U, typename V>
223  void image_3d( GLint level, GLint internal_format, T the_width, U the_height, V the_depth, GLenum format, GLenum type, const GLvoid* data ) {
224  m_width = static_cast<GLsizei>( the_width );
225  m_height = static_cast<GLsizei>( the_height );
226  m_depth = static_cast<GLsizei>( the_depth );
227  bind();
228  check_gl_error( glTexImage3D( Target, level, internal_format, m_width, m_height, m_depth, 0, format, type, data ) );
229  unbind();
230  }
231 
243  template<typename T, typename U>
244  void sub_image_1d( GLint level, T xoffset, U the_width, GLenum format, GLenum type, const GLvoid* data ) {
245  assert( xoffset + the_width < m_width );
246  bind();
247  check_gl_error( glTexSubImage1D( Target, level, static_cast<GLint>( xoffset ), static_cast<GLsizei>( the_width ), format, type, data ) );
248  unbind();
249  }
250 
264  template<typename T, typename U, typename V, typename W>
265  void sub_image_2d( GLint level, T xoffset, U yoffset, V the_width, W the_height, GLenum format, GLenum type, const GLvoid* data ) {
266  assert( xoffset + the_width < m_width );
267  assert( yoffset + the_height < m_height );
268  bind();
269  check_gl_error( glTexSubImage2D( Target, level, static_cast<GLint>( xoffset ), static_cast<GLint>( yoffset ), static_cast<GLsizei>( the_width ), static_cast<GLsizei>( the_height ), format, type, data ) );
270  unbind();
271  }
272 
288  template<typename T, typename U, typename V, typename W, typename X, typename Y>
289  void sub_image_3d( GLint level, T xoffset, U yoffset, V zoffset, W the_width, X the_height, Y the_depth, GLenum format, GLenum type, const GLvoid* data ) {
290  assert( xoffset + the_width < m_width );
291  assert( yoffset + the_height < m_height );
292  assert( zoffset + the_depth < m_depth );
293  bind();
294  check_gl_error( glTexSubImage3D( Target, level, static_cast<GLint>( xoffset ), static_cast<GLint>( yoffset ), static_cast<GLint>( zoffset ), static_cast<GLsizei>( the_width ), static_cast<GLsizei>( the_height ), static_cast<GLsizei>( the_depth ), format, type, data ) );
295  unbind();
296  }
297 
307  void get_image( GLint level, GLint format, GLenum type, GLvoid* img ) {
308  bind();
309  check_gl_error( glGetTexImage( Target, level, format, type, img ) );
310  unbind();
311  }
312 
318  bind();
319  check_gl_error( glGenerateMipmap( Target ) );
320  unbind();
321  }
322 
323 protected:
330  texture(int) {
331  }
332 
333 private:
335  GLsizei m_width = 0;
336  GLsizei m_height = 0;
337  GLsizei m_depth = 0;
338 };
339 
340 }
341 
void parameter(GLenum pname, GLint param)
Set a texture parameter.
Definition: texture.hpp:127
void parameter(GLenum pname, GLfloat param)
Set a texture parameter.
Definition: texture.hpp:112
GLuint name() const
Retrieve the OpenGL name of this texture.
Definition: texture.hpp:47
GLsizei height() const
Retrieve the height of the level 0 image of the allocated texture storage.
Definition: texture.hpp:67
void get_image(GLint level, GLint format, GLenum type, GLvoid *img)
Get image data from this texture.
Definition: texture.hpp:307
GLsizei width() const
Retrieve the width of the level 0 image of the allocated texture storage.
Definition: texture.hpp:57
void sub_image_3d(GLint level, T xoffset, U yoffset, V zoffset, W the_width, X the_height, Y the_depth, GLenum format, GLenum type, const GLvoid *data)
Update a section of a 3D image within the texture.
Definition: texture.hpp:289
void parameter(GLenum pname, const GLint *param)
Set a texture parameter.
Definition: texture.hpp:157
void generate_mipmap()
Generate mipmaps for this texture.
Definition: texture.hpp:317
GLsizei depth() const
Retrieve the depth of the level 0 image of the allocated texture storage.
Definition: texture.hpp:77
void image_2d(GLint level, GLint internal_format, T the_width, U the_height, GLenum format, GLenum type, const GLvoid *data)
Allocate storage for a 2D image and upload texture data.
Definition: texture.hpp:199
void image_3d(GLint level, GLint internal_format, T the_width, U the_height, V the_depth, GLenum format, GLenum type, const GLvoid *data)
Allocate storage for a 3D image and upload texture data.
Definition: texture.hpp:223
texture(int)
Constructor that doesn't set texture parameters.
Definition: texture.hpp:330
static void unbind()
Unbind the current texture from the target.
Definition: texture.hpp:99
void bind()
Bind this texture to its target.
Definition: texture.hpp:88
texture()
Default constructor.
Definition: texture.hpp:36
Definition: buffer.hpp:12
void image_1d(GLint level, GLint internal_format, T the_width, GLenum format, GLenum type, const GLvoid *data)
Allocate storage for a 1D image and upload texture data.
Definition: texture.hpp:177
void sub_image_1d(GLint level, T xoffset, U the_width, GLenum format, GLenum type, const GLvoid *data)
Update a section of a 1D image within the texture.
Definition: texture.hpp:244
void sub_image_2d(GLint level, T xoffset, U yoffset, V the_width, W the_height, GLenum format, GLenum type, const GLvoid *data)
Update a section of a 2D image within the texture.
Definition: texture.hpp:265
Class encapsulating an OpenGL texture object.
Definition: texture.hpp:26
void parameter(GLenum pname, const GLfloat *param)
Set a texture parameter.
Definition: texture.hpp:142