Oolite
Loading...
Searching...
No Matches
TextureStore.m
Go to the documentation of this file.
1/*
2
3TextureStore.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#import "TextureStore.h"
26#import "OOFoundation.h"
27#if !NEW_PLANETS
28
29#import "OOMaths.h"
30
31#ifndef NDEBUG
32#import "MyOpenGLView.h"
33#import "Universe.h"
34#else
35#import "OOColor.h"
36#endif
37
39
40#define DEBUG_DUMP (0 && OOLITE_DEBUG)
41
42static NSString* const kOOLogPlanetTextureGen = @"texture.planet.generate";
43
44#import "OOTextureGenerator.h" // For FloatRGB
45
46static FloatRGB FloatRGBFromDictColor(NSDictionary* dictionary, NSString* key)
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}
58
59static FloatRGB Blend(float fraction, FloatRGB a, FloatRGB b)
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}
67
68static FloatRGB PlanetTextureColor(float q, float impress, float bias, FloatRGB seaColor, FloatRGB paleSeaColor, FloatRGB landColor, FloatRGB paleLandColor)
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}
92
93static void fillSquareImageDataWithCloudTexture(unsigned char* imageBuffer, int width, OOColor* cloudcolor, float impress, float bias);
94static void fillSquareImageWithPlanetTex(unsigned char* imageBuffer, int width, float impress, float bias, FloatRGB seaColor, FloatRGB paleSeaColor, FloatRGB landColor, FloatRGB paleLandColor);
95
96@implementation TextureStore
97
98#define PROC_TEXTURE_SIZE 512
99
100+ (BOOL)getPlanetTextureNameFor:(NSDictionary*)planetInfo intoData:(unsigned char**)textureData width:(GLuint*)textureWidth height:(GLuint*)textureHeight
101{
102 int texture_h = PROC_TEXTURE_SIZE;
103 int texture_w = PROC_TEXTURE_SIZE;
104
105 int tex_bytes = texture_w * texture_h * 4;
106
107 NSParameterAssert(textureData != NULL && textureWidth != NULL && textureHeight != NULL);
108
109 unsigned char* imageBuffer = malloc(tex_bytes);
110 if (imageBuffer == NULL)
111 return NO;
112
113 *textureData = imageBuffer;
114 *textureWidth = texture_w;
115 *textureHeight = texture_h;
116
117 float land_fraction = [[planetInfo objectForKey:@"land_fraction"] floatValue];
118 float sea_bias = land_fraction - 1.0;
119
120 OOLog(kOOLogPlanetTextureGen, @"genning texture for land_fraction %.5f", land_fraction);
121
122 FloatRGB land_color = FloatRGBFromDictColor(planetInfo, @"land_color");
123 FloatRGB sea_color = FloatRGBFromDictColor(planetInfo, @"sea_color");
124 FloatRGB polar_land_color = FloatRGBFromDictColor(planetInfo, @"polar_land_color");
125 FloatRGB polar_sea_color = FloatRGBFromDictColor(planetInfo, @"polar_sea_color");
126
127 // Pale sea colour gives a better transition between land and sea., Backported from the new planets code.
128 FloatRGB pale_sea_color = Blend(0.45, polar_sea_color, Blend(0.7, sea_color, land_color));
129
130 fillSquareImageWithPlanetTex(imageBuffer, texture_w, 1.0, sea_bias,
131 sea_color,
132 pale_sea_color,
133 land_color,
134 polar_land_color);
135
136 return YES;
137}
138
139+ (BOOL)getCloudTextureNameFor:(OOColor*)color impress:(GLfloat)impress bias:(GLfloat)bias intoData:(unsigned char**)textureData width:(GLuint*)textureWidth height:(GLuint*)textureHeight
140{
141 int texture_h = PROC_TEXTURE_SIZE;
142 int texture_w = PROC_TEXTURE_SIZE;
143 int tex_bytes;
144
145 tex_bytes = texture_w * texture_h * 4;
146
147 NSParameterAssert(textureData != NULL && textureWidth != NULL && textureHeight != NULL);
148
149 unsigned char* imageBuffer = malloc(tex_bytes);
150 if (imageBuffer == NULL)
151 return NO;
152
153 *textureData = imageBuffer;
154 *textureWidth = texture_w;
155 *textureHeight = texture_h;
156
157 fillSquareImageDataWithCloudTexture(imageBuffer, texture_w, color, impress, bias);
158
159 return YES;
160}
161
162@end
163
165static float ranNoiseBuffer[128 * 128];
166
168{
170
171 int i;
172 for (i = 0; i < 16384; i++)
173 ranNoiseBuffer[i] = randf();
174}
175
176static void addNoise(float* buffer, int p, int n, float scale)
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}
200
201static 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)
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}
235
236static void fillSquareImageDataWithCloudTexture(unsigned char* imageBuffer, int width, OOColor* cloudcolor, float impress, float bias)
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}
280
281static void fillSquareImageWithPlanetTex(unsigned char* imageBuffer, int width, float impress, float bias,
282 FloatRGB seaColor,
283 FloatRGB paleSeaColor,
284 FloatRGB landColor,
285 FloatRGB paleLandColor)
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}
337
338#endif // !NEW_PLANETS
OOINLINE float OOLerp(float v0, float v1, float fraction) INLINE_CONST_FUNC
#define OOLog(class, format,...)
Definition OOLogging.h:84
return nil
float y
float x
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 RANROTSeed sNoiseSeed
static FloatRGB PlanetTextureColor(float q, float impress, float bias, FloatRGB seaColor, FloatRGB paleSeaColor, FloatRGB landColor, FloatRGB paleLandColor)
static float ranNoiseBuffer[128 *128]
static FloatRGB FloatRGBFromDictColor(NSDictionary *dictionary, NSString *key)
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 fillSquareImageDataWithCloudTexture(unsigned char *imageBuffer, int width, OOColor *cloudcolor, float impress, float bias)
static FloatRGB Blend(float fraction, FloatRGB a, FloatRGB b)
static void fillSquareImageWithPlanetTex(unsigned char *imageBuffer, int width, float impress, float bias, FloatRGB seaColor, FloatRGB paleSeaColor, FloatRGB landColor, FloatRGB paleLandColor)
static void addNoise(float *buffer, int p, int n, float scale)
#define PROC_TEXTURE_SIZE
void fillRanNoiseBuffer()
static NSString *const kOOLogPlanetTextureGen
float alphaComponent()
Definition OOColor.m:442
float blueComponent()
Definition OOColor.m:339
OOColor * colorWithDescription:(id description)
Definition OOColor.m:135
float redComponent()
Definition OOColor.m:329
float greenComponent()
Definition OOColor.m:334
float randf(void)
RANROTSeed RANROTGetFullSeed(void)
OOScalar z
Definition OOVector.h:34