Skip to content

Commit

Permalink
Fix objs that don't have low LOD
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusfavarin committed Sep 28, 2021
1 parent a78a23f commit 36c2a9a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
12 changes: 6 additions & 6 deletions Documentation/RACERS/MDL.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ The geometry section contains the coordinates, colors, drawing format (tri or qu
| 0x0 | 0x1u | Red | Vertex color intensity (R, G, B). Used as shading, since the model is textured. |
| 0x1 | 0x1u | Green | Vertex color intensity (R, G, B). Used as shading, since the model is textured. |
| 0x2 | 0x1u | Blue | Vertex color intensity (R, G, B). Used as shading, since the model is textured. |
| 0x3 | 0x1u | Color command (cmd) | This byte is ignored if you aren't reading the first vertex of a face. If cmd equals 0, then a new mesh starts. If cmd equals 0x38, then the next 3 vertexes will form a quad with the current vertex; if cmd does not equal 0x38, then the next 2 vertexes will make a triangle with the current vertex. |
| 0x3 | 0x1u | Color command (cmd) | This byte is ignored if you aren't reading the first vertex of a face. If cmd equals 0, then a new mesh starts. If a new mesh starts and the red color equals 2, then the object has a low LOD mesh. If cmd equals 0x38, then the next 3 vertexes will form a quad with the current vertex; if cmd does not equal 0x38, then the next 2 vertexes will make a triangle with the current vertex. |
| 0x4 | 0x2s | X | Position of the vertex. |
| 0x6 | 0x2s | Y | Position of the vertex. |
| 0x8 | 0x2s | Z | Position of the vertex. |
| 0xA | 0x2s | Vertex palette | Texture data is still unknown. |

## Mesh metadata

This section starts with a number indicating the number of objects in the file. Each object has a high LOD mesh and a low LOD mesh (LOD = Level of Detail).
This section starts with a number indicating the number of objects in the file. Each object has a high LOD mesh, and may or may not have a low LOD mesh (LOD = Level of Detail).

| Offset (h) | Size (h) | Usage | Notes |
|:---------- |:-------- |:------------------------------ |:------------------------------------------ |
| 0x0 | 0x4u | Number of objects in the file. | Multiplying by 2 gives you the mesh count. |
| Offset (h) | Size (h) | Usage | Notes |
|:---------- |:-------- |:------------------------------ |:----- |
| 0x0 | 0x4u | Number of objects in the file. | N/a. |

It's followed by a data structure with size of 0x4C bytes, which contains metadata of each object.

| Offset (h) | Size (h) | Usage | Notes |
|:---------- |:-------- |:--------------------------------------------- |:---------------------------------------------------- |
| 0x0 | 0x4u | Size of the mesh (high + low LOD). | N/a. |
| 0x0 | 0x4u | Offset to the end of the object halved. | Multiplying by 2 gives the true offset in the file. |
| 0x4 | 0x4s | X translation. | Translation applied to both high and low LOD meshes. |
| 0x8 | 0x4s | Y translation. | Translation applied to both high and low LOD meshes. |
| 0xC | 0x4s | Z translation. | Translation applied to both high and low LOD meshes. |
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CC = g++
FLAGS = -std=c++17
CPP_FILES = main.cpp extractor.cpp racer.cpp course.cpp raw.cpp
CPP_FILES = main.cpp extractor.cpp racer.cpp
PROGRAM = extractor

all: $(CPP_FILES)
Expand Down
20 changes: 14 additions & 6 deletions src/racer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ void mdl_to_obj(fs::path mdl_path, fs::path output_path) {
racer_model.read((char *) &mh, sizeof(mh));

// Reading mesh metadata
MESH_METADATA metadata[mh.mesh_count];
for (int i = 0; i < mh.mesh_count; i++) {
MESH_METADATA metadata[mh.obj_count];
for (int i = 0; i < mh.obj_count; i++) {
racer_model.read((char *) &metadata[i], sizeof(metadata[i]));
metadata[i].obj_size *= 2;
}

// Restoring the seek position to read the mesh data
Expand All @@ -45,11 +46,13 @@ void mdl_to_obj(fs::path mdl_path, fs::path output_path) {
int num_vertex = 0;
// Index to keep track of the number of meshes found
int num_meshes = 0;
// Index to keep track of the number of objects found
int num_objects = 0;
// Bool to keep track of when to create new meshes objects
bool new_obj = true;

// While there are vertexes bytes to be read
while (num_meshes < mh.mesh_count * 2) {
while (num_objects < mh.obj_count * 2) {

/* Read the color command that goes to the GTE. They encoded
in these bytes whether the polygon is a triangle or a quad. Also,
Expand All @@ -63,6 +66,11 @@ void mdl_to_obj(fs::path mdl_path, fs::path output_path) {
// If not, then the next vertex will belong to a new mesh
new_obj = true;
num_meshes++;
// Checking whether this object has a low LOD mesh or not
if (vc.r != 2) {
// If not, then we move on to the next object
num_objects++;
}
continue;
}

Expand All @@ -86,9 +94,9 @@ void mdl_to_obj(fs::path mdl_path, fs::path output_path) {
racer_model.read((char *) &v, sizeof(v));
num_vertex += 1;
output_file << "v "
<< ((v.x + metadata[num_meshes / 2].trans_x) * -1) / 127.0 << " "
<< ((v.y + metadata[num_meshes / 2].trans_y) * -1) / 127.0 << " "
<< ((v.z + metadata[num_meshes / 2].trans_z) * -1) / 127.0 << " "
<< ((v.x + metadata[num_objects].trans_x) * -1) / 127.0 << " "
<< ((v.y + metadata[num_objects].trans_y) * -1) / 127.0 << " "
<< ((v.z + metadata[num_objects].trans_z) * -1) / 127.0 << " "
<< vc.r / 255.0 << " " << vc.g / 255.0 << " " << vc.b / 255.0 << endl;

// If the vertex is the last of the face, write the face to the .obj
Expand Down
4 changes: 2 additions & 2 deletions src/racer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ struct VERTEX {
};

struct MESH_HEADER {
uint32_t mesh_count;
uint32_t obj_count;
};

struct MESH_METADATA {
uint32_t mesh_size;
uint32_t obj_size;
int32_t trans_x;
int32_t trans_y;
int32_t trans_z;
Expand Down

0 comments on commit 36c2a9a

Please sign in to comment.