Oolite 1.91.0.7646-241128-10e222e
Loading...
Searching...
No Matches
TextureStore.m File Reference
import "OOCocoa.h"
import "TextureStore.h"
import "OOMaths.h"
import "Universe.h"
import "MyOpenGLView.h"
import "OOCollectionExtractors.h"
import "OOTextureGenerator.h"
+ Include dependency graph for TextureStore.m:

Go to the source code of this file.

Macros

#define DEBUG_DUMP   ( 0 && OOLITE_DEBUG)
 
#define PROC_TEXTURE_SIZE   512
 

Functions

static FloatRGB FloatRGBFromDictColor (NSDictionary *dictionary, NSString *key)
 
static FloatRGB Blend (float fraction, FloatRGB a, FloatRGB b)
 
static FloatRGB PlanetTextureColor (float q, float impress, float bias, FloatRGB seaColor, FloatRGB paleSeaColor, FloatRGB landColor, FloatRGB paleLandColor)
 
static void fillSquareImageDataWithCloudTexture (unsigned char *imageBuffer, int width, OOColor *cloudcolor, float impress, float bias)
 
static void fillSquareImageWithPlanetTex (unsigned char *imageBuffer, int width, float impress, float bias, FloatRGB seaColor, FloatRGB paleSeaColor, FloatRGB landColor, FloatRGB paleLandColor)
 
void fillRanNoiseBuffer ()
 
static void addNoise (float *buffer, int p, int n, float scale)
 
static float q_factor (float *accbuffer, int x, int y, int width, BOOL polar_y_smooth, float polar_y_value, BOOL polar_x_smooth, float polar_x_value, float impress, float bias)
 

Variables

static NSString *const kOOLogPlanetTextureGen = @"texture.planet.generate"
 
static RANROTSeed sNoiseSeed
 
static float ranNoiseBuffer [128 *128]
 

Macro Definition Documentation

◆ DEBUG_DUMP

#define DEBUG_DUMP   ( 0 && OOLITE_DEBUG)

Definition at line 40 of file TextureStore.m.

◆ PROC_TEXTURE_SIZE

#define PROC_TEXTURE_SIZE   512

Definition at line 112 of file TextureStore.m.

Function Documentation

◆ addNoise()

static void addNoise ( float * buffer,
int p,
int n,
float scale )
static

Definition at line 191 of file TextureStore.m.

192{
193 int x, y;
194
195 float r = (float)p / (float)n;
196 for (y = 0; y < p; y++) for (x = 0; x < p; x++)
197 {
198 int ix = floor( (float)x / r);
199 int jx = (ix + 1) % n;
200 int iy = floor( (float)y / r);
201 int jy = (iy + 1) % n;
202 float qx = x / r - ix;
203 float qy = y / r - iy;
204 ix &= 127;
205 iy &= 127;
206 jx &= 127;
207 jy &= 127;
208 float rix = OOLerp(ranNoiseBuffer[iy * 128 + ix], ranNoiseBuffer[iy * 128 + jx], qx);
209 float rjx = OOLerp(ranNoiseBuffer[jy * 128 + ix], ranNoiseBuffer[jy * 128 + jx], qx);
210 float rfinal = scale * OOLerp(rix, rjx, qy);
211
212 buffer[y * p + x] += rfinal;
213 }
214}
float y
float x
static float ranNoiseBuffer[128 *128]

References ranNoiseBuffer, x, and y.

Referenced by fillSquareImageDataWithCloudTexture(), and TextureStore::getCloudTextureNameFor:color:impress:intoData:width:height:.

+ Here is the caller graph for this function:

◆ Blend()

static FloatRGB Blend ( float fraction,
FloatRGB a,
FloatRGB b )
static

Definition at line 64 of file TextureStore.m.

65{
66 return (FloatRGB)
67 {
68 OOLerp(a.r, b.r, fraction),
69 OOLerp(a.g, b.g, fraction),
70 OOLerp(a.b, b.b, fraction)
71 };
72}

References FloatRGB::b, FloatRGB::g, and FloatRGB::r.

Referenced by PlanetTextureColor().

+ Here is the caller graph for this function:

◆ fillRanNoiseBuffer()

void fillRanNoiseBuffer ( )

Definition at line 181 of file TextureStore.m.

182{
184
185 int i;
186 for (i = 0; i < 16384; i++)
187 ranNoiseBuffer[i] = randf();
188}
static RANROTSeed sNoiseSeed
float randf(void)
RANROTSeed RANROTGetFullSeed(void)

References randf(), ranNoiseBuffer, RANROTGetFullSeed(), and sNoiseSeed.

+ Here is the call graph for this function:

◆ fillSquareImageDataWithCloudTexture()

static void fillSquareImageDataWithCloudTexture ( unsigned char * imageBuffer,
int width,
OOColor * cloudcolor,
float impress,
float bias )
static

Definition at line 247 of file TextureStore.m.

