mdxm: initial

This commit is contained in:
Zack Buhman 2025-04-29 23:04:32 -05:00
parent 4d3aa65c90
commit 0c1da1c858
31 changed files with 371 additions and 0 deletions

133
mdxm/debug.c Normal file
View File

@ -0,0 +1,133 @@
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "mdxm.h"
void print_header(struct mdxm_header * header)
{
printf("magic: ");
putchar(header->magic[0]);
putchar(header->magic[1]);
putchar(header->magic[2]);
putchar(header->magic[3]);
putchar('\n');
printf("version: %x\n", header->version);
}
void print_surface_hierarchy(struct mdxm_header * header)
{
mdxm_surf_hierarchy_t * sh = (mdxm_surf_hierarchy_t *)(((uint8_t *)header) + header->offset_surface_hierarchy);
for (int i = 0; i < header->num_surfaces; i++) {
printf("[%d] %s\n", i, &sh->shader[1]);
printf(" %s\n", sh->name);
int offset = (int)(&(((mdxm_surf_hierarchy_t *)0)->child_indexes[sh->num_children]));
sh = (mdxm_surf_hierarchy_t *)(((uint8_t *)sh) + offset);
}
}
void print_surfaces(struct mdxm_header * header)
{
uint8_t * buf = (uint8_t *)header;
printf("num_lods: %d\n", header->num_lods);
printf("offset_lods: %d\n", header->offset_lods);
printf("num_surfaces: %d\n", header->num_surfaces);
mdxm_lod_t * lod = (mdxm_lod_t *)&buf[header->offset_lods];
const int surface_offset = (sizeof (mdxm_lod_t)) + (header->num_surfaces * (sizeof (mdxm_lod_surf_offset_t)));
for (int l = 0; l < header->num_lods; l++) {
mdxm_surface_t * surf = (mdxm_surface_t *)(((uint8_t *)lod) + surface_offset);
for (int i = 0; i < header->num_surfaces; i++) {
//printf("surf ident: %d\n", surf->ident);
//printf("offset header: %d\n", surf->offset_header);
mdxm_vertex_t * v = (mdxm_vertex_t *) (((uint8_t *)surf) + surf->offset_verts);
mdxm_vertex_texture_coord_t * t = (mdxm_vertex_texture_coord_t *)&v[surf->num_verts];
printf("num_verts %d\n", surf->num_verts);
for (int j = 0; j < surf->num_verts; j++) {
printf("[%d] %f %f %f\n", j, v[j].position[0], v[j].position[1], v[j].position[2]);
printf(" %f %f %f\n", v[j].normal[0], v[j].normal[1], v[j].normal[2]);
}
mdxm_triangle_t * triangles = (mdxm_triangle_t *)(((uint8_t *)surf) + surf->offset_triangles);
printf("num_triangles %d\n", surf->num_triangles);
for (int j = 0; j < surf->num_triangles; j++) {
printf("%d %d %d\n", triangles[j].index[0], triangles[j].index[1], triangles[j].index[2]);
}
// next surface
surf = (mdxm_surface_t *)(((uint8_t *)surf) + surf->offset_end);
}
// next lod
lod = (mdxm_lod_t *)(((uint8_t *)lod) + lod->offset_end);
break;
}
}
int read_file(const char * filename, uint8_t ** buf, uint32_t * size_out)
{
FILE * file = fopen(filename, "rb");
if (file == NULL) {
fprintf(stderr, "fopen(\"%s\", \"rb\"): %s\n", filename, strerror(errno));
return -1;
}
int ret;
ret = fseek(file, 0L, SEEK_END);
if (ret < 0) {
fprintf(stderr, "fseek(SEEK_END)");
return -1;
}
long offset = ftell(file);
if (offset < 0) {
fprintf(stderr, "ftell");
return -1;
}
size_t size = offset;
ret = fseek(file, 0L, SEEK_SET);
if (ret < 0) {
fprintf(stderr, "fseek(SEEK_SET)");
return -1;
}
fprintf(stderr, "read_file: %s size %ld\n", filename, size);
*buf = (uint8_t *)malloc(size);
size_t fread_size = fread(*buf, 1, size, file);
if (fread_size != size) {
fprintf(stderr, "fread `%s` short read: %d ; expected: %d\n", filename, (int)fread_size, (int)size);
return -1;
}
ret = fclose(file);
if (ret < 0) {
fprintf(stderr, "fclose");
return -1;
}
*size_out = size;
return 0;
}
int main(int argc, char * argv[])
{
assert(argc == 2);
const char * filename = argv[1];
uint8_t * buf;
uint32_t size;
int res = read_file(filename, &buf, &size);
if (res != 0)
return EXIT_FAILURE;
mdxm_header_t * header = (mdxm_header_t *)(buf);
print_header(header);
print_surface_hierarchy(header);
//print_surfaces(header);
}

