| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/GLExtensions> |
|---|
| 14 | #include <osg/Texture3D> |
|---|
| 15 | #include <osg/State> |
|---|
| 16 | #include <osg/GLU> |
|---|
| 17 | #include <osg/Notify> |
|---|
| 18 | |
|---|
| 19 | #include <string.h> |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | using namespace osg; |
|---|
| 24 | |
|---|
| 25 | Texture3D::Texture3D(): |
|---|
| 26 | _textureWidth(0), |
|---|
| 27 | _textureHeight(0), |
|---|
| 28 | _textureDepth(0), |
|---|
| 29 | _numMipmapLevels(0) |
|---|
| 30 | { |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | Texture3D::Texture3D(Image* image): |
|---|
| 35 | _textureWidth(0), |
|---|
| 36 | _textureHeight(0), |
|---|
| 37 | _textureDepth(0), |
|---|
| 38 | _numMipmapLevels(0) |
|---|
| 39 | { |
|---|
| 40 | setImage(image); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | Texture3D::Texture3D(const Texture3D& text,const CopyOp& copyop): |
|---|
| 44 | Texture(text,copyop), |
|---|
| 45 | _image(copyop(text._image.get())), |
|---|
| 46 | _textureWidth(text._textureWidth), |
|---|
| 47 | _textureHeight(text._textureHeight), |
|---|
| 48 | _textureDepth(text._textureDepth), |
|---|
| 49 | _numMipmapLevels(text._numMipmapLevels), |
|---|
| 50 | _subloadCallback(text._subloadCallback) |
|---|
| 51 | { |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | Texture3D::~Texture3D() |
|---|
| 55 | { |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | int Texture3D::compare(const StateAttribute& sa) const |
|---|
| 59 | { |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | COMPARE_StateAttribute_Types(Texture3D,sa) |
|---|
| 63 | |
|---|
| 64 | if (_image!=rhs._image) |
|---|
| 65 | { |
|---|
| 66 | if (_image.valid()) |
|---|
| 67 | { |
|---|
| 68 | if (rhs._image.valid()) |
|---|
| 69 | { |
|---|
| 70 | int result = _image->compare(*rhs._image); |
|---|
| 71 | if (result!=0) return result; |
|---|
| 72 | } |
|---|
| 73 | else |
|---|
| 74 | { |
|---|
| 75 | return 1; |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | else if (rhs._image.valid()) |
|---|
| 79 | { |
|---|
| 80 | return -1; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | if (!_image && !rhs._image) |
|---|
| 85 | { |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | int result = compareTextureObjects(rhs); |
|---|
| 92 | if (result!=0) return result; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | int result = compareTexture(rhs); |
|---|
| 96 | if (result!=0) return result; |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | COMPARE_StateAttribute_Parameter(_textureWidth) |
|---|
| 100 | COMPARE_StateAttribute_Parameter(_textureHeight) |
|---|
| 101 | COMPARE_StateAttribute_Parameter(_textureDepth) |
|---|
| 102 | COMPARE_StateAttribute_Parameter(_subloadCallback) |
|---|
| 103 | |
|---|
| 104 | return 0; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | void Texture3D::setImage(Image* image) |
|---|
| 108 | { |
|---|
| 109 | if (_image == image) return; |
|---|
| 110 | |
|---|
| 111 | if (_image.valid() && _image->requiresUpdateCall()) |
|---|
| 112 | { |
|---|
| 113 | setUpdateCallback(0); |
|---|
| 114 | setDataVariance(osg::Object::STATIC); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | dirtyTextureObject(); |
|---|
| 119 | |
|---|
| 120 | _modifiedCount.setAllElementsTo(0); |
|---|
| 121 | |
|---|
| 122 | _image = image; |
|---|
| 123 | |
|---|
| 124 | if (_image.valid() && _image->requiresUpdateCall()) |
|---|
| 125 | { |
|---|
| 126 | setUpdateCallback(new Image::UpdateCallback()); |
|---|
| 127 | setDataVariance(osg::Object::DYNAMIC); |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | void Texture3D::computeRequiredTextureDimensions(State& state, const osg::Image& image,GLsizei& inwidth, GLsizei& inheight,GLsizei& indepth, GLsizei& numMipmapLevels) const |
|---|
| 132 | { |
|---|
| 133 | const unsigned int contextID = state.getContextID(); |
|---|
| 134 | const Extensions* extensions = getExtensions(contextID,true); |
|---|
| 135 | const Texture::Extensions* texExtensions = Texture::getExtensions(contextID,true); |
|---|
| 136 | |
|---|
| 137 | int width,height,depth; |
|---|
| 138 | |
|---|
| 139 | if( !_resizeNonPowerOfTwoHint && texExtensions->isNonPowerOfTwoTextureSupported(_min_filter) ) |
|---|
| 140 | { |
|---|
| 141 | width = image.s(); |
|---|
| 142 | height = image.t(); |
|---|
| 143 | depth = image.r(); |
|---|
| 144 | } |
|---|
| 145 | else |
|---|
| 146 | { |
|---|
| 147 | width = Image::computeNearestPowerOfTwo(image.s()-2*_borderWidth)+2*_borderWidth; |
|---|
| 148 | height = Image::computeNearestPowerOfTwo(image.t()-2*_borderWidth)+2*_borderWidth; |
|---|
| 149 | depth = Image::computeNearestPowerOfTwo(image.r()-2*_borderWidth)+2*_borderWidth; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | if (width>extensions->maxTexture3DSize()) width = extensions->maxTexture3DSize(); |
|---|
| 154 | if (height>extensions->maxTexture3DSize()) height = extensions->maxTexture3DSize(); |
|---|
| 155 | if (depth>extensions->maxTexture3DSize()) depth = extensions->maxTexture3DSize(); |
|---|
| 156 | |
|---|
| 157 | inwidth = width; |
|---|
| 158 | inheight = height; |
|---|
| 159 | indepth = depth; |
|---|
| 160 | |
|---|
| 161 | bool useHardwareMipMapGeneration = !image.isMipmap() && _useHardwareMipMapGeneration && texExtensions->isGenerateMipMapSupported(); |
|---|
| 162 | |
|---|
| 163 | if( _min_filter == LINEAR || _min_filter == NEAREST || useHardwareMipMapGeneration ) |
|---|
| 164 | { |
|---|
| 165 | numMipmapLevels = 1; |
|---|
| 166 | } |
|---|
| 167 | else if( image.isMipmap() ) |
|---|
| 168 | { |
|---|
| 169 | numMipmapLevels = image.getNumMipmapLevels(); |
|---|
| 170 | } |
|---|
| 171 | else |
|---|
| 172 | { |
|---|
| 173 | numMipmapLevels = 0; |
|---|
| 174 | for( ; (width || height || depth) ;++numMipmapLevels) |
|---|
| 175 | { |
|---|
| 176 | |
|---|
| 177 | if (width == 0) |
|---|
| 178 | width = 1; |
|---|
| 179 | if (height == 0) |
|---|
| 180 | height = 1; |
|---|
| 181 | if (depth == 0) |
|---|
| 182 | depth = 1; |
|---|
| 183 | |
|---|
| 184 | width >>= 1; |
|---|
| 185 | height >>= 1; |
|---|
| 186 | depth >>= 1; |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | void Texture3D::apply(State& state) const |
|---|
| 192 | { |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | const unsigned int contextID = state.getContextID(); |
|---|
| 197 | |
|---|
| 198 | Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get(); |
|---|
| 199 | ElapsedTime elapsedTime(&(tom->getApplyTime())); |
|---|
| 200 | tom->getNumberApplied()++; |
|---|
| 201 | |
|---|
| 202 | const Extensions* extensions = getExtensions(contextID,true); |
|---|
| 203 | |
|---|
| 204 | if (!extensions->isTexture3DSupported()) |
|---|
| 205 | { |
|---|
| 206 | OSG_WARN<<"Warning: Texture3D::apply(..) failed, 3D texturing is not support by OpenGL driver."<<std::endl; |
|---|
| 207 | return; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 212 | |
|---|
| 213 | if (textureObject) |
|---|
| 214 | { |
|---|
| 215 | if (_image.valid() && getModifiedCount(contextID) != _image->getModifiedCount()) |
|---|
| 216 | { |
|---|
| 217 | |
|---|
| 218 | computeInternalFormat(); |
|---|
| 219 | |
|---|
| 220 | GLsizei new_width, new_height, new_depth, new_numMipmapLevels; |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | computeRequiredTextureDimensions(state, *_image, new_width, new_height, new_depth, new_numMipmapLevels); |
|---|
| 224 | |
|---|
| 225 | if (!textureObject->match(GL_TEXTURE_3D, new_numMipmapLevels, _internalFormat, new_width, new_height, new_depth, _borderWidth)) |
|---|
| 226 | { |
|---|
| 227 | Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get()); |
|---|
| 228 | _textureObjectBuffer[contextID] = 0; |
|---|
| 229 | textureObject = 0; |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | if (textureObject) |
|---|
| 235 | { |
|---|
| 236 | |
|---|
| 237 | textureObject->bind(); |
|---|
| 238 | |
|---|
| 239 | if (getTextureParameterDirty(state.getContextID())) applyTexParameters(GL_TEXTURE_3D,state); |
|---|
| 240 | |
|---|
| 241 | if (_subloadCallback.valid()) |
|---|
| 242 | { |
|---|
| 243 | _subloadCallback->subload(*this,state); |
|---|
| 244 | } |
|---|
| 245 | else if (_image.get() && getModifiedCount(contextID) != _image->getModifiedCount()) |
|---|
| 246 | { |
|---|
| 247 | computeRequiredTextureDimensions(state,*_image,_textureWidth, _textureHeight, _textureDepth,_numMipmapLevels); |
|---|
| 248 | |
|---|
| 249 | applyTexImage3D(GL_TEXTURE_3D,_image.get(),state, _textureWidth, _textureHeight, _textureDepth,_numMipmapLevels); |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | getModifiedCount(contextID) = _image->getModifiedCount(); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | } |
|---|
| 256 | else if (_subloadCallback.valid()) |
|---|
| 257 | { |
|---|
| 258 | |
|---|
| 259 | _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_3D); |
|---|
| 260 | |
|---|
| 261 | textureObject->bind(); |
|---|
| 262 | |
|---|
| 263 | applyTexParameters(GL_TEXTURE_3D,state); |
|---|
| 264 | |
|---|
| 265 | _subloadCallback->load(*this,state); |
|---|
| 266 | |
|---|
| 267 | textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,_textureDepth,0); |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | } |
|---|
| 276 | else if (_image.valid() && _image->data()) |
|---|
| 277 | { |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | computeInternalFormat(); |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | computeRequiredTextureDimensions(state,*_image,_textureWidth, _textureHeight, _textureDepth,_numMipmapLevels); |
|---|
| 284 | |
|---|
| 285 | textureObject = generateTextureObject(this, contextID,GL_TEXTURE_3D); |
|---|
| 286 | |
|---|
| 287 | textureObject->bind(); |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | applyTexParameters(GL_TEXTURE_3D,state); |
|---|
| 291 | |
|---|
| 292 | applyTexImage3D(GL_TEXTURE_3D,_image.get(),state, _textureWidth, _textureHeight, _textureDepth,_numMipmapLevels); |
|---|
| 293 | |
|---|
| 294 | textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,_textureDepth,0); |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | getModifiedCount(contextID) = _image->getModifiedCount(); |
|---|
| 298 | |
|---|
| 299 | _textureObjectBuffer[contextID] = textureObject; |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | if (isSafeToUnrefImageData(state) && _image->getDataVariance()==STATIC) |
|---|
| 303 | { |
|---|
| 304 | Texture3D* non_const_this = const_cast<Texture3D*>(this); |
|---|
| 305 | non_const_this->_image = NULL; |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | } |
|---|
| 309 | else if ( (_textureWidth!=0) && (_textureHeight!=0) && (_textureDepth!=0) && (_internalFormat!=0) ) |
|---|
| 310 | { |
|---|
| 311 | _textureObjectBuffer[contextID] = textureObject = generateTextureObject( |
|---|
| 312 | this, contextID,GL_TEXTURE_3D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,_textureDepth,0); |
|---|
| 313 | |
|---|
| 314 | textureObject->bind(); |
|---|
| 315 | |
|---|
| 316 | applyTexParameters(GL_TEXTURE_3D,state); |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | extensions->glTexImage3D( GL_TEXTURE_3D, 0, _internalFormat, |
|---|
| 320 | _textureWidth, _textureHeight, _textureDepth, |
|---|
| 321 | _borderWidth, |
|---|
| 322 | _sourceFormat ? _sourceFormat : _internalFormat, |
|---|
| 323 | _sourceType ? _sourceType : GL_UNSIGNED_BYTE, |
|---|
| 324 | 0); |
|---|
| 325 | |
|---|
| 326 | if (_readPBuffer.valid()) |
|---|
| 327 | { |
|---|
| 328 | _readPBuffer->bindPBufferToTexture(GL_FRONT); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | } |
|---|
| 332 | else |
|---|
| 333 | { |
|---|
| 334 | glBindTexture( GL_TEXTURE_3D, 0 ); |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | if (textureObject != 0 && _texMipmapGenerationDirtyList[contextID]) |
|---|
| 339 | { |
|---|
| 340 | generateMipmap(state); |
|---|
| 341 | } |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | void Texture3D::computeInternalFormat() const |
|---|
| 345 | { |
|---|
| 346 | if (_image.valid()) computeInternalFormatWithImage(*_image); |
|---|
| 347 | else computeInternalFormatType(); |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | void Texture3D::applyTexImage3D(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight, GLsizei& indepth, GLsizei& numMipmapLevels) const |
|---|
| 351 | { |
|---|
| 352 | |
|---|
| 353 | if (!image || !image->data()) |
|---|
| 354 | return; |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | const unsigned int contextID = state.getContextID(); |
|---|
| 359 | const Extensions* extensions = getExtensions(contextID,true); |
|---|
| 360 | const Texture::Extensions* texExtensions = Texture::getExtensions(contextID,true); |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | computeInternalFormat(); |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | bool compressed = isCompressedInternalFormat(_internalFormat); |
|---|
| 367 | bool compressed_image = isCompressedInternalFormat((GLenum)image->getPixelFormat()); |
|---|
| 368 | |
|---|
| 369 | if (compressed) |
|---|
| 370 | { |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | if( _resizeNonPowerOfTwoHint || !texExtensions->isNonPowerOfTwoTextureSupported(_min_filter) |
|---|
| 377 | || inwidth > extensions->maxTexture3DSize() |
|---|
| 378 | || inheight > extensions->maxTexture3DSize() |
|---|
| 379 | || indepth > extensions->maxTexture3DSize() ) |
|---|
| 380 | image->ensureValidSizeForTexturing(extensions->maxTexture3DSize()); |
|---|
| 381 | |
|---|
| 382 | glPixelStorei(GL_UNPACK_ALIGNMENT,image->getPacking()); |
|---|
| 383 | |
|---|
| 384 | bool useHardwareMipMapGeneration = !image->isMipmap() && _useHardwareMipMapGeneration && texExtensions->isGenerateMipMapSupported(); |
|---|
| 385 | |
|---|
| 386 | if( _min_filter == LINEAR || _min_filter == NEAREST || useHardwareMipMapGeneration ) |
|---|
| 387 | { |
|---|
| 388 | bool hardwareMipMapOn = false; |
|---|
| 389 | if (_min_filter != LINEAR && _min_filter != NEAREST) |
|---|
| 390 | { |
|---|
| 391 | if (useHardwareMipMapGeneration) glTexParameteri(GL_TEXTURE_3D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE); |
|---|
| 392 | hardwareMipMapOn = true; |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | numMipmapLevels = 1; |
|---|
| 396 | |
|---|
| 397 | if (!compressed_image) |
|---|
| 398 | { |
|---|
| 399 | extensions->glTexImage3D( target, 0, _internalFormat, |
|---|
| 400 | inwidth, inheight, indepth, |
|---|
| 401 | _borderWidth, |
|---|
| 402 | (GLenum)image->getPixelFormat(), |
|---|
| 403 | (GLenum)image->getDataType(), |
|---|
| 404 | image->data() ); |
|---|
| 405 | } |
|---|
| 406 | else if (extensions->isCompressedTexImage3DSupported()) |
|---|
| 407 | { |
|---|
| 408 | |
|---|
| 409 | numMipmapLevels = 1; |
|---|
| 410 | |
|---|
| 411 | GLint blockSize, size; |
|---|
| 412 | getCompressedSize(_internalFormat, inwidth, inheight, indepth, blockSize,size); |
|---|
| 413 | |
|---|
| 414 | extensions->glCompressedTexImage3D(target, 0, _internalFormat, |
|---|
| 415 | inwidth, inheight, indepth, |
|---|
| 416 | _borderWidth, |
|---|
| 417 | size, |
|---|
| 418 | image->data()); |
|---|
| 419 | } |
|---|
| 420 | |
|---|
| 421 | if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_3D, GL_GENERATE_MIPMAP_SGIS,GL_FALSE); |
|---|
| 422 | } |
|---|
| 423 | else |
|---|
| 424 | { |
|---|
| 425 | if(!image->isMipmap()) |
|---|
| 426 | { |
|---|
| 427 | |
|---|
| 428 | numMipmapLevels = 1; |
|---|
| 429 | |
|---|
| 430 | gluBuild3DMipmaps( extensions->glTexImage3D, |
|---|
| 431 | target, _internalFormat, |
|---|
| 432 | image->s(),image->t(),image->r(), |
|---|
| 433 | (GLenum)image->getPixelFormat(), (GLenum)image->getDataType(), |
|---|
| 434 | image->data() ); |
|---|
| 435 | |
|---|
| 436 | } |
|---|
| 437 | else |
|---|
| 438 | { |
|---|
| 439 | numMipmapLevels = image->getNumMipmapLevels(); |
|---|
| 440 | |
|---|
| 441 | int width = image->s(); |
|---|
| 442 | int height = image->t(); |
|---|
| 443 | int depth = image->r(); |
|---|
| 444 | |
|---|
| 445 | for( GLsizei k = 0 ; k < numMipmapLevels && (width || height || depth) ;k++) |
|---|
| 446 | { |
|---|
| 447 | |
|---|
| 448 | if (width == 0) |
|---|
| 449 | width = 1; |
|---|
| 450 | if (height == 0) |
|---|
| 451 | height = 1; |
|---|
| 452 | if (depth == 0) |
|---|
| 453 | depth = 1; |
|---|
| 454 | |
|---|
| 455 | extensions->glTexImage3D( target, k, _internalFormat, |
|---|
| 456 | width, height, depth, _borderWidth, |
|---|
| 457 | (GLenum)image->getPixelFormat(), |
|---|
| 458 | (GLenum)image->getDataType(), |
|---|
| 459 | image->getMipmapData(k)); |
|---|
| 460 | |
|---|
| 461 | width >>= 1; |
|---|
| 462 | height >>= 1; |
|---|
| 463 | depth >>= 1; |
|---|
| 464 | } |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | inwidth = image->s(); |
|---|
| 470 | inheight = image->t(); |
|---|
| 471 | indepth = image->r(); |
|---|
| 472 | |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | void Texture3D::copyTexSubImage3D(State& state, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height ) |
|---|
| 476 | { |
|---|
| 477 | const unsigned int contextID = state.getContextID(); |
|---|
| 478 | const Extensions* extensions = getExtensions(contextID,true); |
|---|
| 479 | |
|---|
| 480 | |
|---|
| 481 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 482 | |
|---|
| 483 | if (textureObject != 0) |
|---|
| 484 | { |
|---|
| 485 | textureObject->bind(); |
|---|
| 486 | |
|---|
| 487 | applyTexParameters(GL_TEXTURE_3D,state); |
|---|
| 488 | extensions->glCopyTexSubImage3D( GL_TEXTURE_3D, 0, xoffset,yoffset,zoffset, x, y, width, height); |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | |
|---|
| 492 | |
|---|
| 493 | |
|---|
| 494 | state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this); |
|---|
| 495 | |
|---|
| 496 | } |
|---|
| 497 | else |
|---|
| 498 | { |
|---|
| 499 | OSG_WARN<<"Warning: Texture3D::copyTexSubImage3D(..) failed, cannot not copy to a non existant texture."<<std::endl; |
|---|
| 500 | } |
|---|
| 501 | } |
|---|
| 502 | |
|---|
| 503 | void Texture3D::allocateMipmap(State& state) const |
|---|
| 504 | { |
|---|
| 505 | const unsigned int contextID = state.getContextID(); |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 509 | |
|---|
| 510 | if (textureObject && _textureWidth != 0 && _textureHeight != 0 && _textureDepth != 0) |
|---|
| 511 | { |
|---|
| 512 | const Extensions* extensions = getExtensions(contextID,true); |
|---|
| 513 | |
|---|
| 514 | |
|---|
| 515 | textureObject->bind(); |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | int width = _textureWidth; |
|---|
| 519 | int height = _textureHeight; |
|---|
| 520 | int depth = _textureDepth; |
|---|
| 521 | int numMipmapLevels = Image::computeNumberOfMipmapLevels(width, height, depth); |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | width >>= 1; |
|---|
| 525 | height >>= 1; |
|---|
| 526 | depth >>= 1; |
|---|
| 527 | |
|---|
| 528 | for( GLsizei k = 1; k < numMipmapLevels && (width || height || depth); k++) |
|---|
| 529 | { |
|---|
| 530 | if (width == 0) |
|---|
| 531 | width = 1; |
|---|
| 532 | if (height == 0) |
|---|
| 533 | height = 1; |
|---|
| 534 | if (depth == 0) |
|---|
| 535 | depth = 1; |
|---|
| 536 | |
|---|
| 537 | extensions->glTexImage3D( GL_TEXTURE_3D, k, _internalFormat, |
|---|
| 538 | width, height, depth, _borderWidth, |
|---|
| 539 | _sourceFormat ? _sourceFormat : _internalFormat, |
|---|
| 540 | _sourceType ? _sourceType : GL_UNSIGNED_BYTE, NULL); |
|---|
| 541 | |
|---|
| 542 | width >>= 1; |
|---|
| 543 | height >>= 1; |
|---|
| 544 | depth >>= 1; |
|---|
| 545 | } |
|---|
| 546 | |
|---|
| 547 | |
|---|
| 548 | state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this); |
|---|
| 549 | } |
|---|
| 550 | } |
|---|
| 551 | |
|---|
| 552 | typedef buffered_value< ref_ptr<Texture3D::Extensions> > BufferedExtensions; |
|---|
| 553 | static BufferedExtensions s_extensions; |
|---|
| 554 | |
|---|
| 555 | Texture3D::Extensions* Texture3D::getExtensions(unsigned int contextID,bool createIfNotInitalized) |
|---|
| 556 | { |
|---|
| 557 | if (!s_extensions[contextID] && createIfNotInitalized) s_extensions[contextID] = new Extensions(contextID); |
|---|
| 558 | return s_extensions[contextID].get(); |
|---|
| 559 | } |
|---|
| 560 | |
|---|
| 561 | void Texture3D::setExtensions(unsigned int contextID,Extensions* extensions) |
|---|
| 562 | { |
|---|
| 563 | s_extensions[contextID] = extensions; |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | #ifndef GL_MAX_3D_TEXTURE_SIZE |
|---|
| 567 | #define GL_MAX_3D_TEXTURE_SIZE 0x8073 |
|---|
| 568 | #endif |
|---|
| 569 | |
|---|
| 570 | Texture3D::Extensions::Extensions(unsigned int contextID) |
|---|
| 571 | { |
|---|
| 572 | setupGLExtensions(contextID); |
|---|
| 573 | } |
|---|
| 574 | |
|---|
| 575 | Texture3D::Extensions::Extensions(const Extensions& rhs): |
|---|
| 576 | Referenced() |
|---|
| 577 | { |
|---|
| 578 | _isTexture3DSupported = rhs._isTexture3DSupported; |
|---|
| 579 | _isTexture3DFast = rhs._isTexture3DFast; |
|---|
| 580 | _maxTexture3DSize = rhs._maxTexture3DSize; |
|---|
| 581 | |
|---|
| 582 | glTexImage3D = rhs.glTexImage3D; |
|---|
| 583 | glTexSubImage3D = rhs.glTexSubImage3D; |
|---|
| 584 | glCompressedTexImage3D = rhs.glCompressedTexImage3D; |
|---|
| 585 | glCompressedTexSubImage3D = rhs.glCompressedTexSubImage3D; |
|---|
| 586 | glCopyTexSubImage3D = rhs.glCopyTexSubImage3D; |
|---|
| 587 | } |
|---|
| 588 | |
|---|
| 589 | void Texture3D::Extensions::lowestCommonDenominator(const Extensions& rhs) |
|---|
| 590 | { |
|---|
| 591 | if (!rhs._isTexture3DSupported) _isTexture3DSupported = false; |
|---|
| 592 | if (!rhs._isTexture3DFast) _isTexture3DFast = false; |
|---|
| 593 | if (rhs._maxTexture3DSize<_maxTexture3DSize) _maxTexture3DSize = rhs._maxTexture3DSize; |
|---|
| 594 | |
|---|
| 595 | if (!rhs.glTexImage3D) glTexImage3D = 0; |
|---|
| 596 | if (!rhs.glTexSubImage3D) glTexSubImage3D = 0; |
|---|
| 597 | if (!rhs.glCompressedTexImage3D) glCompressedTexImage3D = 0; |
|---|
| 598 | if (!rhs.glCompressedTexSubImage3D) glCompressedTexSubImage3D = 0; |
|---|
| 599 | if (!rhs.glCopyTexSubImage3D) glCopyTexSubImage3D = 0; |
|---|
| 600 | } |
|---|
| 601 | |
|---|
| 602 | void Texture3D::Extensions::setupGLExtensions(unsigned int contextID) |
|---|
| 603 | { |
|---|
| 604 | _isTexture3DFast = OSG_GL3_FEATURES || isGLExtensionSupported(contextID,"GL_EXT_texture3D"); |
|---|
| 605 | |
|---|
| 606 | if (_isTexture3DFast) _isTexture3DSupported = true; |
|---|
| 607 | else _isTexture3DSupported = strncmp((const char*)glGetString(GL_VERSION),"1.2",3)>=0; |
|---|
| 608 | |
|---|
| 609 | _maxTexture3DSize = 0; |
|---|
| 610 | glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &_maxTexture3DSize); |
|---|
| 611 | |
|---|
| 612 | setGLExtensionFuncPtr(glTexImage3D,"glTexImage3D","glTexImage3DEXT"); |
|---|
| 613 | setGLExtensionFuncPtr(glTexSubImage3D,"glTexSubImage3D","glTexSubImage3DEXT"); |
|---|
| 614 | setGLExtensionFuncPtr(glCompressedTexImage3D,"glCompressedTexImage3D","glCompressedTexImage3DARB"); |
|---|
| 615 | setGLExtensionFuncPtr(glCompressedTexSubImage3D,"glCompressedTexSubImage3D","glCompressedTexSubImage3DARB"); |
|---|
| 616 | setGLExtensionFuncPtr(glCopyTexSubImage3D,"glCopyTexSubImage3D","glCopyTexSubImage3DEXT"); |
|---|
| 617 | |
|---|
| 618 | } |
|---|