31#define DEBUG_DUMP (0 && OOLITE_DEBUG)
32#define DEBUG_DUMP_RAW (1 && DEBUG_DUMP)
35#define ALBEDO_FACTOR 0.7f
41#ifndef TEXGEN_TEST_RIG
56 void** x_ = (void**)&(x); \
61#define PLANET_TEXTURE_OPTIONS (kOOTextureMinFilterLinear | kOOTextureMagFilterLinear | kOOTextureRepeatS | kOOTextureNoShrink)
64 kRandomBufferSize = 128
79- (id)initWithCacheKey:(NSString*)cacheKey seed:(
RANROTSeed)seed;
81- (void)completeWithData:(
void*)data width:(
unsigned)width height:(
unsigned)height;
93 OOPlanetTextureGenerator* _parent;
98- (void)completeWithData:(
void*)data width:(
unsigned)width height:(
unsigned)height;
104@interface OOPlanetTextureGenerator (Private)
106- (NSString*)cacheKeyForType:(NSString*)type;
107- (OOPlanetNormalMapGenerator*)normalMapGenerator;
108- (OOPlanetAtmosphereGenerator*)atmosphereGenerator;
111- (void)dumpNoiseBuffer:(
float*)noise;
120static float QFactor(
float* accbuffer,
int x,
int y,
unsigned width,
float polar_y_value,
float bias,
float polar_y);
121static float GetQ(
float* qbuffer,
int x,
int y,
unsigned width,
unsigned height,
unsigned widthMask,
unsigned heightMask);
123static FloatRGB
Blend(
float fraction, FloatRGB a, FloatRGB b);
124static float BlendAlpha(
float fraction,
float a,
float b);
130 kPlanetScale256x256 = 1,
132 kPlanetScale1024x1024,
133 kPlanetScale2048x2048,
134 kPlanetScale4096x4096,
136 kPlanetScaleReducedDetail = kPlanetScale512x512,
137 kPlanetScaleFullDetail = kPlanetScale1024x1024,
138 kPlanetScaleExtraDetail = kPlanetScale2048x2048
141@implementation OOPlanetTextureGenerator
143- (id)initWithPlanetInfo:(NSDictionary*)planetInfo
145 OOLog(
@"texture.planet.generate",
@"%@",
@"Initialising planetary generator");
148 if ((
self = [super initWithPath:[NSString stringWithFormat:
@"OOPlanetTexture@%p",
self] options:
kOOTextureAllowCubeMap])) {
149 OOLog(
@"texture.planet.generate",
@"Extracting parameters for generator %@",
self);
151 _info.landFraction =
OOClamp_0_1_f([planetInfo oo_floatForKey:
@"land_fraction" defaultValue:0.3]);
152 _info.polarFraction =
OOClamp_0_1_f([planetInfo oo_floatForKey:
@"polar_fraction" defaultValue:0.05]);
157 [[planetInfo objectForKey:@"noise_map_seed"] getValue:&_info.seed];
158 if ([planetInfo objectForKey:
@"cloud_alpha"]) {
159 OOLog(
@"texture.planet.generate",
@"%@",
@"Extracting atmosphere parameters");
161 _info.cloudAlpha = [planetInfo oo_floatForKey:@"cloud_alpha" defaultValue:1.0f];
162 _info.cloudFraction =
OOClamp_0_1_f([planetInfo oo_floatForKey:
@"cloud_fraction" defaultValue:0.3]);
169#ifndef TEXGEN_TEST_RIG
170 if (detailLevel <
DETAIL_LEVEL_SHADERS || [planetInfo oo_boolForKey:
@"isMiniature" defaultValue:NO]) {
171 _planetScale = kPlanetScaleReducedDetail;
173 _planetScale = kPlanetScaleFullDetail;
175 _planetScale = kPlanetScaleExtraDetail;
178 _planetScale = kPlanetScale4096x4096;
180 _info.perlin3d = [planetInfo oo_boolForKey:@"perlin_3d" defaultValue:detailLevel > DETAIL_LEVEL_SHADERS];
181 _info.planetAspectRatio = _info.perlin3d ? 2 : 1;
182 _info.planetScaleOffset = 8 - _info.planetAspectRatio;
188+ (OOTexture*)planetTextureWithInfo:(NSDictionary*)planetInfo
190 OOTexture* result =
nil;
191 OOPlanetTextureGenerator* generator = [[
self alloc] initWithPlanetInfo:planetInfo];
192 if (generator !=
nil) {
200+ (BOOL)generatePlanetTexture:(OOTexture**)texture andAtmosphere:(OOTexture**)atmosphere withInfo:(NSDictionary*)planetInfo
202 NSParameterAssert(texture != NULL);
204 OOPlanetTextureGenerator* diffuseGen = [[[
self alloc] initWithPlanetInfo:planetInfo] autorelease];
205 if (diffuseGen ==
nil)
208 OOPlanetAtmosphereGenerator* atmoGen = [diffuseGen atmosphereGenerator];
213 if (*atmosphere ==
nil)
218 return *texture !=
nil;
221+ (BOOL)generatePlanetTexture:(OOTexture**)texture secondaryTexture:(OOTexture**)secondaryTexture withInfo:(NSDictionary*)planetInfo
223 NSParameterAssert(texture != NULL);
227 OOPlanetTextureGenerator* diffuseGen = [[[
self alloc] initWithPlanetInfo:planetInfo] autorelease];
228 if (diffuseGen ==
nil)
231 if (secondaryTexture != NULL) {
232 OOPlanetNormalMapGenerator* normalGen = [diffuseGen normalMapGenerator];
233 if (normalGen ==
nil)
237 if (*secondaryTexture ==
nil)
239 enqueue = [normalGen enqueued];
244 return *texture !=
nil;
247+ (BOOL)generatePlanetTexture:(OOTexture**)texture secondaryTexture:(OOTexture**)secondaryTexture andAtmosphere:(OOTexture**)atmosphere withInfo:(NSDictionary*)planetInfo
249 NSParameterAssert(texture != NULL);
253 OOPlanetTextureGenerator* diffuseGen = [[[
self alloc] initWithPlanetInfo:planetInfo] autorelease];
254 if (diffuseGen ==
nil)
257 if (secondaryTexture != NULL) {
258 OOPlanetNormalMapGenerator* normalGen = [diffuseGen normalMapGenerator];
259 if (normalGen ==
nil)
263 if (*secondaryTexture ==
nil)
265 enqueue = [normalGen enqueued];
268 OOPlanetAtmosphereGenerator* atmoGen = [diffuseGen atmosphereGenerator];
273 if (*atmosphere ==
nil) {
274 if (secondaryTexture != NULL) {
275 *secondaryTexture =
nil;
279 enqueue = enqueue || [atmoGen enqueued];
281 OOLog(
@"texture.planet.generate",
@"Generator %@ has atmosphere %@", diffuseGen, *atmosphere);
284 return *texture !=
nil;
295- (NSString*)descriptionComponents
297 return [NSString stringWithFormat:@"seed: %u,%u land: %g", _info.seed.high, _info.seed.low, _info.landFraction];
300- (uint32_t)textureOptions
302 return PLANET_TEXTURE_OPTIONS;
307 NSString* type = (_nMapGenerator ==
nil) ?
@"diffuse-baked" :
@"diffuse-raw";
308 if (_atmoGenerator !=
nil)
309 type = [NSString stringWithFormat:@"%@-atmo", type];
310 return [
self cacheKeyForType:type];
313- (NSString*)cacheKeyForType:(NSString*)type
315 return [NSString stringWithFormat:@"OOPlanetTextureGenerator-%@@%u\n%u,%u/%g/%u,%u/%f,%f,%f/%f,%f,%f/%f,%f,%f/%f,%f,%f",
317 _info.width, _info.height, _info.landFraction, _info.seed.high, _info.seed.low,
318 _info.landColor.r, _info.landColor.g, _info.landColor.b,
319 _info.seaColor.r, _info.seaColor.g, _info.seaColor.b,
320 _info.paleLandColor.r, _info.paleLandColor.g, _info.paleLandColor.b,
321 _info.polarSeaColor.r, _info.polarSeaColor.g, _info.polarSeaColor.b];
324- (OOPlanetNormalMapGenerator*)normalMapGenerator
326 if (_nMapGenerator ==
nil) {
327 _nMapGenerator = [[OOPlanetNormalMapGenerator alloc] initWithCacheKey:[
self cacheKeyForType:@"normal"] seed:_info.seed];
329 return _nMapGenerator;
332- (OOPlanetAtmosphereGenerator*)atmosphereGenerator
334 if (_atmoGenerator ==
nil) {
335 _atmoGenerator = [[OOPlanetAtmosphereGenerator alloc] initWithCacheKey:[
self cacheKeyForType:@"atmo"] seed:_info.seed andParent:
self];
337 return _atmoGenerator;
342 width:(uint32_t*)outWidth
343 height:(uint32_t*)outHeight
346 if (![
self isReady]) {
348 OOLog(
@"texture.planet.generate.wait",
@"%s generator %@",
"Waiting for",
self);
351 BOOL result = [
super getResult:outData format:outFormat originalWidth:outWidth originalHeight:outHeight];
354 OOLog(
@"texture.planet.generate.dequeue",
@"%s generator %@", result ?
"Dequeued" :
"Failed to dequeue",
self);
356 OOLog(
@"texture.planet.generate.dequeue",
@"%s generator %@ without waiting.", result ?
"Dequeued" :
"Failed to dequeue",
self);
364 OOLog(
@"texture.planet.generate.begin",
@"Started generator %@",
self);
367 BOOL generateNormalMap = (_nMapGenerator !=
nil);
368 BOOL generateAtmosphere = (_atmoGenerator !=
nil);
370 uint8_t *buffer = NULL, *px = NULL;
371 uint8_t *nBuffer = NULL, *npx = NULL;
372 uint8_t *aBuffer = NULL, *apx = NULL;
373 float* randomBuffer = NULL;
375 _height = _info.height = 1 << (_planetScale + _info.planetScaleOffset);
376 _width = _info.width = _height * _info.planetAspectRatio;
378#define FAIL_IF(cond) \
380 if (EXPECT_NOT(cond)) \
383#define FAIL_IF_NULL(x) FAIL_IF((x) == NULL)
385 buffer = malloc(4 * _width * _height);
386 FAIL_IF_NULL(buffer);
389 if (generateNormalMap) {
390 nBuffer = malloc(4 * _width * _height);
391 FAIL_IF_NULL(nBuffer);
395 if (generateAtmosphere) {
396 aBuffer = malloc(4 * _width * _height);
397 FAIL_IF_NULL(aBuffer);
401 FAIL_IF(!FillFBMBuffer(&_info));
403 [
self dumpNoiseBuffer:_info.fbmBuffer];
406 float paleClouds = (_info.cloudFraction * _info.fbmBuffer[0] < 1.0f - _info.cloudFraction) ? 0.0f : 1.0f;
407 float poleValue = (_info.landFraction > 0.5f) ? 0.5f * _info.landFraction : 0.0f;
408 float seaBias = _info.landFraction - 1.0f;
410 _info.paleSeaColor =
Blend(0.35f, _info.polarSeaColor,
Blend(0.7f, _info.seaColor, _info.landColor));
411 float normalScale = (1 << _planetScale)
414 * [[NSUserDefaults standardUserDefaults] oo_floatForKey:
@"p3dnsf" defaultValue:1.0f]
417 if (!generateNormalMap)
421 _info.deepSeaColor =
Blend(0.85f, _info.seaColor, (FloatRGB) { 0, 0, 0 });
426 float q, yN, yS, yW, yE, nearPole;
428 float rHeight = 1.0f / _height;
429 float fy, fHeight = _height;
431 SetMixConstants(&_info, 1.0f - _info.polarFraction);
434 _info.qBuffer = malloc(_width * _height *
sizeof(
float));
435 FAIL_IF_NULL(_info.qBuffer);
437 for (
y = (
int)_height - 1, fy = (
float)
y;
y >= 0;
y--, fy--) {
438 nearPole = (2.0f * fy - fHeight) * rHeight;
439 nearPole *= nearPole;
441 for (
x = (
int)_width - 1;
x >= 0;
x--) {
442 _info.qBuffer[y * _width + x] = QFactor(_info.fbmBuffer,
x,
y, _width, poleValue, seaBias, nearPole);
447 float cloudFraction = _info.cloudFraction;
448 unsigned widthMask = _width - 1;
449 unsigned heightMask = _height - 1;
451 for (
y = (
int)_height - 1, fy = (
float)
y;
y >= 0;
y--, fy--) {
452 nearPole = (2.0f * fy - fHeight) * rHeight;
453 nearPole *= nearPole;
455 for (
x = (
int)_width - 1;
x >= 0;
x--) {
456 q = _info.qBuffer[y * _width + x];
457 yN = GetQ(_info.qBuffer,
x,
y - 1, _width, _height, widthMask, heightMask);
458 yS = GetQ(_info.qBuffer,
x,
y + 1, _width, _height, widthMask, heightMask);
459 yW = GetQ(_info.qBuffer,
x - 1,
y, _width, _height, widthMask, heightMask);
460 yE = GetQ(_info.qBuffer,
x + 1,
y, _width, _height, widthMask, heightMask);
462 color = PlanetMix(&_info, q, nearPole);
465 if (generateNormalMap) {
472 *npx++ = 127.5f * (norm.
y + 1.0f);
473 *npx++ = 127.5f * (-norm.
x + 1.0f);
474 *npx++ = 127.5f * (norm.z + 1.0f);
476 *npx++ = 255.0f * color.a;
486 shade += color.a - color.a * shade;
489 *px++ = 255.0f * color.r * shade;
490 *px++ = 255.0f * color.g * shade;
491 *px++ = 255.0f * color.b * shade;
495 if (generateAtmosphere) {
496 q = QFactor(_info.fbmBuffer,
x,
y, _width, paleClouds, cloudFraction, nearPole);
497 color = CloudMix(&_info, q, nearPole);
498 *apx++ = 255.0f * color.r;
499 *apx++ = 255.0f * color.g;
500 *apx++ = 255.0f * color.b;
501 *apx++ = 255.0f * color.a * _info.cloudAlpha;
510 FREE(_info.fbmBuffer);
515 if (generateNormalMap)
516 [_nMapGenerator completeWithData:nBuffer width:_width height:_height];
517 if (generateAtmosphere)
518 [_atmoGenerator completeWithData:aBuffer width:_width height:_height];
527 OOLog(
@"texture.planet.generate.complete",
@"Completed generator %@ %@successfully",
self, success ?
@"" :
@"un");
531 NSString* diffuseName = [NSString stringWithFormat:@"planet-%u-%u-diffuse-new", _info.seed.high, _info.seed.low];
532 NSString* lightsName = [NSString stringWithFormat:@"planet-%u-%u-lights-new", _info.seed.high, _info.seed.low];
534 [[UNIVERSE gameView] dumpRGBAToRGBFileNamed:diffuseName
535 andGrayFileNamed:lightsName
539 rowBytes:_width * 4];
546- (void)dumpNoiseBuffer:(
float*)noise
548 NSString* noiseName = [NSString stringWithFormat:@"planet-%u-%u-noise-new", _info.seed.high, _info.seed.low];
550 uint8_t* noisePx = malloc(_width * _height);
552 for (
y = 0;
y < _height;
y++) {
553 for (
x = 0;
x < _width;
x++) {
554 noisePx[y * _width + x] = 255.0f * noise[y * _width + x];
558 [[UNIVERSE gameView] dumpGrayToFileNamed:noiseName
570OOINLINE float Lerp(
float v0,
float v1,
float fraction)
573 return v0 + fraction * (v1 - v0);
576static FloatRGB
Blend(
float fraction, FloatRGB a, FloatRGB b)
579 Lerp(b.
r, a.
r, fraction),
580 Lerp(b.
g, a.
g, fraction),
581 Lerp(b.
b, a.
b, fraction)
585static float BlendAlpha(
float fraction,
float a,
float b)
587 return Lerp(b, a, fraction);
595#define AIR_ALPHA (0.05f)
596#define CLOUD_ALPHA (2.0f)
598#define POLAR_BOUNDARY (0.33f)
599#define CLOUD_BOUNDARY (0.5f)
600#define RECIP_CLOUD_BOUNDARY (1.0f / CLOUD_BOUNDARY)
603 float alpha = info->
cloudAlpha, portion = 0.0f;
605 q -= CLOUD_BOUNDARY * 0.5f;
607 if (nearPole > POLAR_BOUNDARY) {
608 portion = nearPole > POLAR_BOUNDARY + 0.2f ? 1.0f : (nearPole - POLAR_BOUNDARY) * 5.0f;
611 portion = nearPole > POLAR_BOUNDARY + 0.625f ? 1.0f : (nearPole - POLAR_BOUNDARY) * 1.6f;
615 if (q >= -CLOUD_BOUNDARY) {
616 alpha *= BlendAlpha(-q * 0.5f * RECIP_CLOUD_BOUNDARY + 0.5f,
CLOUD_ALPHA, AIR_ALPHA);
621 if (q < CLOUD_BOUNDARY) {
622 alpha *= BlendAlpha(q * 0.5f * RECIP_CLOUD_BOUNDARY + 0.5f, AIR_ALPHA,
CLOUD_ALPHA);
628 alpha *= BlendAlpha(portion, 0.6f, 1.0f);
633 return (FloatRGBA) { cloudColor.
r, cloudColor.
g, cloudColor.
b, alpha };
638#define RECIP_COASTLINE_PORTION (160.0f)
639#define COASTLINE_PORTION (1.0f / RECIP_COASTLINE_PORTION)
640#define SHALLOWS (2.0f * COASTLINE_PORTION)
641#define RECIP_SHALLOWS (1.0f / SHALLOWS)
643#define DEEPS (40.0f * COASTLINE_PORTION)
644#define RECIP_DEEPS (1.0f / DEEPS)
646 const FloatRGB white = { 1.0f, 1.0f, 1.0f };
650 float specular = 0.01f;
664 specular = Lerp(1.0f, 0.85f, -q);
666 }
else if (q < COASTLINE_PORTION) {
668 specular = q * RECIP_COASTLINE_PORTION;
670 specular = 1.0f - specular;
671 }
else if (q > 1.0f) {
674 }
else if (q > info->
mix_hi) {
683 float phi = info->
mix_polarCap + info->mix_polarCap - nearPole;
687 specular = q > phi + 0.02f ? 1.0f : 0.2f + (q - phi) * 40.0f;
690 specular = specular * 0.5f;
694 return (FloatRGBA) { diffuse.
r, diffuse.
g, diffuse.
b, specular };
699 OOColor* color = [dictionary objectForKey:key];
700 NSCAssert1([color isKindOfClass:[OOColor
class]],
@"Expected OOColor, got %@", [color
class]);
707 return 3.0f * q * q - 2.0f * q * q * q;
718OOINLINE int32_t fast_floor(
double val)
720 val += 68719476736.0 * 1.5;
721 return (((int32_t*)&val)[iman_] >> 16);
729 NSCParameterAssert(info != NULL);
735 GenerateFBMNoise(info);
737 GenerateFBMNoise3D(info);
747 kPermutationCount = 1 << 10,
748 kPermutationMask = kPermutationCount - 1,
757static const Vector kGradients[kGradientCount] = {
772static const uint8_t kGradients[kGradientCount][3] = {
795OOINLINE float TDot3(
const uint8_t grad[3],
float x,
float y,
float z)
797 float xt[3] = { -
x, 0.0f,
x };
798 float yt[3] = { -
y, 0.0f,
y };
799 float zt[3] = { -z, 0.0f, z };
801 return xt[grad[0]] + yt[grad[1]] + zt[grad[2]];
811 float fx = floor(p.
x);
812 float fy = floor(p.
y);
813 float fz = floor(p.
z);
822#define PERM(v) permutations[(v) & kPermutationMask]
824 unsigned PZ0 = PERM(Z);
825 unsigned PZ1 = PERM(Z + 1);
827 unsigned PY0Z0 = PERM(Y + PZ0);
828 unsigned PY1Z0 = PERM(Y + 1 + PZ0);
829 unsigned PY0Z1 = PERM(Y + PZ1);
830 unsigned PY1Z1 = PERM(Y + 1 + PZ1);
832 unsigned gi000 = PERM(X + PY0Z0);
833 unsigned gi010 = PERM(X + PY1Z0);
834 unsigned gi100 = PERM(X + 1 + PY0Z0);
835 unsigned gi110 = PERM(X + 1 + PY1Z0);
836 unsigned gi001 = PERM(X + PY0Z1);
837 unsigned gi011 = PERM(X + PY1Z1);
838 unsigned gi101 = PERM(X + 1 + PY0Z1);
839 unsigned gi111 = PERM(X + 1 + PY1Z1);
845#define DOT3(idx, x_, y_, z_) dot_product(kGradients[(idx) % kGradientCount], (Vector) { (x_), (y_), (z_) })
847#define DOT3(idx, x_, y_, z_) TDot3(kGradients[(idx) % kGradientCount], (x_), (y_), (z_))
853 float n000 = DOT3(gi000,
x,
y, z);
854 float n010 = DOT3(gi010,
x, y1, z);
855 float n100 = DOT3(gi100, x1,
y, z);
856 float n110 = DOT3(gi110, x1, y1, z);
857 float n001 = DOT3(gi001,
x,
y, z1);
858 float n011 = DOT3(gi011,
x, y1, z1);
859 float n101 = DOT3(gi101, x1,
y, z1);
860 float n111 = DOT3(gi111, x1, y1, z1);
865 float u = Hermite(
x);
866 float v = Hermite(
y);
867 float w = Hermite(z);
870 float nx00 = Lerp(n000, n100, u);
871 float nx01 = Lerp(n001, n101, u);
872 float nx10 = Lerp(n010, n110, u);
873 float nx11 = Lerp(n011, n111, u);
875 float nxy0 = Lerp(nx00, nx10, v);
876 float nxy1 = Lerp(nx01, nx11, v);
878 float nxyz = Lerp(nxy0, nxy1, w);
886 uint16_t* perms = malloc(
sizeof *info->
permutations * kPermutationCount);
892 for (n = 1; n < kPermutationCount; n++) {
904 FAIL_IF(!MakePermutationTable(info));
908 float dlon = 2.0f *
M_PI / width;
909 float dlat =
M_PI / height;
912 for (
y = 0, lat = -
M_PI_2;
y < height;
y++, lat += dlat) {
913 float las = sin(lat);
914 float lac = cos(lat);
916 for (
x = 0, lon = -
M_PI;
x < width;
x++, lon += dlon) {
919 float los = sin(lon);
920 float loc = cos(lon);
930 unsigned octaveMask = 4;
931 float octave = octaveMask;
936 while ((octaveMask + 1) < height) {
938 sum += scale * SampleNoise3D(info, ps);
941 octaveMask = (octaveMask << 1) | 1;
947 float sum = 0.5f * SampleNoise3D(info, p);
961static void FillRandomBuffer(
float* randomBuffer,
RANROTSeed seed)
963 unsigned i, len = kRandomBufferSize * kRandomBufferSize;
964 for (i = 0; i < len; i++) {
969static void AddNoise(
OOPlanetTextureGeneratorInfo* info,
float* randomBuffer,
float octave,
unsigned octaveMask,
float scale,
float* qxBuffer,
int* ixBuffer)
972 unsigned width = info->
width, height = info->
height;
974 float rr = octave / width;
975 float fx, fy, qx, qy, rix, rjx, rfinal;
978 for (fy = 0,
y = 0;
y < height; fy++,
y++) {
981 jy = (iy + 1) & octaveMask;
982 qy = Hermite(qy - iy);
983 iy &= (kRandomBufferSize - 1);
984 jy &= (kRandomBufferSize - 1);
986 for (fx = 0,
x = 0;
x < width; fx++,
x++) {
992 ix &= (kRandomBufferSize - 1);
994 qxBuffer[x] = Hermite(qx);
1001 jx = (ix + 1) & octaveMask;
1002 jx &= (kRandomBufferSize - 1);
1004 rix = Lerp(randomBuffer[iy * kRandomBufferSize + ix], randomBuffer[iy * kRandomBufferSize + jx], qx);
1005 rjx = Lerp(randomBuffer[jy * kRandomBufferSize + ix], randomBuffer[jy * kRandomBufferSize + jx], qx);
1006 rfinal = Lerp(rix, rjx, qy);
1008 *dst++ += scale * rfinal;
1016 size_t randomBufferSize = kRandomBufferSize * kRandomBufferSize *
sizeof(float);
1017 size_t qxBufferSize = info->
width *
sizeof(float);
1018 size_t ixBufferSize = info->
width *
sizeof(
int);
1019 char* sharedBuffer = malloc(randomBufferSize + qxBufferSize + ixBufferSize);
1020 if (sharedBuffer == NULL)
1023 float* randomBuffer = (
float*)sharedBuffer;
1024 float* qxBuffer = (
float*)(sharedBuffer + randomBufferSize);
1025 int* ixBuffer = (
int*)(sharedBuffer + randomBufferSize + qxBufferSize);
1028 FillRandomBuffer(randomBuffer, info->
seed);
1031 unsigned height = info->
height;
1033 float octave = octaveMask;
1037 while ((octaveMask + 1) < height) {
1038 AddNoise(info, randomBuffer, octave, octaveMask, scale, qxBuffer, ixBuffer);
1040 octaveMask = (octaveMask << 1) | 1;
1048static float QFactor(
float* accbuffer,
int x,
int y,
unsigned width,
float polar_y_value,
float bias,
float polar_y)
1050 float q = accbuffer[y * width + x];
1054 q = q * (1.0f - polar_y) + polar_y * polar_y_value;
1059static float GetQ(
float* qbuffer,
int x,
int y,
unsigned width,
unsigned height,
unsigned widthMask,
unsigned heightMask)
1070 y = (
y ^ heightMask) & heightMask;
1075 return qbuffer[y * width + x];
1082 info->
mix_ih = 1.0f / (1.0f - info->mix_hi);
1083 info->
mix_polarCap = temperatureFraction * (0.28f + 0.24f * info->landFraction);
1086@implementation OOPlanetNormalMapGenerator
1088- (id)initWithCacheKey:(NSString*)cacheKey seed:(
RANROTSeed)seed
1091 if ((
self = [super initWithPath:[NSString stringWithFormat:
@"OOPlanetNormalTexture@%p",
self] options:
kOOTextureAllowCubeMap])) {
1093 _cacheKey = [cacheKey copy];
1106- (NSString*)cacheKey
1111- (uint32_t)textureOptions
1113 return PLANET_TEXTURE_OPTIONS;
1137- (void)completeWithData:(
void*)data_ width:(
unsigned)width_ height:(
unsigned)height_
1148 NSString* normalName = [NSString stringWithFormat:@"planet-%u-%u-normal-new", _seed.high, _seed.low];
1149 NSString* specularName = [NSString stringWithFormat:@"planet-%u-%u-specular-new", _seed.high, _seed.low];
1151 [[UNIVERSE gameView] dumpRGBAToRGBFileNamed:normalName
1152 andGrayFileNamed:specularName
1156 rowBytes:_width * 4];
1162@implementation OOPlanetAtmosphereGenerator
1164- (id)initWithCacheKey:(NSString*)cacheKey seed:(
RANROTSeed)seed andParent:(OOPlanetTextureGenerator*)parent
1166 OOLog(
@"texture.planet.generate",
@"Initialising atmosphere generator %@", cacheKey);
1168 if ((
self = [super initWithPath:[NSString stringWithFormat:
@"OOPlanetAtmoTexture@%p",
self] options:
kOOTextureAllowCubeMap])) {
1169 _cacheKey = [cacheKey copy];
1171 _parent = [parent retain];
1184- (NSString*)cacheKey
1189- (uint32_t)textureOptions
1191 return PLANET_TEXTURE_OPTIONS;
1221 originalWidth:(uint32_t*)outWidth
1222 originalHeight:(uint32_t*)outHeight
1228 return [
super getResult:result
1230 originalWidth:outWidth
1231 originalHeight:outHeight];
1234- (void)completeWithData:(
void*)data_ width:(
unsigned)width_ height:(
unsigned)height_
1236 OOLog(
@"texture.planet.generate",
@"%@",
@"Completing atmosphere generator");
1247 NSString* rgbName = [NSString stringWithFormat:@"planet-%u-%u-atmosphere-rgb-new", _seed.high, _seed.low];
1248 NSString* alphaName = [NSString stringWithFormat:@"planet-%u-%u-atmosphere-alpha-new", _seed.high, _seed.low];
1250 [[UNIVERSE gameView] dumpRGBAToRGBFileNamed:rgbName
1251 andGrayFileNamed:alphaName
1255 rowBytes:_width * 4];
OOINLINE float OOClamp_0_1_f(float value) INLINE_CONST_FUNC
#define OOLog(class, format,...)
struct OOPlanetTextureGeneratorInfo OOPlanetTextureGeneratorInfo
OOPixMapFormat OOTextureDataFormat
OOINLINE Vector make_vector(OOScalar vx, OOScalar vy, OOScalar vz) INLINE_CONST_FUNC
OOINLINE Vector OOVectorInterpolate(Vector a, Vector b, OOScalar where) INLINE_CONST_FUNC
const Vector kBasisZVector
OOINLINE Vector vector_multiply_scalar(Vector v, OOScalar s) INLINE_CONST_FUNC
OOINLINE Vector vector_normal(Vector vec) INLINE_CONST_FUNC
static FloatRGB FloatRGBFromDictColor(NSDictionary *dictionary, NSString *key)
static FloatRGB Blend(float fraction, FloatRGB a, FloatRGB b)
void waitForTaskToComplete:(id< OOAsyncWorkTask > task)
OOAsyncWorkManager * sharedAsyncWorkManager()
id textureWithGenerator:(OOTextureGenerator *generator)
id textureWithGenerator:enqueue:(OOTextureGenerator *generator,[enqueue] BOOL enqueue)
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
float randfWithSeed(RANROTSeed *ioSeed)
unsigned RanrotWithSeed(RANROTSeed *ioSeed)
struct RANROTSeed RANROTSeed
unsigned planetAspectRatio