88
mdxm/mdxm.h Normal file
View File

@ -0,0 +1,88 @@
#ifdef __cplusplus
extern "C" {
#endif
#define MDX_IDENT (('M'<<24)+('G'<<16)+('L'<<8)+'2')
#define MAX_QPATH 64
// mdxm = mod->mdxm = (mdxmHeader_t*)
typedef struct mdxm_header {
union {
int ident;
uint8_t magic[4];
};
int version;
char name[MAX_QPATH];
char anim_name[MAX_QPATH];
int anim_index;
int num_bones;
int num_lods;
int offset_lods;
int num_surfaces;
int offset_surface_hierarchy;
int offset_end;
} mdxm_header_t;
// lod = (mdxmLOD_t *) ( (byte *)mdxm + mdxm->ofsLODs );
typedef struct mdxm_lod {
int offset_end;
} mdxm_lod_t;
typedef struct mdxm_lod_surf_offset {
int offsets[1];
} mdxm_lod_surf_offset_t;
typedef struct {
char name[MAX_QPATH];
uint32_t flags;
char shader[MAX_QPATH];
int shader_index;
int parent_index;
int num_children;
int child_indexes[1];
} mdxm_surf_hierarchy_t;
// surf = (mdxmSurface_t *) ( (byte *)lod + sizeof (mdxmLOD_t) + (mdxm->numSurfaces * sizeof(mdxmLODSurfOffset_t)) );
typedef struct mdxm_surface {
int ident;
int this_surface_index;
int offset_header;
int num_verts;
int offset_verts;
int num_triangles;
int offset_triangles;
int num_bone_references;
int offset_bone_references;
int offset_end;
} mdxm_surface_t;
/*
v = (mdxmVertex_t *) ((byte *)surface + surface->offset_verts);
pTexCoords = (mdxmVertexTexCoord_t *) &v[numVerts];
*/
typedef struct mdxm_triangle {
int index[3];
} mdxm_triangle_t;
typedef float mdxm_vec2_t[2];
typedef float mdxm_vec3_t[3];
typedef struct mdxm_vertex {
mdxm_vec3_t normal;
mdxm_vec3_t position;
uint32_t flags;
uint8_t bone_weightings[4];
} mdxm_vertex_t;
typedef struct mdxm_vertex_texture_coord {
mdxm_vec2_t texture;
} mdxm_vertex_texture_coord_t;
#ifdef __cplusplus
}
#endif

BIN
model/tavion_new/arm.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

BIN
model/tavion_new/face.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

BIN
model/tavion_new/hands.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
model/tavion_new/head.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

BIN
model/tavion_new/legs.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

BIN
model/tavion_new/model.glm Normal file

Binary file not shown.

View File