248{
249 NSCParameterAssert(width > 0);
250
251 float accbuffer[width * width];
252 memset(accbuffer, 0, sizeof accbuffer);
253 int x, y;
254
255 GLfloat rgba[4];
256 rgba[0] = [cloudcolor redComponent];
257 rgba[1] = [cloudcolor greenComponent];
258 rgba[2] = [cloudcolor blueComponent];
259 rgba[3] = [cloudcolor alphaComponent];
260
261 int octave = 8;
262 float scale = 0.5;
263 while (octave < width)
264 {
265 addNoise(accbuffer, width, octave, scale);
266 octave *= 2;
267 scale *= 0.5;
268 }
269
270 float pole_value = (impress * accbuffer[0] - bias < 0.0)? 0.0: 1.0;
271
272 for (y = 0; y < width; y++) for (x = 0; x < width; x++)
273 {
274 float q = q_factor(accbuffer, x, y, width, YES, pole_value, NO, 0.0, impress, bias);
275
276 imageBuffer[0 + 4 * (y * width + x) ] = 255 * rgba[0];
277 imageBuffer[1 + 4 * (y * width + x) ] = 255 * rgba[1];
278 imageBuffer[2 + 4 * (y * width + x) ] = 255 * rgba[2];
279 imageBuffer[3 + 4 * (y * width + x) ] = 255 * rgba[3] * q;
280 }
281#if DEBUG_DUMP
282 NSString *name = [NSString stringWithFormat:@"atmosphere-%u-%u-old", sNoiseSeed.high, sNoiseSeed.low];
283 OOLog(@"planetTex.dump", [NSString stringWithFormat:@"Saving generated texture to file %@.", name]);
284
285 [[UNIVERSE gameView] dumpRGBAToFileNamed:name
286 bytes:imageBuffer
287 width:width
288 height:width
289 rowBytes:width * 4];
290#endif
291}
#define OOLog(class, format,...)
Definition OOLogging.h:88
static float q_factor(float *accbuffer, int x, int y, int width, BOOL polar_y_smooth, float polar_y_value, BOOL polar_x_smooth, float polar_x_value, float impress, float bias)
static void addNoise(float *buffer, int p, int n, float scale)
float alphaComponent()
Definition OOColor.m:486
float blueComponent()
Definition OOColor.m:362
float redComponent()
Definition OOColor.m:350
float greenComponent()
Definition OOColor.m:356

References addNoise(), OOColor::alphaComponent, OOColor::blueComponent, OOColor::greenComponent, OOLog, q_factor(), OOColor::redComponent, x, and y.

+ Here is the call graph for this function:

◆ fillSquareImageWithPlanetTex()

static void fillSquareImageWithPlanetTex ( unsigned char * imageBuffer,
int width,
float impress,
float bias,
FloatRGB seaColor,
FloatRGB paleSeaColor,
FloatRGB landColor,
FloatRGB paleLandColor )
static

Definition at line 293 of file TextureStore.m.

298{
299 float accbuffer[width * width];
300 memset(accbuffer, 0, sizeof accbuffer);
301
302 int octave = 8;
303 float scale = 0.5;
304 while (octave < width)
305 {
306 addNoise(accbuffer, width, octave, scale);
307 octave *= 2;
308 scale *= 0.5;
309 }
310
311 float pole_value = (impress + bias > 0.5)? 0.5 * (impress + bias) : 0.0;
312
313 int x, y;
314 for (y = 0; y < width; y++) for (x = 0; x < width; x++)
315 {
316 float q = q_factor(accbuffer, x, y, width, YES, pole_value, NO, 0.0, impress, bias);
317
318 float yN = q_factor(accbuffer, x, y - 1, width, YES, pole_value, NO, 0.0, impress, bias);
319 float yS = q_factor(accbuffer, x, y + 1, width, YES, pole_value, NO, 0.0, impress, bias);
320 float yW = q_factor(accbuffer, x - 1, y, width, YES, pole_value, NO, 0.0, impress, bias);
321 float yE = q_factor(accbuffer, x + 1, y, width, YES, pole_value, NO, 0.0, impress, bias);
322
323 Vector norm = make_vector( 24.0 * (yW - yE), 24.0 * (yS - yN), 2.0);
324
325 norm = vector_normal(norm);
326
327 GLfloat shade = pow(norm.z, 3.2);
328
329 FloatRGB color = PlanetTextureColor(q, impress, bias, seaColor, paleSeaColor, landColor, paleLandColor);
330
331 color.r *= shade;
332 color.g *= shade;
333 color.b *= shade;
334
335 imageBuffer[0 + 4 * (y * width + x)] = 255 * color.r;
336 imageBuffer[1 + 4 * (y * width + x)] = 255 * color.g;
337 imageBuffer[2 + 4 * (y * width + x)] = 255 * color.b;
338 imageBuffer[3 + 4 * (y * width + x)] = 255;
339 }
340#if DEBUG_DUMP
341 OOLog(@"planetTex.dump", [NSString stringWithFormat:@"Saving generated texture to file planet-%u-%u-old.", sNoiseSeed.high, sNoiseSeed.low]);
342
343 [[UNIVERSE gameView] dumpRGBAToFileNamed:[NSString stringWithFormat:@"planet-%u-%u-old", sNoiseSeed.high, sNoiseSeed.low]
344 bytes:imageBuffer
345 width:width
346 height:width
347 rowBytes:width * 4];
348#endif
349}
static FloatRGB PlanetTextureColor(float q, float impress, float bias, FloatRGB seaColor, FloatRGB paleSeaColor, FloatRGB landColor, FloatRGB paleLandColor)
uint32_t low
uint32_t high

