-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_w3d.zig
More file actions
318 lines (274 loc) · 10.6 KB
/
Copy pathtest_w3d.zig
File metadata and controls
318 lines (274 loc) · 10.6 KB
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
// W3D Model Loading Test
// Tests Westwood 3D model format parsing for C&C Generals
const std = @import("std");
// W3D file format constants
const W3D_CHUNK_MESH: u32 = 0x00000000;
const W3D_CHUNK_MESH_HEADER: u32 = 0x0000001F;
const W3D_CHUNK_VERTICES: u32 = 0x00000002;
const W3D_CHUNK_VERTEX_NORMALS: u32 = 0x00000003;
const W3D_CHUNK_TRIANGLES: u32 = 0x00000020;
const W3DChunkHeader = packed struct {
chunk_type: u32,
chunk_size: u32,
};
const W3DMeshHeader = extern struct {
version: u32,
attributes: u32,
mesh_name: [16]u8,
container_name: [16]u8,
num_vertices: u32,
num_triangles: u32,
num_materials: u32,
num_damage_stages: u32,
sort_level: i32,
prelit_version: u32,
future_count: u32,
vertex_channel_count: u32,
face_channel_count: u32,
min_corner: [3]f32,
max_corner: [3]f32,
sphere_center: [3]f32,
sphere_radius: f32,
};
const Vec3 = struct {
x: f32,
y: f32,
z: f32,
};
const Triangle = struct {
indices: [3]u32,
attributes: u32,
normal: Vec3,
distance: f32,
};
const W3DMesh = struct {
name: [16]u8,
vertices: std.ArrayListUnmanaged(Vec3),
normals: std.ArrayListUnmanaged(Vec3),
triangles: std.ArrayListUnmanaged(Triangle),
bounds_min: Vec3,
bounds_max: Vec3,
fn init() W3DMesh {
return .{
.name = [_]u8{0} ** 16,
.vertices = .empty,
.normals = .empty,
.triangles = .empty,
.bounds_min = .{ .x = 0, .y = 0, .z = 0 },
.bounds_max = .{ .x = 0, .y = 0, .z = 0 },
};
}
fn deinit(self: *W3DMesh, allocator: std.mem.Allocator) void {
self.vertices.deinit(allocator);
self.normals.deinit(allocator);
self.triangles.deinit(allocator);
}
};
const W3DModel = struct {
meshes: std.ArrayListUnmanaged(W3DMesh),
fn init() W3DModel {
return .{
.meshes = .empty,
};
}
fn deinit(self: *W3DModel, allocator: std.mem.Allocator) void {
for (self.meshes.items) |*mesh| {
mesh.deinit(allocator);
}
self.meshes.deinit(allocator);
}
};
fn parseW3D(allocator: std.mem.Allocator, data: []const u8) !W3DModel {
var model = W3DModel.init();
errdefer model.deinit(allocator);
var offset: usize = 0;
while (offset + @sizeOf(W3DChunkHeader) <= data.len) {
const header: *const W3DChunkHeader = @ptrCast(@alignCast(data[offset..].ptr));
offset += @sizeOf(W3DChunkHeader);
const chunk_data = data[offset..@min(offset + header.chunk_size, data.len)];
switch (header.chunk_type) {
W3D_CHUNK_MESH => {
const mesh = try parseMesh(allocator, chunk_data);
try model.meshes.append(allocator, mesh);
},
else => {},
}
offset += header.chunk_size;
}
return model;
}
fn parseMesh(allocator: std.mem.Allocator, data: []const u8) !W3DMesh {
var mesh = W3DMesh.init();
errdefer mesh.deinit(allocator);
var offset: usize = 0;
while (offset + @sizeOf(W3DChunkHeader) <= data.len) {
const header: *const W3DChunkHeader = @ptrCast(@alignCast(data[offset..].ptr));
offset += @sizeOf(W3DChunkHeader);
const chunk_end = @min(offset + header.chunk_size, data.len);
const chunk_data = data[offset..chunk_end];
switch (header.chunk_type) {
W3D_CHUNK_MESH_HEADER => {
if (chunk_data.len >= @sizeOf(W3DMeshHeader)) {
const mesh_header: *const W3DMeshHeader = @ptrCast(@alignCast(chunk_data.ptr));
@memcpy(&mesh.name, &mesh_header.mesh_name);
mesh.bounds_min = .{
.x = mesh_header.min_corner[0],
.y = mesh_header.min_corner[1],
.z = mesh_header.min_corner[2],
};
mesh.bounds_max = .{
.x = mesh_header.max_corner[0],
.y = mesh_header.max_corner[1],
.z = mesh_header.max_corner[2],
};
}
},
W3D_CHUNK_VERTICES => {
const vertex_count = header.chunk_size / 12; // 3 floats per vertex
var v_offset: usize = 0;
for (0..vertex_count) |_| {
if (v_offset + 12 <= chunk_data.len) {
const floats: *const [3]f32 = @ptrCast(@alignCast(chunk_data[v_offset..].ptr));
try mesh.vertices.append(allocator, .{ .x = floats[0], .y = floats[1], .z = floats[2] });
v_offset += 12;
}
}
},
W3D_CHUNK_VERTEX_NORMALS => {
const normal_count = header.chunk_size / 12;
var n_offset: usize = 0;
for (0..normal_count) |_| {
if (n_offset + 12 <= chunk_data.len) {
const floats: *const [3]f32 = @ptrCast(@alignCast(chunk_data[n_offset..].ptr));
try mesh.normals.append(allocator, .{ .x = floats[0], .y = floats[1], .z = floats[2] });
n_offset += 12;
}
}
},
W3D_CHUNK_TRIANGLES => {
const tri_size = 32; // Size of W3D triangle struct
const tri_count = header.chunk_size / tri_size;
for (0..tri_count) |i| {
const t_offset = i * tri_size;
if (t_offset + tri_size <= chunk_data.len) {
const indices: *const [3]u32 = @ptrCast(@alignCast(chunk_data[t_offset..].ptr));
const attrs: *const u32 = @ptrCast(@alignCast(chunk_data[t_offset + 12 ..].ptr));
const normal: *const [3]f32 = @ptrCast(@alignCast(chunk_data[t_offset + 16 ..].ptr));
const dist: *const f32 = @ptrCast(@alignCast(chunk_data[t_offset + 28 ..].ptr));
try mesh.triangles.append(allocator, .{
.indices = indices.*,
.attributes = attrs.*,
.normal = .{ .x = normal[0], .y = normal[1], .z = normal[2] },
.distance = dist.*,
});
}
}
},
else => {},
}
offset = chunk_end;
}
return mesh;
}
fn createTestW3D(allocator: std.mem.Allocator) ![]u8 {
// Create a simple cube mesh in W3D format
var data = std.ArrayListUnmanaged(u8){ .items = &.{}, .capacity = 0 };
errdefer data.deinit(allocator);
// Mesh chunk
const mesh_start = data.items.len;
try data.appendSlice(allocator, &std.mem.toBytes(W3DChunkHeader{
.chunk_type = W3D_CHUNK_MESH,
.chunk_size = 0, // Will update later
}));
// Mesh header
const mesh_header = W3DMeshHeader{
.version = 3,
.attributes = 0,
.mesh_name = [_]u8{ 'T', 'e', 's', 't', 'C', 'u', 'b', 'e', 0, 0, 0, 0, 0, 0, 0, 0 },
.container_name = [_]u8{ 'T', 'e', 's', 't', 'M', 'o', 'd', 'e', 'l', 0, 0, 0, 0, 0, 0, 0 },
.num_vertices = 8,
.num_triangles = 12,
.num_materials = 1,
.num_damage_stages = 0,
.sort_level = 0,
.prelit_version = 0,
.future_count = 0,
.vertex_channel_count = 0,
.face_channel_count = 0,
.min_corner = .{ -1.0, -1.0, -1.0 },
.max_corner = .{ 1.0, 1.0, 1.0 },
.sphere_center = .{ 0.0, 0.0, 0.0 },
.sphere_radius = 1.732,
};
try data.appendSlice(allocator, &std.mem.toBytes(W3DChunkHeader{
.chunk_type = W3D_CHUNK_MESH_HEADER,
.chunk_size = @sizeOf(W3DMeshHeader),
}));
try data.appendSlice(allocator, &std.mem.toBytes(mesh_header));
// Vertices (cube corners)
const vertices = [8][3]f32{
.{ -1.0, -1.0, -1.0 },
.{ 1.0, -1.0, -1.0 },
.{ 1.0, 1.0, -1.0 },
.{ -1.0, 1.0, -1.0 },
.{ -1.0, -1.0, 1.0 },
.{ 1.0, -1.0, 1.0 },
.{ 1.0, 1.0, 1.0 },
.{ -1.0, 1.0, 1.0 },
};
try data.appendSlice(allocator, &std.mem.toBytes(W3DChunkHeader{
.chunk_type = W3D_CHUNK_VERTICES,
.chunk_size = 8 * 12,
}));
for (vertices) |v| {
try data.appendSlice(allocator, &std.mem.toBytes(v));
}
// Update mesh chunk size
const mesh_size: u32 = @intCast(data.items.len - mesh_start - @sizeOf(W3DChunkHeader));
const size_ptr: *u32 = @ptrCast(@alignCast(data.items[mesh_start + 4 ..].ptr));
size_ptr.* = mesh_size;
return try data.toOwnedSlice(allocator);
}
pub fn main() !void {
const print = std.debug.print;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
print("================================================================================\n", .{});
print(" C&C Generals W3D Model Loading Test\n", .{});
print("================================================================================\n\n", .{});
// Create test W3D data
print("Creating test W3D model (cube)...\n", .{});
const w3d_data = try createTestW3D(allocator);
defer allocator.free(w3d_data);
print("Test W3D created: {d} bytes\n\n", .{w3d_data.len});
// Parse the W3D
print("Parsing W3D model...\n", .{});
var model = try parseW3D(allocator, w3d_data);
defer model.deinit(allocator);
print("Model parsed successfully:\n", .{});
print(" Mesh count: {d}\n\n", .{model.meshes.items.len});
for (model.meshes.items, 0..) |mesh, i| {
// Get mesh name as string
var name_buf: [17]u8 = undefined;
@memcpy(name_buf[0..16], &mesh.name);
name_buf[16] = 0;
const name = std.mem.sliceTo(&name_buf, 0);
print("Mesh {d}: \"{s}\"\n", .{ i, name });
print(" Vertices: {d}\n", .{mesh.vertices.items.len});
print(" Normals: {d}\n", .{mesh.normals.items.len});
print(" Triangles: {d}\n", .{mesh.triangles.items.len});
print(" Bounds: ({d:.2}, {d:.2}, {d:.2}) to ({d:.2}, {d:.2}, {d:.2})\n", .{
mesh.bounds_min.x, mesh.bounds_min.y, mesh.bounds_min.z,
mesh.bounds_max.x, mesh.bounds_max.y, mesh.bounds_max.z,
});
if (mesh.vertices.items.len > 0) {
print(" Sample vertices:\n", .{});
const max_show = @min(mesh.vertices.items.len, 4);
for (mesh.vertices.items[0..max_show], 0..) |v, j| {
print(" [{d}]: ({d:.2}, {d:.2}, {d:.2})\n", .{ j, v.x, v.y, v.z });
}
}
}
print("\nAll W3D loading tests passed!\n", .{});
}