@ -0,0 +1,37 @@
hips,models/players/tavion_new/legs_blue.tga
hips_belt,models/players/tavion_new/legs_blue.tga
hips_pockets,models/players/tavion_new/legs_blue.tga
torso,models/players/tavion_new/torso_blue.tga
torso_armor,models/players/tavion_new/torso_blue.tga
head,models/players/tavion_new/head.tga
head_hair,models/players/tavion_new/head.tga
head_teeth,models/players/tavion_new/head.tga
head_twills,models/players/tavion_new/head.tga
head_feathers,models/players/tavion_new/feathers.tga
head_eyelashes,models/players/tavion_new/feathers.tga
head_ahair,models/players/tavion_new/feathers.tga
head_cap_torso_off,models/players/stormtrooper/caps.tga
head_face,models/players/tavion_new/face.tga
head_moutheyes,models/players/tavion_new/face.tga
r_arm,models/players/tavion_new/arm_blue.tga
r_hand,models/players/tavion_new/hands.tga
r_hand_cap_r_arm_off,models/players/stormtrooper/caps.tga
r_arm_gauntlet,models/players/tavion_new/arm_blue.tga
r_arm_cap_r_hand_off,models/players/stormtrooper/caps.tga
r_arm_cap_torso_off,models/players/stormtrooper/caps.tga
l_arm,models/players/tavion_new/arm_blue.tga
l_hand,models/players/tavion_new/hands.tga
l_hand_cap_l_arm_off,models/players/stormtrooper/caps.tga
l_arm_cap_l_hand_off,models/players/stormtrooper/caps.tga
l_arm_cap_torso_off,models/players/stormtrooper/caps.tga
torso_cap_head_off,models/players/stormtrooper/caps.tga
torso_cap_hips_off,models/players/stormtrooper/caps.tga
torso_cap_l_arm_off,models/players/stormtrooper/caps.tga
torso_cap_r_arm_off,models/players/stormtrooper/caps.tga
r_leg,models/players/tavion_new/legs_blue.tga
r_leg_cap_hips_off,models/players/stormtrooper/caps.tga
l_leg,models/players/tavion_new/legs_blue.tga
l_leg_cap_hips_off,models/players/stormtrooper/caps.tga
hips_cap_l_leg_off,models/players/stormtrooper/caps.tga
hips_cap_r_leg_off,models/players/stormtrooper/caps.tga
hips_cap_torso_off,models/players/stormtrooper/caps.tga

View File

@ -0,0 +1,37 @@
hips,models/players/tavion_new/legs.tga
hips_belt,models/players/tavion_new/legs.tga
hips_pockets,models/players/tavion_new/legs.tga
torso,models/players/tavion_new/torso.tga
torso_armor,models/players/tavion_new/torso.tga
head,models/players/tavion_new/head.tga
head_hair,models/players/tavion_new/head.tga
head_teeth,models/players/tavion_new/face.tga
head_twills,models/players/tavion_new/head.tga
head_feathers,models/players/tavion_new/feathers.tga
head_eyelashes,models/players/tavion_new/feathers.tga
head_ahair,models/players/tavion_new/feathers.tga
head_cap_torso_off,models/players/stormtrooper/caps.tga
head_face,models/players/tavion_new/face.tga
head_moutheyes,models/players/tavion_new/face.tga
r_arm,models/players/tavion_new/arm.tga
r_hand,models/players/tavion_new/hands.tga
r_hand_cap_r_arm_off,models/players/stormtrooper/caps.tga
r_arm_gauntlet,models/players/tavion_new/arm.tga
r_arm_cap_r_hand_off,models/players/stormtrooper/caps.tga
r_arm_cap_torso_off,models/players/stormtrooper/caps.tga
l_arm,models/players/tavion_new/arm.tga
l_hand,models/players/tavion_new/hands.tga
l_hand_cap_l_arm_off,models/players/stormtrooper/caps.tga
l_arm_cap_l_hand_off,models/players/stormtrooper/caps.tga
l_arm_cap_torso_off,models/players/stormtrooper/caps.tga
torso_cap_head_off,models/players/stormtrooper/caps.tga
torso_cap_hips_off,models/players/stormtrooper/caps.tga
torso_cap_l_arm_off,models/players/stormtrooper/caps.tga
torso_cap_r_arm_off,models/players/stormtrooper/caps.tga
r_leg,models/players/tavion_new/legs.tga
r_leg_cap_hips_off,models/players/stormtrooper/caps.tga
l_leg,models/players/tavion_new/legs.tga
l_leg_cap_hips_off,models/players/stormtrooper/caps.tga
hips_cap_l_leg_off,models/players/stormtrooper/caps.tga
hips_cap_r_leg_off,models/players/stormtrooper/caps.tga
hips_cap_torso_off,models/players/stormtrooper/caps.tga

View File

