Oolite
Loading...
Searching...
No Matches
OOProbabilisticTextureManager.m
Go to the documentation of this file.
1/*
2
3OOProbabilisticTextureManager.m
4
5
6Copyright (C) 2007-2013 Jens Ayton
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
30#import "OOTexture.h"
32#import "ResourceManager.h"
33
35
36- (id)initWithPListName:(NSString*)plistName
37 options:(uint32_t)options
38 anisotropy:(GLfloat)anisotropy
39 lodBias:(GLfloat)lodBias
40{
41 return [self initWithPListName:plistName
42 options:options
43 anisotropy:anisotropy
44 lodBias:lodBias
45 seed:RANROTGetFullSeed()];
46}
47
48- (id)initWithPListName:(NSString*)plistName
49 options:(uint32_t)options
50 anisotropy:(GLfloat)anisotropy
51 lodBias:(GLfloat)lodBias
52 seed:(RANROTSeed)seed
53{
54 BOOL OK = YES;
55 NSArray* config = nil;
56 NSUInteger i, count, j;
57 id entry = nil;
58 NSString* name = nil;
59 float probability;
60 OOTexture* texture = nil;
61 int galID = -1;
62 id object = nil;
63
64 self = [super init];
65 if (self == nil)
66 OK = NO;
67
68 if (OK) {
69 config = [ResourceManager arrayFromFilesNamed:plistName inFolder:@"Config" andMerge:YES];
70 if (config == nil)
71 OK = NO;
72 }
73
74 if (OK) {
75 count = [config count];
76
77 _textures = malloc(sizeof *_textures * count);
78 _prob = malloc(sizeof *_prob * count);
79 _galaxy = malloc(sizeof *_galaxy * count);
80 _probMaxGal = malloc(sizeof *_probMaxGal * (kOOMaximumGalaxyID + 1));
81
82 if (_textures == NULL || _prob == NULL || _galaxy == NULL)
83 OK = NO;
84 }
85
86 if (OK) {
87 for (i = 0; i <= kOOMaximumGalaxyID; i++)
88 _probMaxGal[i] = 0;
89
90 // Go through list and load textures.
91 for (i = 0; i != count; ++i) {
92 entry = [config objectAtIndex:i];
93 galID = -1;
94 if ([entry isKindOfClass:[NSDictionary class]]) {
95 name = [(NSDictionary*)entry oo_stringForKey:@"texture"];
96 probability = [entry oo_floatForKey:@"probability" defaultValue:1.0f];
97 object = [entry objectForKey:@"galaxy"];
98 if ([object isKindOfClass:[NSString class]]) {
99 galID = [object intValue];
100 } else if (object != nil) {
101 OOLog(@"textures.load", @"***** ERROR: %@ for texture %@ is not a string.", @"galaxy", name);
102 }
103 } else if ([entry isKindOfClass:[NSString class]]) {
104 name = entry;
105 probability = 1.0f;
106 } else {
107 name = nil;
108 }
109
110 if (name != nil && 0.0f < probability) {
111 texture = [OOTexture textureWithName:name
112 inFolder:@"Textures"
113 options:options
114 anisotropy:anisotropy
115 lodBias:lodBias];
116 if (texture != nil) {
117 _textures[_count] = [texture retain];
118 _prob[_count] = probability + (galID >= 0 ? _probMaxGal[galID] : _probMax);
119 _galaxy[_count] = (galID >= 0 ? galID : -1);
120 if (galID >= 0) {
121 _probMaxGal[galID] += probability;
122 } else {
123 for (j = 0; j <= kOOMaximumGalaxyID; j++)
124 _probMaxGal[j] += probability;
125 _probMax += probability;
126 }
127 ++_count;
128 }
129 }
130 }
131
132 if (_count == 0)
133 OK = NO;
134 }
135
136 if (OK)
137 _seed = seed;
138
139 if (!OK) {
140 [self release];
141 self = nil;
142 }
143
144 return self;
145}
146
147- (void)dealloc
148{
149 unsigned i;
150
151 if (_textures != NULL) {
152 for (i = 0; i != _count; ++i) {
153 [_textures[i] release];
154 }
155 free(_textures);
156 }
157
158 if (_prob != NULL)
159 free(_prob);
160 if (_galaxy != NULL)
161 free(_galaxy);
162 if (_probMaxGal != NULL)
163 free(_probMaxGal);
164
165 [super dealloc];
166}
167
168- (NSString*)description
169{
170 return [NSString stringWithFormat:@"<%@ %p>{%u textures, cumulative probability=%g}", [self class], self, _count, _probMax];
171}
172
174{
175 float selection;
176 unsigned i;
177 int hold = -1;
178 int galID = (int)[PLAYER currentGalaxyID];
179
180 selection = randfWithSeed(&_seed);
181
182 selection *= _probMaxGal[galID];
183
184 for (i = 0; i != _count; ++i) {
185 if (_galaxy[i] == -1 || _galaxy[i] == galID) {
186 // make a note of the index of the first texture that meets the galaxy list criteria (but only if _galaxy is set)
187 if (hold == -1 && _prob[i] > 0 && _galaxy[i] == galID)
188 hold = i;
189 if (selection <= _prob[i])
190 return _textures[i];
191 }
192 }
193
194 // first catch point if loop above fails to return a texture
195 // return first texture that meets the galaxy list criteria and has a probability > 0
196 if (hold >= 0) {
197 OOLog(@"probabilisticTextureManager.internalWarning", @"%s: overrun! Galaxy List requirements not met. Choosing first texture available for galaxy.", __PRETTY_FUNCTION__);
198 return _textures[hold];
199 }
200
201 OOLog(@"probabilisticTextureManager.internalFailure", @"%s: overrun! Choosing last texture.", __PRETTY_FUNCTION__);
202 return _textures[_count - 1];
203}
204
205- (unsigned)textureCount
206{
207 return _count;
208}
209
211{
212 unsigned i;
213
214 for (i = 0; i != _count; ++i) {
216 }
217}
218
220{
221 return _seed;
222}
223
224- (void)setSeed:(RANROTSeed)seed
225{
226 _seed = seed;
227}
228
229@end
#define OOLog(class, format,...)
Definition OOLogging.h:84
unsigned count
return nil
@ kOOMaximumGalaxyID
Definition OOTypes.h:186
#define PLAYER
id initWithPListName:options:anisotropy:lodBias:seed:(NSString *plistName,[options] uint32_t options,[anisotropy] GLfloat anisotropy,[lodBias] GLfloat lodBias,[seed] RANROTSeed seed)
id textureWithName:inFolder:options:anisotropy:lodBias:(NSString *name,[inFolder] NSString *directory,[options] OOTextureFlags options,[anisotropy] GLfloat anisotropy,[lodBias] GLfloat lodBias)
Definition OOTexture.m:132
void ensureFinishedLoading()
Definition OOTexture.m:278
NSArray * arrayFromFilesNamed:inFolder:andMerge:(NSString *fileName,[inFolder] NSString *folderName,[andMerge] BOOL mergeFiles)
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
float randfWithSeed(RANROTSeed *ioSeed)