KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gr_opengl_texture.h
Go to the documentation of this file.
1 /* $Id: gr_opengl_texture.h 18278 2014-11-25 01:42:10Z ksimek $ */
2 /* {{{=========================================================================== *
3  |
4  | Copyright (c) 1994-2010 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== }}}*/
19 
20 // vim: tabstop=4 shiftwidth=4 foldmethod=marker
21 #ifndef KJB_CPP_OPENGL_TEXTURE
22 #define KJB_CPP_OPENGL_TEXTURE
23 
24 // this must be before opengl.h
26 
27 #ifdef DEBUGGING
28 #include <gr_cpp/gr_glut.h>
29 #endif
30 
31 #ifdef KJB_HAVE_OPENGL
32 
33 #include <map>
34 #include <m_cpp/m_matrix.h>
35 #include <i_cpp/i_image.h>
36 #include <l_cpp/l_util.h>
37 
38 typedef unsigned int GLunum;
39 namespace kjb
40 {
41 namespace opengl
42 {
43 
44 class Int_matrix;
45 
58 class Texture
59 {
60 typedef Texture Self;
61 
62 public:
70  Texture();
71 
77  Texture(GLuint texture_id, int width, int height);
78 
79  Texture(const Texture& other);
80 
81  int get_width() const;
82 
83  int get_height() const;
84 
85  int get_num_cols() const;
86 
87  int get_num_rows() const;
88 
89 
94  void free();
95 
100  Self& operator=(const Self& src);
101 
117  Texture& set(GLenum target, int level, GLint internal_format, GLsizei width, GLsizei height, int border, GLenum format, const GLfloat* data = 0);
118 
119  Texture& set(GLenum target, int level, GLint internal_format, GLsizei width, GLsizei height, int border, GLenum format, const GLint* data = 0);
120 
121  Texture& set(GLenum target, int level, GLint internal_format, GLsizei width, GLsizei height, int border, GLenum format, const GLuint* data = 0);
122 
123  Texture& set(GLenum target, int level, GLint internal_format, GLsizei width, GLsizei height, int border, GLenum format, const GLubyte* data = 0);
124 
125  Texture& set(GLenum target, int level, GLint internal_format, GLsizei width, GLsizei height, int border, GLenum format, const GLushort* data = 0);
126 
137  template <class T>
138  Texture& set(GLsizei width, GLsizei height, GLenum format, const T* data = 0)
139  {
140  return set(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, format, data);
141  }
142 
147  Self& allocate(GLenum internal_format, GLsizei width, GLsizei height);
148 
153  Self& allocate(GLenum internal_format, GLsizei width, GLsizei height, GLenum format);
154 
164  Texture& set(const Image& img);
165 
172  Texture& set(
173  const Image& img,
174  GLenum target,
175  GLint level,
176  GLint border = 0 );
177 
178 
190  Texture& set(const Matrix& mat);
191 
192 
202  Texture& set(const Matrix& mat, GLenum target, GLenum level, GLint border);
203 
204 
205  Self& set_float(const Matrix& mat, GLenum target = GL_TEXTURE_2D);
206 
207 
208 
226  Self& set_packed_float(const Matrix& mat, GLenum target = GL_TEXTURE_2D);
227 
228 
234  template <class Matrix_type>
235  Self& set_mask_4ui(const Matrix_type& mat, GLenum target = GL_TEXTURE_2D)
236  {
237  GLubyte* array = create_gl_mask_4ui(mat);
238 
239  set(target, 0, GL_RGBA8, mat.get_num_cols(), mat.get_num_rows(), 0, GL_RGBA, array);
240 
241  delete[] array;
242  return *this;
243  }
244 
250  Self& set_mask_1f(const kjb::Matrix& mat, GLenum target = GL_TEXTURE_2D);
251 
252 
258  Self& set_mask_1f(const kjb::Int_matrix& mat, GLenum target = GL_TEXTURE_2D);
259 
260 
261 
262  ~Texture();
263 
267  void bind() const;
268 
274  void unbind() const;
275 
276  static int max_color_attachments();
277 
282  operator GLuint() const;
283 
284  GLuint get() const;
285 
286 
287 protected:
288 
297  static float* create_gl_array(const Matrix& mat);
298 
308  template <class Matrix_type>
309  static GLubyte* create_gl_mask_4ui(const Matrix_type& mat)
310  {
311  GLubyte* array = new GLubyte[mat.get_length() * 4];
312  GLubyte* cur = array;
313 
314  const int num_rows = mat.get_num_rows();
315  const int num_cols = mat.get_num_cols();
316 
317  for(int row = num_rows - 1; row >= 0; row--)
318  for(int col = 0; col < num_cols; col++)
319  {
320  *cur++ = (mat(row, col) == 0 ? 0 : 255);
321  *cur++ = (mat(row, col) == 0 ? 0 : 255);
322  *cur++ = (mat(row, col) == 0 ? 0 : 255);
323  *cur++ = (mat(row, col) == 0 ? 0 : 255);
324  }
325 
326  return array;
327  }
328 
329 private:
330  template <class T>
331  Texture& set(
332  GLenum target,
333  int level,
334  GLint internal_format,
335  GLsizei width,
336  GLsizei height,
337  int border,
338  GLenum format,
339  const T* data = 0);
340 private:
341  static std::map<int, int> ref_count_;
342 
343  GLuint handle_;
344  int width_;
345  int height_;
346 };
347 
348 void draw_fullscreen_textured_quad(const Texture& texture);
349 
350 } // namespace opengl
351 } // namespace kjb
352 
353 #endif /* KJB_HAVE_OPENGL */
354 #endif /* KJB_OPENGL_TEXTURE */
Definition for the Matrix class, a thin wrapper on the KJB Matrix struct and its related functionalit...
int GLenum
Definition: gr_opengl_headers.h:65
This class implements matrices, in the linear-algebra sense, restricted to integer-valued elements...
Definition: l_int_matrix.h:71
height
Definition: APPgetLargeConnectedEdges.m:33
int GLuint
Definition: gr_opengl_headers.h:64
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
Code for a wrapper class around the C struct KJB_Image.