1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// normal_map.rs - Extract normal maps from ADT files
use crate::Adt;
use crate::error::Result;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;
/// Format for normal map export
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NormalMapFormat {
/// Raw data format (just the values)
Raw,
/// PNG format (requires image feature)
PNG,
// /// DirectDraw Surface format (requires dds_encoder feature)
// DDS,
}
/// Options for normal map extraction
#[derive(Debug, Clone)]
pub struct NormalMapOptions {
/// Output format
pub format: NormalMapFormat,
/// Whether to invert Y axis
pub invert_y: bool,
/// Whether to use tangent space normals (vs object space)
pub tangent_space: bool,
/// Whether to flip the Y axis in the image
pub flip_y: bool,
/// Whether to flip the X axis in the image
pub flip_x: bool,
/// Normal map channel encoding mode
pub encoding: NormalChannelEncoding,
}
/// Normal map channel encoding
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NormalChannelEncoding {
/// Standard RGB encoding where R=X, G=Y, B=Z
RGB,
/// OpenGL normal map format (Y flipped)
OpenGL,
/// DirectX normal map format (Y and Z flipped)
DirectX,
}
impl Default for NormalMapOptions {
fn default() -> Self {
Self {
format: NormalMapFormat::PNG,
invert_y: false,
tangent_space: true,
flip_y: false,
flip_x: false,
encoding: NormalChannelEncoding::RGB,
}
}
}
/// Extract a normal map from an ADT file
pub fn extract_normal_map<P: AsRef<Path>>(
adt: &Adt,
output_path: P,
options: NormalMapOptions,
) -> Result<()> {
match options.format {
NormalMapFormat::Raw => extract_raw_normal_map(adt, output_path, &options),
NormalMapFormat::PNG => {
#[cfg(feature = "image")]
{
extract_png_normal_map(adt, output_path, &options)
}
#[cfg(not(feature = "image"))]
{
Err(crate::error::AdtError::NotImplemented(
"PNG export requires the 'image' feature to be enabled".to_string(),
))
}
} // NormalMapFormat::DDS => {
// #[cfg(feature = "dds_encoder")]
// {
// extract_dds_normal_map(adt, output_path, &options)
// }
// #[cfg(not(feature = "dds_encoder"))]
// {
// Err(crate::error::AdtError::NotImplemented(
// "DDS export requires the 'dds_encoder' feature to be enabled".to_string(),
// ))
// }
// }
}
}
/// Extract a raw normal map (just the values)
fn extract_raw_normal_map<P: AsRef<Path>>(
adt: &Adt,
output_path: P,
options: &NormalMapOptions,
) -> Result<()> {
let file = File::create(output_path)?;
let mut writer = BufWriter::new(file);
// The normal map is a grid of normal vectors for each vertex in the heightmap
// For each MCNK in the grid
for y in 0..16 {
for x in 0..16 {
let chunk_index = y * 16 + x;
if chunk_index < adt.mcnk_chunks.len() {
let chunk = &adt.mcnk_chunks[chunk_index];
// Get the normal values from this chunk
for normal in &chunk.normals {
// Convert normal from [u8; 3] format to normalized [-1, 1] floats
let nx = (normal[0] as f32) / 127.0;
let mut ny = (normal[1] as f32) / 127.0;
let mut nz = (normal[2] as f32) / 127.0;
// Apply options
if options.invert_y {
ny = -ny;
}
if options.encoding == NormalChannelEncoding::DirectX {
ny = -ny;
nz = -nz;
} else if options.encoding == NormalChannelEncoding::OpenGL {
ny = -ny;
}
// Write the normal components
writer.write_all(&nx.to_le_bytes())?;
writer.write_all(&ny.to_le_bytes())?;
writer.write_all(&nz.to_le_bytes())?;
}
}
}
}
Ok(())
}
#[cfg(feature = "image")]
fn extract_png_normal_map<P: AsRef<Path>>(
adt: &Adt,
output_path: P,
options: &NormalMapOptions,
) -> Result<()> {
use image::{ImageBuffer, Rgb};
// Calculate final image dimensions
// Each MCNK contains a 9x9 grid of vertices
let width = 145; // 9*16 + 1 (with overlap)
let height = 145; // 9*16 + 1 (with overlap)
// Create a new RGB image
let mut img = ImageBuffer::<Rgb<u8>, Vec<u8>>::new(width, height);
// Collect all normals first
let mut normals = vec![[0u8; 3]; width as usize * height as usize];
// Process each MCNK to extract normals
for y in 0..16 {
for x in 0..16 {
let chunk_index = y * 16 + x;
if chunk_index < adt.mcnk_chunks.len() {
let chunk = &adt.mcnk_chunks[chunk_index];
// Map the chunk to the image grid
let chunk_x = x * 9;
let chunk_y = y * 9;
// Place normals on the grid
for i in 0..9 {
for j in 0..9 {
let normal_index = i * 9 + j;
let pos_x = chunk_x + j;
let pos_y = if options.flip_y {
height as usize - 1 - (chunk_y + i)
} else {
chunk_y + i
};
if normal_index < chunk.normals.len() {
let normal = chunk.normals[normal_index];
// Store normal
let combined_index = pos_y * width as usize + pos_x;
if combined_index < normals.len() {
normals[combined_index] = normal;
}
}
}
}
}
}
}
// Convert normals to RGB image
for (i, normal) in normals.iter().enumerate() {
let x = (i % width as usize) as u32;
let y = (i / width as usize) as u32;
// Convert from [-127, 127] to [0, 255]
// Note: ADT normals are actually stored as signed bytes (-127 to 127)
// Get normal components - convert from u8 to i8 first
let mut nx = normal[0] as i8;
let mut ny = normal[1] as i8;
let nz = normal[2] as i8;
// Apply options
if options.invert_y {
ny = -ny;
}
if options.flip_x {
nx = -nx;
}
// Convert to [0, 255] range (127 is 0, 255 is 1.0, 0 is -1.0)
let r = ((nx + 127) as u8).clamp(0, 255);
let g = ((ny + 127) as u8).clamp(0, 255);
let b = ((nz + 127) as u8).clamp(0, 255);
// Set the pixel
img.put_pixel(x, y, Rgb([r, g, b]));
}
// Save the image
img.save(output_path).map_err(|e| {
crate::error::AdtError::Io(std::io::Error::other(format!(
"Failed to save PNG image: {e}"
)))
})?;
Ok(())
}
// #[cfg(feature = "dds_encoder")]
// fn extract_dds_normal_map<P: AsRef<Path>>(
// _adt: &Adt,
// _output_path: P,
// _options: &NormalMapOptions,
// ) -> Result<()> {
// // Implementation would use a DDS encoder library to save the normal map
// // For now, return a not implemented error
// Err(crate::error::AdtError::NotImplemented(
// "DDS export is not yet implemented".to_string(),
// ))
// }
/// Generate a normal map from a height map
#[allow(dead_code)]
pub fn generate_normal_map_from_heightmap(
heightmap: &[f32],
width: usize,
height: usize,
scale: f32,
) -> Vec<[i8; 3]> {
let mut normals = vec![[0i8; 3]; width * height];
// For each vertex in the height map
for y in 0..height {
for x in 0..width {
// Get surrounding heights (with bounds checking)
let h_center = heightmap[y * width + x];
let h_left = if x > 0 {
heightmap[y * width + (x - 1)]
} else {
h_center
};
let h_right = if x < width - 1 {
heightmap[y * width + (x + 1)]
} else {
h_center
};
let h_up = if y > 0 {
heightmap[(y - 1) * width + x]
} else {
h_center
};
let h_down = if y < height - 1 {
heightmap[(y + 1) * width + x]
} else {
h_center
};
// Calculate derivatives
let dx = (h_right - h_left) * scale;
let dy = (h_down - h_up) * scale;
// Calculate normal vector using cross product
let nx = -dx;
let ny = -dy;
let nz = 2.0; // Fixed step size
// Normalize
let length = (nx * nx + ny * ny + nz * nz).sqrt();
// Convert to -127 to 127 range for ADT normals
let nx_scaled = ((nx / length) * 127.0) as i8;
let ny_scaled = ((ny / length) * 127.0) as i8;
let nz_scaled = ((nz / length) * 127.0) as i8;
// Store normal
normals[y * width + x] = [nx_scaled, ny_scaled, nz_scaled];
}
}
normals
}