32#define DEBUG_DUMP (0 && OOLITE_DEBUG)
33#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
67@interface OOStandaloneAtmosphereGenerator (Private)
70- (void)dumpNoiseBuffer:(
float*)noise;
78static float QFactor(
float* accbuffer,
int x,
int y,
unsigned width,
float polar_y_value,
float bias,
float polar_y);
79static float BlendAlpha(
float fraction,
float a,
float b);
83 kPlanetScale256x256 = 1,
85 kPlanetScale1024x1024,
86 kPlanetScale2048x2048,
87 kPlanetScale4096x4096,
89 kPlanetScaleReducedDetail = kPlanetScale512x512,
90 kPlanetScaleFullDetail = kPlanetScale1024x1024
93@implementation OOStandaloneAtmosphereGenerator
95- (id)initWithPlanetInfo:(NSDictionary*)planetInfo
97 OOLog(
@"texture.planet.generate",
@"%@",
@"Initialising standalone atmosphere generator");
100 if ((
self = [super initWithPath:[NSString stringWithFormat:
@"OOStandaloneAtmosphereTexture@%p",
self] options:
kOOTextureAllowCubeMap])) {
101 OOLog(
@"texture.planet.generate",
@"Extracting parameters for generator %@",
self);
102 [[planetInfo objectForKey:@"noise_map_seed"] getValue:&_info.seed];
103 OOLog(
@"texture.planet.generate",
@"%@",
@"Extracting atmosphere parameters");
105 _info.cloudAlpha = [planetInfo oo_floatForKey:@"cloud_alpha" defaultValue:1.0f];
106 _info.cloudFraction =
OOClamp_0_1_f([planetInfo oo_floatForKey:
@"cloud_fraction" defaultValue:0.3]);
112#ifndef TEXGEN_TEST_RIG
114 _planetScale = kPlanetScaleReducedDetail;
116 _planetScale = kPlanetScaleFullDetail;
119 _planetScale = kPlanetScale4096x4096;
121 _info.perlin3d = [planetInfo oo_boolForKey:@"perlin_3d" defaultValue:detailLevel > DETAIL_LEVEL_SHADERS];
122 _info.planetAspectRatio = _info.perlin3d ? 2 : 1;
123 _info.planetScaleOffset = 8 - _info.planetAspectRatio;
129+ (OOTexture*)planetTextureWithInfo:(NSDictionary*)planetInfo
131 OOTexture* result =
nil;
132 OOStandaloneAtmosphereGenerator* generator = [[
self alloc] initWithPlanetInfo:planetInfo];
133 if (generator !=
nil) {
141+ (BOOL)generateAtmosphereTexture:(OOTexture**)texture withInfo:(NSDictionary*)planetInfo
143 NSParameterAssert(texture != NULL);
145 OOStandaloneAtmosphereGenerator* atmoGen = [[[
self alloc] initWithPlanetInfo:planetInfo] autorelease];
151 return *texture !=
nil;
159- (NSString*)descriptionComponents
161 return [NSString stringWithFormat:@"seed: %u,%u", _info.seed.high, _info.seed.low];
164- (uint32_t)textureOptions
166 return PLANET_TEXTURE_OPTIONS;
171 return [NSString stringWithFormat:@"OOStandaloneAtmosphereGenerator-@%u\n%u,%u/%u,%u/%f/%f/%f,%f,%f/%f,%f,%f/%f,%f,%f",
173 _info.width, _info.height, _info.seed.high, _info.seed.low,
174 _info.cloudAlpha, _info.cloudFraction,
175 _info.airColor.r, _info.airColor.g, _info.airColor.b,
176 _info.cloudColor.r, _info.cloudColor.g, _info.cloudColor.b,
177 _info.paleCloudColor.r, _info.paleCloudColor.g, _info.paleCloudColor.b];
182 width:(uint32_t*)outWidth
183 height:(uint32_t*)outHeight
186 if (![
self isReady]) {
188 OOLog(
@"texture.planet.generate.wait",
@"%s generator %@",
"Waiting for",
self);
191 BOOL result = [
super getResult:outData format:outFormat originalWidth:outWidth originalHeight:outHeight];
194 OOLog(
@"texture.planet.generate.dequeue",
@"%s generator %@", result ?
"Dequeued" :
"Failed to dequeue",
self);
196 OOLog(
@"texture.planet.generate.dequeue",
@"%s generator %@ without waiting.", result ?
"Dequeued" :
"Failed to dequeue",
self);
206 OOLog(
@"texture.planet.generate.begin",
@"Started generator %@",
self);
210 uint8_t *aBuffer = NULL, *apx = NULL;
211 float* randomBuffer = NULL;
213 _height = _info.height = 1 << (_planetScale + _info.planetScaleOffset);
214 _width = _info.width = _height * _info.planetAspectRatio;
216#define FAIL_IF(cond) \
218 if (EXPECT_NOT(cond)) \
221#define FAIL_IF_NULL(x) FAIL_IF((x) == NULL)
223 aBuffer = malloc(4 * _width * _height);
224 FAIL_IF_NULL(aBuffer);
227 FAIL_IF(!FillFBMBuffer(&_info));
229 [
self dumpNoiseBuffer:_info.fbmBuffer];
232 float paleClouds = (_info.cloudFraction * _info.fbmBuffer[0] < 1.0f - _info.cloudFraction) ? 0.0f : 1.0f;
237 float rHeight = 1.0f / _height;
238 float fy, fHeight = _height;
240 float cloudFraction = _info.cloudFraction;
242 for (
y = (
int)_height - 1, fy = (
float)
y;
y >= 0;
y--, fy--) {
243 nearPole = (2.0f * fy - fHeight) * rHeight;
244 nearPole *= nearPole;
246 for (
x = (
int)_width - 1;
x >= 0;
x--) {
247 q = QFactor(_info.fbmBuffer,
x,
y, _width, paleClouds, cloudFraction, nearPole);
248 color = CloudMix(&_info, q, nearPole);
249 *apx++ = 255.0f * color.r;
250 *apx++ = 255.0f * color.g;
251 *apx++ = 255.0f * color.b;
252 *apx++ = 255.0f * color.a * _info.cloudAlpha;
260 FREE(_info.fbmBuffer);
268 OOLog(
@"texture.planet.generate.complete",
@"Completed generator %@ %@successfully",
self, success ?
@"" :
@"un");
272 NSString* diffuseName = [NSString stringWithFormat:@"atmosphere-%u-%u-diffuse-new", _info.seed.high, _info.seed.low];
273 NSString* lightsName = [NSString stringWithFormat:@"atmosphere-%u-%u-alpha-new", _info.seed.high, _info.seed.low];
275 [[UNIVERSE gameView] dumpRGBAToRGBFileNamed:diffuseName
276 andGrayFileNamed:lightsName
280 rowBytes:_width * 4];
287- (void)dumpNoiseBuffer:(
float*)noise
289 NSString* noiseName = [NSString stringWithFormat:@"atmosphere-%u-%u-noise-new", _info.seed.high, _info.seed.low];
291 uint8_t* noisePx = malloc(_width * _height);
293 for (
y = 0;
y < _height;
y++) {
294 for (
x = 0;
x < _width;
x++) {
295 noisePx[y * _width + x] = 255.0f * noise[y * _width + x];
299 [[UNIVERSE gameView] dumpGrayToFileNamed:noiseName
311OOINLINE float Lerp(
float v0,
float v1,
float fraction)
314 return v0 + fraction * (v1 - v0);
317static FloatRGB
Blend(
float fraction, FloatRGB a, FloatRGB b)
320 Lerp(b.
r, a.
r, fraction),
321 Lerp(b.
g, a.
g, fraction),
322 Lerp(b.
b, a.
b, fraction)
326static float BlendAlpha(
float fraction,
float a,
float b)
328 return Lerp(b, a, fraction);
336#define AIR_ALPHA (0.05f)
337#define CLOUD_ALPHA (2.0f)
339#define POLAR_BOUNDARY (0.33f)
340#define CLOUD_BOUNDARY (0.5f)
341#define RECIP_CLOUD_BOUNDARY (1.0f / CLOUD_BOUNDARY)
344 float alpha = info->
cloudAlpha, portion = 0.0f;
346 q -= CLOUD_BOUNDARY * 0.5f;
348 if (nearPole > POLAR_BOUNDARY) {
349 portion = nearPole > POLAR_BOUNDARY + 0.2f ? 1.0f : (nearPole - POLAR_BOUNDARY) * 5.0f;
352 portion = nearPole > POLAR_BOUNDARY + 0.625f ? 1.0f : (nearPole - POLAR_BOUNDARY) * 1.6f;
356 if (q >= -CLOUD_BOUNDARY) {
357 alpha *= BlendAlpha(-q * 0.5f * RECIP_CLOUD_BOUNDARY + 0.5f,
CLOUD_ALPHA, AIR_ALPHA);
362 if (q < CLOUD_BOUNDARY) {
363 alpha *= BlendAlpha(q * 0.5f * RECIP_CLOUD_BOUNDARY + 0.5f, AIR_ALPHA,
CLOUD_ALPHA);
369 alpha *= BlendAlpha(portion, 0.6f, 1.0f);
374 return (FloatRGBA) { cloudColor.
r, cloudColor.
g, cloudColor.
b, alpha };
379 OOColor* color = [dictionary objectForKey:key];
380 NSCAssert1([color isKindOfClass:[OOColor
class]],
@"Expected OOColor, got %@", [color
class]);
387 return 3.0f * q * q - 2.0f * q * q * q;
398OOINLINE int32_t fast_floor(
double val)
400 val += 68719476736.0 * 1.5;
401 return (((int32_t*)&val)[iman_] >> 16);
409 NSCParameterAssert(info != NULL);
415 GenerateFBMNoise(info);
417 GenerateFBMNoise3D(info);
427 kPermutationCount = 1 << 10,
428 kPermutationMask = kPermutationCount - 1,
437static const Vector kGradients[kGradientCount] = {
452static const uint8_t kGradients[kGradientCount][3] = {
475OOINLINE float TDot3(
const uint8_t grad[3],
float x,
float y,
float z)
477 float xt[3] = { -
x, 0.0f,
x };
478 float yt[3] = { -
y, 0.0f,
y };
479 float zt[3] = { -z, 0.0f, z };
481 return xt[grad[0]] + yt[grad[1]] + zt[grad[2]];
491 float fx = floor(p.
x);
492 float fy = floor(p.
y);
493 float fz = floor(p.
z);
502#define PERM(v) permutations[(v) & kPermutationMask]
504 unsigned PZ0 = PERM(Z);
505 unsigned PZ1 = PERM(Z + 1);
507 unsigned PY0Z0 = PERM(Y + PZ0);
508 unsigned PY1Z0 = PERM(Y + 1 + PZ0);
509 unsigned PY0Z1 = PERM(Y + PZ1);
510 unsigned PY1Z1 = PERM(Y + 1 + PZ1);
512 unsigned gi000 = PERM(X + PY0Z0);
513 unsigned gi010 = PERM(X + PY1Z0);
514 unsigned gi100 = PERM(X + 1 + PY0Z0);
515 unsigned gi110 = PERM(X + 1 + PY1Z0);
516 unsigned gi001 = PERM(X + PY0Z1);
517 unsigned gi011 = PERM(X + PY1Z1);
518 unsigned gi101 = PERM(X + 1 + PY0Z1);
519 unsigned gi111 = PERM(X + 1 + PY1Z1);
525#define DOT3(idx, x_, y_, z_) dot_product(kGradients[(idx) % kGradientCount], (Vector) { (x_), (y_), (z_) })
527#define DOT3(idx, x_, y_, z_) TDot3(kGradients[(idx) % kGradientCount], (x_), (y_), (z_))
533 float n000 = DOT3(gi000,
x,
y, z);
534 float n010 = DOT3(gi010,
x, y1, z);
535 float n100 = DOT3(gi100, x1,
y, z);
536 float n110 = DOT3(gi110, x1, y1, z);
537 float n001 = DOT3(gi001,
x,
y, z1);
538 float n011 = DOT3(gi011,
x, y1, z1);
539 float n101 = DOT3(gi101, x1,
y, z1);
540 float n111 = DOT3(gi111, x1, y1, z1);
545 float u = Hermite(
x);
546 float v = Hermite(
y);
547 float w = Hermite(z);
550 float nx00 = Lerp(n000, n100, u);
551 float nx01 = Lerp(n001, n101, u);
552 float nx10 = Lerp(n010, n110, u);
553 float nx11 = Lerp(n011, n111, u);
555 float nxy0 = Lerp(nx00, nx10, v);
556 float nxy1 = Lerp(nx01, nx11, v);
558 float nxyz = Lerp(nxy0, nxy1, w);
571 uint16_t* perms = malloc(
sizeof *info->
permutations * kPermutationCount);
582 uint16_t* curr = perms;
584 for (n = 1; n < kPermutationCount; n++) {
598 FAIL_IF(!MakePermutationTable(info));
602 float dlon = 2.0f *
M_PI / width;
603 float dlat =
M_PI / height;
606 for (
y = 0, lat = -
M_PI_2;
y < height;
y++, lat += dlat) {
607 float las = sin(lat);
608 float lac = cos(lat);
610 for (
x = 0, lon = -
M_PI;
x < width;
x++, lon += dlon) {
613 float los = sin(lon);
614 float loc = cos(lon);
624 unsigned octaveMask = 4;
625 float octave = octaveMask;
630 while ((octaveMask + 1) < height) {
632 sum += scale * SampleNoise3D(info, ps);
635 octaveMask = (octaveMask << 1) | 1;
641 float sum = 0.5f * SampleNoise3D(info, p);
655static void FillRandomBuffer(
float* randomBuffer,
RANROTSeed seed)
657 unsigned i, len = kRandomBufferSize * kRandomBufferSize;
658 for (i = 0; i < len; i++) {
666 unsigned width = info->
width, height = info->
height;
668 float rr = octave / width;
669 float fx, fy, qx, qy, rix, rjx, rfinal;
672 for (fy = 0,
y = 0;
y < height; fy++,
y++) {
675 jy = (iy + 1) & octaveMask;
676 qy = Hermite(qy - iy);
677 iy &= (kRandomBufferSize - 1);
678 jy &= (kRandomBufferSize - 1);
680 for (fx = 0,
x = 0;
x < width; fx++,
x++) {
686 ix &= (kRandomBufferSize - 1);
688 qxBuffer[x] = Hermite(qx);
695 jx = (ix + 1) & octaveMask;
696 jx &= (kRandomBufferSize - 1);
698 rix = Lerp(randomBuffer[iy * kRandomBufferSize + ix], randomBuffer[iy * kRandomBufferSize + jx], qx);
699 rjx = Lerp(randomBuffer[jy * kRandomBufferSize + ix], randomBuffer[jy * kRandomBufferSize + jx], qx);
700 rfinal = Lerp(rix, rjx, qy);
702 *dst++ += scale * rfinal;
710 size_t randomBufferSize = kRandomBufferSize * kRandomBufferSize *
sizeof(float);
711 size_t qxBufferSize = info->
width *
sizeof(float);
712 size_t ixBufferSize = info->
width *
sizeof(
int);
713 char* sharedBuffer = malloc(randomBufferSize + qxBufferSize + ixBufferSize);
714 if (sharedBuffer == NULL)
717 float* randomBuffer = (
float*)sharedBuffer;
718 float* qxBuffer = (
float*)(sharedBuffer + randomBufferSize);
719 int* ixBuffer = (
int*)(sharedBuffer + randomBufferSize + qxBufferSize);
722 FillRandomBuffer(randomBuffer, info->
seed);
725 unsigned height = info->
height;
727 float octave = octaveMask;
731 while ((octaveMask + 1) < height) {
732 AddNoise(info, randomBuffer, octave, octaveMask, scale, qxBuffer, ixBuffer);
734 octaveMask = (octaveMask << 1) | 1;
742static float QFactor(
float* accbuffer,
int x,
int y,
unsigned width,
float polar_y_value,
float bias,
float polar_y)
744 float q = accbuffer[y * width + x];
748 q = q * (1.0f - polar_y) + polar_y * polar_y_value;
OOINLINE float OOClamp_0_1_f(float value) INLINE_CONST_FUNC
#define OOLog(class, format,...)
struct OOStandaloneAtmosphereGeneratorInfo OOStandaloneAtmosphereGeneratorInfo
OOPixMapFormat OOTextureDataFormat
OOINLINE Vector vector_multiply_scalar(Vector v, OOScalar s) INLINE_CONST_FUNC
static FloatRGB FloatRGBFromDictColor(NSDictionary *dictionary, NSString *key)
static FloatRGB Blend(float fraction, FloatRGB a, FloatRGB b)
id textureWithGenerator:(OOTextureGenerator *generator)
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
float randfWithSeed(RANROTSeed *ioSeed)
unsigned RanrotWithSeed(RANROTSeed *ioSeed)
struct RANROTSeed RANROTSeed
unsigned planetAspectRatio