Skip to content

Commit

Permalink
"Fix" the empty LOD issue
Browse files Browse the repository at this point in the history
I'm gonna fix this properly one-day. This should only cause issues
for Land Contact LODs, but I don't think I've ever seen an empty
one in the wild.
  • Loading branch information
KoffeinFlummi committed May 15, 2016
1 parent 89436a7 commit 3d6c94f
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions src/p3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ int read_lods(FILE *f_source, struct mlod_lod *mlod_lods, uint32_t num_lods) {
int j;
int fp_tmp;
int fp_taggs;
bool empty;
uint32_t tagg_len;

fseek(f_source, 12, SEEK_SET);
Expand All @@ -66,9 +67,20 @@ int read_lods(FILE *f_source, struct mlod_lod *mlod_lods, uint32_t num_lods) {
fread(&mlod_lods[i].num_faces, 4, 1, f_source);
fseek(f_source, 4, SEEK_CUR);

mlod_lods[i].points = (struct point *)malloc(sizeof(struct point) * mlod_lods[i].num_points);
for (j = 0; j < mlod_lods[i].num_points; j++)
fread(&mlod_lods[i].points[j], sizeof(struct point), 1, f_source);
empty = mlod_lods[i].num_points == 0;

if (empty) {
mlod_lods[i].num_points = 1;
mlod_lods[i].points = (struct point *)malloc(sizeof(struct point));
mlod_lods[i].points[0].x = 0.0f;
mlod_lods[i].points[0].y = 0.0f;
mlod_lods[i].points[0].z = 0.0f;
mlod_lods[i].points[0].point_flags = 0;
} else {
mlod_lods[i].points = (struct point *)malloc(sizeof(struct point) * mlod_lods[i].num_points);
for (j = 0; j < mlod_lods[i].num_points; j++)
fread(&mlod_lods[i].points[j], sizeof(struct point), 1, f_source);
}

mlod_lods[i].facenormals = (struct triplet *)malloc(sizeof(struct triplet) * mlod_lods[i].num_facenormals);
for (j = 0; j < mlod_lods[i].num_facenormals; j++)
Expand Down Expand Up @@ -154,16 +166,26 @@ int read_lods(FILE *f_source, struct mlod_lod *mlod_lods, uint32_t num_lods) {

strcpy(mlod_lods[i].selections[j].name, buffer);

mlod_lods[i].selections[j].points = (uint8_t *)malloc(mlod_lods[i].num_points);
mlod_lods[i].selections[j].faces = (uint8_t *)malloc(mlod_lods[i].num_faces);
if (empty) {
mlod_lods[i].selections[j].points = (uint8_t *)malloc(1);
mlod_lods[i].selections[j].points[0] = 0;
} else {
mlod_lods[i].selections[j].points = (uint8_t *)malloc(mlod_lods[i].num_points);
fread(mlod_lods[i].selections[j].points, mlod_lods[i].num_points, 1, f_source);
}

fread(mlod_lods[i].selections[j].points, mlod_lods[i].num_points, 1, f_source);
mlod_lods[i].selections[j].faces = (uint8_t *)malloc(mlod_lods[i].num_faces);
fread(mlod_lods[i].selections[j].faces, mlod_lods[i].num_faces, 1, f_source);
}

if (strcmp(buffer, "#Mass#") == 0) {
mlod_lods[i].mass = (float *)malloc(sizeof(float) * mlod_lods[i].num_points);
fread(mlod_lods[i].mass, sizeof(float) * mlod_lods[i].num_points, 1, f_source);
if (empty) {
mlod_lods[i].mass = (float *)malloc(sizeof(float));
mlod_lods[i].mass[0] = 0.0f;
} else {
mlod_lods[i].mass = (float *)malloc(sizeof(float) * mlod_lods[i].num_points);
fread(mlod_lods[i].mass, sizeof(float) * mlod_lods[i].num_points, 1, f_source);
}
}

if (strcmp(buffer, "#SharpEdges#") == 0) {
Expand Down

0 comments on commit 3d6c94f

Please sign in to comment.