Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
glfTexture.hh
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2008, 2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #ifndef __glfTexture_hh__
26 #define __glfTexture_hh__
27 
28 #include "glfCommon.hh"
29 #include <Base/Image/ImageBase.hh>
30 #include <map>
31 
32 namespace BIAS {
33 
34  /**
35  * \brief Base class for textures. When creating a texture, call:
36  * 1. Create()
37  * 2. Set texture parameters.
38  * 3. Upload...(...) method of a derived class.
39  * \ingroup g_openglframework
40  * \author jkollmann
41  */
42  class BIASOpenGLFramework_EXPORT glfTexture {
43  public:
44  virtual ~glfTexture();
45 
46  /**
47  * Creates the texture but does not upload any data yet.
48  */
49  void Create();
50 
51  /**
52  * Sets the minifying function.
53  * Valid values are: GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST,
54  * GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR,
55  * GL_LINEAR_MIPMAP_LINEAR.
56  */
57  void SetMinFilter(GLenum minFilter);
58 
59  /**
60  * Sets the magnification function.
61  * Valid values are: GL_NEAREST, GL_LINEAR.
62  */
63  void SetMagFilter(GLenum magFilter);
64 
65  /**
66  * Sets the wrapping mode for the 1st texture coordinate.
67  * Valid values are: GL_CLAMP, GL_CLAMP_TO_BORDER, GL_CLAMP_TO_EDGE,
68  * GL_MIRRORED_REPEAT, GL_REPEAT.
69  */
70  void SetWrapS(GLenum wrapS);
71 
72  /**
73  * Sets the wrapping mode for the 2nd texture coordinate.
74  * \see TextureParameters::SetWrapS.
75  */
76  void SetWrapT(GLenum wrapT);
77 
78  /**
79  * Sets the wrapping mode for the 3rd texture coordinate.
80  * \see TextureParameters::SetWrapS.
81  */
82  void SetWrapR(GLenum wrapR);
83 
84  /**
85  * Sets whether to automatically generate mipmaps when the first mipmap is uploaded.
86  */
87  void SetGenerateMipMap(bool generateMipMap);
88 
89  /**
90  * Sets the minimum (base) mip map level to be used.
91  */
92  void SetBaseLevel(int baseLevel);
93 
94  /**
95  * Sets the maximum mip map level to be used.
96  */
97  void SetMaxLevel(int maxLevel);
98 
99  /**
100  * Sets the border color for the texture to be used.
101  */
102  void SetBorderColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a);
103 
104  /**
105  * Sets whether to enlarge each texture dimension to the next power of two,
106  * if non-power-of-two textures are not supported. Otherwise, uploading
107  * a non-power-of-two texture will cause an exception to be thrown, if the
108  * hardware does not support it. The default is 'false'.
109  * Must be called before any data is uploaded to the texture.
110  */
111  void EnlargeToPowerOfTwoIfRequired(bool enlargeToPotIfRequired = true);
112 
113  /**
114  * Explicitly generates the mipmaps of the texture in hardware.
115  * This can be used, when the texture was generated by rendering to a
116  * framebuffer object.
117  */
118  void GenerateMipMap();
119 
120  /**
121  * Binds the texture.
122  * \attention For internal usage inside OpenGLFramework Base library only.
123  */
124  void Bind() const;
125 
126  /**
127  * Binds the texture.
128  * Set texture active. Use this for multitexturing shaders without the use of a glfbatch.
129  * \attention For internal usage inside OpenGLFramework Base library only.
130  */
131  void BindTU() const;
132 
133  /**
134  * Binds the texture.
135  * Sets the texture active with GL_TEXTURE0+number, before binding
136  * \attention For internal usage inside OpenGLFramework Base library only.
137  */
138  void BindTU(int number) const;
139 
140  /**
141  * Returns the OpenGL texture id.
142  * \attention For internal usage inside OpenGLFramework Base library only.
143  */
144  GLuint GetTextureID() const { return id_; }
145 
146  /**
147  * Returns the OpenGL texture target.
148  * \attention For internal usage inside OpenGLFramework Base library only.
149  */
150  GLenum GetTextureTarget() const { return target_; }
151 
152  /**
153  * Returns the OpenGL texture number.
154  * \attention For internal usage inside OpenGLFramework Base library only.
155  */
156  inline GLint GetTextureNr() { return textureNr_; }
157 
158  /**
159  * Sets the OpenGL texture number (GL_TEXTURE0 - GL_TEXTURE[MaxTextureUnit-1]).
160  * \attention For internal usage inside OpenGLFramework Base library only.
161  */
162  void SetTextureNr(GLint textureNr);
163 
164  /**
165  * Sets the OpenGL texture number (0 - MaxTextureUnit-1).
166  */
167  void SetTextureNrInUniformFormat(int textureNr);
168 
169  /**
170  * Returns the OpenGL texture number in uniform format or -1 for error.
171  * \attention For internal usage inside OpenGLFramework Base library only.
172  *
173  * Intended usage:
174  *
175  * renderedTexture.SetTextureNr(GL_TEXTURE5);
176  *
177  * renderedTexture2.SetTextureNr(GL_TEXTURE6);
178  *
179  * shaderProg.SetUniform("renderedTexture", renderedTexture.GetTextureNrInUniformFormat());
180  *
181  * shaderProg.SetUniform("renderedTexture2", renderedTexture2.GetTextureNrInUniformFormat());
182  */
183  inline int GetTextureNrInUniformFormat() { return textureNr_ - GL_TEXTURE0;}
184 
185  /** Swap the GL object IDs.
186  * Will throw an exception if one texture has id 0 or if textures have different targets.
187  * \author bartczak 03/2009
188  **/
189  friend BIASOpenGLFramework_EXPORT void SwapGLObjects(glfTexture& A, glfTexture& B);
190 
191  protected:
192  glfTexture(GLenum target);
193 
194  /**
195  * \name Utility functions used by derived classes.
196  */
197  //@{
198 
199  /**
200  * Returns the smallest power of two that is greater than <code>i</code>.
201  */
202  static int NextPowerOfTwo(int i);
203 
204  /**
205  * Computes a pixel format that can be used with <code>glGetTexImage</code>
206  * for an internal format of a texture. Also returns the number of channels.
207  * For example: If internalFormat = GL_R3_G3_B2, then format = GL_RGB
208  * and numChannels = 3.
209  * \deprecated use glfFormatDB
210  */
211 // static void GetFormatForInternalFormat(GLenum internalFormat,
212 // GLenum& format,
213 // int& numChannels);
214 
215  /**
216  * Returns the OpenGL pixel type (GL_UNSIGNED_BYTE etc.) for a BIAS storage type.
217  * \deprecated use glfFormatDB
218  */
219 // static GLenum GetPixelTypeForStorageType(ImageBase::EStorageType storageType);
220 
221 
222 
223  /** Returns the OpenGL pixel format (GL_RGB etc.) for a BIAS color model.
224  * \deprecated use glfFormatDB
225  */
226 // static GLenum GetFormatForColorModel(ImageBase::EColorModel colorModel);
227 
228 
229  /* Returns an OpenGL internal pixel format for a BIAS storage type and color model.
230  * \deprecated use glfFormatDB
231  */
232 // static GLenum ProposeInternalFormat(ImageBase::EStorageType storageType,
233 // ImageBase::EColorModel colorModel);
234 
235  /**
236  * cp from glfBatch
237  */
238  static int GetMaxSupportedTextureUnits();
239 
240  //@}
241 
242  GLuint id_;
243  GLenum target_;
244  GLint textureNr_;
247 
248  private:
249  /** An intuitive assignment operator should generate a copy, as long as nobody is willing to implement
250  * the generation of a copy in gl controlled memory this operator cannot be used.
251  */
252  glfTexture& operator=(const glfTexture& b);
253 
254  /** An intuitive copy constructor should generate a copy, as long as nobody is willing to implement
255  * the generation of a copy in gl controlled memory this constructor cannot be used.
256  */
257  glfTexture(const glfTexture& t);
258  };
259 
260  BIASOpenGLFramework_EXPORT void SwapGLObjects(glfTexture& A, glfTexture& B);
261 
262 } // namespace BIAS
263 
264 #endif // __glfTexture_hh__
BIASOpenGLFramework_EXPORT void SwapGLObjects(glfTexture &A, glfTexture &B)
Definition: glfTexture.cpp:31
int GetTextureNrInUniformFormat()
Returns the OpenGL texture number in uniform format or -1 for error.
Definition: glfTexture.hh:183
GLuint GetTextureID() const
Returns the OpenGL texture id.
Definition: glfTexture.hh:144
Base class for textures.
Definition: glfTexture.hh:42
GLint GetTextureNr()
Returns the OpenGL texture number.
Definition: glfTexture.hh:156
GLenum GetTextureTarget() const
Returns the OpenGL texture target.
Definition: glfTexture.hh:150