| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/GLExtensions> |
|---|
| 14 | #include <osg/Texture1D> |
|---|
| 15 | #include <osg/ImageSequence> |
|---|
| 16 | #include <osg/State> |
|---|
| 17 | #include <osg/GLU> |
|---|
| 18 | |
|---|
| 19 | typedef void (APIENTRY * MyCompressedTexImage1DArbProc) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLint border, GLsizei imageSize, const GLvoid *data); |
|---|
| 20 | |
|---|
| 21 | using namespace osg; |
|---|
| 22 | |
|---|
| 23 | Texture1D::Texture1D(): |
|---|
| 24 | _textureWidth(0), |
|---|
| 25 | _numMipmapLevels(0) |
|---|
| 26 | { |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | Texture1D::Texture1D(osg::Image* image): |
|---|
| 30 | _textureWidth(0), |
|---|
| 31 | _numMipmapLevels(0) |
|---|
| 32 | { |
|---|
| 33 | setImage(image); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | Texture1D::Texture1D(const Texture1D& text,const CopyOp& copyop): |
|---|
| 37 | Texture(text,copyop), |
|---|
| 38 | _image(copyop(text._image.get())), |
|---|
| 39 | _textureWidth(text._textureWidth), |
|---|
| 40 | _numMipmapLevels(text._numMipmapLevels), |
|---|
| 41 | _subloadCallback(text._subloadCallback) |
|---|
| 42 | { |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | Texture1D::~Texture1D() |
|---|
| 46 | { |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | int Texture1D::compare(const StateAttribute& sa) const |
|---|
| 50 | { |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | COMPARE_StateAttribute_Types(Texture1D,sa) |
|---|
| 54 | |
|---|
| 55 | if (_image!=rhs._image) |
|---|
| 56 | { |
|---|
| 57 | if (_image.valid()) |
|---|
| 58 | { |
|---|
| 59 | if (rhs._image.valid()) |
|---|
| 60 | { |
|---|
| 61 | int result = _image->compare(*rhs._image); |
|---|
| 62 | if (result!=0) return result; |
|---|
| 63 | } |
|---|
| 64 | else |
|---|
| 65 | { |
|---|
| 66 | return 1; |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | else if (rhs._image.valid()) |
|---|
| 70 | { |
|---|
| 71 | return -1; |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | if (!_image && !rhs._image) |
|---|
| 76 | { |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | int result = compareTextureObjects(rhs); |
|---|
| 83 | if (result!=0) return result; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | int result = compareTexture(rhs); |
|---|
| 87 | if (result!=0) return result; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | COMPARE_StateAttribute_Parameter(_textureWidth) |
|---|
| 91 | COMPARE_StateAttribute_Parameter(_subloadCallback) |
|---|
| 92 | |
|---|
| 93 | return 0; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | void Texture1D::setImage(Image* image) |
|---|
| 97 | { |
|---|
| 98 | if (_image == image) return; |
|---|
| 99 | |
|---|
| 100 | if (dynamic_cast<osg::ImageSequence*>(_image.get())) |
|---|
| 101 | { |
|---|
| 102 | setUpdateCallback(0); |
|---|
| 103 | setDataVariance(osg::Object::STATIC); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | dirtyTextureObject(); |
|---|
| 108 | |
|---|
| 109 | _image = image; |
|---|
| 110 | _modifiedCount.setAllElementsTo(0); |
|---|
| 111 | |
|---|
| 112 | if (dynamic_cast<osg::ImageSequence*>(_image.get())) |
|---|
| 113 | { |
|---|
| 114 | setUpdateCallback(new ImageSequence::UpdateCallback()); |
|---|
| 115 | setDataVariance(osg::Object::DYNAMIC); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | void Texture1D::apply(State& state) const |
|---|
| 121 | { |
|---|
| 122 | #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | const unsigned int contextID = state.getContextID(); |
|---|
| 126 | |
|---|
| 127 | Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get(); |
|---|
| 128 | ElapsedTime elapsedTime(&(tom->getApplyTime())); |
|---|
| 129 | tom->getNumberApplied()++; |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 133 | |
|---|
| 134 | if (textureObject != 0) |
|---|
| 135 | { |
|---|
| 136 | textureObject->bind(); |
|---|
| 137 | |
|---|
| 138 | if (getTextureParameterDirty(state.getContextID())) applyTexParameters(GL_TEXTURE_1D,state); |
|---|
| 139 | |
|---|
| 140 | if (_subloadCallback.valid()) |
|---|
| 141 | { |
|---|
| 142 | _subloadCallback->subload(*this,state); |
|---|
| 143 | } |
|---|
| 144 | else if (_image.valid() && getModifiedCount(contextID) != _image->getModifiedCount()) |
|---|
| 145 | { |
|---|
| 146 | applyTexImage1D(GL_TEXTURE_1D,_image.get(),state, _textureWidth, _numMipmapLevels); |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | getModifiedCount(contextID) = _image->getModifiedCount(); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | } |
|---|
| 153 | else if (_subloadCallback.valid()) |
|---|
| 154 | { |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID, GL_TEXTURE_1D); |
|---|
| 158 | |
|---|
| 159 | textureObject->bind(); |
|---|
| 160 | |
|---|
| 161 | applyTexParameters(GL_TEXTURE_1D,state); |
|---|
| 162 | |
|---|
| 163 | _subloadCallback->load(*this,state); |
|---|
| 164 | |
|---|
| 165 | textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,1,1,0); |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | } |
|---|
| 174 | else if (_image.valid() && _image->data()) |
|---|
| 175 | { |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | textureObject = generateTextureObject(this, contextID,GL_TEXTURE_1D); |
|---|
| 179 | |
|---|
| 180 | textureObject->bind(); |
|---|
| 181 | |
|---|
| 182 | applyTexParameters(GL_TEXTURE_1D,state); |
|---|
| 183 | |
|---|
| 184 | applyTexImage1D(GL_TEXTURE_1D,_image.get(),state, _textureWidth, _numMipmapLevels); |
|---|
| 185 | |
|---|
| 186 | textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,1,1,0); |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | getModifiedCount(contextID) = _image->getModifiedCount(); |
|---|
| 190 | |
|---|
| 191 | _textureObjectBuffer[contextID] = textureObject; |
|---|
| 192 | |
|---|
| 193 | if (state.getMaxTexturePoolSize()==0 && _unrefImageDataAfterApply && areAllTextureObjectsLoaded() && _image->getDataVariance()==STATIC) |
|---|
| 194 | { |
|---|
| 195 | Texture1D* non_const_this = const_cast<Texture1D*>(this); |
|---|
| 196 | non_const_this->_image = 0; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | } |
|---|
| 200 | else if ( (_textureWidth!=0) && (_internalFormat!=0) ) |
|---|
| 201 | { |
|---|
| 202 | _textureObjectBuffer[contextID] = textureObject = generateTextureObject( |
|---|
| 203 | this,contextID,GL_TEXTURE_1D,_numMipmapLevels,_internalFormat,_textureWidth,1,1,0); |
|---|
| 204 | |
|---|
| 205 | textureObject->bind(); |
|---|
| 206 | |
|---|
| 207 | applyTexParameters(GL_TEXTURE_1D,state); |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | glTexImage1D( GL_TEXTURE_1D, 0, _internalFormat, |
|---|
| 211 | _textureWidth, _borderWidth, |
|---|
| 212 | _sourceFormat ? _sourceFormat : _internalFormat, |
|---|
| 213 | _sourceType ? _sourceType : GL_UNSIGNED_BYTE, |
|---|
| 214 | 0); |
|---|
| 215 | |
|---|
| 216 | if (_readPBuffer.valid()) |
|---|
| 217 | { |
|---|
| 218 | _readPBuffer->bindPBufferToTexture(GL_FRONT); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | } |
|---|
| 222 | else |
|---|
| 223 | { |
|---|
| 224 | glBindTexture( GL_TEXTURE_1D, 0 ); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | if (textureObject != 0 && _texMipmapGenerationDirtyList[contextID]) |
|---|
| 229 | { |
|---|
| 230 | generateMipmap(state); |
|---|
| 231 | } |
|---|
| 232 | #else |
|---|
| 233 | osg::notify(osg::NOTICE)<<"Warning: Texture1D::apply(State& state) not supported."<<std::endl; |
|---|
| 234 | #endif |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | void Texture1D::computeInternalFormat() const |
|---|
| 238 | { |
|---|
| 239 | if (_image.valid()) computeInternalFormatWithImage(*_image); |
|---|
| 240 | else computeInternalFormatType(); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | void Texture1D::applyTexImage1D(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& numMipmapLevels) const |
|---|
| 244 | { |
|---|
| 245 | #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) |
|---|
| 246 | |
|---|
| 247 | if (!image || !image->data()) |
|---|
| 248 | return; |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | const unsigned int contextID = state.getContextID(); |
|---|
| 253 | const Extensions* extensions = getExtensions(contextID,true); |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | computeInternalFormat(); |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | bool compressed = isCompressedInternalFormat(_internalFormat); |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | if( _resizeNonPowerOfTwoHint || !extensions->isNonPowerOfTwoTextureSupported(_min_filter) || inwidth > extensions->maxTextureSize() ) |
|---|
| 264 | { |
|---|
| 265 | |
|---|
| 266 | image->ensureValidSizeForTexturing(extensions->maxTextureSize()); |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | glPixelStorei(GL_UNPACK_ALIGNMENT,image->getPacking()); |
|---|
| 270 | |
|---|
| 271 | static MyCompressedTexImage1DArbProc glCompressedTexImage1D_ptr = |
|---|
| 272 | convertPointerType<MyCompressedTexImage1DArbProc, void*>(getGLExtensionFuncPtr("glCompressedTexImage1DARB")); |
|---|
| 273 | |
|---|
| 274 | if( _min_filter == LINEAR || _min_filter == NEAREST ) |
|---|
| 275 | { |
|---|
| 276 | if ( !compressed ) |
|---|
| 277 | { |
|---|
| 278 | numMipmapLevels = 1; |
|---|
| 279 | glTexImage1D( target, 0, _internalFormat, |
|---|
| 280 | image->s(), _borderWidth, |
|---|
| 281 | (GLenum)image->getPixelFormat(), |
|---|
| 282 | (GLenum)image->getDataType(), |
|---|
| 283 | image->data() ); |
|---|
| 284 | |
|---|
| 285 | } |
|---|
| 286 | else if(glCompressedTexImage1D_ptr) |
|---|
| 287 | { |
|---|
| 288 | numMipmapLevels = 1; |
|---|
| 289 | GLint blockSize = ( _internalFormat == GL_COMPRESSED_RGB_S3TC_DXT1_EXT ? 8 : 16 ); |
|---|
| 290 | GLint size = ((image->s()+3)/4)*((image->t()+3)/4)*blockSize; |
|---|
| 291 | glCompressedTexImage1D_ptr(target, 0, _internalFormat, |
|---|
| 292 | image->s(), _borderWidth, |
|---|
| 293 | size, |
|---|
| 294 | image->data()); |
|---|
| 295 | |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | } |
|---|
| 299 | else |
|---|
| 300 | { |
|---|
| 301 | if(!image->isMipmap()) |
|---|
| 302 | { |
|---|
| 303 | |
|---|
| 304 | numMipmapLevels = 1; |
|---|
| 305 | |
|---|
| 306 | #ifdef OSG_GLU_AVAILABLE |
|---|
| 307 | gluBuild1DMipmaps( target, _internalFormat, |
|---|
| 308 | image->s(), |
|---|
| 309 | (GLenum)image->getPixelFormat(), (GLenum)image->getDataType(), |
|---|
| 310 | image->data() ); |
|---|
| 311 | #else |
|---|
| 312 | osg::notify(osg::NOTICE)<<"Warning: gluBuild1DMipmaps(..) not supported."<<std::endl; |
|---|
| 313 | #endif |
|---|
| 314 | |
|---|
| 315 | } |
|---|
| 316 | else |
|---|
| 317 | { |
|---|
| 318 | numMipmapLevels = image->getNumMipmapLevels(); |
|---|
| 319 | |
|---|
| 320 | int width = image->s(); |
|---|
| 321 | |
|---|
| 322 | if( !compressed ) |
|---|
| 323 | { |
|---|
| 324 | for( GLsizei k = 0 ; k < numMipmapLevels && width ;k++) |
|---|
| 325 | { |
|---|
| 326 | |
|---|
| 327 | glTexImage1D( target, k, _internalFormat, |
|---|
| 328 | width,_borderWidth, |
|---|
| 329 | (GLenum)image->getPixelFormat(), |
|---|
| 330 | (GLenum)image->getDataType(), |
|---|
| 331 | image->getMipmapData(k)); |
|---|
| 332 | |
|---|
| 333 | width >>= 1; |
|---|
| 334 | } |
|---|
| 335 | } |
|---|
| 336 | else if(glCompressedTexImage1D_ptr) |
|---|
| 337 | { |
|---|
| 338 | GLint blockSize = ( _internalFormat == GL_COMPRESSED_RGB_S3TC_DXT1_EXT ? 8 : 16 ); |
|---|
| 339 | GLint size = 0; |
|---|
| 340 | for( GLsizei k = 0 ; k < numMipmapLevels && width ;k++) |
|---|
| 341 | { |
|---|
| 342 | |
|---|
| 343 | size = ((width+3)/4)*blockSize; |
|---|
| 344 | glCompressedTexImage1D_ptr(target, k, _internalFormat, |
|---|
| 345 | width, _borderWidth, size, image->getMipmapData(k)); |
|---|
| 346 | |
|---|
| 347 | width >>= 1; |
|---|
| 348 | } |
|---|
| 349 | } |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | inwidth = image->s(); |
|---|
| 355 | #else |
|---|
| 356 | osg::notify(osg::NOTICE)<<"Warning: Texture1D::applyTexImage1D(State& state) not supported."<<std::endl; |
|---|
| 357 | #endif |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | void Texture1D::copyTexImage1D(State& state, int x, int y, int width) |
|---|
| 361 | { |
|---|
| 362 | #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) |
|---|
| 363 | const unsigned int contextID = state.getContextID(); |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 367 | |
|---|
| 368 | if (textureObject != 0) |
|---|
| 369 | { |
|---|
| 370 | if (width==(int)_textureWidth) |
|---|
| 371 | { |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | copyTexSubImage1D(state,0 ,x, y, width); |
|---|
| 377 | return; |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | dirtyTextureObject(); |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | _image = NULL; |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | _min_filter = LINEAR; |
|---|
| 395 | _mag_filter = LINEAR; |
|---|
| 396 | |
|---|
| 397 | _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_1D); |
|---|
| 398 | |
|---|
| 399 | textureObject->bind(); |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | applyTexParameters(GL_TEXTURE_1D,state); |
|---|
| 403 | glCopyTexImage1D( GL_TEXTURE_1D, 0, GL_RGBA, x, y, width, 0 ); |
|---|
| 404 | |
|---|
| 405 | _textureWidth = width; |
|---|
| 406 | _numMipmapLevels = 1; |
|---|
| 407 | |
|---|
| 408 | textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,1,1,0); |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this); |
|---|
| 412 | #else |
|---|
| 413 | osg::notify(osg::NOTICE)<<"Warning: Texture1D::copyTexImage1D(..) not supported."<<std::endl; |
|---|
| 414 | #endif |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | void Texture1D::copyTexSubImage1D(State& state, int xoffset, int x, int y, int width) |
|---|
| 418 | { |
|---|
| 419 | #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) |
|---|
| 420 | const unsigned int contextID = state.getContextID(); |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 424 | |
|---|
| 425 | if (textureObject != 0) |
|---|
| 426 | { |
|---|
| 427 | |
|---|
| 428 | textureObject->bind(); |
|---|
| 429 | |
|---|
| 430 | |
|---|
| 431 | applyTexParameters(GL_TEXTURE_1D,state); |
|---|
| 432 | glCopyTexSubImage1D( GL_TEXTURE_1D, 0, xoffset, x, y, width); |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this); |
|---|
| 436 | |
|---|
| 437 | } |
|---|
| 438 | else |
|---|
| 439 | { |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | copyTexImage1D(state,x,y,width); |
|---|
| 443 | } |
|---|
| 444 | #else |
|---|
| 445 | osg::notify(osg::NOTICE)<<"Warning: Texture1D::copyTexSubImage1D(..) not supported."<<std::endl; |
|---|
| 446 | #endif |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | void Texture1D::allocateMipmap(State& state) const |
|---|
| 450 | { |
|---|
| 451 | #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) |
|---|
| 452 | const unsigned int contextID = state.getContextID(); |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | TextureObject* textureObject = getTextureObject(contextID); |
|---|
| 456 | |
|---|
| 457 | if (textureObject && _textureWidth != 0) |
|---|
| 458 | { |
|---|
| 459 | |
|---|
| 460 | textureObject->bind(); |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | int width = _textureWidth; |
|---|
| 464 | int numMipmapLevels = Image::computeNumberOfMipmapLevels(width); |
|---|
| 465 | |
|---|
| 466 | |
|---|
| 467 | width >>= 1; |
|---|
| 468 | |
|---|
| 469 | for( GLsizei k = 1; k < numMipmapLevels && width; k++) |
|---|
| 470 | { |
|---|
| 471 | if (width == 0) |
|---|
| 472 | width = 1; |
|---|
| 473 | |
|---|
| 474 | glTexImage1D( GL_TEXTURE_1D, k, _internalFormat, |
|---|
| 475 | width, _borderWidth, |
|---|
| 476 | _sourceFormat ? _sourceFormat : _internalFormat, |
|---|
| 477 | _sourceType ? _sourceType : GL_UNSIGNED_BYTE, NULL); |
|---|
| 478 | |
|---|
| 479 | width >>= 1; |
|---|
| 480 | } |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this); |
|---|
| 484 | } |
|---|
| 485 | #else |
|---|
| 486 | osg::notify(osg::NOTICE)<<"Warning: Texture1D::allocateMipmap(..) not supported."<<std::endl; |
|---|
| 487 | #endif |
|---|
| 488 | } |
|---|