@ -0,0 +1,37 @@
hips,models/players/tavion_new/legs_blue_glow.tga
hips_belt,models/players/tavion_new/legs_blue_glow.tga
hips_pockets,models/players/tavion_new/legs_blue_glow.tga
torso,models/players/tavion_new/torso_blue_glow.tga
torso_armor,models/players/tavion_new/torso_blue_glow.tga
head,models/players/tavion_new/head_glow.tga
head_hair,models/players/tavion_new/head_glow.tga
head_teeth,models/players/tavion_new/head_glow.tga
head_twills,models/players/tavion_new/head_glow.tga
head_feathers,models/players/tavion_new/feathers_glow.tga
head_eyelashes,models/players/tavion_new/feathers_glow.tga
head_ahair,models/players/tavion_new/feathers_glow.tga
head_cap_torso_off,models/players/stormtrooper/caps.tga
head_face,models/players/tavion_new/face_glow.tga
head_moutheyes,models/players/tavion_new/face_glow.tga
r_arm,models/players/tavion_new/arm_blue_glow.tga
r_hand,models/players/tavion_new/hands_glow.tga
r_hand_cap_r_arm_off,models/players/stormtrooper/caps.tga
r_arm_gauntlet,models/players/tavion_new/arm_blue_glow.tga
r_arm_cap_r_hand_off,models/players/stormtrooper/caps.tga
r_arm_cap_torso_off,models/players/stormtrooper/caps.tga
l_arm,models/players/tavion_new/arm_blue_glow.tga
l_hand,models/players/tavion_new/hands_glow.tga
l_hand_cap_l_arm_off,models/players/stormtrooper/caps.tga
l_arm_cap_l_hand_off,models/players/stormtrooper/caps.tga
l_arm_cap_torso_off,models/players/stormtrooper/caps.tga
torso_cap_head_off,models/players/stormtrooper/caps.tga
torso_cap_hips_off,models/players/stormtrooper/caps.tga
torso_cap_l_arm_off,models/players/stormtrooper/caps.tga
torso_cap_r_arm_off,models/players/stormtrooper/caps.tga
r_leg,models/players/tavion_new/legs_blue_glow.tga
r_leg_cap_hips_off,models/players/stormtrooper/caps.tga
l_leg,models/players/tavion_new/legs_blue_glow.tga
l_leg_cap_hips_off,models/players/stormtrooper/caps.tga
hips_cap_l_leg_off,models/players/stormtrooper/caps.tga
hips_cap_r_leg_off,models/players/stormtrooper/caps.tga
hips_cap_torso_off,models/players/stormtrooper/caps.tga

View File

@ -0,0 +1,37 @@
hips,models/players/tavion_new/legs_red.tga
hips_belt,models/players/tavion_new/legs_red.tga
hips_pockets,models/players/tavion_new/legs_red.tga
torso,models/players/tavion_new/torso_red.tga
torso_armor,models/players/tavion_new/torso_red.tga
head,models/players/tavion_new/head.tga
head_hair,models/players/tavion_new/head.tga
head_teeth,models/players/tavion_new/head.tga
head_twills,models/players/tavion_new/head.tga
head_feathers,models/players/tavion_new/feathers.tga
head_eyelashes,models/players/tavion_new/feathers.tga
head_ahair,models/players/tavion_new/feathers.tga
head_cap_torso_off,models/players/stormtrooper/caps.tga
head_face,models/players/tavion_new/face.tga
head_moutheyes,models/players/tavion_new/face.tga
r_arm,models/players/tavion_new/arm_red.tga
r_hand,models/players/tavion_new/hands.tga
r_hand_cap_r_arm_off,models/players/stormtrooper/caps.tga
r_arm_gauntlet,models/players/tavion_new/arm_red.tga
r_arm_cap_r_hand_off,models/players/stormtrooper/caps.tga
r_arm_cap_torso_off,models/players/stormtrooper/caps.tga
l_arm,models/players/tavion_new/arm_red.tga
l_hand,models/players/tavion_new/hands.tga
l_hand_cap_l_arm_off,models/players/stormtrooper/caps.tga
l_arm_cap_l_hand_off,models/players/stormtrooper/caps.tga
l_arm_cap_torso_off,models/players/stormtrooper/caps.tga
torso_cap_head_off,models/players/stormtrooper/caps.tga
torso_cap_hips_off,models/players/stormtrooper/caps.tga
torso_cap_l_arm_off,models/players/stormtrooper/caps.tga
torso_cap_r_arm_off,models/players/stormtrooper/caps.tga
r_leg,models/players/tavion_new/legs_red.tga
r_leg_cap_hips_off,models/players/stormtrooper/caps.tga
l_leg,models/players/tavion_new/legs_red.tga
l_leg_cap_hips_off,models/players/stormtrooper/caps.tga
hips_cap_l_leg_off,models/players/stormtrooper/caps.tga
hips_cap_r_leg_off,models/players/stormtrooper/caps.tga
hips_cap_torso_off,models/players/stormtrooper/caps.tga

View File

@ -0,0 +1,2 @@
tavion
f

BIN
model/tavion_new/torso.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB