24 |
#include <stdint.h> |
#include <stdint.h> |
25 |
|
|
26 |
#include <Foundation/NSArray.h> |
#include <Foundation/NSArray.h> |
27 |
|
#include <Foundation/NSData.h> |
28 |
#include <Foundation/NSException.h> |
#include <Foundation/NSException.h> |
29 |
#include <AppKit/NSBitmapImageRep.h> |
#include <AppKit/NSBitmapImageRep.h> |
30 |
|
|
70 |
|
|
71 |
- (void) dealloc |
- (void) dealloc |
72 |
{ |
{ |
73 |
RELEASE (_shadows); |
RELEASE(_shadows); |
74 |
|
RELEASE(_normalsData); |
75 |
|
|
76 |
[super dealloc]; |
[super dealloc]; |
77 |
} |
} |
112 |
} |
} |
113 |
|
|
114 |
/** |
/** |
115 |
|
* Precalculate normals, for latter use. |
116 |
|
*/ |
117 |
|
- (void) precalculateNormals |
118 |
|
{ |
119 |
|
const unsigned width = [self width]; |
120 |
|
const unsigned height = [self height]; |
121 |
|
|
122 |
|
NSMutableData *normalsData |
123 |
|
= [[NSMutableData alloc] |
124 |
|
initWithLength: sizeof(coord_t) * width * height]; |
125 |
|
coord_t *normals = [normalsData mutableBytes]; |
126 |
|
|
127 |
|
int k, l; |
128 |
|
for (l = 0; l < height; l++) |
129 |
|
{ |
130 |
|
for (k = 0; k < width; k++) |
131 |
|
{ |
132 |
|
normals[k + l * width] |
133 |
|
= [self normalAtPosition: MakeMapCoord (k, l)]; |
134 |
|
} |
135 |
|
} |
136 |
|
|
137 |
|
ASSIGN(_normalsData, normalsData); |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
|
/** |
142 |
|
* Discards the use of the precalculated normals. |
143 |
|
* Frees memory used by them. |
144 |
|
*/ |
145 |
|
- (void) discardPrecalculatedNormals |
146 |
|
{ |
147 |
|
DESTROY(_normalsData); |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
/** |
152 |
* Returns the normal for a given position. |
* Returns the normal for a given position. |
153 |
|
* Use precalulated normals if they are available. |
154 |
*/ |
*/ |
155 |
- (coord_t) normalAtPosition: (map_coord_t)position |
- (coord_t) normalAtPosition: (map_coord_t)position |
156 |
{ |
{ |
157 |
const real_t h1 = [self heightAtPosition: MakeMapCoord (position.x + 1, |
if (_normalsData) |
158 |
position.z)]; |
{ |
159 |
|
return ((const coord_t *)[_normalsData bytes]) |
160 |
const real_t h2 = [self heightAtPosition: MakeMapCoord (position.x, |
[position.x + [self width] * position.z]; |
161 |
position.z + 1)]; |
} |
162 |
|
else |
163 |
const real_t h3 = [self heightAtPosition: MakeMapCoord (position.x - 1, |
{ |
164 |
position.z)]; |
const real_t h1 = [self heightAtPosition: MakeMapCoord (position.x + 1, |
165 |
|
position.z)]; |
166 |
const real_t h4 = [self heightAtPosition: MakeMapCoord (position.x, |
|
167 |
position.z - 1)]; |
const real_t h2 = [self heightAtPosition: MakeMapCoord (position.x, |
168 |
|
position.z + 1)]; |
169 |
|
|
170 |
|
const real_t h3 = [self heightAtPosition: MakeMapCoord (position.x - 1, |
171 |
|
position.z)]; |
172 |
|
|
173 |
|
const real_t h4 = [self heightAtPosition: MakeMapCoord (position.x, |
174 |
|
position.z - 1)]; |
175 |
|
|
176 |
return MakeCoord (h3 - h1, 2, h4 - h2); |
return MakeCoord (h3 - h1, 2, h4 - h2); |
177 |
|
} |
178 |
} |
} |
179 |
|
|
180 |
/** |
/** |