Oolite
Loading...
Searching...
No Matches
OOTextureLoader(OOPrivate) Category Reference

Instance Methods

(void) - applySettings
(void) - getDesiredWidth:andHeight:

Class Methods

(void) + setUp

Detailed Description

Definition at line 59 of file OOTextureLoader.m.

Method Documentation

◆ applySettings

- (void) applySettings

Extends class OOTextureLoader.

Definition at line 347 of file OOTextureLoader.m.

348{
349 OOPixMapDimension desiredWidth, desiredHeight;
350 BOOL rescale;
351 size_t newSize;
352 uint8_t components;
353 OOPixMap pixMap;
354
355 components = OOTextureComponentsForFormat(_format);
356
357 // Apply defaults.
358 if (_originalWidth == 0)
359 _originalWidth = _width;
360 if (_originalHeight == 0)
361 _originalHeight = _height;
362 if (_rowBytes == 0)
363 _rowBytes = _width * components;
364
365 pixMap = OOMakePixMap(_data, _width, _height, components, _rowBytes, 0);
366
367 if (_extractChannel) {
368 if (OOExtractPixMapChannel(&pixMap, _extractChannelIndex, NO)) {
369 _format = kOOTextureDataGrayscale;
370 components = 1;
371 } else {
372 OOLogWARN(@"texture.load.extractChannel.invalid", @"Cannot extract channel from texture \"%@\"", [_path lastPathComponent]);
373 }
374 }
375
376 [self getDesiredWidth:&desiredWidth andHeight:&desiredHeight];
377
378 if (_isCubeMap && !OOCubeMapsAvailable()) {
379 OOPixMapToRGBA(&pixMap);
380 desiredHeight = MIN(desiredWidth * 2, 512U);
381 if (sReducedDetail && desiredHeight > kCubeShrinkThreshold)
382 desiredHeight /= 2;
383 desiredWidth = desiredHeight * 2;
384
385 OOPixMap converted = OOConvertCubeMapToLatLong(pixMap, desiredHeight, _generateMipMaps);
386 OOFreePixMap(&pixMap);
387 pixMap = converted;
388 _isCubeMap = NO;
389
390#if DUMP_CONVERTED_CUBE_MAPS
391 OODumpPixMap(pixMap, [NSString stringWithFormat:@"converted cube map %@", [[_path lastPathComponent] stringByDeletingPathExtension]]);
392#endif
393 }
394
395 // Rescale if needed.
396 rescale = (_width != desiredWidth || _height != desiredHeight);
397 if (rescale) {
398 BOOL leaveSpaceForMipMaps = _generateMipMaps;
399#if OO_TEXTURE_CUBE_MAP
400 if (_isCubeMap)
401 leaveSpaceForMipMaps = NO;
402#endif
403
404 OOLog(@"texture.load.rescale", @"Rescaling texture \"%@\" from %u x %u to %u x %u.", [_path lastPathComponent], pixMap.width, pixMap.height, desiredWidth, desiredHeight);
405
406 pixMap = OOScalePixMap(pixMap, desiredWidth, desiredHeight, leaveSpaceForMipMaps);
407 if (EXPECT_NOT(!OOIsValidPixMap(pixMap)))
408 return;
409
410 _data = pixMap.pixels;
411 _width = pixMap.width;
412 _height = pixMap.height;
413 _rowBytes = pixMap.rowBytes;
414 }
415
416#if OO_TEXTURE_CUBE_MAP
417 if (_isCubeMap) {
418 if (_generateMipMaps) {
419 [self generateMipMapsForCubeMap];
420 }
421 return;
422 }
423#endif
424
425 // Generate mip maps if needed.
426 if (_generateMipMaps) {
427 // Make space if needed.
428 newSize = desiredWidth * components * desiredHeight;
429 newSize = (newSize * 4) / 3;
430 // +1 to fix overrun valgrind spotted - CIM
431 _generateMipMaps = OOExpandPixMap(&pixMap, newSize + 1);
432
433 _data = pixMap.pixels;
434 _width = pixMap.width;
435 _height = pixMap.height;
436 _rowBytes = pixMap.rowBytes;
437 }
438 if (_generateMipMaps) {
439 OOGenerateMipMaps(_data, _width, _height, _format);
440 }
441
442 // All done.
443}
OOPixMap OOConvertCubeMapToLatLong(OOPixMap sourcePixMap, OOPixMapDimension height, BOOL leaveSpaceForMipMaps)
#define EXPECT_NOT(x)
#define OOLogWARN(class, format,...)
Definition OOLogging.h:126
#define OOLog(class, format,...)
Definition OOLogging.h:84
#define MIN(A, B)
Definition OOMaths.h:91
BOOL OOExtractPixMapChannel(OOPixMap *ioPixMap, uint8_t channelIndex, BOOL compactWhenDone)
BOOL OOPixMapToRGBA(OOPixMap *ioPixMap)
void OOFreePixMap(OOPixMap *ioPixMap)
Definition OOPixMap.m:80
uint32_t OOPixMapDimension
Definition OOPixMap.h:32
void OODumpPixMap(OOPixMap pixMap, NSString *name)
Definition OOPixMap.m:140
OOPixMap OOMakePixMap(void *pixels, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
Definition OOPixMap.m:44
BOOL OOIsValidPixMap(OOPixMap pixMap)
Definition OOPixMap.m:39
BOOL OOExpandPixMap(OOPixMap *ioPixMap, size_t desiredSize)
Definition OOPixMap.m:125
struct OOPixMap OOPixMap
@ kCubeShrinkThreshold
static BOOL sReducedDetail
OOPixMap OOScalePixMap(OOPixMap srcPixMap, OOPixMapDimension dstWidth, OOPixMapDimension dstHeight, BOOL leaveSpaceForMipMaps)
BOOL OOGenerateMipMaps(void *textureBytes, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format)
BOOL OOCubeMapsAvailable(void)
Definition OOTexture.m:643
uint8_t OOTextureComponentsForFormat(OOTextureDataFormat format)
Definition OOTexture.m:624
@ kOOTextureDataGrayscale
Definition OOTexture.h:104
OOPixMapDimension height
Definition OOPixMap.h:45
size_t rowBytes
Definition OOPixMap.h:47
void * pixels
Definition OOPixMap.h:44
OOPixMapDimension width
Definition OOPixMap.h:45

References applySettings, EXPECT_NOT, OOTextureLoader::generateMipMapsForCubeMap, OOTextureLoader::getDesiredWidth:andHeight:, OOPixMap::height, kCubeShrinkThreshold, kOOTextureDataGrayscale, MIN, OOConvertCubeMapToLatLong(), OOCubeMapsAvailable(), OODumpPixMap(), OOExpandPixMap(), OOExtractPixMapChannel(), OOFreePixMap(), OOGenerateMipMaps(), OOIsValidPixMap(), OOLog, OOLogWARN, OOMakePixMap(), OOPixMapToRGBA(), OOScalePixMap(), OOTextureComponentsForFormat(), OOPixMap::pixels, OOPixMap::rowBytes, sReducedDetail, and OOPixMap::width.

Referenced by applySettings, OOTextureLoader::performAsyncTask, and setUp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getDesiredWidth:andHeight:

- (void) getDesiredWidth: (OOPixMapDimension *) outDesiredWidth
andHeight: (OOPixMapDimension *) outDesiredHeight 

Extends class OOTextureLoader.

Definition at line 445 of file OOTextureLoader.m.

445 :(OOPixMapDimension*)outDesiredWidth andHeight:(OOPixMapDimension*)outDesiredHeight
446{
447 OOPixMapDimension desiredWidth, desiredHeight;
448
449 // Work out appropriate final size for textures.
450 if (!_noScalingWhatsoever) {
451 // Cube maps are six times as high as they are wide, and we need to preserve that.
452 if (_allowCubeMap && _height == _width * 6) {
453 _isCubeMap = YES;
454
455 desiredWidth = OORoundUpToPowerOf2_PixMap((2 * _width) / 3);
456 desiredWidth = MIN(desiredWidth, sGLMaxSize / 8);
457 if (sReducedDetail) {
458 if (256 < desiredWidth)
459 desiredWidth /= 2;
460 }
461 desiredWidth = MIN(desiredWidth, sUserMaxSize / 4);
462
463 desiredHeight = desiredWidth * 6;
464 } else {
465 if (!sHaveNPOTTextures) {
466 // Round to nearest power of two. NOTE: this is duplicated in OOTextureVerifierStage.m.
467 desiredWidth = OORoundUpToPowerOf2_PixMap((2 * _width) / 3);
468 desiredHeight = OORoundUpToPowerOf2_PixMap((2 * _height) / 3);
469 } else {
470 desiredWidth = _width;
471 desiredHeight = _height;
472 }
473
474 desiredWidth = MIN(desiredWidth, sGLMaxSize);
475 desiredHeight = MIN(desiredHeight, sGLMaxSize);
476
477 if (!_avoidShrinking) {
478 if (sReducedDetail) {
479 if (_shrinkThreshold < desiredWidth)
480 desiredWidth /= 2;
481 if (_shrinkThreshold < desiredHeight)
482 desiredHeight /= 2;
483 }
484
485 desiredWidth = MIN(desiredWidth, _maxSize);
486 desiredHeight = MIN(desiredHeight, _maxSize);
487 }
488 }
489 } else {
490 desiredWidth = _width;
491 desiredHeight = _height;
492 }
493
494 if (outDesiredWidth != NULL)
495 *outDesiredWidth = desiredWidth;
496 if (outDesiredHeight != NULL)
497 *outDesiredHeight = desiredHeight;
498}
#define OORoundUpToPowerOf2_PixMap
Definition OOPixMap.h:34
static unsigned sGLMaxSize
static uint32_t sUserMaxSize
static BOOL sHaveNPOTTextures

References MIN, OORoundUpToPowerOf2_PixMap, sGLMaxSize, sHaveNPOTTextures, sReducedDetail, and sUserMaxSize.

Referenced by applySettings.

Here is the caller graph for this function:

◆ setUp

+ (void) setUp

Extends class OOTextureLoader.

References applySettings, and setUp.

Referenced by setUp.

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this category was generated from the following file: