Oolite
Loading...
Searching...
No Matches
TextureStore.m File Reference
import "TextureStore.h"
import "OOFoundation.h"
import "OOMaths.h"
import "MyOpenGLView.h"
import "Universe.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

Function Documentation

◆ addNoise()

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

Definition at line 176 of file TextureStore.m.

177{
178 int x, y;
179
180 float r = (float)p / (float)n;
181 for (y = 0; y < p; y++)
182 for (x = 0; x < p; x++) {
183 int ix = floor((float)x / r);
184 int jx = (ix + 1) % n;
185 int iy = floor((float)y / r);
186 int jy = (iy + 1) % n;
187 float qx = x / r - ix;
188 float qy = y / r - iy;
189 ix &= 127;
190 iy &= 127;
191 jx &= 127;
192 jy &= 127;
193 float rix = OOLerp(ranNoiseBuffer[iy * 128 + ix], ranNoiseBuffer[iy * 128 + jx], qx);
194 float rjx = OOLerp(ranNoiseBuffer[jy * 128 + ix], ranNoiseBuffer[jy * 128 + jx], qx);
195 float rfinal = scale * OOLerp(rix, rjx, qy);
196
197 buffer[y * p + x] += rfinal;
198 }
199}
OOINLINE float OOLerp(float v0, float v1, float fraction) INLINE_CONST_FUNC
float y
float x
static float ranNoiseBuffer[128 *128]

References OOLerp(), ranNoiseBuffer, x, and y.

Referenced by fillSquareImageDataWithCloudTexture(), and fillSquareImageWithPlanetTex().

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

◆ Blend()

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

Definition at line 59 of file TextureStore.m.

60{
61 return (FloatRGB) {
62 OOLerp(a.r, b.r, fraction),
63 OOLerp(a.g, b.g, fraction),
64 OOLerp(a.b, b.b, fraction)
65 };
66}

References FloatRGB::b, FloatRGB::g, OOLerp(), and FloatRGB::r.

Referenced by TextureStore::getPlanetTextureNameFor:intoData:width:height:, and PlanetTextureColor().

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

◆ fillRanNoiseBuffer()

void fillRanNoiseBuffer ( )

Definition at line 167 of file TextureStore.m.

168{
170
171 int i;
172 for (i = 0; i < 16384; i++)
173 ranNoiseBuffer[i] = randf();
174}
static RANROTSeed sNoiseSeed
float randf(void)
RANROTSeed RANROTGetFullSeed(void)

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

Referenced by PlanetEntity(OOPrivate)::cloudTextureWithCloudColor:cloudImpress:cloudBias:, and PlanetEntity(OOPrivate)::planetTextureWithInfo:.

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

◆ fillSquareImageDataWithCloudTexture()

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

Definition at line 236 of file TextureStore.m.

237{
238 NSCParameterAssert(width > 0);
239
240 float accbuffer[width * width];
241 memset(accbuffer, 0, sizeof accbuffer);
242 int x, y;
243
244 GLfloat rgba[4];
245 rgba[0] = [cloudcolor redComponent];
246 rgba[1] = [cloudcolor greenComponent];
247 rgba[2] = [cloudcolor blueComponent];
248 rgba[3] = [cloudcolor alphaComponent];
249
250 int octave = 8;
251 float scale = 0.5;
252 while (octave < width) {
253 addNoise(accbuffer, width, octave, scale);
254 octave *= 2;
255 scale *= 0.5;
256 }
257
258 float pole_value = (impress * accbuffer[0] - bias < 0.0) ? 0.0 : 1.0;
259
260 for (y = 0; y < width; y++)
261 for (x = 0; x < width; x++) {
262 float q = q_factor(accbuffer, x, y, width, YES, pole_value, NO, 0.0, impress, bias);
263
264 imageBuffer[0 + 4 * (y * width + x)] = 255 * rgba[0];
265 imageBuffer[1 + 4 * (y * width + x)] = 255 * rgba[1];
266 imageBuffer[2 + 4 * (y * width + x)] = 255 * rgba[2];
267 imageBuffer[3 + 4 * (y * width + x)] = 255 * rgba[3] * q;
268 }
269#if DEBUG_DUMP
270 NSString* name = [NSString stringWithFormat:@"atmosphere-%u-%u-old", sNoiseSeed.high, sNoiseSeed.low];
271 OOLog(@"planetTex.dump", [NSString stringWithFormat:@"Saving generated texture to file %@.", name]);
272
273 [[UNIVERSE gameView] dumpRGBAToFileNamed:name
274 bytes:imageBuffer
275 width:width
276 height:width
277 rowBytes:width * 4];
278#endif
279}
#define OOLog(class, format,...)
Definition OOLogging.h:84
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:442
float blueComponent()
Definition OOColor.m:339
float redComponent()
Definition OOColor.m:329
float greenComponent()
Definition OOColor.m:334

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

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

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

◆ fillSquareImageWithPlanetTex()

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

Definition at line 281 of file TextureStore.m.