◆ FloatRGBFromDictColor()

static FloatRGB FloatRGBFromDictColor ( NSDictionary * dictionary,
NSString * key )
static

Definition at line 49 of file TextureStore.m.

50{
51 OOColor *color = [dictionary objectForKey:key];
52 if (color == nil)
53 {
54 // could not get a color from the dicitionary, return white color instead of hitting the assert below
55 color = [OOColor colorWithDescription:@"whiteColor"];
56 OOLog(@"textureStore.FloatRGBFromDictColor.nilColor", @"Expected color for key \"%@\" in dictionary %@, got nil. Setting color to %@", key, dictionary, [color rgbaDescription]);
57 }
58 NSCAssert1([color isKindOfClass:[OOColor class]], @"Expected OOColor, got %@", [color class]);
59
60 return (FloatRGB){ [color redComponent], [color greenComponent], [color blueComponent] };
61}
return nil
OOColor * colorWithDescription:(id description)
Definition OOColor.m:127

References OOColor::blueComponent, OOColor::colorWithDescription:, OOColor::greenComponent, nil, OOLog, and OOColor::redComponent.

+ Here is the call graph for this function:

◆ PlanetTextureColor()

static FloatRGB PlanetTextureColor ( float q,
float impress,
float bias,
FloatRGB seaColor,
FloatRGB paleSeaColor,
FloatRGB landColor,
FloatRGB paleLandColor )
static

Definition at line 75 of file TextureStore.m.

76{
77 const FloatRGB kWhite = { 1.0, 1.0, 1.0 };
78 float maxq = impress + bias;
79
80 float hi = 0.66667 * maxq;
81 float oh = 1.0 / hi;
82 float ih = 1.0 / (1.0 - hi);
83
84 if (q <= 0.0)
85 {
86 return seaColor;
87 }
88 if (q > 1.0)
89 {
90 return (FloatRGB){ 1.0f, 1.0f, 1.0f };
91 }
92 if (q < 0.01)
93 {
94 return Blend(q * 100.0f, paleSeaColor, landColor);
95 }
96 if (q > hi)
97 {
98 return Blend((q - hi) * ih, paleLandColor, kWhite); // snow capped peaks
99 }
100
101 return Blend((hi - q) * oh, paleLandColor, landColor);
102}
static FloatRGB Blend(float fraction, FloatRGB a, FloatRGB b)

References Blend().

Referenced by TextureStore::getCloudTextureNameFor:color:impress:intoData:width:height:.

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

◆ q_factor()

static float q_factor ( float * accbuffer,
int x,
int y,
int width,
BOOL polar_y_smooth,
float polar_y_value,
BOOL polar_x_smooth,
float polar_x_value,
float impress,
float bias )
static

Definition at line 217 of file TextureStore.m.

218{
219 while ( x < 0 ) x+= width;
220 while ( y < 0 ) y+= width;
221 while ( x >= width ) x-= width;
222 while ( y >= width ) y-= width;
223
224 float q = accbuffer[ y * width + x]; // 0.0 -> 1.0
225
226 q *= impress; // impress
227 q += bias; // + bias
228
229 float polar_y = (2.0f * y - width) / (float) width;
230 float polar_x = (2.0f * x - width) / (float) width;
231
232 polar_x *= polar_x;
233 polar_y *= polar_y;
234
235 if (polar_x_smooth)
236 q = q * (1.0 - polar_x) + polar_x * polar_x_value;
237 if (polar_y_smooth)
238 q = q * (1.0 - polar_y) + polar_y * polar_y_value;
239
240 if (q > 1.0) q = 1.0;
241 if (q < 0.0) q = 0.0;
242
243 return q;
244}

References x, and y.

Referenced by fillSquareImageDataWithCloudTexture(), and TextureStore::getCloudTextureNameFor:color:impress:intoData:width:height:.

+ Here is the caller graph for this function:

Variable Documentation

◆ kOOLogPlanetTextureGen

NSString* const kOOLogPlanetTextureGen = @"texture.planet.generate"
static

Definition at line 43 of file TextureStore.m.

◆ ranNoiseBuffer

float ranNoiseBuffer[128 *128]
static

Definition at line 179 of file TextureStore.m.

Referenced by addNoise(), and fillRanNoiseBuffer().

◆ sNoiseSeed