Oolite
Loading...
Searching...
No Matches
PlanetEntity.m
Go to the documentation of this file.
1/*
2
3PlanetEntity.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 "PlanetEntity.h"
26
27#if !NEW_PLANETS
28
30
31#import "AI.h"
32#import "MyOpenGLView.h"
33#import "OOCharacter.h"
35#import "OOColor.h"
36#import "OODebugFlags.h"
39#import "OOStringParsing.h"
40#import "OOTexture.h"
41#import "OOTextureInternal.h" // For GL_TEXTURE_CUBE_MAP -- need to clean this up.
42#import "PlayerEntity.h"
43#import "ShipEntityAI.h"
44#import "TextureStore.h"
45#import "Universe.h"
46
47#if !OOLITE_MAC_OS_X
48#define NSIntegerMapKeyCallBacks NSIntMapKeyCallBacks
49#define NSIntegerMapValueCallBacks NSIntMapValueCallBacks
50#endif
51
52// straight C
53static Vector base_vertex_array[MAX_PLANET_VERTICES];
54static int base_terrain_array[MAX_PLANET_VERTICES];
55static unsigned next_free_vertex;
56static NSMapTable* sEdgeToVertex;
57
58static int n_triangles[MAX_SUBDIVIDE];
59static int triangle_start[MAX_SUBDIVIDE];
60static GLuint vertex_index_array[3 * (20 + 80 + 320 + 1280 + 5120 + 20480)];
61
62static GLfloat texture_uv_array[MAX_PLANET_VERTICES * 2];
63
64@interface PlanetEntity (OOPrivate) <OOGraphicsResetClient>
65
66- (double)sqrtZeroDistance;
67
68- (void)drawModelWithVertexArraysAndSubdivision:(int)subdivide;
69
71
72- (void)initialiseBaseTerrainArray:(int)percent_land;
73- (void)paintVertex:(unsigned)vi seed:(int)seed;
74- (void)scaleVertices;
75
76- (id)initAsAtmosphereForPlanet:(PlanetEntity*)planet dictionary:(NSDictionary*)dict;
77- (void)setTextureColorForPlanet:(BOOL)isMain inSystem:(BOOL)isLocal;
78
79- (id)initMiniatureFromPlanet:(PlanetEntity*)planet withAlpha:(float)alpha;
80
81- (void)loadTexture:(NSDictionary*)configuration;
82- (OOTexture*)planetTextureWithInfo:(NSDictionary*)info;
83- (OOTexture*)cloudTextureWithCloudColor:(OOColor*)cloudColor cloudImpress:(GLfloat)cloud_impress cloudBias:(GLfloat)cloud_bias;
84
85- (void)deleteDisplayLists;
86
87- (void)setUseTexturedModel:(BOOL)flag;
88
89@end
90
91static unsigned baseVertexIndexForEdge(GLushort va, GLushort vb, BOOL textured);
92
93typedef struct
94{
95 unsigned v;
96 float s;
97 float t;
98} BaseFace;
99
100static const Vector kUntexturedVertices[] = {
101 { +0.000000, -0.850664, -0.525710 },
102 { +0.000000, -0.850664, +0.525710 },
103 { +0.000000, +0.850664, +0.525710 },
104 { +0.000000, +0.850664, -0.525710 },
105 { -0.525710, +0.000000, -0.850664 },
106 { +0.525710, +0.000000, -0.850664 },
107 { +0.525710, +0.000000, +0.850664 },
108 { -0.525710, +0.000000, +0.850664 },
109 { -0.850664, -0.525710, +0.000000 },
110 { -0.850664, +0.525710, +0.000000 },
111 { +0.850664, +0.525710, +0.000000 },
112 { +0.850664, -0.525710, +0.000000 }
113};
114
115static const BaseFace kUntexturedFaces[][3] = {
116 { { 9, +0.600000, +0.666667 }, { 7, +0.800000, +0.666667 }, { 8, +0.800000, +0.333333 } },
117 { { 8, +0.800000, +0.333333 }, { 4, +0.600000, +0.333333 }, { 9, +0.600000, +0.666667 } },
118 { { 3, +0.400000, +0.666667 }, { 9, +0.600000, +0.666667 }, { 4, +0.600000, +0.333333 } },
119 { { 8, +0.800000, +0.333333 }, { 0, +0.500000, +0.000000 }, { 4, +0.600000, +0.333333 } },
120 { { 7, +0.800000, +0.666667 }, { 1, +1.000000, +0.333333 }, { 8, +0.800000, +0.333333 } },
121 { { 2, +0.500000, +1.000000 }, { 7, +0.800000, +0.666667 }, { 9, +0.600000, +0.666667 } },
122 { { 2, +0.500000, +1.000000 }, { 9, +0.600000, +0.666667 }, { 3, +0.400000, +0.666667 } },
123 { { 1, +1.000000, +0.333333 }, { 0, +0.500000, +0.000000 }, { 8, +0.800000, +0.333333 } },
124 { { 4, +0.600000, +0.333333 }, { 0, +0.500000, +0.000000 }, { 5, +0.400000, +0.333333 } },
125 { { 5, +0.400000, +0.333333 }, { 3, +0.400000, +0.666667 }, { 4, +0.600000, +0.333333 } },
126 { { 6, +1.000000, +0.666667 }, { 1, +1.000000, +0.333333 }, { 7, +0.800000, +0.666667 } },
127 { { 7, +0.800000, +0.666667 }, { 2, +0.500000, +1.000000 }, { 6, +1.000000, +0.666667 } },
128 { { 1, +0.000000, +0.333333 }, { 11, +0.200000, +0.333333 }, { 0, +0.500000, +0.000000 } },
129 { { 3, +0.400000, +0.666667 }, { 10, +0.200000, +0.666667 }, { 2, +0.500000, +1.000000 } },
130 { { 0, +0.500000, +0.000000 }, { 11, +0.200000, +0.333333 }, { 5, +0.400000, +0.333333 } },
131 { { 5, +0.400000, +0.333333 }, { 10, +0.200000, +0.666667 }, { 3, +0.400000, +0.666667 } },
132 { { 1, +0.000000, +0.333333 }, { 6, +0.000000, +0.666667 }, { 11, +0.200000, +0.333333 } },
133 { { 6, +0.000000, +0.666667 }, { 2, +0.500000, +1.000000 }, { 10, +0.200000, +0.666667 } },
134 { { 5, +0.400000, +0.333333 }, { 11, +0.200000, +0.333333 }, { 10, +0.200000, +0.666667 } },
135 { { 11, +0.200000, +0.333333 }, { 6, +0.000000, +0.666667 }, { 10, +0.200000, +0.666667 } }
136};
137
138static const Vector kTexturedVertices[] = {
139 { +0.525731, +0.000000, +0.850651 },
140 { -0.525731, +0.000000, +0.850651 },
141 { +0.525731, +0.000000, -0.850651 },
142 { -0.525731, +0.000000, -0.850651 },
143 { +0.000000, +0.850651, +0.525731 },
144 { +0.000000, +0.850651, -0.525731 },
145 { +0.000000, -0.850651, +0.525731 },
146 { +0.000000, -0.850651, -0.525731 },
147 { -0.850651, +0.525731, +0.000000 },
148 { +0.850651, +0.525731, +0.000000 },
149 { -0.850651, -0.525731, +0.000000 },
150 { +0.850651, -0.525731, +0.000000 },
151 { +0.000000, +0.850651, -0.525731 },
152 { +0.525731, +0.000000, -0.850651 }
153};
154
155static const BaseFace kTexturedFaces[][3] = {
156 { { 1, +0.400000, +0.666667 }, { 0, +0.600000, +0.666667 }, { 6, +0.500000, +0.333333 } },
157 { { 3, +0.100000, +0.333333 }, { 2, -0.100000, +0.333333 }, { 5, +0.000000, +0.666667 } },
158 { { 4, +0.500000, +1.000000 }, { 0, +0.600000, +0.666667 }, { 1, +0.400000, +0.666667 } },
159 { { 4, +0.500000, +1.000000 }, { 1, +0.400000, +0.666667 }, { 8, +0.200000, +0.666667 } },
160 { { 12, +1.000000, +0.666667 }, { 13, +0.900000, +0.333333 }, { 9, +0.800000, +0.666667 } },
161 { { 5, +0.000000, +0.666667 }, { 4, +0.500000, +1.000000 }, { 8, +0.200000, +0.666667 } },
162 { { 6, +0.500000, +0.333333 }, { 0, +0.600000, +0.666667 }, { 11, +0.700000, +0.333333 } },
163 { { 7, +0.500000, +0.000000 }, { 2, -0.100000, +0.333333 }, { 3, +0.100000, +0.333333 } },
164 { { 7, +0.500000, +0.000000 }, { 3, +0.100000, +0.333333 }, { 10, +0.300000, +0.333333 } },
165 { { 7, +0.500000, +0.000000 }, { 6, +0.500000, +0.333333 }, { 11, +0.700000, +0.333333 } },
166 { { 8, +0.200000, +0.666667 }, { 1, +0.400000, +0.666667 }, { 10, +0.300000, +0.333333 } },
167 { { 8, +0.200000, +0.666667 }, { 3, +0.100000, +0.333333 }, { 5, +0.000000, +0.666667 } },
168 { { 9, +0.800000, +0.666667 }, { 0, +0.600000, +0.666667 }, { 4, +0.500000, +1.000000 } },
169 { { 9, +0.800000, +0.666667 }, { 13, +0.900000, +0.333333 }, { 11, +0.700000, +0.333333 } },
170 { { 9, +0.800000, +0.666667 }, { 4, +0.500000, +1.000000 }, { 12, +1.000000, +0.666667 } },
171 { { 10, +0.300000, +0.333333 }, { 1, +0.400000, +0.666667 }, { 6, +0.500000, +0.333333 } },
172 { { 10, +0.300000, +0.333333 }, { 3, +0.100000, +0.333333 }, { 8, +0.200000, +0.666667 } },
173 { { 10, +0.300000, +0.333333 }, { 6, +0.500000, +0.333333 }, { 7, +0.500000, +0.000000 } },
174 { { 11, +0.700000, +0.333333 }, { 0, +0.600000, +0.666667 }, { 9, +0.800000, +0.666667 } },
175 { { 11, +0.700000, +0.333333 }, { 13, +0.900000, +0.333333 }, { 7, +0.500000, +0.000000 } }
176};
177
178@implementation PlanetEntity
179
180- (id)init
181{
182 [self release];
183 [NSException raise:NSInternalInconsistencyException format:@"%s, believed dead, called.", __PRETTY_FUNCTION__];
184 return nil;
185}
186
187- (id)initAsAtmosphereForPlanet:(PlanetEntity*)planet dictionary:(NSDictionary*)dict
188{
189 BOOL procGen = [UNIVERSE doProcedurallyTexturedPlanets];
190
191 if (dict == nil)
192 dict = [NSDictionary dictionary];
193
194 self = [super init];
195
196 int percent_land = 100 - [dict oo_intForKey:@"percent_cloud" defaultValue:100 - (3 + (gen_rnd_number() & 31) + (gen_rnd_number() & 31))];
197
198 polar_color_factor = 1.0;
199
200#define CLEAR_SKY_ALPHA 0.05
201#define CLOUD_ALPHA 0.50
202#define POLAR_CLEAR_SKY_ALPHA 0.34
203#define POLAR_CLOUD_ALPHA 0.75
204
205 amb_land[0] = gen_rnd_number() / 256.0;
206 amb_land[1] = gen_rnd_number() / 256.0;
207 amb_land[2] = gen_rnd_number() / 256.0;
208 amb_land[3] = CLEAR_SKY_ALPHA; // blue sky, zero clouds
209 amb_sea[0] = 0.5 + gen_rnd_number() / 512.0;
210 amb_sea[1] = 0.5 + gen_rnd_number() / 512.0;
211 amb_sea[2] = 0.5 + gen_rnd_number() / 512.0;
212 amb_sea[3] = CLOUD_ALPHA; // 50% opaque clouds
213 amb_polar_land[0] = gen_rnd_number() / 256.0;
214 amb_polar_land[1] = gen_rnd_number() / 256.0;
215 amb_polar_land[2] = gen_rnd_number() / 256.0;
216 amb_polar_land[3] = POLAR_CLEAR_SKY_ALPHA; // 34% gray clouds
217 amb_polar_sea[0] = 0.9 + gen_rnd_number() / 2560.0;
218 amb_polar_sea[1] = 0.9 + gen_rnd_number() / 2560.0;
219 amb_polar_sea[2] = 0.9 + gen_rnd_number() / 2560.0;
220 amb_polar_sea[3] = POLAR_CLOUD_ALPHA; // 75% clouds
221
222 // Colour overrides from dictionary
223 OOColor* clearSkyColor = nil;
224 OOColor* cloudColor = nil;
225 OOColor* polarClearSkyColor = nil;
226 OOColor* polarCloudColor = nil;
227 float cloudAlpha;
228
229 clearSkyColor = [OOColor colorWithDescription:[dict objectForKey:@"atmosphere_color"]];
230 cloudColor = [OOColor colorWithDescription:[dict objectForKey:@"cloud_color"]];
231 polarClearSkyColor = [OOColor colorWithDescription:[dict objectForKey:@"polar_atmosphere_color"]];
232 polarCloudColor = [OOColor colorWithDescription:[dict objectForKey:@"polar_cloud_color"]];
233 cloudAlpha = OOClamp_0_1_f([dict oo_floatForKey:@"cloud_alpha" defaultValue:1.0]);
234
235 if (clearSkyColor != nil) {
236 [clearSkyColor getRed:&amb_land[0] green:&amb_land[1] blue:&amb_land[2] alpha:&amb_land[3]];
237 }
238
239 if (cloudColor != nil) {
240 [cloudColor getRed:&amb_sea[0] green:&amb_sea[1] blue:&amb_sea[2] alpha:&amb_sea[3]];
241 }
242
243 if (polarClearSkyColor != nil) {
244 [polarClearSkyColor getRed:&amb_polar_land[0] green:&amb_polar_land[1] blue:&amb_polar_land[2] alpha:&amb_polar_land[3]];
245 } else if (clearSkyColor != nil) {
246 memmove(amb_polar_land, amb_land, sizeof amb_polar_land);
247 amb_polar_land[3] = OOClamp_0_1_f(amb_polar_land[3] * (POLAR_CLEAR_SKY_ALPHA / CLEAR_SKY_ALPHA));
248 }
249
250 if (polarCloudColor != nil) {
251 [polarCloudColor getRed:&amb_polar_sea[0] green:&amb_polar_sea[1] blue:&amb_polar_sea[2] alpha:&amb_polar_sea[3]];
252 } else if (cloudColor != nil) {
253 memmove(amb_polar_sea, amb_sea, sizeof amb_polar_sea);
254 amb_polar_sea[3] *= (POLAR_CLOUD_ALPHA / CLOUD_ALPHA);
255 }
256
257 // amb_land[3] is already 0.05
258 amb_sea[3] *= cloudAlpha;
259 amb_polar_land[3] *= cloudAlpha;
260 amb_polar_sea[3] *= cloudAlpha;
261
262 amb_sea[3] = OOClamp_0_1_f(amb_sea[3]);
263 amb_polar_sea[3] = OOClamp_0_1_f(amb_polar_sea[3]);
264
265 atmosphere = nil;
266
267 if (procGen) {
268 RANROTSeed ranrotSavedSeed = RANROTGetFullSeed();
269 RNG_Seed saved_seed = currentRandomSeed();
270 cloudColor = [OOColor colorWithRed:amb_sea[0] green:amb_sea[1] blue:amb_sea[2] alpha:amb_sea[3]];
271 float cloud_bias = -0.01 * (float)percent_land;
272 float cloud_impress = 1.0 - cloud_bias;
273
274 _texture = [self cloudTextureWithCloudColor:cloudColor cloudImpress:cloud_impress cloudBias:cloud_bias];
275 [_texture retain];
276 isTextureImage = NO;
277
278 setRandomSeed(saved_seed);
279 RANROTSetFullSeed(ranrotSavedSeed);
280 }
281
282 if (!planet) {
283 OOLogERR(@"planet.atmosphere.init.noPlanet", @"planet entity initAsAtmosphereForPlanet: no planet found.");
284 return self;
285 }
286
287 [self setOwner:planet];
288
289 position = [planet position];
290 orientation = [planet orientation];
291
293 collision_radius = planet->collision_radius + ATMOSPHERE_DEPTH; // atmosphere is 500m deep only
294 if (planet->planet_type == STELLAR_TYPE_MINIATURE)
295 collision_radius = planet->collision_radius + ATMOSPHERE_DEPTH * PLANET_MINIATURE_FACTOR * 2.0; // not to scale: invisible otherwise
296
297 shuttles_on_ground = 0;
298 last_launch_time = 0.0;
299 shuttle_launch_interval = 3600.0;
300
301 scanClass = CLASS_NO_DRAW;
302
303 // orientation.w = M_SQRT1_2; // is already planet->orientation
304 // orientation.x = M_SQRT1_2;
305 // orientation.y = 0.0;
306 // orientation.z = 0.0;
307
308 planet_type = STELLAR_TYPE_ATMOSPHERE;
309
310 planet_seed = ranrot_rand(); // random set-up for vertex colours
311
312 [self setUseTexturedModel:YES];
314 [self initialiseBaseTerrainArray:percent_land];
315 unsigned i;
316 for (i = 0; i < next_free_vertex; i++) {
317 [self paintVertex:i:planet_seed];
318 }
319
320 [self scaleVertices];
321
322 // set speed of rotation
323 rotational_velocity = [dict oo_floatForKey:@"atmosphere_rotational_velocity" defaultValue:[planet rotationalVelocity] * (0.9 + (randf() * 0.2))]; // 90-110% of planet rotation speed
324
325 root_planet = planet;
326
327 rotationAxis = vector_up_from_quaternion(orientation);
328
330
331 return self;
332}
333
335{
338 last_launch_time = 0.0;
340 [self setStatus:STATUS_COCKPIT_DISPLAY];
342 [self scaleVertices];
343 if (atmosphere != nil) {
344 atmosphere->collision_radius = collision_radius + ATMOSPHERE_DEPTH * PLANET_MINIATURE_FACTOR * 2.0; // not to scale: invisible otherwise
346 }
347 rotational_velocity = 0.04;
349}
350
351- (id)initFromDictionary:(NSDictionary*)dict withAtmosphere:(BOOL)atmo andSeed:(Random_Seed)p_seed
352{
353 BOOL procGen = [UNIVERSE doProcedurallyTexturedPlanets];
354
355 if (dict == nil)
356 dict = [NSDictionary dictionary];
357 RANROTSeed ranrotSavedSeed = RANROTGetFullSeed();
358
359 self = [super init];
360
362
363 if (atmo)
364 planet_seed = p_seed.a * 13 + p_seed.c * 11 + p_seed.e * 7; // pseudo-random set-up for vertex colours
365 else
366 planet_seed = p_seed.a * 7 + p_seed.c * 11 + p_seed.e * 13; // pseudo-random set-up for vertex colours
367
368 OOTexture* texture = [dict oo_objectOfClass:[OOTexture class] forKey:@"_oo_textureObject"];
369 if (texture != nil) {
370 _texture = [texture retain];
371 isTextureImage = [dict oo_boolForKey:@"_oo_isExplicitlyTextured"];
372 } else {
373 NSDictionary* textureSpec = [dict oo_textureSpecifierForKey:@"texture" defaultName:nil];
374 if (textureSpec != nil) {
375 [self loadTexture:textureSpec];
376 }
377
378 // CIM: nothing actually uses textureSpec after the assignment below, so skip it
379 /* if (textureSpec == nil && !procGen && !atmo)
380 {
381 // Moons use metal.png by default.
382 textureSpec = OOTextureSpecFromObject(@"metal.png", nil);
383 } */
384
385 NSString* seedStr = [dict oo_stringForKey:@"seed"];
386 if (seedStr != nil) {
387 Random_Seed seed = RandomSeedFromString(seedStr);
388 if (!is_nil_seed(seed)) {
389 p_seed = seed;
390 } else {
391 OOLogERR(@"planet.fromDict", @"could not interpret \"%@\" as planet seed, using default.", seedStr);
392 }
393 }
394 }
395
397
398 NSMutableDictionary* planetInfo = [NSMutableDictionary dictionaryWithDictionary:[UNIVERSE generateSystemData:p_seed]];
399 int radius_km = [dict oo_intForKey:KEY_RADIUS
400 defaultValue:[planetInfo oo_intForKey:KEY_RADIUS]];
401 int techlevel = [dict oo_intForKey:KEY_TECHLEVEL
402 defaultValue:[planetInfo oo_intForKey:KEY_TECHLEVEL]];
403
404 shuttles_on_ground = 1 + floor(techlevel * 0.5);
405 last_launch_time = 0.0;
406 shuttle_launch_interval = 3600.0 / shuttles_on_ground; // all are launched in an hour
407
408 last_launch_time = [UNIVERSE getTime] + 30.0 - shuttle_launch_interval; // debug - launch 30s after player enters universe
409
410 collision_radius = radius_km * 10.0; // scale down by a factor of 100 !
411
412 scanClass = CLASS_NO_DRAW;
413
416 orientation.y = 0.0;
417 orientation.z = 0.0;
418
419 [self setUseTexturedModel:(procGen || _texture != nil)];
420
421 int percent_land = [planetInfo oo_intForKey:@"percent_land" defaultValue:24 + (gen_rnd_number() % 48)];
422 // if (isTextured) percent_land = atmo ? 0 :100; // moon/planet override
423
424 // save the current random number generator seed
425 RNG_Seed saved_seed = currentRandomSeed();
426
427 // For historical reasons, stir the PRNG vertexCount times.
428 unsigned i;
429 for (i = 0; i < vertexCount; i++)
431
432 [planetInfo setObject:[NSNumber numberWithFloat:0.01 * percent_land] forKey:@"land_fraction"];
433
434 polar_color_factor = [dict oo_doubleForKey:@"polar_color_factor" defaultValue:0.5f];
435
436 Vector land_hsb, sea_hsb, land_polar_hsb, sea_polar_hsb;
437
438 if (isTextureImage) {
439 // standard overlay colours.
440 land_hsb.x = 0.0;
441 land_hsb.y = 0.0;
442 land_hsb.z = 1.0; // non-saturated fully bright (white)
443 sea_hsb.x = 0.0;
444 sea_hsb.y = 1.0;
445 sea_hsb.z = 1.0; // fully-saturated fully bright (red)
446 // override the mainPlanet texture colour...
447 [self setTextureColorForPlanet:!![dict objectForKey:@"mainForLocalSystem"] inSystem:[dict oo_boolForKey:@"mainForLocalSystem" defaultValue:NO]];
448 } else {
449 // random land & sea colours.
450 land_hsb.x = gen_rnd_number() / 256.0;
451 land_hsb.y = gen_rnd_number() / 256.0;
452 land_hsb.z = 0.5 + gen_rnd_number() / 512.0;
453 sea_hsb.x = gen_rnd_number() / 256.0;
454 sea_hsb.y = gen_rnd_number() / 256.0;
455 sea_hsb.z = 0.5 + gen_rnd_number() / 512.0;
456 while (dot_product(land_hsb, sea_hsb) > .80) // make sure land and sea colors differ significantly
457 {
458 sea_hsb.x = gen_rnd_number() / 256.0;
459 sea_hsb.y = gen_rnd_number() / 256.0;
460 sea_hsb.z = 0.5 + gen_rnd_number() / 512.0;
461 }
462
463 // assign land_hsb and sea_hsb overrides from planetinfo.plist if they're there.
464 ScanVectorFromString([dict objectForKey:@"land_hsb_color"], &land_hsb);
465 ScanVectorFromString([dict objectForKey:@"sea_hsb_color"], &sea_hsb);
466
467 // polar areas are brighter but have less color (closer to white)
468 land_polar_hsb.x = land_hsb.x;
469 land_polar_hsb.y = (land_hsb.y / 4.0);
470 land_polar_hsb.z = 1.0 - (land_hsb.z / 10.0);
471 sea_polar_hsb.x = sea_hsb.x;
472 sea_polar_hsb.y = (sea_hsb.y / 4.0);
473 sea_polar_hsb.z = 1.0 - (sea_hsb.z / 10.0);
474
475 OOColor* amb_land_color = [OOColor colorWithHue:land_hsb.x saturation:land_hsb.y brightness:land_hsb.z alpha:1.0];
476 OOColor* amb_sea_color = [OOColor colorWithHue:sea_hsb.x saturation:sea_hsb.y brightness:sea_hsb.z alpha:1.0];
477 OOColor* amb_polar_land_color = [OOColor colorWithHue:land_polar_hsb.x saturation:land_polar_hsb.y brightness:land_polar_hsb.z alpha:1.0];
478 OOColor* amb_polar_sea_color = [OOColor colorWithHue:sea_polar_hsb.x saturation:sea_polar_hsb.y brightness:sea_polar_hsb.z alpha:1.0];
479
483 amb_land[3] = 1.0;
487 amb_sea[3] = 1.0;
491 amb_polar_land[3] = 1.0;
495 amb_polar_sea[3] = 1.0;
496
497 [planetInfo setObject:amb_land_color forKey:@"land_color"];
498 [planetInfo setObject:amb_sea_color forKey:@"sea_color"];
499 [planetInfo setObject:amb_polar_land_color forKey:@"polar_land_color"];
500 [planetInfo setObject:amb_polar_sea_color forKey:@"polar_sea_color"];
501 }
502
503 if (procGen && _texture == nil) {
504 _texture = [self planetTextureWithInfo:planetInfo];
505 isTextureImage = NO;
506 [_texture retain];
507 }
508
510 [self initialiseBaseTerrainArray:percent_land];
511
512 for (i = 0; i < next_free_vertex; i++) {
513 [self paintVertex:i:planet_seed];
514 }
515
516 [self scaleVertices];
517 // set speed of rotation
518 if ([dict objectForKey:@"rotational_velocity"]) {
519 rotational_velocity = [dict oo_floatForKey:@"rotational_velocity" defaultValue:0.01f * randf()]; // 0.0 .. 0.01 avr 0.005
520 } else {
521 rotational_velocity = [planetInfo oo_floatForKey:@"rotation_speed" defaultValue:0.002 * (0.5 + 0.5 * randf())]; // 0.001 .. 0.002 avr 0.0015
522 rotational_velocity *= [planetInfo oo_floatForKey:@"rotation_speed_factor" defaultValue:1.0f];
523 }
524
525 // do atmosphere
526 NSDictionary* atmoDict = dict;
527 if (_texture != nil)
528 atmoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"0", @"percent_cloud", [NSNumber numberWithFloat:[planetInfo oo_floatForKey:@"cloud_alpha" defaultValue:1.0]], @"cloud_alpha", nil];
529 if (atmo)
530 atmosphere = [[PlanetEntity alloc] initAsAtmosphereForPlanet:self dictionary:atmoDict];
531
532 setRandomSeed(saved_seed);
533 RANROTSetFullSeed(ranrotSavedSeed);
534
535 // set energy
536 energy = collision_radius * 1000.0;
537
538 root_planet = self;
539
541
542 /* MKW - rotate planet based on current time.
543 * - do it here so that we catch all planets (OXP-added and otherwise! */
544 int deltaT = floor(fmod([PLAYER clockTimeAdjusted], 86400));
546
547 [self setStatus:STATUS_ACTIVE];
548
550
551 return self;
552}
553
554- (void)dealloc
555{
556 [self deleteDisplayLists];
557
561
563
564 [super dealloc];
565}
566
568{
569 NSString* typeString;
570 switch (planet_type) {
572 typeString = @"STELLAR_TYPE_MINIATURE";
573 break;
575 typeString = @"STELLAR_TYPE_NORMAL_PLANET";
576 break;
578 typeString = @"STELLAR_TYPE_ATMOSPHERE";
579 break;
581 typeString = @"STELLAR_TYPE_MOON";
582 break;
583
584 default:
585 typeString = @"UNKNOWN";
586 }
587 return [NSString stringWithFormat:@"ID: %u position: %@ type: %@ radius: %.3fkm", [self universalID], HPVectorDescription([self position]), typeString, 0.001 * [self radius]];
588}
589
591{
592 switch (planet_type) {
595 return NO;
596 break;
599 case STELLAR_TYPE_SUN:
600 return YES;
601 break;
602 }
603 return YES;
604}
605
606- (BOOL)checkCloseCollisionWith:(Entity*)other
607{
608#ifdef OO_DEBUG_MONITOR_ENABLED
610 OOLog(@"planet.collide", @"PLANET Collision!");
611#endif
612
613 if (!other)
614 return NO;
615 if (other->isShip) {
616 ShipEntity* ship = (ShipEntity*)other;
617 if ([ship behaviour] == BEHAVIOUR_LAND_ON_PLANET) {
618 return NO;
619 }
620#ifndef NDEBUG
621 if ([ship reportAIMessages]) {
622 HPVector p1 = ship->position;
623 OOLog(@"planet.collide.shipHit", @"DEBUG: %@ %d collided with planet at (%.1f,%.1f,%.1f)", [ship name], [ship universalID], p1.x, p1.y, p1.z);
624 }
625#endif
626 }
627
628 return YES;
629}
630
631- (void)update:(OOTimeDelta)delta_t
632{
633 [super update:delta_t];
635
636 switch (planet_type) {
638 // we have atmosphere in any case.
639 {
641 double atmo = 10.0 * (atmosphere->collision_radius - collision_radius); // effect starts at 10x the height of the clouds
642
643 if ((alt > 0) && (alt <= atmo)) {
644 double aleph = (atmo - alt) / atmo;
645 if (aleph < 0.0)
646 aleph = 0.0;
647 if (aleph > 1.0)
648 aleph = 1.0;
649
650 [UNIVERSE setSkyColorRed:0.8 * aleph * aleph
651 green:0.8 * aleph * aleph
652 blue:0.9 * aleph
653 alpha:aleph];
654 } else if (alt > 0 && alt <= atmo * 1.5) {
655 /* Without this, if you leave the atmosphere at very low
656 * frame rates, you might still have skyalpha > 0 when you
657 * go sun-skimming. Since skyalpha > 0 simulates atmospheric
658 * friction and continually pumps heat into the ship, this
659 * is rapidly fatal - CIM */
660 [UNIVERSE setSkyColorRed:0.0f
661 green:0.0f
662 blue:0.0f
663 alpha:0.0f];
664 }
665 }
666 case STELLAR_TYPE_MOON: {
667 double ugt = [UNIVERSE getTime];
668
670 [self launchShuttle];
672 last_launch_time = ugt;
673 }
674 }
675
677 // normal planetary rotation
678 if (atmosphere)
679 [atmosphere update:delta_t];
681 [self orientationChanged];
682 break;
683
685 // atmospheric rotation
687 [self orientationChanged];
688 } break;
689
690 case STELLAR_TYPE_SUN:
691 break;
692 }
693}
694
695- (void)setPosition:(HPVector)posn
696{
697 position = posn;
698 [atmosphere setPosition:posn];
699}
700
701- (void)setOrientation:(Quaternion)inOrientation
702{
704 [super setOrientation:inOrientation];
705 if (atmosphere)
706 [atmosphere setOrientation:inOrientation];
707}
708
709- (void)setUseTexturedModel:(BOOL)flag
710{
711 if (flag) {
712 useTexturedModel = YES;
713 vertexCount = sizeof kTexturedVertices / sizeof *kTexturedVertices;
714 } else {
715 useTexturedModel = NO;
716 vertexCount = sizeof kUntexturedVertices / sizeof *kTexturedVertices;
717 }
718}
719
720// TODO: some translucent stuff is drawn in the opaque pass, which is Naughty.
721- (void)drawImmediate:(bool)immediate translucent:(bool)translucent
722{
723 if ([UNIVERSE breakPatternHide] || translucent || immediate)
724 return; // DON'T DRAW
725 GLfloat radWithAtmosphere = [self radius] + ATMOSPHERE_DEPTH;
726 if (radWithAtmosphere * radWithAtmosphere * 1.1 < cam_zero_distance) {
727 // for some reason this check doesn't work when extremely close to
728 // the planet and with the horizon near the side of the frame (FP
729 // inaccuracy?)
730 if (![UNIVERSE viewFrustumIntersectsSphereAt:cameraRelativePosition withRadius:radWithAtmosphere]) {
731 // Don't draw
732 return;
733 }
734 }
735
736 [self drawUnconditionally];
737}
738
740{
741 uint8_t subdivideLevel = 2; // 4 is probably the maximum!
742
743 double drawFactor = [[UNIVERSE gameView] viewSize].width / 100.0;
744 double drawRatio2 = drawFactor * collision_radius / sqrt_zero_distance; // equivalent to size on screen in pixels
745
746 if (cam_zero_distance > 0.0) {
747 subdivideLevel = 2 + floor(drawRatio2);
748 if (subdivideLevel > 4)
749 subdivideLevel = 4;
750 }
751
753 subdivideLevel = [UNIVERSE reducedDetail] ? 3 : 4; // max detail or less
754 }
755
756 lastSubdivideLevel = subdivideLevel; // record
757
759 OOGL(glPushAttrib(GL_ENABLE_BIT));
760
761 // OOGL(glEnable(GL_LIGHTING));
762 // OOGL(glEnable(GL_LIGHT1));
763 GLfloat specular[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
764 OOGL(glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular));
765 OOGL(glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 0));
766
767 /*
768
769 The depth test gets disabled in parts of this and instead
770 we rely on the painters algorithm instead.
771
772 The depth buffer isn't granular enough to cope with huge objects at vast
773 distances.
774
775 */
776
777 BOOL ignoreDepthBuffer = (planet_type == STELLAR_TYPE_ATMOSPHERE);
778
779 if (cam_zero_distance > collision_radius * collision_radius * 25) // is 'far away'
780 {
781 ignoreDepthBuffer |= YES;
782 }
783
785
786 switch (planet_type) {
788 if (root_planet) {
789 subdivideLevel = root_planet->lastSubdivideLevel; // copy it from the planet (stops jerky LOD and such)
790 }
791 OOGLMultModelVew(rotMatrix); // rotate the clouds!
792 OOGL(glEnable(GL_BLEND));
793 // OOGL(glDisable(GL_LIGHTING));
794 // Fall through.
795
799 // if ((gDebugFlags & DEBUG_WIREFRAME_GRAPHICS)
800 if ([UNIVERSE wireframeGraphics]) {
801 // Drop the detail level a bit, it still looks OK in wireframe and does not penalize
802 // the system that much.
803 subdivideLevel = 2;
805 }
806
807 {
808 GLfloat mat1[] = { 1.0, 1.0, 1.0, 1.0 }; // opaque white
809
810 if (_texture != nil) {
811 if ([_texture isCubeMap]) {
812#if OO_TEXTURE_CUBE_MAP
813 OOGL(glDisable(GL_TEXTURE_2D));
814 OOGL(glEnable(GL_TEXTURE_CUBE_MAP));
815#endif
816 } else {
817 // OOGL(glEnable(GL_TEXTURE_2D));
818 }
819 OOGL(glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, mat1));
820 [_texture apply];
821 } else {
822 OOGL(glDisable(GL_TEXTURE_2D));
824 }
825
826 OOGL(glShadeModel(GL_SMOOTH));
827
828 // far enough away to draw flat ?
829 if (ignoreDepthBuffer) {
830 OOGL(glDisable(GL_DEPTH_TEST));
831 }
832
833 OOGL(glColor4fv(mat1));
834 OOGL(glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat1));
835
836 OOGL(glEnableClientState(GL_COLOR_ARRAY));
837 OOGL(glColorPointer(4, GL_FLOAT, 0, vertexdata.color_array));
838 // OOGL(glEnableClientState(GL_VERTEX_ARRAY));
839 OOGL(glVertexPointer(3, GL_FLOAT, 0, vertexdata.vertex_array));
840 // OOGL(glEnableClientState(GL_NORMAL_ARRAY));
841 OOGL(glNormalPointer(GL_FLOAT, 0, vertexdata.normal_array));
842
843 if (_texture != nil) {
844 OOGL(glEnableClientState(GL_TEXTURE_COORD_ARRAY));
845 if ([_texture isCubeMap]) {
846 OOGL(glTexCoordPointer(3, GL_FLOAT, 0, vertexdata.vertex_array));
847 } else {
848 OOGL(glTexCoordPointer(2, GL_FLOAT, 0, vertexdata.uv_array));
849 }
850 } else {
851 OOGL(glDisableClientState(GL_TEXTURE_COORD_ARRAY));
852 }
853
854 if (displayListNames[subdivideLevel] != 0) {
855 OOGL(glCallList(displayListNames[subdivideLevel]));
856 } else {
857
858 OOGL(displayListNames[subdivideLevel] = glGenLists(1));
859 if (displayListNames[subdivideLevel] != 0) // sanity check
860 {
861 OOGL(glNewList(displayListNames[subdivideLevel], GL_COMPILE_AND_EXECUTE));
862
863 OOGL(glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE));
864 OOGL(glEnable(GL_COLOR_MATERIAL));
865
866 [self drawModelWithVertexArraysAndSubdivision:subdivideLevel];
867
868 OOGL(glDisable(GL_COLOR_MATERIAL));
869 OOGL(glEndList());
870 }
871 }
872
873#if OO_TEXTURE_CUBE_MAP
874 if ([_texture isCubeMap]) {
875 OOGL(glDisable(GL_TEXTURE_CUBE_MAP));
876 OOGL(glEnable(GL_TEXTURE_2D));
877 }
878#endif
879
880 OOGL(glDisableClientState(GL_COLOR_ARRAY));
881 OOGL(glDisableClientState(GL_TEXTURE_COORD_ARRAY));
882 OOGL(glEnable(GL_DEPTH_TEST));
883 }
884
885 // if ((gDebugFlags & DEBUG_WIREFRAME_GRAPHICS)
886 if ([UNIVERSE wireframeGraphics]) {
888 }
889 // OOGL(glDisableClientState(GL_VERTEX_ARRAY));
890 // OOGL(glDisableClientState(GL_NORMAL_ARRAY));
891
892 break;
893
894 case STELLAR_TYPE_SUN:
895 break;
896 }
897
898 if (ignoreDepthBuffer) {
899 OOGL(glEnable(GL_DEPTH_TEST));
900 }
901 OOGL(glEnable(GL_TEXTURE_2D));
902 // OOGL(glEnable(GL_LIGHTING));
903 OOGL(glDisable(GL_BLEND));
904
905 OOGL(glPopAttrib());
907 OOCheckOpenGLErrors(@"PlanetEntity after drawing %@", self);
908
909 if (atmosphere) {
910 OOGLPopModelView(); // get old draw matrix back
911 OOGLPushModelView(); // and store it again
912 OOGLTranslateModelView(cameraRelativePosition); // centre on the planet
913 // rotate
914 // OOGLMultModelView([atmosphere rotationMatrix]);
915 // draw atmosphere entity
917 }
918}
919
920#ifdef OO_DEBUG_MONITOR_ENABLED
921- (PlanetEntity*)atmosphere
922{
923 return atmosphere;
924}
925#endif
926
928{
929 return planet_seed;
930}
931
933{
934 return _texture != nil;
935}
936
937- (NSString*)textureFileName
938{
939 return _textureFileName;
940}
941
942- (void)setTextureColorForPlanet:(BOOL)isMain inSystem:(BOOL)isLocal
943{
944 Vector land_hsb, land_polar_hsb;
945 land_hsb.x = 0.0;
946 land_hsb.y = 0.0;
947 land_hsb.z = 1.0; // white
948
949 // the colour override should only apply to main planets
950 if (isMain) {
951 if (isLocal)
952 ScanVectorFromString([[UNIVERSE currentSystemData] objectForKey:@"texture_hsb_color"], &land_hsb);
953 else
954 ScanVectorFromString([[UNIVERSE generateSystemData:[PLAYER target_system_seed]] objectForKey:@"texture_hsb_color"], &land_hsb);
955 }
956
957 land_polar_hsb.x = land_hsb.x;
958 land_polar_hsb.y = (land_hsb.y / 5.0);
959 land_polar_hsb.z = 1.0 - (land_hsb.z / 10.0);
960
961 amb_sea[0] = amb_land[0] = [[OOColor colorWithHue:land_hsb.x saturation:land_hsb.y brightness:land_hsb.z alpha:1.0] redComponent];
962 amb_sea[1] = amb_land[1] = [[OOColor colorWithHue:land_hsb.x saturation:land_hsb.y brightness:land_hsb.z alpha:1.0] blueComponent];
963 amb_sea[2] = amb_land[2] = [[OOColor colorWithHue:land_hsb.x saturation:land_hsb.y brightness:land_hsb.z alpha:1.0] greenComponent];
964 amb_sea[3] = amb_land[3] = 1.0;
965 amb_polar_sea[0] = amb_polar_land[0] = [[OOColor colorWithHue:land_polar_hsb.x saturation:land_polar_hsb.y brightness:land_polar_hsb.z alpha:1.0] redComponent];
966 amb_polar_sea[1] = amb_polar_land[1] = [[OOColor colorWithHue:land_polar_hsb.x saturation:land_polar_hsb.y brightness:land_polar_hsb.z alpha:1.0] blueComponent];
967 amb_polar_sea[2] = amb_polar_land[2] = [[OOColor colorWithHue:land_polar_hsb.x saturation:land_polar_hsb.y brightness:land_polar_hsb.z alpha:1.0] greenComponent];
968 amb_polar_sea[3] = amb_polar_land[3] = 1.0;
969}
970
971- (BOOL)setUpPlanetFromTexture:(NSString*)fileName
972{
973 if (fileName == nil)
974 return NO;
975
976 [self loadTexture:OOTextureSpecFromObject(fileName, nil)];
977 [self deleteDisplayLists];
978
979 unsigned i;
980 [self setUseTexturedModel:YES];
981
982 // recolour main planet according to "texture_hsb_color"
983 // this function is only called for local systems!
984 [self setTextureColorForPlanet:([UNIVERSE planet] == self) inSystem:YES];
985
988 for (i = 0; i < next_free_vertex; i++) {
989 [self paintVertex:i:planet_seed];
990 }
991
992 [self scaleVertices];
993
994 GLfloat oldCloudAlpha = 0.0;
995 if (atmosphere) {
996 oldCloudAlpha = [atmosphere amb_sea][3] / CLOUD_ALPHA;
997 }
998
999 NSDictionary* atmo_dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"0", @"percent_cloud", [NSNumber numberWithFloat:oldCloudAlpha], @"cloud_alpha", nil];
1000 [atmosphere autorelease];
1001 atmosphere = [self hasAtmosphere] ? [[PlanetEntity alloc] initAsAtmosphereForPlanet:self dictionary:atmo_dictionary] : nil;
1002
1003 // rotationAxis = vector_up_from_quaternion(orientation);
1004
1005 return [self isTextured];
1006}
1007
1009{
1010 return polar_color_factor;
1011}
1012
1013- (GLfloat*)amb_land
1014{
1015 return amb_land;
1016}
1017
1019{
1020 return amb_polar_land;
1021}
1022
1023- (GLfloat*)amb_sea
1024{
1025 return amb_sea;
1026}
1027
1028- (GLfloat*)amb_polar_sea
1029{
1030 return amb_polar_sea;
1031}
1032
1034{
1035 return planet_type;
1036}
1037
1038- (void)setPlanetType:(OOStellarBodyType)pt
1039{
1040 planet_type = pt;
1041}
1042
1043- (double)radius
1044{
1045 return collision_radius;
1046}
1047
1048- (void)setRadius:(GLfloat)rad
1049{
1050 collision_radius = rad;
1051}
1052
1054{
1055 return rotational_velocity;
1056}
1057
1058- (void)setRotationalVelocity:(double)v
1059{
1060 if (atmosphere) // change rotation speed proportionally
1061 {
1063 }
1065}
1066
1068{
1069 return sqrt_zero_distance;
1070}
1071
1073{
1074 return atmosphere != nil;
1075}
1076
1077- (void)drawModelWithVertexArraysAndSubdivision:(int)subdivide
1078{
1079 OOGL(glDrawElements(GL_TRIANGLES, 3 * n_triangles[subdivide], GL_UNSIGNED_INT, &vertexdata.index_array[triangle_start[subdivide]]));
1080}
1081
1083{
1084 ShipEntity* shuttle_ship;
1085 Quaternion q1;
1086 HPVector launch_pos = position;
1087 double start_distance = collision_radius + 125.0;
1088
1090
1092
1093 launch_pos.x += start_distance * vf.x;
1094 launch_pos.y += start_distance * vf.y;
1095 launch_pos.z += start_distance * vf.z;
1096
1097 shuttle_ship = [UNIVERSE newShipWithRole:@"shuttle"]; // retain count = 1
1098 if (shuttle_ship) {
1099 if (![shuttle_ship crew]) {
1101 }
1102
1103 [shuttle_ship setPosition:launch_pos];
1105
1106 [shuttle_ship setScanClass:CLASS_NEUTRAL];
1107 [shuttle_ship setCargoFlag:CARGO_FLAG_FULL_PLENTIFUL];
1108 [shuttle_ship switchAITo:@"oolite-shuttleAI.js"];
1109 [UNIVERSE addEntity:shuttle_ship];
1110
1111 [shuttle_ship release];
1112 }
1113}
1114
1115- (void)welcomeShuttle:(ShipEntity*)shuttle
1116{
1118}
1119
1121{
1122 BOOL isTextured = [self isTextured];
1123 static BOOL lastOneWasTextured;
1124
1125 if (lastOneWasTextured != isTextured) {
1126 if (sEdgeToVertex != NULL) {
1127 NSFreeMapTable(sEdgeToVertex);
1128 sEdgeToVertex = NULL;
1129 }
1130 lastOneWasTextured = isTextured;
1131 }
1132
1133 if (sEdgeToVertex == NULL) {
1134 sEdgeToVertex = NSCreateMapTable(NSIntegerMapKeyCallBacks, NSIntegerMapValueCallBacks, 7680); // make a new one
1135 next_free_vertex = 0;
1136
1137 const Vector* vertices = NULL;
1138 const BaseFace(*faces)[3] = NULL;
1139 GLuint faceCount = 0;
1140 if (useTexturedModel) {
1141 vertices = kTexturedVertices;
1142 faces = kTexturedFaces;
1143 faceCount = sizeof kTexturedFaces / sizeof *kTexturedFaces;
1144 } else {
1145 vertices = kUntexturedVertices;
1146 faces = kUntexturedFaces;
1147 faceCount = sizeof kUntexturedFaces / sizeof *kUntexturedFaces;
1148 }
1149
1150 // set first 12 or 14 vertices
1151 GLuint vi;
1152 for (vi = 0; vi < vertexCount; vi++) {
1153 base_vertex_array[next_free_vertex++] = vertices[vi];
1154 }
1155
1156 // set first 20 triangles
1157 triangle_start[0] = 0;
1158 n_triangles[0] = faceCount;
1159 GLuint fi;
1160 for (fi = 0; fi < faceCount; fi++) {
1161 unsigned j;
1162 for (j = 0; j < 3; j++) {
1163 vertex_index_array[fi * 3 + j] = faces[fi][j].v;
1164 texture_uv_array[faces[fi][j].v * 2 + 0] = faces[fi][j].s;
1165 texture_uv_array[faces[fi][j].v * 2 + 1] = faces[fi][j].t;
1166 }
1167 }
1168
1169 // for the next levels of subdivision simply build up from the level below!...
1170 unsigned sublevel;
1171 for (sublevel = 0; sublevel < MAX_SUBDIVIDE - 1; sublevel++) {
1172 int newlevel = sublevel + 1;
1175
1176 int tri;
1177 for (tri = 0; tri < n_triangles[sublevel]; tri++) {
1178 // get the six vertices for this group of four triangles
1179 int v0 = vertex_index_array[triangle_start[sublevel] + tri * 3 + 0];
1180 int v1 = vertex_index_array[triangle_start[sublevel] + tri * 3 + 1];
1181 int v2 = vertex_index_array[triangle_start[sublevel] + tri * 3 + 2];
1182 int v01 = baseVertexIndexForEdge(v0, v1, isTextured); // sets it up if required
1183 int v12 = baseVertexIndexForEdge(v1, v2, isTextured); // ..
1184 int v20 = baseVertexIndexForEdge(v2, v0, isTextured); // ..
1185 // v0 v01 v20
1186 vertex_index_array[triangle_start[newlevel] + tri * 12 + 0] = v0;
1187 vertex_index_array[triangle_start[newlevel] + tri * 12 + 1] = v01;
1188 vertex_index_array[triangle_start[newlevel] + tri * 12 + 2] = v20;
1189 // v01 v1 v12
1190 vertex_index_array[triangle_start[newlevel] + tri * 12 + 3] = v01;
1191 vertex_index_array[triangle_start[newlevel] + tri * 12 + 4] = v1;
1192 vertex_index_array[triangle_start[newlevel] + tri * 12 + 5] = v12;
1193 // v20 v12 v2
1194 vertex_index_array[triangle_start[newlevel] + tri * 12 + 6] = v20;
1195 vertex_index_array[triangle_start[newlevel] + tri * 12 + 7] = v12;
1196 vertex_index_array[triangle_start[newlevel] + tri * 12 + 8] = v2;
1197 // v01 v12 v20
1198 vertex_index_array[triangle_start[newlevel] + tri * 12 + 9] = v01;
1199 vertex_index_array[triangle_start[newlevel] + tri * 12 + 10] = v12;
1200 vertex_index_array[triangle_start[newlevel] + tri * 12 + 11] = v20;
1201 }
1202 }
1203 }
1204
1205 // all done - copy the indices to the instance
1206 unsigned i;
1207 for (i = 0; i < MAX_TRI_INDICES; i++) {
1208 vertexdata.index_array[i] = vertex_index_array[i];
1209 }
1210}
1211
1212static unsigned baseVertexIndexForEdge(GLushort va, GLushort vb, BOOL textured)
1213{
1214 if (va < vb) {
1215 GLushort temp = va;
1216 va = vb;
1217 vb = temp;
1218 }
1219 void* key = (void*)(((uintptr_t)va << 16) | vb);
1220 uintptr_t num = (uintptr_t)NSMapGet(sEdgeToVertex, key);
1221 if (num != 0) {
1222 // Overall cache hit rate is just over 83 %.
1223 return (unsigned)num - 1;
1224 } else {
1225 unsigned vindex = next_free_vertex++;
1226 NSCAssert(vindex < sizeof base_vertex_array / sizeof *base_vertex_array, @"Vertex array overflow in planet setup.");
1227
1228 // calculate position of new vertex
1230 pos = vector_normal(pos); // guaranteed non-zero
1232
1233 if (textured) {
1234 // calculate new texture coordinates
1235 NSPoint uva = (NSPoint) { texture_uv_array[va * 2], texture_uv_array[va * 2 + 1] };
1236 NSPoint uvb = (NSPoint) { texture_uv_array[vb * 2], texture_uv_array[vb * 2 + 1] };
1237
1238 // if either of these is the polar vertex treat it specially to help with polar distortion:
1239 if (uva.y == 0.0 || uva.y == 1.0)
1240 uva.x = uvb.x;
1241 if (uvb.y == 0.0 || uvb.y == 1.0)
1242 uvb.x = uva.x;
1243
1244 texture_uv_array[vindex * 2] = 0.5 * (uva.x + uvb.x);
1245 texture_uv_array[vindex * 2 + 1] = 0.5 * (uva.y + uvb.y);
1246 }
1247
1248 // add new edge to the look-up
1249 num = vindex + 1;
1250 NSMapInsertKnownAbsent(sEdgeToVertex, key, (void*)num);
1251 return vindex;
1252 }
1253}
1254
1255- (void)initialiseBaseTerrainArray:(int)percent_land
1256{
1257 RANROTSeed saved_seed = RANROTGetFullSeed();
1258
1259 // set first 12 or 14 vertices
1260 if (percent_land >= 0) {
1261 GLuint vi;
1262 for (vi = 0; vi < vertexCount; vi++) {
1263 if (gen_rnd_number() < 256 * percent_land / 100)
1264 base_terrain_array[vi] = 0; // land
1265 else
1266 base_terrain_array[vi] = 100; // sea
1267 }
1268 }
1269
1270 // for the next levels of subdivision simply build up from the level below!...
1271 BOOL isTextured = [self isTextured];
1272
1273 int sublevel;
1274 for (sublevel = 0; sublevel < MAX_SUBDIVIDE - 1; sublevel++) {
1275 int tri;
1276 for (tri = 0; tri < n_triangles[sublevel]; tri++) {
1277 // get the six vertices for this group of four triangles
1278 int v0 = vertex_index_array[triangle_start[sublevel] + tri * 3 + 0];
1279 int v1 = vertex_index_array[triangle_start[sublevel] + tri * 3 + 1];
1280 int v2 = vertex_index_array[triangle_start[sublevel] + tri * 3 + 2];
1281 int v01 = baseVertexIndexForEdge(v0, v1, isTextured); // sets it up if required
1282 int v12 = baseVertexIndexForEdge(v1, v2, isTextured); // ..
1283 int v20 = baseVertexIndexForEdge(v2, v0, isTextured); // ..
1284 // v01
1287 else {
1288 uint32_t s1 = 0xffff0000 * base_vertex_array[v01].x;
1289 uint32_t s2 = 0x00ffff00 * base_vertex_array[v01].y;
1290 uint32_t s3 = 0x0000ffff * base_vertex_array[v01].z;
1291 ranrot_srand(s1 + s2 + s3);
1292 base_terrain_array[v01] = (ranrot_rand() & 4) * 25;
1293 }
1294 // v12
1297 else {
1298 uint32_t s1 = 0xffff0000 * base_vertex_array[v12].x;
1299 uint32_t s2 = 0x00ffff00 * base_vertex_array[v12].y;
1300 uint32_t s3 = 0x0000ffff * base_vertex_array[v12].z;
1301 ranrot_srand(s1 + s2 + s3);
1302 base_terrain_array[v12] = (ranrot_rand() & 4) * 25;
1303 }
1304 // v20
1307 else {
1308 uint32_t s1 = 0xffff0000 * base_vertex_array[v20].x;
1309 uint32_t s2 = 0x00ffff00 * base_vertex_array[v20].y;
1310 uint32_t s3 = 0x0000ffff * base_vertex_array[v20].z;
1311 ranrot_srand(s1 + s2 + s3);
1312 base_terrain_array[v20] = (ranrot_rand() & 4) * 25;
1313 }
1314 }
1315 }
1316
1317 RANROTSetFullSeed(saved_seed);
1318}
1319
1320- (void)paintVertex:(unsigned)vi seed:(int)seed
1321{
1322 RANROTSeed saved_seed = RANROTGetFullSeed();
1323 BOOL isTextured = _texture != nil;
1324
1325 GLfloat paint_land[4] = { 0.2, 0.9, 0.0, 1.0 };
1326 GLfloat paint_sea[4] = { 0.0, 0.2, 0.9, 1.0 };
1327 GLfloat paint_color[4];
1329 int r = isTextured ? 0 : base_terrain_array[vi]; // use land color (0) for textured planets
1330 int i;
1331 double pole_blend = v.z * v.z * polar_color_factor;
1332 if (pole_blend < 0.0)
1333 pole_blend = 0.0;
1334 if (pole_blend > 1.0)
1335 pole_blend = 1.0;
1336
1337 paint_land[0] = (1.0 - pole_blend) * amb_land[0] + pole_blend * amb_polar_land[0];
1338 paint_land[1] = (1.0 - pole_blend) * amb_land[1] + pole_blend * amb_polar_land[1];
1339 paint_land[2] = (1.0 - pole_blend) * amb_land[2] + pole_blend * amb_polar_land[2];
1340 paint_sea[0] = (1.0 - pole_blend) * amb_sea[0] + pole_blend * amb_polar_sea[0];
1341 paint_sea[1] = (1.0 - pole_blend) * amb_sea[1] + pole_blend * amb_polar_sea[1];
1342 paint_sea[2] = (1.0 - pole_blend) * amb_sea[2] + pole_blend * amb_polar_sea[2];
1343 if (planet_type == STELLAR_TYPE_ATMOSPHERE) // do alphas
1344 {
1345 paint_land[3] = (1.0 - pole_blend) * amb_land[3] + pole_blend * amb_polar_land[3];
1346 paint_sea[3] = (1.0 - pole_blend) * amb_sea[3] + pole_blend * amb_polar_sea[3];
1347 }
1348
1349 ranrot_srand((uint32_t)(seed + v.x * 1000 + v.y * 100 + v.z * 10));
1350
1351 for (i = 0; i < 3; i++) {
1352 double cv = (ranrot_rand() % 100) * 0.01; // 0..1 ***** DON'T CHANGE THIS LINE, '% 100' MAY NOT BE EFFICIENT BUT THE PATTERNING IS GOOD.
1353 paint_land[i] += (cv - 0.5) * 0.1;
1354 paint_sea[i] += (cv - 0.5) * 0.1;
1355 }
1356
1357 for (i = 0; i < 4; i++) {
1358 if (planet_type == STELLAR_TYPE_ATMOSPHERE && isTextured)
1359 paint_color[i] = 1.0;
1360 else
1361 paint_color[i] = (r * paint_sea[i]) * 0.01 + ((100 - r) * paint_land[i]) * 0.01;
1362 // finally initialise the color array entry
1363 vertexdata.color_array[vi * 4 + i] = paint_color[i];
1364 }
1365
1366 RANROTSetFullSeed(saved_seed);
1367}
1368
1370{
1371 NSUInteger vi;
1372 for (vi = 0; vi < next_free_vertex; vi++) {
1374 vertexdata.normal_array[vi] = v;
1375 vertexdata.vertex_array[vi] = make_vector(v.x * collision_radius, v.y * collision_radius, v.z * collision_radius);
1376
1377 vertexdata.uv_array[vi * 2] = texture_uv_array[vi * 2];
1378 vertexdata.uv_array[vi * 2 + 1] = texture_uv_array[vi * 2 + 1];
1379 }
1380}
1381
1383{
1384 unsigned i;
1385 for (i = 0; i < MAX_SUBDIVIDE; i++) {
1386 if (displayListNames[i] != 0) {
1387 glDeleteLists(displayListNames[i], 1);
1388 displayListNames[i] = 0;
1389 }
1390 }
1391}
1392
1394{
1395 [self deleteDisplayLists];
1396}
1397
1399{
1400 return isTextureImage;
1401}
1402
1404{
1405 return _texture;
1406}
1407
1408- (void)loadTexture:(NSDictionary*)configuration
1409{
1410 [_texture release];
1411 _texture = [OOTexture textureWithConfiguration:configuration extraOptions:kOOTextureAllowCubeMap | kOOTextureRepeatS];
1412 [_texture retain];
1413
1414 [_textureFileName release];
1415 if (_texture != nil) {
1416 _textureFileName = [[configuration oo_stringForKey:@"name"] copy];
1417 isTextureImage = YES;
1418 } else {
1419 _textureFileName = nil;
1420 isTextureImage = NO;
1421 }
1422}
1423
1424/* Ideally, these would use OOPlanetTextureGenerator. However, it isn't
1425 designed to use separate invocations for diffuse and cloud generation,
1426 while old-style PlanetEntity calls these from different places for
1427 different objects.
1428 -- Ahruman 2010-06-04
1429*/
1430- (OOTexture*)planetTextureWithInfo:(NSDictionary*)info
1431{
1432 unsigned char* data;
1433 GLuint width, height;
1434
1436 if (![TextureStore getPlanetTextureNameFor:info
1437 intoData:&data
1438 width:&width
1439 height:&height]) {
1440 return nil;
1441 }
1442
1443 OOPixMap pm = OOMakePixMap(data, width, height, kOOPixMapRGBA, 0, 0);
1444 OOTextureGenerator* loader = [[OOPixMapTextureLoader alloc] initWithPixMap:pm
1445 textureOptions:kOOTextureDefaultOptions | kOOTextureRepeatS
1446 freeWhenDone:YES];
1447 [loader autorelease];
1448
1449 return [OOTexture textureWithGenerator:loader];
1450}
1451
1452- (OOTexture*)cloudTextureWithCloudColor:(OOColor*)cloudColor cloudImpress:(GLfloat)cloud_impress cloudBias:(GLfloat)cloud_bias
1453{
1454 unsigned char* data;
1455 GLuint width, height;
1456
1458 if (![TextureStore getCloudTextureNameFor:
1459 cloudColor:cloud_impress
1460 :cloud_bias
1461 intoData:&data
1462 width:&width
1463 height:&height]) {
1464 return nil;
1465 }
1466
1467 OOPixMap pm = OOMakePixMap(data, width, height, kOOPixMapRGBA, 0, 0);
1468 OOTextureGenerator* loader = [[OOPixMapTextureLoader alloc] initWithPixMap:pm
1469 textureOptions:kOOTextureDefaultOptions | kOOTextureRepeatS
1470 freeWhenDone:YES];
1471 [loader autorelease];
1472
1473 return [OOTexture textureWithGenerator:loader];
1474}
1475
1476#ifdef OO_DEBUG_MONITOR_ENABLED
1477- (NSSet*)allTextures
1478{
1479 if (_texture != nil)
1480 return [NSSet setWithObject:_texture];
1481 else
1482 return nil;
1483}
1484#endif
1485
1487{
1488 return YES;
1489}
1490
1492{
1493 return YES;
1494}
1495
1496@end
1497
1498#endif // !NEW_PLANETS
#define gDebugFlags
@ DEBUG_COLLISIONS
Definition OODebugFlags.h:6
OOINLINE float OOClamp_0_1_f(float value) INLINE_CONST_FUNC
#define DESTROY(x)
#define OOLogERR(class, format,...)
Definition OOLogging.h:125
#define OOLog(class, format,...)
Definition OOLogging.h:84
#define M_SQRT1_2
Definition OOMaths.h:75
void OOGLPushModelView(void)
void OOGLTranslateModelView(Vector vector)
OOMatrix OOGLPopModelView(void)
void OOGLWireframeModeOn(void)
Definition OOOpenGL.m:85
void OOGLWireframeModeOff(void)
Definition OOOpenGL.m:100
@ OPENGL_STATE_OPAQUE
Definition OOOpenGL.h:121
#define OOVerifyOpenGLState()
Definition OOOpenGL.h:133
BOOL OOCheckOpenGLErrors(NSString *format,...)
Definition OOOpenGL.m:38
#define OOSetOpenGLState(STATE)
Definition OOOpenGL.h:132
#define OOGL(statement)
Definition OOOpenGL.h:262
@ kOOPixMapRGBA
Definition OOPixMap.h:40
OOPixMap OOMakePixMap(void *pixels, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
Definition OOPixMap.m:44
return nil
Vector vector_forward_from_quaternion(Quaternion quat) CONST_FUNC
void quaternion_rotate_about_axis(Quaternion *quat, Vector axis, OOScalar angle) NONNULL_FUNC
Vector vector_up_from_quaternion(Quaternion quat) CONST_FUNC
void quaternion_set_random(Quaternion *quat) NONNULL_FUNC
#define PLANET_MINIATURE_FACTOR
#define ATMOSPHERE_DEPTH
OOStellarBodyType
@ STELLAR_TYPE_MOON
@ STELLAR_TYPE_ATMOSPHERE
@ STELLAR_TYPE_MINIATURE
@ STELLAR_TYPE_SUN
@ STELLAR_TYPE_NORMAL_PLANET
#define MAX_SUBDIVIDE
Random_Seed RandomSeedFromString(NSString *abcdefString)
BOOL ScanVectorFromString(NSString *xyzString, Vector *outVector)
double OOTimeDelta
Definition OOTypes.h:193
OOINLINE Vector vector_add(Vector a, Vector b) INLINE_CONST_FUNC
Definition OOVector.h:195
OOINLINE Vector make_vector(OOScalar vx, OOScalar vy, OOScalar vz) INLINE_CONST_FUNC
Definition OOVector.h:149
OOINLINE OOScalar dot_product(Vector first, Vector second) INLINE_CONST_FUNC
Definition OOVector.h:299
const Vector kBasisYVector
Definition OOVector.h:46
OOINLINE Vector vector_normal(Vector vec) INLINE_CONST_FUNC
Definition OOVector.h:270
#define PlanetEntity
#define MAX_TRI_INDICES
#define CLOUD_ALPHA
static GLuint vertex_index_array[3 *(20+80+320+1280+5120+20480)]
#define POLAR_CLEAR_SKY_ALPHA
static const Vector kUntexturedVertices[]
static unsigned baseVertexIndexForEdge(GLushort va, GLushort vb, BOOL textured)
static Vector base_vertex_array[MAX_PLANET_VERTICES]
#define POLAR_CLOUD_ALPHA
static const Vector kTexturedVertices[]
static int base_terrain_array[MAX_PLANET_VERTICES]
static NSMapTable * sEdgeToVertex
static int triangle_start[MAX_SUBDIVIDE]
static GLfloat texture_uv_array[MAX_PLANET_VERTICES *2]
static int n_triangles[MAX_SUBDIVIDE]
static unsigned next_free_vertex
static const BaseFace kUntexturedFaces[][3]
static const BaseFace kTexturedFaces[][3]
#define CLEAR_SKY_ALPHA
#define PLAYER
void fillRanNoiseBuffer()
#define UNIVERSE
Definition Universe.h:827
GLfloat collision_radius
Definition Entity.h:107
OOUniversalID universalID
Definition Entity.m:522
Quaternion orientation
Definition Entity.m:682
void setOrientation:(Quaternion quat)
Definition Entity.m:676
void update:(OOTimeDelta delta_t)
Definition Entity.m:850
GLfloat energy
Definition Entity.m:753
unsigned isShip
Definition Entity.m:125
GLfloat collisionRadius()
Definition Entity.m:830
OOScanClass scanClass
Definition Entity.m:743
void setOwner:(Entity *ent)
Definition Entity.m:542
void orientationChanged()
Definition Entity.m:697
void setScanClass:(OOScanClass sClass)
Definition Entity.m:738
Vector cameraRelativePosition
Definition Entity.m:581
GLfloat cam_zero_distance
Definition Entity.h:105
HPVector position
Definition Entity.m:576
OOMatrix rotMatrix
Definition Entity.h:134
id init()
Definition Entity.m:64
void setStatus:(OOEntityStatus stat)
Definition Entity.m:728
void dealloc()
Definition Entity.m:98
void setPosition:(HPVector posn)
Definition Entity.m:608
OOColor * colorWithRed:green:blue:alpha:(float red,[green] float green,[blue] float blue,[alpha] float alpha)
Definition OOColor.m:107
float blueComponent()
Definition OOColor.m:339
OOColor * colorWithDescription:(id description)
Definition OOColor.m:135
void getRed:green:blue:alpha:(float *red,[green] float *green,[blue] float *blue,[alpha] float *alpha)
Definition OOColor.m:344
OOColor * colorWithHue:saturation:brightness:alpha:(float hue,[saturation] float saturation,[brightness] float brightness,[alpha] float alpha)
Definition OOColor.m:100
float redComponent()
Definition OOColor.m:329
float greenComponent()
Definition OOColor.m:334
void registerClient:(id< OOGraphicsResetClient > client)
void unregisterClient:(id< OOGraphicsResetClient > client)
OOGraphicsResetManager * sharedManager()
id textureWithGenerator:(OOTextureGenerator *generator)
Definition OOTexture.m:209
id textureWithConfiguration:extraOptions:(id configuration,[extraOptions] OOTextureFlags extraOptions)
Definition OOTexture.m:191
void apply()
Definition OOTexture.m:258
void applyNone()
Definition OOTexture.m:263
void ensureFinishedLoading()
Definition OOTexture.m:278
void drawImmediate:translucent:(bool immediate, [translucent] bool translucent)
double rotational_velocity
void setPosition:(HPVector posn)
void setTextureColorForPlanet:inSystem:(BOOL isMain, [inSystem] BOOL isLocal)
double last_launch_time
Vector rotationAxis
NSString * textureFileName()
void initialiseBaseTerrainArray:(int percent_land)
GLfloat amb_polar_sea[4]
double shuttle_launch_interval
double sqrt_zero_distance
PlanetEntity * root_planet
void update:(OOTimeDelta delta_t)
void setUseTexturedModel:(BOOL flag)
void loadTexture:(NSDictionary *configuration)
uint8_t lastSubdivideLevel
void drawModelWithVertexArraysAndSubdivision:(int subdivide)
GLuint vertexCount
OOTexture * _texture
void setOrientation:(Quaternion inOrientation)
GLfloat amb_polar_land[4]
NSString * _textureFileName
OOTexture * texture()
double polar_color_factor
void setRotationalVelocity:(double v)
BOOL isExplicitlyTextured()
void drawUnconditionally()
void resetGraphicsState()
void initialiseBaseVertexArray()
GLfloat amb_land[4]
PlanetEntity * atmosphere
BOOL isTextureImage
OOTexture * cloudTextureWithCloudColor:cloudImpress:cloudBias:(OOColor *cloudColor, [cloudImpress] GLfloat cloud_impress, [cloudBias] GLfloat cloud_bias)
void deleteDisplayLists()
GLfloat amb_sea[4]
NSString * descriptionComponents()
GLuint displayListNames[MAX_SUBDIVIDE]
OOStellarBodyType planet_type
int shuttles_on_ground
OOTexture * planetTextureWithInfo:(NSDictionary *info)
static unsigned baseVertexIndexForEdge(GLushort va, GLushort vb, BOOL textured)
double rotationalVelocity()
OOStellarBodyType planetType()
VertexData vertexdata
void setCargoFlag:(OOCargoFlag flag)
void setSingleCrewWithRole:(NSString *crewRole)
void switchAITo:(NSString *aiString)
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
RANROTSeed RANROTGetFullSeed(void)
void ranrot_srand(uint32_t seed)
void setRandomSeed(RNG_Seed a_seed)
RNG_Seed currentRandomSeed(void)
void RANROTSetFullSeed(RANROTSeed seed)
int gen_rnd_number(void)
void seed_for_planet_description(Random_Seed s_seed)
OOINLINE int is_nil_seed(Random_Seed a_seed) INLINE_CONST_FUNC
#define ranrot_rand()
NSString * name()
unsigned v
OOHPScalar x
Definition OOHPVector.h:36
OOHPScalar y
Definition OOHPVector.h:37
OOHPScalar z
Definition OOHPVector.h:38
OOScalar x
Definition OOVector.h:32
OOScalar y
Definition OOVector.h:33
OOScalar z
Definition OOVector.h:34