| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSG_TEXTURE |
|---|
| 15 | #define OSG_TEXTURE 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/GL> |
|---|
| 18 | #include <osg/Image> |
|---|
| 19 | #include <osg/StateAttribute> |
|---|
| 20 | #include <osg/GraphicsContext> |
|---|
| 21 | #include <osg/ref_ptr> |
|---|
| 22 | #include <osg/Vec4> |
|---|
| 23 | #include <osg/Vec4d> |
|---|
| 24 | #include <osg/buffered_value> |
|---|
| 25 | |
|---|
| 26 | #include <list> |
|---|
| 27 | #include <map> |
|---|
| 28 | |
|---|
| 29 | // If not defined by gl.h use the definition found in: |
|---|
| 30 | // http://oss.sgi.com/projects/ogl-sample/registry/EXT/texture_filter_anisotropic.txt |
|---|
| 31 | #ifndef GL_TEXTURE_MAX_ANISOTROPY_EXT |
|---|
| 32 | #define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE |
|---|
| 33 | #endif |
|---|
| 34 | |
|---|
| 35 | #ifndef GL_ARB_texture_compression |
|---|
| 36 | #define GL_COMPRESSED_ALPHA_ARB 0x84E9 |
|---|
| 37 | #define GL_COMPRESSED_LUMINANCE_ARB 0x84EA |
|---|
| 38 | #define GL_COMPRESSED_LUMINANCE_ALPHA_ARB 0x84EB |
|---|
| 39 | #define GL_COMPRESSED_INTENSITY_ARB 0x84EC |
|---|
| 40 | #define GL_COMPRESSED_RGB_ARB 0x84ED |
|---|
| 41 | #define GL_COMPRESSED_RGBA_ARB 0x84EE |
|---|
| 42 | #define GL_TEXTURE_COMPRESSION_HINT_ARB 0x84EF |
|---|
| 43 | #define GL_TEXTURE_COMPRESSED_ARB 0x86A1 |
|---|
| 44 | #define GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB 0x86A2 |
|---|
| 45 | #define GL_COMPRESSED_TEXTURE_FORMATS_ARB 0x86A3 |
|---|
| 46 | #endif |
|---|
| 47 | |
|---|
| 48 | #ifndef GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB |
|---|
| 49 | #define GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB 0x86A0 |
|---|
| 50 | #endif |
|---|
| 51 | |
|---|
| 52 | #ifndef GL_EXT_texture_compression_s3tc |
|---|
| 53 | #define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0 |
|---|
| 54 | #define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1 |
|---|
| 55 | #define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2 |
|---|
| 56 | #define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3 |
|---|
| 57 | #endif |
|---|
| 58 | |
|---|
| 59 | #ifndef GL_ARB_INTERNAL_TEXTURE_FORMAT |
|---|
| 60 | #define GL_RGBA32F_ARB 0x8814 |
|---|
| 61 | #define GL_RGB32F_ARB 0x8815 |
|---|
| 62 | #define GL_ALPHA32F_ARB 0x8816 |
|---|
| 63 | #define GL_INTENSITY32F_ARB 0x8817 |
|---|
| 64 | #define GL_LUMINANCE32F_ARB 0x8818 |
|---|
| 65 | #define GL_LUMINANCE_ALPHA32F_ARB 0x8819 |
|---|
| 66 | #define GL_RGBA16F_ARB 0x881A |
|---|
| 67 | #define GL_RGB16F_ARB 0x881B |
|---|
| 68 | #define GL_ALPHA16F_ARB 0x881C |
|---|
| 69 | #define GL_INTENSITY16F_ARB 0x881D |
|---|
| 70 | #define GL_LUMINANCE16F_ARB 0x881E |
|---|
| 71 | #define GL_LUMINANCE_ALPHA16F_ARB 0x881F |
|---|
| 72 | #endif |
|---|
| 73 | |
|---|
| 74 | #ifndef GL_ARB_PIXEL_DATA |
|---|
| 75 | #define GL_HALF_FLOAT_ARB 0x140B |
|---|
| 76 | #endif |
|---|
| 77 | |
|---|
| 78 | #ifndef GL_NV_texture_shader |
|---|
| 79 | #define GL_HILO_NV 0x86F4 |
|---|
| 80 | #define GL_DSDT_NV 0x86F5 |
|---|
| 81 | #define GL_DSDT_MAG_NV 0x86F6 |
|---|
| 82 | #define GL_DSDT_MAG_VIB_NV 0x86F7 |
|---|
| 83 | #define GL_HILO16_NV 0x86F8 |
|---|
| 84 | #define GL_SIGNED_HILO_NV 0x86F9 |
|---|
| 85 | #define GL_SIGNED_HILO16_NV 0x86FA |
|---|
| 86 | #define GL_SIGNED_RGBA_NV 0x86FB |
|---|
| 87 | #define GL_SIGNED_RGBA8_NV 0x86FC |
|---|
| 88 | #define GL_SIGNED_RGB_NV 0x86FE |
|---|
| 89 | #define GL_SIGNED_RGB8_NV 0x86FF |
|---|
| 90 | #define GL_SIGNED_LUMINANCE_NV 0x8701 |
|---|
| 91 | #define GL_SIGNED_LUMINANCE8_NV 0x8702 |
|---|
| 92 | #define GL_SIGNED_LUMINANCE_ALPHA_NV 0x8703 |
|---|
| 93 | #define GL_SIGNED_LUMINANCE8_ALPHA8_NV 0x8704 |
|---|
| 94 | #define GL_SIGNED_ALPHA_NV 0x8705 |
|---|
| 95 | #define GL_SIGNED_ALPHA8_NV 0x8706 |
|---|
| 96 | #define GL_SIGNED_INTENSITY_NV 0x8707 |
|---|
| 97 | #define GL_SIGNED_INTENSITY8_NV 0x8708 |
|---|
| 98 | #define GL_DSDT8_NV 0x8709 |
|---|
| 99 | #define GL_DSDT8_MAG8_NV 0x870A |
|---|
| 100 | #define GL_DSDT8_MAG8_INTENSITY8_NV 0x870B |
|---|
| 101 | #define GL_SIGNED_RGB_UNSIGNED_ALPHA_NV 0x870C |
|---|
| 102 | #define GL_SIGNED_RGB8_UNSIGNED_ALPHA8_NV 0x870D |
|---|
| 103 | #endif |
|---|
| 104 | |
|---|
| 105 | #ifndef GL_NV_float_buffer |
|---|
| 106 | #define GL_FLOAT_R_NV 0x8880 |
|---|
| 107 | #define GL_FLOAT_RG_NV 0x8881 |
|---|
| 108 | #define GL_FLOAT_RGB_NV 0x8882 |
|---|
| 109 | #define GL_FLOAT_RGBA_NV 0x8883 |
|---|
| 110 | #define GL_FLOAT_R16_NV 0x8884 |
|---|
| 111 | #define GL_FLOAT_R32_NV 0x8885 |
|---|
| 112 | #define GL_FLOAT_RG16_NV 0x8886 |
|---|
| 113 | #define GL_FLOAT_RG32_NV 0x8887 |
|---|
| 114 | #define GL_FLOAT_RGB16_NV 0x8888 |
|---|
| 115 | #define GL_FLOAT_RGB32_NV 0x8889 |
|---|
| 116 | #define GL_FLOAT_RGBA16_NV 0x888A |
|---|
| 117 | #define GL_FLOAT_RGBA32_NV 0x888B |
|---|
| 118 | #endif |
|---|
| 119 | |
|---|
| 120 | #ifndef GL_NV_half_float |
|---|
| 121 | #define GL_HALF_FLOAT_NV 0x140B |
|---|
| 122 | #endif |
|---|
| 123 | |
|---|
| 124 | #ifndef GL_ATI_texture_float |
|---|
| 125 | #define GL_RGBA_FLOAT32_ATI 0x8814 |
|---|
| 126 | #define GL_RGB_FLOAT32_ATI 0x8815 |
|---|
| 127 | #define GL_ALPHA_FLOAT32_ATI 0x8816 |
|---|
| 128 | #define GL_INTENSITY_FLOAT32_ATI 0x8817 |
|---|
| 129 | #define GL_LUMINANCE_FLOAT32_ATI 0x8818 |
|---|
| 130 | #define GL_LUMINANCE_ALPHA_FLOAT32_ATI 0x8819 |
|---|
| 131 | #define GL_RGBA_FLOAT16_ATI 0x881A |
|---|
| 132 | #define GL_RGB_FLOAT16_ATI 0x881B |
|---|
| 133 | #define GL_ALPHA_FLOAT16_ATI 0x881C |
|---|
| 134 | #define GL_INTENSITY_FLOAT16_ATI 0x881D |
|---|
| 135 | #define GL_LUMINANCE_FLOAT16_ATI 0x881E |
|---|
| 136 | #define GL_LUMINANCE_ALPHA_FLOAT16_ATI 0x881F |
|---|
| 137 | #endif |
|---|
| 138 | |
|---|
| 139 | #ifndef GL_MIRRORED_REPEAT_IBM |
|---|
| 140 | #define GL_MIRRORED_REPEAT_IBM 0x8370 |
|---|
| 141 | #endif |
|---|
| 142 | |
|---|
| 143 | #ifndef GL_CLAMP_TO_EDGE |
|---|
| 144 | #define GL_CLAMP_TO_EDGE 0x812F |
|---|
| 145 | #endif |
|---|
| 146 | |
|---|
| 147 | #ifndef GL_CLAMP_TO_BORDER_ARB |
|---|
| 148 | #define GL_CLAMP_TO_BORDER_ARB 0x812D |
|---|
| 149 | #endif |
|---|
| 150 | |
|---|
| 151 | #ifndef GL_GENERATE_MIPMAP_SGIS |
|---|
| 152 | #define GL_GENERATE_MIPMAP_SGIS 0x8191 |
|---|
| 153 | #define GL_GENERATE_MIPMAP_HINT_SGIS 0x8192 |
|---|
| 154 | #endif |
|---|
| 155 | |
|---|
| 156 | #ifndef GL_TEXTURE_3D |
|---|
| 157 | #define GL_TEXTURE_3D 0x806F |
|---|
| 158 | #endif |
|---|
| 159 | |
|---|
| 160 | #ifndef GL_TEXTURE_2D_ARRAY_EXT |
|---|
| 161 | #define GL_TEXTURE_2D_ARRAY_EXT 0x8C1A |
|---|
| 162 | #define GL_TEXTURE_2D_ARRAY_EXT 0x8C1A |
|---|
| 163 | #define GL_PROXY_TEXTURE_2D_ARRAY_EXT 0x8C1B |
|---|
| 164 | #define GL_TEXTURE_BINDING_2D_ARRAY_EXT 0x8C1D |
|---|
| 165 | #define GL_MAX_ARRAY_TEXTURE_LAYERS_EXT 0x88FF |
|---|
| 166 | #define GL_COMPARE_REF_DEPTH_TO_TEXTURE_EXT 0x884E |
|---|
| 167 | #define GL_SAMPLER_2D_ARRAY_EXT 0x8DC1 |
|---|
| 168 | #define GL_SAMPLER_2D_ARRAY_SHADOW_EXT 0x8DC4 |
|---|
| 169 | #define GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER_EXT 0x8CD4 |
|---|
| 170 | #endif |
|---|
| 171 | |
|---|
| 172 | #ifndef GL_TEXTURE_BINDING_3D |
|---|
| 173 | #define GL_TEXTURE_BINDING_3D 0x806A |
|---|
| 174 | #endif |
|---|
| 175 | |
|---|
| 176 | #ifndef GL_DEPTH_TEXTURE_MODE_ARB |
|---|
| 177 | #define GL_DEPTH_TEXTURE_MODE_ARB 0x884B |
|---|
| 178 | #endif |
|---|
| 179 | |
|---|
| 180 | #ifndef GL_TEXTURE_COMPARE_MODE_ARB |
|---|
| 181 | #define GL_TEXTURE_COMPARE_MODE_ARB 0x884C |
|---|
| 182 | #endif |
|---|
| 183 | #ifndef GL_TEXTURE_COMPARE_FUNC_ARB |
|---|
| 184 | #define GL_TEXTURE_COMPARE_FUNC_ARB 0x884D |
|---|
| 185 | #endif |
|---|
| 186 | #ifndef GL_COMPARE_R_TO_TEXTURE_ARB |
|---|
| 187 | #define GL_COMPARE_R_TO_TEXTURE_ARB 0x884E |
|---|
| 188 | #endif |
|---|
| 189 | |
|---|
| 190 | #ifndef TEXTURE_COMPARE_FAIL_VALUE_ARB |
|---|
| 191 | #define TEXTURE_COMPARE_FAIL_VALUE_ARB 0x80BF |
|---|
| 192 | #endif |
|---|
| 193 | |
|---|
| 194 | #if !defined( GL_MAX_TEXTURE_UNITS ) |
|---|
| 195 | #define GL_MAX_TEXTURE_UNITS 0x84E2 |
|---|
| 196 | #endif |
|---|
| 197 | |
|---|
| 198 | #ifndef GL_TEXTURE_DEPTH |
|---|
| 199 | #define GL_TEXTURE_DEPTH 0x8071 |
|---|
| 200 | #endif |
|---|
| 201 | |
|---|
| 202 | // Integer teture extension as in http://www.opengl.org/registry/specs/EXT/texture_integer.txt |
|---|
| 203 | #ifndef GL_EXT_texture_integer |
|---|
| 204 | #define GL_RGBA32UI_EXT 0x8D70 |
|---|
| 205 | #define GL_RGB32UI_EXT 0x8D71 |
|---|
| 206 | #define GL_ALPHA32UI_EXT 0x8D72 |
|---|
| 207 | #define GL_INTENSITY32UI_EXT 0x8D73 |
|---|
| 208 | #define GL_LUMINANCE32UI_EXT 0x8D74 |
|---|
| 209 | #define GL_LUMINANCE_ALPHA32UI_EXT 0x8D75 |
|---|
| 210 | |
|---|
| 211 | #define GL_RGBA16UI_EXT 0x8D76 |
|---|
| 212 | #define GL_RGB16UI_EXT 0x8D77 |
|---|
| 213 | #define GL_ALPHA16UI_EXT 0x8D78 |
|---|
| 214 | #define GL_INTENSITY16UI_EXT 0x8D79 |
|---|
| 215 | #define GL_LUMINANCE16UI_EXT 0x8D7A |
|---|
| 216 | #define GL_LUMINANCE_ALPHA16UI_EXT 0x8D7B |
|---|
| 217 | |
|---|
| 218 | #define GL_RGBA8UI_EXT 0x8D7C |
|---|
| 219 | #define GL_RGB8UI_EXT 0x8D7D |
|---|
| 220 | #define GL_ALPHA8UI_EXT 0x8D7E |
|---|
| 221 | #define GL_INTENSITY8UI_EXT 0x8D7F |
|---|
| 222 | #define GL_LUMINANCE8UI_EXT 0x8D80 |
|---|
| 223 | #define GL_LUMINANCE_ALPHA8UI_EXT 0x8D81 |
|---|
| 224 | |
|---|
| 225 | #define GL_RGBA32I_EXT 0x8D82 |
|---|
| 226 | #define GL_RGB32I_EXT 0x8D83 |
|---|
| 227 | #define GL_ALPHA32I_EXT 0x8D84 |
|---|
| 228 | #define GL_INTENSITY32I_EXT 0x8D85 |
|---|
| 229 | #define GL_LUMINANCE32I_EXT 0x8D86 |
|---|
| 230 | #define GL_LUMINANCE_ALPHA32I_EXT 0x8D87 |
|---|
| 231 | |
|---|
| 232 | #define GL_RGBA16I_EXT 0x8D88 |
|---|
| 233 | #define GL_RGB16I_EXT 0x8D89 |
|---|
| 234 | #define GL_ALPHA16I_EXT 0x8D8A |
|---|
| 235 | #define GL_INTENSITY16I_EXT 0x8D8B |
|---|
| 236 | #define GL_LUMINANCE16I_EXT 0x8D8C |
|---|
| 237 | #define GL_LUMINANCE_ALPHA16I_EXT 0x8D8D |
|---|
| 238 | |
|---|
| 239 | #define GL_RGBA8I_EXT 0x8D8E |
|---|
| 240 | #define GL_RGB8I_EXT 0x8D8F |
|---|
| 241 | #define GL_ALPHA8I_EXT 0x8D90 |
|---|
| 242 | #define GL_INTENSITY8I_EXT 0x8D91 |
|---|
| 243 | #define GL_LUMINANCE8I_EXT 0x8D92 |
|---|
| 244 | #define GL_LUMINANCE_ALPHA8I_EXT 0x8D93 |
|---|
| 245 | |
|---|
| 246 | #define GL_RED_INTEGER_EXT 0x8D94 |
|---|
| 247 | #define GL_GREEN_INTEGER_EXT 0x8D95 |
|---|
| 248 | #define GL_BLUE_INTEGER_EXT 0x8D96 |
|---|
| 249 | #define GL_ALPHA_INTEGER_EXT 0x8D97 |
|---|
| 250 | #define GL_RGB_INTEGER_EXT 0x8D98 |
|---|
| 251 | #define GL_RGBA_INTEGER_EXT 0x8D99 |
|---|
| 252 | #define GL_BGR_INTEGER_EXT 0x8D9A |
|---|
| 253 | #define GL_BGRA_INTEGER_EXT 0x8D9B |
|---|
| 254 | #define GL_LUMINANCE_INTEGER_EXT 0x8D9C |
|---|
| 255 | #define GL_LUMINANCE_ALPHA_INTEGER_EXT 0x8D9D |
|---|
| 256 | |
|---|
| 257 | #define GL_RGBA_INTEGER_MODE_EXT 0x8D9E |
|---|
| 258 | #endif |
|---|
| 259 | |
|---|
| 260 | namespace osg { |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | /** Texture pure virtual base class that encapsulates OpenGl texture |
|---|
| 264 | * functionality common to the various types of OSG textures. |
|---|
| 265 | */ |
|---|
| 266 | class OSG_EXPORT Texture : public osg::StateAttribute |
|---|
| 267 | { |
|---|
| 268 | |
|---|
| 269 | public : |
|---|
| 270 | |
|---|
| 271 | static unsigned int s_numberTextureReusedLastInLastFrame; |
|---|
| 272 | static unsigned int s_numberNewTextureInLastFrame; |
|---|
| 273 | static unsigned int s_numberDeletedTextureInLastFrame; |
|---|
| 274 | |
|---|
| 275 | Texture(); |
|---|
| 276 | |
|---|
| 277 | /** Copy constructor using CopyOp to manage deep vs shallow copy. */ |
|---|
| 278 | Texture(const Texture& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY); |
|---|
| 279 | |
|---|
| 280 | virtual osg::Object* cloneType() const = 0; |
|---|
| 281 | virtual osg::Object* clone(const CopyOp& copyop) const = 0; |
|---|
| 282 | virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Texture *>(obj)!=NULL; } |
|---|
| 283 | virtual const char* libraryName() const { return "osg"; } |
|---|
| 284 | virtual const char* className() const { return "Texture"; } |
|---|
| 285 | |
|---|
| 286 | /** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/ |
|---|
| 287 | virtual Texture* asTexture() { return this; } |
|---|
| 288 | |
|---|
| 289 | /** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/ |
|---|
| 290 | virtual const Texture* asTexture() const { return this; } |
|---|
| 291 | |
|---|
| 292 | virtual Type getType() const { return TEXTURE; } |
|---|
| 293 | |
|---|
| 294 | virtual bool isTextureAttribute() const { return true; } |
|---|
| 295 | |
|---|
| 296 | virtual GLenum getTextureTarget() const = 0; |
|---|
| 297 | |
|---|
| 298 | virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const |
|---|
| 299 | { |
|---|
| 300 | usage.usesTextureMode(getTextureTarget()); |
|---|
| 301 | return true; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | virtual int getTextureWidth() const { return 0; } |
|---|
| 305 | virtual int getTextureHeight() const { return 0; } |
|---|
| 306 | virtual int getTextureDepth() const { return 0; } |
|---|
| 307 | |
|---|
| 308 | enum WrapParameter { |
|---|
| 309 | WRAP_S, |
|---|
| 310 | WRAP_T, |
|---|
| 311 | WRAP_R |
|---|
| 312 | }; |
|---|
| 313 | |
|---|
| 314 | enum WrapMode { |
|---|
| 315 | CLAMP = GL_CLAMP, |
|---|
| 316 | CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE, |
|---|
| 317 | CLAMP_TO_BORDER = GL_CLAMP_TO_BORDER_ARB, |
|---|
| 318 | REPEAT = GL_REPEAT, |
|---|
| 319 | MIRROR = GL_MIRRORED_REPEAT_IBM |
|---|
| 320 | }; |
|---|
| 321 | |
|---|
| 322 | /** Sets the texture wrap mode. */ |
|---|
| 323 | void setWrap(WrapParameter which, WrapMode wrap); |
|---|
| 324 | /** Gets the texture wrap mode. */ |
|---|
| 325 | WrapMode getWrap(WrapParameter which) const; |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | /** Sets the border color. Only used when wrap mode is CLAMP_TO_BORDER. |
|---|
| 329 | * The border color will be casted to the appropriate type to match the |
|---|
| 330 | * internal pixel format of the texture. */ |
|---|
| 331 | void setBorderColor(const Vec4d& color) { _borderColor = color; dirtyTextureParameters(); } |
|---|
| 332 | |
|---|
| 333 | /** Gets the border color. */ |
|---|
| 334 | const Vec4d& getBorderColor() const { return _borderColor; } |
|---|
| 335 | |
|---|
| 336 | /** Sets the border width. */ |
|---|
| 337 | void setBorderWidth(GLint width) { _borderWidth = width; dirtyTextureParameters(); } |
|---|
| 338 | |
|---|
| 339 | GLint getBorderWidth() const { return _borderWidth; } |
|---|
| 340 | |
|---|
| 341 | enum FilterParameter { |
|---|
| 342 | MIN_FILTER, |
|---|
| 343 | MAG_FILTER |
|---|
| 344 | }; |
|---|
| 345 | |
|---|
| 346 | enum FilterMode { |
|---|
| 347 | LINEAR = GL_LINEAR, |
|---|
| 348 | LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR, |
|---|
| 349 | LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST, |
|---|
| 350 | NEAREST = GL_NEAREST, |
|---|
| 351 | NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR, |
|---|
| 352 | NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST |
|---|
| 353 | }; |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | /** Sets the texture filter mode. */ |
|---|
| 357 | void setFilter(FilterParameter which, FilterMode filter); |
|---|
| 358 | |
|---|
| 359 | /** Gets the texture filter mode. */ |
|---|
| 360 | FilterMode getFilter(FilterParameter which) const; |
|---|
| 361 | |
|---|
| 362 | /** Sets the maximum anisotropy value, default value is 1.0 for no |
|---|
| 363 | * anisotropic filtering. If hardware does not support anisotropic |
|---|
| 364 | * filtering, use normal filtering (equivalent to a max anisotropy |
|---|
| 365 | * value of 1.0. Valid range is 1.0f upwards. The maximum value |
|---|
| 366 | * depends on the graphics system. */ |
|---|
| 367 | void setMaxAnisotropy(float anis); |
|---|
| 368 | |
|---|
| 369 | /** Gets the maximum anisotropy value. */ |
|---|
| 370 | inline float getMaxAnisotropy() const { return _maxAnisotropy; } |
|---|
| 371 | |
|---|
| 372 | /** Sets the hardware mipmap generation hint. If enabled, it will |
|---|
| 373 | * only be used if supported in the graphics system. */ |
|---|
| 374 | inline void setUseHardwareMipMapGeneration(bool useHardwareMipMapGeneration) { _useHardwareMipMapGeneration = useHardwareMipMapGeneration; } |
|---|
| 375 | |
|---|
| 376 | /** Gets the hardware mipmap generation hint. */ |
|---|
| 377 | inline bool getUseHardwareMipMapGeneration() const { return _useHardwareMipMapGeneration; } |
|---|
| 378 | |
|---|
| 379 | /** Sets whether or not the apply() function will unreference the image |
|---|
| 380 | * data. If enabled, and the image data is only referenced by this |
|---|
| 381 | * Texture, apply() will delete the image data. */ |
|---|
| 382 | inline void setUnRefImageDataAfterApply(bool flag) { _unrefImageDataAfterApply = flag; } |
|---|
| 383 | |
|---|
| 384 | /** Gets whether or not apply() unreferences image data. */ |
|---|
| 385 | inline bool getUnRefImageDataAfterApply() const { return _unrefImageDataAfterApply; } |
|---|
| 386 | |
|---|
| 387 | /** Sets whether to use client storage for the texture, if supported |
|---|
| 388 | * by the graphics system. Note: If enabled, and the graphics system |
|---|
| 389 | * supports it, the osg::Image(s) associated with this texture cannot |
|---|
| 390 | * be deleted, so the UnRefImageDataAfterApply flag would be ignored. */ |
|---|
| 391 | inline void setClientStorageHint(bool flag) { _clientStorageHint = flag; } |
|---|
| 392 | |
|---|
| 393 | /** Gets whether to use client storage for the texture. */ |
|---|
| 394 | inline bool getClientStorageHint() const { return _clientStorageHint; } |
|---|
| 395 | |
|---|
| 396 | /** Sets whether to force the texture to resize images that have dimensions |
|---|
| 397 | * that are not a power of two. If enabled, NPOT images will be resized, |
|---|
| 398 | * whether or not NPOT textures are supported by the hardware. If disabled, |
|---|
| 399 | * NPOT images will not be resized if supported by hardware. */ |
|---|
| 400 | inline void setResizeNonPowerOfTwoHint(bool flag) { _resizeNonPowerOfTwoHint = flag; } |
|---|
| 401 | |
|---|
| 402 | /** Gets whether texture will force non power to two images to be resized. */ |
|---|
| 403 | inline bool getResizeNonPowerOfTwoHint() const { return _resizeNonPowerOfTwoHint; } |
|---|
| 404 | |
|---|
| 405 | enum InternalFormatMode { |
|---|
| 406 | USE_IMAGE_DATA_FORMAT, |
|---|
| 407 | USE_USER_DEFINED_FORMAT, |
|---|
| 408 | USE_ARB_COMPRESSION, |
|---|
| 409 | USE_S3TC_DXT1_COMPRESSION, |
|---|
| 410 | USE_S3TC_DXT3_COMPRESSION, |
|---|
| 411 | USE_S3TC_DXT5_COMPRESSION |
|---|
| 412 | }; |
|---|
| 413 | |
|---|
| 414 | /** Sets the internal texture format mode. Note: If the texture format is |
|---|
| 415 | * USE_IMAGE_DATA_FORMAT, USE_ARB_COMPRESSION, or USE_S3TC_COMPRESSION, |
|---|
| 416 | * the internal format mode is set automatically and will overwrite the |
|---|
| 417 | * previous _internalFormat. */ |
|---|
| 418 | inline void setInternalFormatMode(InternalFormatMode mode) { _internalFormatMode = mode; } |
|---|
| 419 | |
|---|
| 420 | /** Gets the internal texture format mode. */ |
|---|
| 421 | inline InternalFormatMode getInternalFormatMode() const { return _internalFormatMode; } |
|---|
| 422 | |
|---|
| 423 | /** Sets the internal texture format. Implicitly sets the |
|---|
| 424 | * internalFormatMode to USE_USER_DEFINED_FORMAT. |
|---|
| 425 | * The corresponding internal format type will be computed. */ |
|---|
| 426 | inline void setInternalFormat(GLint internalFormat) |
|---|
| 427 | { |
|---|
| 428 | _internalFormatMode = USE_USER_DEFINED_FORMAT; |
|---|
| 429 | _internalFormat = internalFormat; |
|---|
| 430 | computeInternalFormatType(); |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | /** Gets the internal texture format. */ |
|---|
| 435 | inline GLint getInternalFormat() const { if (_internalFormat==0) computeInternalFormat(); return _internalFormat; } |
|---|
| 436 | |
|---|
| 437 | /** Return true if the internal format is one of the compressed formats.*/ |
|---|
| 438 | bool isCompressedInternalFormat() const; |
|---|
| 439 | |
|---|
| 440 | /** Sets the external source image format, used as a fallback when no osg::Image is attached to provide the source image format. */ |
|---|
| 441 | inline void setSourceFormat(GLenum sourceFormat) { _sourceFormat = sourceFormat; } |
|---|
| 442 | |
|---|
| 443 | /** Gets the external source image format. */ |
|---|
| 444 | inline GLenum getSourceFormat() const { return _sourceFormat; } |
|---|
| 445 | |
|---|
| 446 | /** Sets the external source data type, used as a fallback when no osg::Image is attached to provide the source image format.*/ |
|---|
| 447 | inline void setSourceType(GLenum sourceType) { _sourceType = sourceType; } |
|---|
| 448 | |
|---|
| 449 | /** Gets the external source data type.*/ |
|---|
| 450 | inline GLenum getSourceType() const { return _sourceType; } |
|---|
| 451 | |
|---|
| 452 | /** Texture type determined by the internal texture format */ |
|---|
| 453 | enum InternalFormatType{ |
|---|
| 454 | |
|---|
| 455 | //! default OpenGL format (clamped values to [0,1) or [0,255]) |
|---|
| 456 | NORMALIZED = 0x0, |
|---|
| 457 | |
|---|
| 458 | //! float values, Shader Model 3.0 (see ARB_texture_float) |
|---|
| 459 | FLOAT = 0x1, |
|---|
| 460 | |
|---|
| 461 | //! Signed integer values (see EXT_texture_integer) |
|---|
| 462 | SIGNED_INTEGER = 0x2, |
|---|
| 463 | |
|---|
| 464 | //! Unsigned integer value (see EXT_texture_integer) |
|---|
| 465 | UNSIGNED_INTEGER = 0x4 |
|---|
| 466 | }; |
|---|
| 467 | |
|---|
| 468 | /** Get the internal texture format type. */ |
|---|
| 469 | inline InternalFormatType getInternalFormatType() const { return _internalFormatType; } |
|---|
| 470 | |
|---|
| 471 | class TextureObject; |
|---|
| 472 | |
|---|
| 473 | /** Returns a pointer to the texture object for the current context. */ |
|---|
| 474 | inline TextureObject* getTextureObject(unsigned int contextID) const |
|---|
| 475 | { |
|---|
| 476 | return _textureObjectBuffer[contextID].get(); |
|---|
| 477 | } |
|---|
| 478 | |
|---|
| 479 | /** Forces a recompile on next apply() of associated OpenGL texture |
|---|
| 480 | * objects. */ |
|---|
| 481 | void dirtyTextureObject(); |
|---|
| 482 | |
|---|
| 483 | /** Returns true if the texture objects for all the required graphics |
|---|
| 484 | * contexts are loaded. */ |
|---|
| 485 | bool areAllTextureObjectsLoaded() const; |
|---|
| 486 | |
|---|
| 487 | |
|---|
| 488 | /** Gets the dirty flag for the current contextID. */ |
|---|
| 489 | inline unsigned int& getTextureParameterDirty(unsigned int contextID) const |
|---|
| 490 | { |
|---|
| 491 | return _texParametersDirtyList[contextID]; |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | /** Force a reset on next apply() of associated OpenGL texture |
|---|
| 496 | * parameters. */ |
|---|
| 497 | void dirtyTextureParameters(); |
|---|
| 498 | |
|---|
| 499 | /** Force a manual allocation of the mipmap levels on the next apply() call. |
|---|
| 500 | * User is responsible for filling the mipmap levels with valid data. |
|---|
| 501 | * The OpenGL's glGenerateMipmapEXT function is used to generate the mipmap levels. |
|---|
| 502 | * If glGenerateMipmapEXT is not supported or texture's internal format is not supported |
|---|
| 503 | * by the glGenerateMipmapEXT, then empty mipmap levels will |
|---|
| 504 | * be allocated manually. The mipmap levels are also allocated if a non-mipmapped |
|---|
| 505 | * min filter is used. */ |
|---|
| 506 | void allocateMipmapLevels(); |
|---|
| 507 | |
|---|
| 508 | |
|---|
| 509 | /** Sets GL_TEXTURE_COMPARE_MODE_ARB to GL_COMPARE_R_TO_TEXTURE_ARB |
|---|
| 510 | * See http://oss.sgi.com/projects/ogl-sample/registry/ARB/shadow.txt. */ |
|---|
| 511 | void setShadowComparison(bool flag) { _use_shadow_comparison = flag; } |
|---|
| 512 | |
|---|
| 513 | enum ShadowCompareFunc { |
|---|
| 514 | NEVER = GL_NEVER, |
|---|
| 515 | LESS = GL_LESS, |
|---|
| 516 | EQUAL = GL_EQUAL, |
|---|
| 517 | LEQUAL = GL_LEQUAL, |
|---|
| 518 | GREATER = GL_GREATER, |
|---|
| 519 | NOTEQUAL = GL_NOTEQUAL, |
|---|
| 520 | GEQUAL = GL_GEQUAL, |
|---|
| 521 | ALWAYS = GL_ALWAYS |
|---|
| 522 | }; |
|---|
| 523 | |
|---|
| 524 | /** Sets shadow texture comparison function. */ |
|---|
| 525 | void setShadowCompareFunc(ShadowCompareFunc func) { _shadow_compare_func = func; } |
|---|
| 526 | ShadowCompareFunc getShadowCompareFunc() const { return _shadow_compare_func; } |
|---|
| 527 | |
|---|
| 528 | enum ShadowTextureMode { |
|---|
| 529 | LUMINANCE = GL_LUMINANCE, |
|---|
| 530 | INTENSITY = GL_INTENSITY, |
|---|
| 531 | ALPHA = GL_ALPHA |
|---|
| 532 | }; |
|---|
| 533 | |
|---|
| 534 | /** Sets shadow texture mode after comparison. */ |
|---|
| 535 | void setShadowTextureMode(ShadowTextureMode mode) { _shadow_texture_mode = mode; } |
|---|
| 536 | ShadowTextureMode getShadowTextureMode() const { return _shadow_texture_mode; } |
|---|
| 537 | |
|---|
| 538 | /** Sets the TEXTURE_COMPARE_FAIL_VALUE_ARB texture parameter. See |
|---|
| 539 | * http://oss.sgi.com/projects/ogl-sample/registry/ARB/shadow_ambient.txt. */ |
|---|
| 540 | void setShadowAmbient(float shadow_ambient) { _shadow_ambient = shadow_ambient; } |
|---|
| 541 | float getShadowAmbient() const { return _shadow_ambient; } |
|---|
| 542 | |
|---|
| 543 | |
|---|
| 544 | /** Sets the texture image for the specified face. */ |
|---|
| 545 | virtual void setImage(unsigned int face, Image* image) = 0; |
|---|
| 546 | |
|---|
| 547 | /** Gets the texture image for the specified face. */ |
|---|
| 548 | virtual Image* getImage(unsigned int face) = 0; |
|---|
| 549 | |
|---|
| 550 | /** Gets the const texture image for specified face. */ |
|---|
| 551 | virtual const Image* getImage(unsigned int face) const = 0; |
|---|
| 552 | |
|---|
| 553 | /** Gets the number of images that can be assigned to this Texture. */ |
|---|
| 554 | virtual unsigned int getNumImages() const = 0; |
|---|
| 555 | |
|---|
| 556 | |
|---|
| 557 | /** Set the PBuffer graphics context to read from when using PBuffers for RenderToTexture.*/ |
|---|
| 558 | void setReadPBuffer(GraphicsContext* context) { _readPBuffer = context; } |
|---|
| 559 | |
|---|
| 560 | /** Get the PBuffer graphics context to read from when using PBuffers for RenderToTexture.*/ |
|---|
| 561 | GraphicsContext* getReadPBuffer() { return _readPBuffer.get(); } |
|---|
| 562 | |
|---|
| 563 | /** Get the const PBuffer graphics context to read from when using PBuffers for RenderToTexture.*/ |
|---|
| 564 | const GraphicsContext* getReadPBuffer() const { return _readPBuffer.get(); } |
|---|
| 565 | |
|---|
| 566 | /** Texture is a pure virtual base class, apply must be overridden. */ |
|---|
| 567 | virtual void apply(State& state) const = 0; |
|---|
| 568 | |
|---|
| 569 | /** Calls apply(state) to compile the texture. */ |
|---|
| 570 | virtual void compileGLObjects(State& state) const; |
|---|
| 571 | |
|---|
| 572 | /** Resize any per context GLObject buffers to specified size. */ |
|---|
| 573 | virtual void resizeGLObjectBuffers(unsigned int maxSize); |
|---|
| 574 | |
|---|
| 575 | /** If State is non-zero, this function releases OpenGL objects for |
|---|
| 576 | * the specified graphics context. Otherwise, releases OpenGL objects |
|---|
| 577 | * for all graphics contexts. */ |
|---|
| 578 | virtual void releaseGLObjects(State* state=0) const; |
|---|
| 579 | |
|---|
| 580 | /** Encapsulates queries of extension availability, obtains extension |
|---|
| 581 | * function pointers, and provides convenience wrappers for |
|---|
| 582 | * calling extension functions. */ |
|---|
| 583 | class OSG_EXPORT Extensions : public osg::Referenced |
|---|
| 584 | { |
|---|
| 585 | public: |
|---|
| 586 | Extensions(unsigned int contextID); |
|---|
| 587 | |
|---|
| 588 | Extensions(const Extensions& rhs); |
|---|
| 589 | |
|---|
| 590 | void lowestCommonDenominator(const Extensions& rhs); |
|---|
| 591 | |
|---|
| 592 | void setupGLExtensions(unsigned int contextID); |
|---|
| 593 | |
|---|
| 594 | void setMultiTexturingSupported(bool flag) { _isMultiTexturingSupported=flag; } |
|---|
| 595 | bool isMultiTexturingSupported() const { return _isMultiTexturingSupported; } |
|---|
| 596 | |
|---|
| 597 | void setTextureFilterAnisotropicSupported(bool flag) { _isTextureFilterAnisotropicSupported=flag; } |
|---|
| 598 | bool isTextureFilterAnisotropicSupported() const { return _isTextureFilterAnisotropicSupported; } |
|---|
| 599 | |
|---|
| 600 | void setTextureCompressionARBSupported(bool flag) { _isTextureCompressionARBSupported=flag; } |
|---|
| 601 | bool isTextureCompressionARBSupported() const { return _isTextureCompressionARBSupported; } |
|---|
| 602 | |
|---|
| 603 | void setTextureCompressionS3TCSupported(bool flag) { _isTextureCompressionS3TCSupported=flag; } |
|---|
| 604 | bool isTextureCompressionS3TCSupported() const { return _isTextureCompressionS3TCSupported; } |
|---|
| 605 | |
|---|
| 606 | void setTextureMirroredRepeatSupported(bool flag) { _isTextureMirroredRepeatSupported=flag; } |
|---|
| 607 | bool isTextureMirroredRepeatSupported() const { return _isTextureMirroredRepeatSupported; } |
|---|
| 608 | |
|---|
| 609 | void setTextureEdgeClampSupported(bool flag) { _isTextureEdgeClampSupported=flag; } |
|---|
| 610 | bool isTextureEdgeClampSupported() const { return _isTextureEdgeClampSupported; } |
|---|
| 611 | |
|---|
| 612 | void setTextureBorderClampSupported(bool flag) { _isTextureBorderClampSupported=flag; } |
|---|
| 613 | bool isTextureBorderClampSupported() const { return _isTextureBorderClampSupported; } |
|---|
| 614 | |
|---|
| 615 | void setGenerateMipMapSupported(bool flag) { _isGenerateMipMapSupported=flag; } |
|---|
| 616 | bool isGenerateMipMapSupported() const { return _isGenerateMipMapSupported; } |
|---|
| 617 | |
|---|
| 618 | void setShadowSupported(bool flag) { _isShadowSupported = flag; } |
|---|
| 619 | bool isShadowSupported() const { return _isShadowSupported; } |
|---|
| 620 | |
|---|
| 621 | void setShadowAmbientSupported(bool flag) { _isShadowAmbientSupported = flag; } |
|---|
| 622 | bool isShadowAmbientSupported() const { return _isShadowAmbientSupported; } |
|---|
| 623 | |
|---|
| 624 | void setMaxTextureSize(GLint maxsize) { _maxTextureSize=maxsize; } |
|---|
| 625 | GLint maxTextureSize() const { return _maxTextureSize; } |
|---|
| 626 | |
|---|
| 627 | void setNumTextureUnits(GLint nunits ) { _numTextureUnits=nunits; } |
|---|
| 628 | GLint numTextureUnits() const { return _numTextureUnits; } |
|---|
| 629 | |
|---|
| 630 | bool isCompressedTexImage2DSupported() const { return _glCompressedTexImage2D!=0; } |
|---|
| 631 | bool isCompressedTexSubImage2DSupported() const { return _glCompressedTexSubImage2D!=0; } |
|---|
| 632 | |
|---|
| 633 | void glCompressedTexImage2D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data) const; |
|---|
| 634 | void glCompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei type, const GLvoid *data) const; |
|---|
| 635 | void glGetCompressedTexImage(GLenum target, GLint level, GLvoid *data) const; |
|---|
| 636 | |
|---|
| 637 | bool isClientStorageSupported() const { return _isClientStorageSupported; } |
|---|
| 638 | |
|---|
| 639 | bool isNonPowerOfTwoTextureSupported(GLenum filter) const |
|---|
| 640 | { |
|---|
| 641 | return (filter==GL_LINEAR || filter==GL_NEAREST) ? |
|---|
| 642 | _isNonPowerOfTwoTextureNonMipMappedSupported : |
|---|
| 643 | _isNonPowerOfTwoTextureMipMappedSupported; |
|---|
| 644 | } |
|---|
| 645 | |
|---|
| 646 | void setTextureIntegerSupported(bool flag) { _isTextureIntegerEXTSupported=flag; } |
|---|
| 647 | bool isTextureIntegerSupported() const { return _isTextureIntegerEXTSupported; } |
|---|
| 648 | |
|---|
| 649 | void glTexParameterIiv(GLenum target, GLenum pname, const GLint* data) const; |
|---|
| 650 | void glTexParameterIuiv(GLenum target, GLenum pname, const GLuint* data) const; |
|---|
| 651 | |
|---|
| 652 | protected: |
|---|
| 653 | |
|---|
| 654 | ~Extensions() {} |
|---|
| 655 | |
|---|
| 656 | bool _isMultiTexturingSupported; |
|---|
| 657 | bool _isTextureFilterAnisotropicSupported; |
|---|
| 658 | bool _isTextureCompressionARBSupported; |
|---|
| 659 | bool _isTextureCompressionS3TCSupported; |
|---|
| 660 | bool _isTextureMirroredRepeatSupported; |
|---|
| 661 | bool _isTextureEdgeClampSupported; |
|---|
| 662 | bool _isTextureBorderClampSupported; |
|---|
| 663 | bool _isGenerateMipMapSupported; |
|---|
| 664 | bool _isShadowSupported; |
|---|
| 665 | bool _isShadowAmbientSupported; |
|---|
| 666 | bool _isClientStorageSupported; |
|---|
| 667 | bool _isNonPowerOfTwoTextureMipMappedSupported; |
|---|
| 668 | bool _isNonPowerOfTwoTextureNonMipMappedSupported; |
|---|
| 669 | bool _isTextureIntegerEXTSupported; |
|---|
| 670 | |
|---|
| 671 | GLint _maxTextureSize; |
|---|
| 672 | GLint _numTextureUnits; |
|---|
| 673 | |
|---|
| 674 | typedef void (APIENTRY * CompressedTexImage2DArbProc) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data); |
|---|
| 675 | typedef void (APIENTRY * CompressedTexSubImage2DArbProc) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const GLvoid *data); |
|---|
| 676 | typedef void (APIENTRY * GetCompressedTexImageArbProc) (GLenum target, GLint level, GLvoid *data); |
|---|
| 677 | typedef void (APIENTRY * TexParameterIivProc)(GLenum target, GLenum pname, const GLint* data); |
|---|
| 678 | typedef void (APIENTRY * TexParameterIuivProc)(GLenum target, GLenum pname, const GLuint* data); |
|---|
| 679 | |
|---|
| 680 | CompressedTexImage2DArbProc _glCompressedTexImage2D; |
|---|
| 681 | CompressedTexSubImage2DArbProc _glCompressedTexSubImage2D; |
|---|
| 682 | GetCompressedTexImageArbProc _glGetCompressedTexImage; |
|---|
| 683 | TexParameterIivProc _glTexParameterIiv; |
|---|
| 684 | TexParameterIuivProc _glTexParameterIuiv; |
|---|
| 685 | |
|---|
| 686 | }; |
|---|
| 687 | |
|---|
| 688 | /** Gets the extension for the specified context. Creates the |
|---|
| 689 | * Extensions object for that context if it doesn't exist. |
|---|
| 690 | * Returns NULL if the Extensions object for the context doesn't |
|---|
| 691 | * exist and the createIfNotInitalized flag is false. */ |
|---|
| 692 | static Extensions* getExtensions(unsigned int contextID,bool createIfNotInitalized); |
|---|
| 693 | |
|---|
| 694 | /** Overrides Extensions objects across graphics contexts. Typically |
|---|
| 695 | * used to ensure the same lowest common denominator of extensions |
|---|
| 696 | * on systems with different graphics pipes. */ |
|---|
| 697 | static void setExtensions(unsigned int contextID,Extensions* extensions); |
|---|
| 698 | |
|---|
| 699 | /** Determine whether the given internalFormat is a compressed |
|---|
| 700 | * image format. */ |
|---|
| 701 | static bool isCompressedInternalFormat(GLint internalFormat); |
|---|
| 702 | |
|---|
| 703 | /** Determine the size of a compressed image, given the internalFormat, |
|---|
| 704 | * the width, the height, and the depth of the image. The block size |
|---|
| 705 | * and the size are output parameters. */ |
|---|
| 706 | static void getCompressedSize(GLenum internalFormat, GLint width, GLint height, GLint depth, GLint& blockSize, GLint& size); |
|---|
| 707 | |
|---|
| 708 | |
|---|
| 709 | /** Helper method. Creates the texture, but doesn't set or use a |
|---|
| 710 | * texture binding. Note: Don't call this method directly unless |
|---|
| 711 | * you're implementing a subload callback. */ |
|---|
| 712 | void applyTexImage2D_load(State& state, GLenum target, const Image* image, GLsizei width, GLsizei height,GLsizei numMipmapLevels) const; |
|---|
| 713 | |
|---|
| 714 | /** Helper method. Subloads images into the texture, but doesn't set |
|---|
| 715 | * or use a texture binding. Note: Don't call this method directly |
|---|
| 716 | * unless you're implementing a subload callback. */ |
|---|
| 717 | void applyTexImage2D_subload(State& state, GLenum target, const Image* image, GLsizei width, GLsizei height, GLint inInternalFormat, GLsizei numMipmapLevels) const; |
|---|
| 718 | |
|---|
| 719 | /** Returned by mipmapBeforeTexImage() to indicate what |
|---|
| 720 | * mipmapAfterTexImage() should do */ |
|---|
| 721 | enum GenerateMipmapMode |
|---|
| 722 | { |
|---|
| 723 | GENERATE_MIPMAP_NONE, |
|---|
| 724 | GENERATE_MIPMAP, |
|---|
| 725 | GENERATE_MIPMAP_TEX_PARAMETER |
|---|
| 726 | }; |
|---|
| 727 | |
|---|
| 728 | protected : |
|---|
| 729 | |
|---|
| 730 | virtual ~Texture(); |
|---|
| 731 | |
|---|
| 732 | virtual void computeInternalFormat() const = 0; |
|---|
| 733 | |
|---|
| 734 | void computeInternalFormatWithImage(const osg::Image& image) const; |
|---|
| 735 | |
|---|
| 736 | void computeRequiredTextureDimensions(State& state, const osg::Image& image,GLsizei& width, GLsizei& height,GLsizei& numMipmapLevels) const; |
|---|
| 737 | |
|---|
| 738 | void computeInternalFormatType() const; |
|---|
| 739 | |
|---|
| 740 | /** Helper method. Sets texture parameters. */ |
|---|
| 741 | void applyTexParameters(GLenum target, State& state) const; |
|---|
| 742 | |
|---|
| 743 | /** Returns true if _useHardwareMipMapGeneration is true and either |
|---|
| 744 | * glGenerateMipmapEXT() or GL_GENERATE_MIPMAP_SGIS are supported. */ |
|---|
| 745 | bool isHardwareMipmapGenerationEnabled(const State& state) const; |
|---|
| 746 | |
|---|
| 747 | /** Helper methods to be called before and after calling |
|---|
| 748 | * gl[Compressed][Copy]Tex[Sub]Image2D to handle generating mipmaps. */ |
|---|
| 749 | GenerateMipmapMode mipmapBeforeTexImage(const State& state, bool hardwareMipmapOn) const; |
|---|
| 750 | void mipmapAfterTexImage(State& state, GenerateMipmapMode beforeResult) const; |
|---|
| 751 | |
|---|
| 752 | /** Helper method to generate mipmap levels by calling of glGenerateMipmapEXT. |
|---|
| 753 | * If it is not supported, then call the virtual allocateMipmap() method */ |
|---|
| 754 | void generateMipmap(State& state) const; |
|---|
| 755 | |
|---|
| 756 | /** Allocate mipmap levels of the texture by subsequent calling of glTexImage* function. */ |
|---|
| 757 | virtual void allocateMipmap(State& state) const = 0; |
|---|
| 758 | |
|---|
| 759 | /** Returns -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */ |
|---|
| 760 | int compareTexture(const Texture& rhs) const; |
|---|
| 761 | |
|---|
| 762 | /** Returns -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */ |
|---|
| 763 | int compareTextureObjects(const Texture& rhs) const; |
|---|
| 764 | |
|---|
| 765 | typedef buffered_value<unsigned int> TexParameterDirtyList; |
|---|
| 766 | mutable TexParameterDirtyList _texParametersDirtyList; |
|---|
| 767 | mutable TexParameterDirtyList _texMipmapGenerationDirtyList; |
|---|
| 768 | |
|---|
| 769 | WrapMode _wrap_s; |
|---|
| 770 | WrapMode _wrap_t; |
|---|
| 771 | WrapMode _wrap_r; |
|---|
| 772 | |
|---|
| 773 | FilterMode _min_filter; |
|---|
| 774 | FilterMode _mag_filter; |
|---|
| 775 | float _maxAnisotropy; |
|---|
| 776 | bool _useHardwareMipMapGeneration; |
|---|
| 777 | bool _unrefImageDataAfterApply; |
|---|
| 778 | bool _clientStorageHint; |
|---|
| 779 | bool _resizeNonPowerOfTwoHint; |
|---|
| 780 | |
|---|
| 781 | Vec4d _borderColor; |
|---|
| 782 | GLint _borderWidth; |
|---|
| 783 | |
|---|
| 784 | InternalFormatMode _internalFormatMode; |
|---|
| 785 | mutable InternalFormatType _internalFormatType; |
|---|
| 786 | mutable GLint _internalFormat; |
|---|
| 787 | mutable GLenum _sourceFormat; |
|---|
| 788 | mutable GLenum _sourceType; |
|---|
| 789 | |
|---|
| 790 | bool _use_shadow_comparison; |
|---|
| 791 | ShadowCompareFunc _shadow_compare_func; |
|---|
| 792 | ShadowTextureMode _shadow_texture_mode; |
|---|
| 793 | float _shadow_ambient; |
|---|
| 794 | |
|---|
| 795 | public: |
|---|
| 796 | |
|---|
| 797 | class TextureObject : public osg::Referenced |
|---|
| 798 | { |
|---|
| 799 | public: |
|---|
| 800 | |
|---|
| 801 | inline TextureObject(GLuint id,GLenum target): |
|---|
| 802 | _id(id), |
|---|
| 803 | _target(target), |
|---|
| 804 | _numMipmapLevels(0), |
|---|
| 805 | _internalFormat(0), |
|---|
| 806 | _width(0), |
|---|
| 807 | _height(0), |
|---|
| 808 | _depth(0), |
|---|
| 809 | _border(0), |
|---|
| 810 | _allocated(false), |
|---|
| 811 | _timeStamp(0) {} |
|---|
| 812 | |
|---|
| 813 | inline TextureObject(GLuint id, |
|---|
| 814 | GLenum target, |
|---|
| 815 | GLint numMipmapLevels, |
|---|
| 816 | GLenum internalFormat, |
|---|
| 817 | GLsizei width, |
|---|
| 818 | GLsizei height, |
|---|
| 819 | GLsizei depth, |
|---|
| 820 | GLint border): |
|---|
| 821 | _id(id), |
|---|
| 822 | _target(target), |
|---|
| 823 | _numMipmapLevels(numMipmapLevels), |
|---|
| 824 | _internalFormat(internalFormat), |
|---|
| 825 | _width(width), |
|---|
| 826 | _height(height), |
|---|
| 827 | _depth(depth), |
|---|
| 828 | _border(border), |
|---|
| 829 | _allocated(false), |
|---|
| 830 | _timeStamp(0) {} |
|---|
| 831 | |
|---|
| 832 | inline bool match(GLenum target, |
|---|
| 833 | GLint numMipmapLevels, |
|---|
| 834 | GLenum internalFormat, |
|---|
| 835 | GLsizei width, |
|---|
| 836 | GLsizei height, |
|---|
| 837 | GLsizei depth, |
|---|
| 838 | GLint border) |
|---|
| 839 | { |
|---|
| 840 | return isReusable() && |
|---|
| 841 | (_target == target) && |
|---|
| 842 | (_numMipmapLevels == numMipmapLevels) && |
|---|
| 843 | (_internalFormat == internalFormat) && |
|---|
| 844 | (_width == width) && |
|---|
| 845 | (_height == height) && |
|---|
| 846 | (_depth == depth) && |
|---|
| 847 | (_border == border); |
|---|
| 848 | } |
|---|
| 849 | |
|---|
| 850 | |
|---|
| 851 | inline void bind() |
|---|
| 852 | { |
|---|
| 853 | glBindTexture( _target, _id); |
|---|
| 854 | } |
|---|
| 855 | |
|---|
| 856 | |
|---|
| 857 | inline void setAllocated(bool allocated=true) { _allocated = allocated; } |
|---|
| 858 | |
|---|
| 859 | inline void setAllocated(GLint numMipmapLevels, |
|---|
| 860 | GLenum internalFormat, |
|---|
| 861 | GLsizei width, |
|---|
| 862 | GLsizei height, |
|---|
| 863 | GLsizei depth, |
|---|
| 864 | GLint border) |
|---|
| 865 | { |
|---|
| 866 | _allocated=true; |
|---|
| 867 | _numMipmapLevels = numMipmapLevels; |
|---|
| 868 | _internalFormat = internalFormat; |
|---|
| 869 | _width = width; |
|---|
| 870 | _height = height; |
|---|
| 871 | _depth = depth; |
|---|
| 872 | _border = border; |
|---|
| 873 | } |
|---|
| 874 | |
|---|
| 875 | inline bool isAllocated() const { return _allocated; } |
|---|
| 876 | |
|---|
| 877 | inline bool isReusable() const { return _allocated && _width!=0; } |
|---|
| 878 | |
|---|
| 879 | GLuint _id; |
|---|
| 880 | GLenum _target; |
|---|
| 881 | GLint _numMipmapLevels; |
|---|
| 882 | GLenum _internalFormat; |
|---|
| 883 | GLsizei _width; |
|---|
| 884 | GLsizei _height; |
|---|
| 885 | GLsizei _depth; |
|---|
| 886 | GLint _border; |
|---|
| 887 | |
|---|
| 888 | bool _allocated; |
|---|
| 889 | double _timeStamp; |
|---|
| 890 | }; |
|---|
| 891 | |
|---|
| 892 | typedef std::list< ref_ptr<TextureObject> > TextureObjectList; |
|---|
| 893 | typedef osg::buffered_object<TextureObjectList> TextureObjectListMap; |
|---|
| 894 | |
|---|
| 895 | /** Takes the active texture objects from the Texture and places them |
|---|
| 896 | * in the specified TextureObjectListMap. */ |
|---|
| 897 | void takeTextureObjects(TextureObjectListMap& toblm); |
|---|
| 898 | |
|---|
| 899 | |
|---|
| 900 | static TextureObject* generateTextureObject(unsigned int contextID,GLenum target); |
|---|
| 901 | |
|---|
| 902 | static TextureObject* generateTextureObject(unsigned int contextID, |
|---|
| 903 | GLenum target, |
|---|
| 904 | GLint numMipmapLevels, |
|---|
| 905 | GLenum internalFormat, |
|---|
| 906 | GLsizei width, |
|---|
| 907 | GLsizei height, |
|---|
| 908 | GLsizei depth, |
|---|
| 909 | GLint border); |
|---|
| 910 | |
|---|
| 911 | |
|---|
| 912 | /** Set the minimum number of texture objects to retain in the deleted display list cache. */ |
|---|
| 913 | static void setMinimumNumberOfTextureObjectsToRetainInCache(unsigned int minimum); |
|---|
| 914 | |
|---|
| 915 | /** Get the minimum number of display lists to retain in the deleted display list cache. */ |
|---|
| 916 | static unsigned int getMinimumNumberOfTextureObjectsToRetainInCache(); |
|---|
| 917 | |
|---|
| 918 | static void flushAllDeletedTextureObjects(unsigned int contextID); |
|---|
| 919 | |
|---|
| 920 | static void discardAllDeletedTextureObjects(unsigned int contextID); |
|---|
| 921 | |
|---|
| 922 | static void flushDeletedTextureObjects(unsigned int contextID,double currentTime, double& availableTime); |
|---|
| 923 | |
|---|
| 924 | |
|---|
| 925 | protected: |
|---|
| 926 | |
|---|
| 927 | typedef buffered_object< ref_ptr<TextureObject> > TextureObjectBuffer; |
|---|
| 928 | mutable TextureObjectBuffer _textureObjectBuffer; |
|---|
| 929 | mutable ref_ptr<GraphicsContext> _readPBuffer; |
|---|
| 930 | |
|---|
| 931 | }; |
|---|
| 932 | |
|---|
| 933 | } |
|---|
| 934 | |
|---|
| 935 | #endif |
|---|