286{
287 float accbuffer[width * width];
288 memset(accbuffer, 0, sizeof accbuffer);
289
290 int octave = 8;
291 float scale = 0.5;
292 while (octave < width) {
293 addNoise(accbuffer, width, octave, scale);
294 octave *= 2;
295 scale *= 0.5;
296 }
297
298 float pole_value = (impress + bias > 0.5) ? 0.5 * (impress + bias) : 0.0;
299
300 int x, y;
301 for (y = 0; y < width; y++)
302 for (x = 0; x < width; x++) {
303 float q = q_factor(accbuffer, x, y, width, YES, pole_value, NO, 0.0, impress, bias);
304
305 float yN = q_factor(accbuffer, x, y - 1, width, YES, pole_value, NO, 0.0, impress, bias);
306 float yS = q_factor(accbuffer, x, y + 1, width, YES, pole_value, NO, 0.0, impress, bias);
307 float yW = q_factor(accbuffer, x - 1, y, width, YES, pole_value, NO, 0.0, impress, bias);
308 float yE = q_factor(accbuffer, x + 1, y, width, YES, pole_value, NO, 0.0, impress, bias);
309
310 Vector norm = make_vector(24.0 * (yW - yE), 24.0 * (yS - yN), 2.0);
311
312 norm = vector_normal(norm);
313
314 GLfloat shade = pow(norm.z, 3.2);
315
316 FloatRGB color = PlanetTextureColor(q, impress, bias, seaColor, paleSeaColor, landColor, paleLandColor);
317
318 color.r *= shade;
319 color.g *= shade;
320 color.b *= shade;
321
322 imageBuffer[0 + 4 * (y * width + x)] = 255 * color.r;
323 imageBuffer[1 + 4 * (y * width + x)] = 255 * color.g;
324 imageBuffer[2 + 4 * (y * width + x)] = 255 * color.b;
325 imageBuffer[3 + 4 * (y * width + x)] = 255;
326 }
327#if DEBUG_DUMP
328 OOLog(@"planetTex.dump", [NSString stringWithFormat:@"Saving generated texture to file planet-%u-%u-old.", sNoiseSeed.high, sNoiseSeed.low]);
329
330 [[UNIVERSE gameView] dumpRGBAToFileNamed:[NSString stringWithFormat:@"planet-%u-%u-old", sNoiseSeed.high, sNoiseSeed.low]
331 bytes:imageBuffer
332 width:width
333 height:width
334 rowBytes:width * 4];
335#endif
336}
OOINLINE Vector make_vector(OOScalar vx, OOScalar vy, OOScalar vz) INLINE_CONST_FUNC
Definition OOVector.h:149
OOINLINE Vector vector_normal(Vector vec) INLINE_CONST_FUNC
Definition OOVector.h:270
static FloatRGB PlanetTextureColor(float q, float impress, float bias, FloatRGB seaColor, FloatRGB paleSeaColor, FloatRGB landColor, FloatRGB paleLandColor)
OOScalar z
Definition OOVector.h:34

References addNoise(), FloatRGB::b, FloatRGB::g, make_vector(), OOLog, PlanetTextureColor(), q_factor(), FloatRGB::r, sNoiseSeed, vector_normal(), x, y, and Vector::z.

Referenced by TextureStore::getPlanetTextureNameFor:intoData:width:height:.

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

◆ FloatRGBFromDictColor()

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

Definition at line 46 of file TextureStore.m.

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

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

Referenced by TextureStore::getPlanetTextureNameFor:intoData:width:height:.

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

◆ PlanetTextureColor()

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

Definition at line 68 of file TextureStore.m.

69{
70 const FloatRGB kWhite = { 1.0, 1.0, 1.0 };
71 float maxq = impress + bias;
72
73 float hi = 0.66667 * maxq;
74 float oh = 1.0 / hi;
75 float ih = 1.0 / (1.0 - hi);
76
77 if (q <= 0.0) {
78 return seaColor;
79 }
80 if (q > 1.0) {
81 return (FloatRGB) { 1.0f, 1.0f, 1.0f };
82 }
83 if (q < 0.01) {
84 return Blend(q * 100.0f, paleSeaColor, landColor);
85 }
86 if (q > hi) {
87 return Blend((q - hi) * ih, paleLandColor, kWhite); // snow capped peaks
88 }
89
90 return Blend((hi - q) * oh, paleLandColor, landColor);
91}
static FloatRGB Blend(float fraction, FloatRGB a, FloatRGB b)

References Blend().

Referenced by fillSquareImageWithPlanetTex().

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

◆ q_factor()

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 201 of file TextureStore.m.

202{
203 while (x < 0)
204 x += width;
205 while (y < 0)
206 y += width;
207 while (x >= width)
208 x -= width;
209 while (y >= width)
210 y -= width;
211
212 float q = accbuffer[y * width + x]; // 0.0 -> 1.0
213
214 q *= impress; // impress
215 q += bias; // + bias
216
217 float polar_y = (2.0f * y - width) / (float)width;
218 float polar_x = (2.0f * x - width) / (float)width;
219
220 polar_x *= polar_x;
221 polar_y *= polar_y;
222
223 if (polar_x_smooth)
224 q = q * (1.0 - polar_x) + polar_x * polar_x_value;
225 if (polar_y_smooth)
226 q = q * (1.0 - polar_y) + polar_y * polar_y_value;
227
228 if (q > 1.0)
229 q = 1.0;
230 if (q < 0.0)
231 q = 0.0;
232
233 return q;
234}

References x, and y.

Referenced by fillSquareImageDataWithCloudTexture(), and fillSquareImageWithPlanetTex().

Here is the caller graph for this function:

Variable Documentation

◆ kOOLogPlanetTextureGen

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

◆ ranNoiseBuffer

float ranNoiseBuffer[128 *128]
static

Definition at line 165 of file TextureStore.m.

Referenced by addNoise(), and fillRanNoiseBuffer().

◆ sNoiseSeed

RANROTSeed sNoiseSeed
static

Definition at line 164 of file TextureStore.m.

Referenced by fillRanNoiseBuffer(), and fillSquareImageWithPlanetTex().