1 //------------------------------------------------------------------------------
2 // File : RenderTexture.h
3 //------------------------------------------------------------------------------
4 // Copyright (c) 2002-2004 Mark J. Harris
5 //---------------------------------------------------------------------------
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any
8 // damages arising from the use of this software.
10 // Permission is granted to anyone to use this software for any
11 // purpose, including commercial applications, and to alter it and
12 // redistribute it freely, subject to the following restrictions:
14 // 1. The origin of this software must not be misrepresented; you
15 // must not claim that you wrote the original software. If you use
16 // this software in a product, an acknowledgment in the product
17 // documentation would be appreciated but is not required.
19 // 2. Altered source versions must be plainly marked as such, and
20 // must not be misrepresented as being the original software.
22 // 3. This notice may not be removed or altered from any source
25 // -----------------------------------------------------------------------------
27 // Original RenderTexture code: Mark J. Harris
28 // Original Render-to-depth-texture support: Thorsten Scheuermann
29 // Linux Copy-to-texture: Eric Werness
30 // Various Bug Fixes: Daniel (Redge) Sperl
33 // -----------------------------------------------------------------------------
35 * @file RenderTexture.h
37 * Interface definition for class RenderTexture. A multi-format render to
40 #ifndef __RENDERTEXTURE2_HPP__
41 #define __RENDERTEXTURE2_HPP__
47 * Jan. 2005, Removed GLEW dependencies, Erik Hofman
49 #include <simgear/compiler.h>
51 #if !defined( _WIN32 ) && !defined( __APPLE__ )
52 # include <X11/Xlib.h>
62 /* The pixel format for the pbuffer is controlled by the mode string passed
63 * into the PBuffer constructor. This string can have the following attributes:
65 * To specify the pixel format, use the following syntax.
67 * <channels> must match one of the following.
69 * r - r pixel format (for float buffer).
70 * rg - rg pixel format (for float buffer).
71 * rgb - rgb pixel format. 8 bit or 16/32 bit in float buffer mode
72 * rgba - same as "rgb alpha" string
74 * <bits> can either be a scalar--meaning the same bit depth for each
75 * channel-- or a 2-, 3-, 4-component vector matching the specified number of
76 * channels. Vector components should be comma separated. An optional 'f'
77 * suffix on the bit depth specifies float components. In this case <bits>
78 * must be either "32f" or "16f". If <bits> is empty, the default 8 bits per
79 * channel will be used.
85 * The following other attributes are supported.
87 * depth=n - must have n-bit depth buffer, omit n for default (24 bits)
88 * stencil=n - must have n-bit stencil buffer, omit n for default (8 bits)
89 * samples=n - must support n-sample antialiasing (n can be 2 or 4)
90 * aux=n - must have n AUX buffers
91 * doublebuffer - must support double buffered rendering
95 * texCUBE - must support binding pbuffer as texture to specified target
96 * - binding the depth buffer is also supported by specifying
100 * - Both depth and color texture binding, may be specified, but
101 * the targets must match!
102 * For example: "tex2D depthTex2D" or "texRECT depthTexRECT"
105 * ctt - These mutually exclusive options specify the update method used
106 * for render textures that support texture binding. "rtt"
107 * indicates that render to texture will be used to update the
108 * texture. "ctt" indicates that copy to texture will be used
109 * (i.e. glCopyTexSubImage2D()). "rtt" is the default if neither is
110 * specified, and one of the "tex*" options above is.
113 *---------------------------------------------------------------------------
117 * * Texture Parameters:
118 * The default texture wrap mode is GL_CLAMP_TO_EDGE for all textures, and
119 * the default texture filtering modes (min and mag) are GL_NEAREST.
120 * To change these parameters, simply bind the RenderTexture (using the
121 * Bind() method), and set them the way you would for any GL texture object.
122 * The same goes for depth textures.
124 * * Enabling Mipmapping:
125 * This is similar to the texture parameters above. When "rtt" is specified
126 * in the mode string, "mipmap" must also be specified in order to enable
127 * a mipmapped pbuffer. Then, the mipmaps must be created by enabling the
128 * GL_SGIS_GENERATE_MIPMAP texture parameter in the same way as above, and
129 * the min filter mode must be set to a mipmap filter mode, as with any
130 * mipmapped texture object.
132 * * Enabling Anisotropic Filtering
133 * As with the texture parameters above, except as in the following code:
134 * glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, max);
135 * glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, <value < max>);
142 RT_RENDER_TO_TEXTURE,
148 RenderTexture(const char *strMode="rgb tex2D");
151 //! Call this once before use. Set bShare to true to share lists, textures,
152 //! and program objects between the render texture context and the
153 //! current active GL context.
154 bool Initialize(int width, int height,
155 bool shareObjects=true,
156 bool copyContext=false);
158 // !Change the render texture format.
159 bool Reset(const char* strMode,...);
160 // !Change the size of the render texture.
161 bool Resize(int width, int height);
163 // !Begin drawing to the texture. (i.e. use as "output" texture)
165 // !Ends drawing to 'current', begins drawing to this RenderTexture
166 bool BeginCapture(RenderTexture* current);
167 // !End drawing to the texture.
170 // !Bind the texture to the active texture unit for use as an "input" texture
173 // !Bind the depth texture to the active texture unit for use as an "input" texture
174 void BindDepth() const;
176 // !Associate the RTT texture with 'iBuffer' (default is WGL_FRONT_LEFT_ARB)
177 bool BindBuffer( int iBuffer );
179 //! Enables the texture target appropriate for this render texture.
180 void EnableTextureTarget() const
181 { if (_bInitialized) glEnable(_iTextureTarget); }
182 //! Disables the texture target appropriate for this render texture.
183 void DisableTextureTarget() const
184 { if (_bInitialized) glDisable(_iTextureTarget); }
186 //! Returns the texture ID. Useful in Cg applications.
187 unsigned int GetTextureID() const { return _iTextureID; }
188 //! Returns the depth texture ID. Useful in Cg applications.
189 unsigned int GetDepthTextureID() const { return _iDepthTextureID; }
190 //! Returns the texture target this texture is bound to.
191 unsigned int GetTextureTarget() const { return _iTextureTarget; }
192 //! Conversion operator allows RenderTexture to be passed to GL calls
193 operator unsigned int()const{return _iTextureID;}
195 //! Returns the width of the offscreen buffer.
196 int GetWidth() const { return _iWidth; }
197 //! Returns the width of the offscreen buffer.
198 int GetHeight() const { return _iHeight; }
199 //! Returns the maximum S texture coordinate.
200 int GetMaxS() const { return IsRectangleTexture() ? _iWidth : 1; }
201 //! Returns the maximum T texture coordinate.
202 int GetMaxT() const { return IsRectangleTexture() ? _iHeight : 1; }
204 //! Returns the number of red bits allocated.
205 int GetRedBits() const { return _iNumColorBits[0]; }
206 //! Returns the number of green bits allocated.
207 int GetGreenBits() const { return _iNumColorBits[1]; }
208 //! Returns the number of blue bits allocated.
209 int GetBlueBits() const { return _iNumColorBits[2]; }
210 //! Returns the number of alpha bits allocated.
211 int GetAlphaBits() const { return _iNumColorBits[3]; }
213 //! Returns the number of depth bits allocated.
214 int GetDepthBits() const { return _iNumDepthBits; }
215 //! Returns the number of stencil bits allocated.
216 int GetStencilBits() const { return _iNumStencilBits; }
218 //! True if this RenderTexture has been properly initialized.
219 bool IsInitialized() const { return _bInitialized; }
220 //! True if this is a texture and not just an offscreen buffer.
221 bool IsTexture() const { return _bIsTexture; }
222 //! True if this is a depth texture and not just an offscreen buffer.
223 bool IsDepthTexture() const { return _bIsDepthTexture; }
224 //! True if this is a floating point buffer / texture.
225 bool IsFloatTexture() const { return _bFloat; }
226 //! True if this is a double-buffered pbuffer
227 bool IsDoubleBuffered() const { return _bDoubleBuffered; }
228 //! True if this texture has non-power-of-two dimensions.
229 bool IsRectangleTexture() const { return _bRectangle; }
230 //! True if this texture has non-power-of-two dimensions.
231 //! True if this pbuffer has a depth buffer.
232 bool HasDepth() const { return (_iNumDepthBits > 0); }
233 //! True if this pbuffer has a stencil buffer.
234 bool HasStencil() const { return (_iNumStencilBits > 0); }
235 //! True if this texture has mipmaps.
236 bool IsMipmapped() const { return _bMipmap; }
239 * @fn IsPowerOfTwo(int n)
240 * @brief Returns true if /param n is an integer power of 2.
242 * Taken from Steve Baker's Cute Code Collection.
243 * http://www.sjbaker.org/steve/software/cute_code.html
245 static bool IsPowerOfTwo(int n) { return ((n&(n-1))==0); }
248 /////////////////////////////////////////////////////////////////////////
249 // This is the deprecated (old) interface. It will likely be removed
250 // in a future version, so it is recommended that you transition to the
251 // new mode-string-based interface.
252 RenderTexture(int width, int height,
253 bool bIsTexture = true,
254 bool bIsDepthTexture = false);
256 // Call this once before use. Set bShare to true to share lists,
257 // textures, and program objects between the render texture context
258 // and the current active GL context. [deprecated]
259 bool Initialize(bool bShare = true,
261 bool bStencil = false,
262 bool bMipmap = false,
263 bool bAnisoFilter = false,
264 unsigned int iRBits = 8,
265 unsigned int iGBits = 8,
266 unsigned int iBBits = 8,
267 unsigned int iABits = 8,
268 // Only Win32 has RT now, so only make it default there
270 UpdateMode updateMode = RT_RENDER_TO_TEXTURE
272 UpdateMode updateMode = RT_COPY_TO_TEXTURE
275 // !Change the render texture resolution. [deprecated]
276 bool Reset(int iWidth, int iHeight);
278 /////////////////////////////////////////////////////////////////////////
281 protected: // methods
284 typedef std::pair<std::string, std::string> KeyVal;
286 void _ParseModeString(const char *modeString,
287 std::vector<int> &pixelFormatAttribs,
288 std::vector<int> &pbufferAttribs);
290 std::vector<int> _ParseBitVector(std::string bitVector);
291 KeyVal _GetKeyValuePair(std::string token);
294 bool _VerifyExtensions();
295 bool _InitializeTextures();
297 void _MaybeCopyBuffer();
298 bool _ReleaseBoundBuffers();
300 bool _BindDepthBuffer( ) const;
303 int _iWidth; // width of the pbuffer
304 int _iHeight; // height of the pbuffer
307 bool _bIsDepthTexture;
308 bool _bHasARBDepthTexture; // [Redge]
310 UpdateMode _eUpdateMode;
314 unsigned int _iNumAuxBuffers;
315 bool _bIsBufferBound;
316 int _iCurrentBoundBuffer;
318 unsigned int _iNumComponents;
319 unsigned int _iNumColorBits[4];
320 unsigned int _iNumDepthBits;
321 unsigned int _iNumStencilBits;
325 bool _bDoubleBuffered;
334 HDC _hDC; // Handle to a device context.
335 HGLRC _hGLContext; // Handle to a GL context.
336 HPBUFFERARB _hPBuffer; // Handle to a pbuffer.
339 HGLRC _hPreviousContext;
340 #elif defined( __APPLE__ )
344 GLXContext _hGLContext;
345 GLXPbuffer _hPBuffer;
347 GLXDrawable _hPreviousDrawable;
348 GLXContext _hPreviousContext;
352 GLenum _iTextureTarget;
353 unsigned int _iTextureID;
354 unsigned int _iDepthTextureID;
356 unsigned short* _pPoorDepthTexture; // [Redge]
358 std::vector<int> _pixelFormatAttribs;
359 std::vector<int> _pbufferAttribs;
362 // Using these could lead to some odd behavior
363 RenderTexture(const RenderTexture&);
364 RenderTexture& operator=(const RenderTexture&);
367 #endif //__RENDERTEXTURE2_HPP__