-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssm.c
177 lines (117 loc) · 4.21 KB
/
ssm.c
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
#include "ssm.h"
#include "mesh.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <cglm/cglm.h>
void ssm_init(struct ssm* ssm, const char* path)
{
FILE* fp;
char* data;
size_t offset;
size_t length;
short string_len;
data = NULL;
offset = 0;
length = 0;
memset(ssm, 0, sizeof(struct ssm));
fp = fopen(path, "r");
if(fp == NULL) {
fprintf(stderr, "error: %s\n", strerror(errno));
goto ssmi_done;
}
fseek(fp, 0, SEEK_END);
length = ftell(fp);
fseek(fp, 0, SEEK_SET);
data = malloc(length);
fread(data, 1, length, fp);
fclose(fp);
if(strncmp(data, "SSMO", 4) != 0) {
fprintf(stderr, "error: %s\n", "not a valid SSM file.");
goto ssmi_done;
}
offset = 8;
ssm->num_vertices = *(unsigned short*)(data + offset);
ssm->num_indices = *(unsigned short*)(data + offset + 2);
ssm->num_textures = *(unsigned short*)(data + offset + 4);
ssm->num_frames = *(unsigned short*)(data + offset + 6);
ssm->num_anims = *(unsigned short*)(data + offset + 8);
offset += 10;
ssm->num_meshes = *(data + offset);
*(data + offset) = 0; /* number of meshes is encoded in first index */
ssm->index_table = calloc(ssm->num_indices, sizeof(struct ssm_index));
ssm->anim_table = calloc(ssm->num_anims, sizeof(struct ssm_anim));
ssm->frame_table = calloc(ssm->num_frames, sizeof(float**));
ssm->meshes = calloc(ssm->num_meshes, sizeof(struct mesh));
for(int i = 0; i < ssm->num_indices; i++) {
ssm->index_table[i].mesh_id = *(data + offset);
ssm->index_table[i].p0 = *(unsigned short*)(data + offset + 4);
ssm->index_table[i].p1 = *(unsigned short*)(data + offset + 6);
ssm->index_table[i].p2 = *(unsigned short*)(data + offset + 8);
offset += 36;
}
offset += 8;
for(int i = 0; i < ssm->num_textures; i++) {
string_len = *(unsigned short*)(data + offset);
offset += 2;
offset += string_len + 4;
}
offset += 4;
for(int i = 0; i < ssm->num_frames; i++) {
ssm->frame_table[i] = calloc(3 * ssm->num_vertices, sizeof(float));
string_len = *(unsigned short*)(data + offset);
offset += 2;
offset += string_len + ssm->num_meshes;
memcpy(ssm->frame_table[i], data + offset, 12 *ssm->num_vertices);
offset += 12 * ssm->num_vertices + 8;
}
for(int i = 0; i < ssm->num_anims; i++) {
short s;
ssm->anim_table[i].num_frames = *(unsigned short*)(data + offset);
string_len = *(short*)(data + offset + 2);
ssm->anim_table[i].name = calloc(string_len, sizeof(char));
strncpy(ssm->anim_table[i].name, data + offset + 4, string_len);
offset += string_len + 4;
ssm->anim_table[i].frame_ids = calloc(ssm->anim_table[i].num_frames, sizeof(int));
for(int j = 0; j < ssm->anim_table[i].num_frames; j++) {
s = *(unsigned short*)(data + offset);
ssm->anim_table[i].frame_ids[j] = s;
offset += 2;
}
/* The usage of the data here is not known */
offset += 4 * ssm->anim_table[i].num_frames + 8;
}
for(int i = 0, j = 0; i < ssm->num_meshes; i++) {
int j0;
int* inds;
j0 = j;
while(ssm->index_table[j].mesh_id == i) j++;
inds = calloc(3 * (j - j0), sizeof(int));
for(int k = 0; k < (j - j0); k++) {
inds[3 * k + 0] = ssm->index_table[k + j0].p0;
inds[3 * k + 1] = ssm->index_table[k + j0].p1;
inds[3 * k + 2] = ssm->index_table[k + j0].p2;
}
mesh_init(&ssm->meshes[i], ssm->num_vertices,
(struct vertex*)ssm->frame_table[0], 3 * (j - j0), inds);
free(inds);
}
ssmi_done:
free(data);
}
void ssm_term(struct ssm* ssm)
{
for(int i = 0; i < ssm->num_frames; i++) {
free(ssm->frame_table[i]);
}
for(int i = 0; i < ssm->num_anims; i++) {
free(ssm->anim_table[i].frame_ids);
}
free(ssm->anim_table);
free(ssm->frame_table);
free(ssm->index_table);
for(int i = 0; i < ssm->num_meshes; i++) {
mesh_term(&ssm->meshes[i]);
}
}