35 dmd2frame_t *src_frame;
36 dmd2trivertx_t *src_vert;
37 dmd2triangle_t *src_tri;
49 int numverts, numindices;
50 char skinname[MAX_QPATH];
51 vec_t scale_s, scale_t;
55 if (length <
sizeof(header)) {
56 return Q_ERR_FILE_TOO_SMALL;
60 header = *(dmd2header_t *)rawdata;
61 for (i = 0; i <
sizeof(header) / 4; i++) {
62 ((uint32_t *)&header)[i] = LittleLong(((uint32_t *)&header)[i]);
68 if (ret == Q_ERR_TOO_FEW) {
70 model->type = MOD_EMPTY;
78 src_tri = (dmd2triangle_t *)((
byte *)rawdata + header.ofs_tris);
79 for (i = 0; i < header.num_tris; i++) {
80 for (j = 0; j < 3; j++) {
81 uint16_t idx_xyz = LittleShort(src_tri->index_xyz[j]);
82 uint16_t idx_st = LittleShort(src_tri->index_st[j]);
85 if (idx_xyz >= header.num_xyz || idx_st >= header.num_st) {
89 vertIndices[numindices + j] = idx_xyz;
90 tcIndices[numindices + j] = idx_st;
100 return Q_ERR_TOO_FEW;
103 for (i = 0; i < numindices; i++) {
109 src_tc = (dmd2stvert_t *)((
byte *)rawdata + header.ofs_st);
110 for (i = 0; i < numindices; i++) {
111 if (remap[i] != 0xFFFF) {
115 for (j = i + 1; j < numindices; j++) {
116 if (vertIndices[i] == vertIndices[j] &&
117 (src_tc[tcIndices[i]].s == src_tc[tcIndices[j]].s &&
118 src_tc[tcIndices[i]].t == src_tc[tcIndices[j]].t)) {
121 finalIndices[j] = numverts;
127 finalIndices[i] = numverts++;
131 return Q_ERR_TOO_MANY;
135 model->type = MOD_ALIAS;
136 model->nummeshes = 1;
137 model->numframes = header.num_frames;
139 model->frames = MOD_Malloc(header.num_frames *
sizeof(
maliasframe_t));
141 dst_mesh = model->meshes;
142 dst_mesh->
numtris = numindices / 3;
145 dst_mesh->
numskins = header.num_skins;
146 dst_mesh->
verts = MOD_Malloc(numverts * header.num_frames *
sizeof(
maliasvert_t));
150 if (dst_mesh->
numtris != header.num_tris) {
151 Com_DPrintf(
"%s has %d bad triangles\n", model->name, header.num_tris - dst_mesh->
numtris);
155 for (i = 0; i < numindices; i++) {
156 dst_mesh->
indices[i] = finalIndices[i];
160 src_skin = (
char *)rawdata + header.ofs_skins;
161 for (i = 0; i < header.num_skins; i++) {
162 if (!
Q_memccpy(skinname, src_skin, 0,
sizeof(skinname))) {
163 ret = Q_ERR_STRING_TRUNCATED;
168 src_skin += MD2_MAX_SKINNAME;
172 src_tc = (dmd2stvert_t *)((
byte *)rawdata + header.ofs_st);
174 scale_s = 1.0f / header.skinwidth;
175 scale_t = 1.0f / header.skinheight;
176 for (i = 0; i < numindices; i++) {
180 dst_tc[finalIndices[i]].
st[0] =
181 (int16_t)LittleShort(src_tc[tcIndices[i]].s) * scale_s;
182 dst_tc[finalIndices[i]].
st[1] =
183 (int16_t)LittleShort(src_tc[tcIndices[i]].t) * scale_t;
187 src_frame = (dmd2frame_t *)((
byte *)rawdata + header.ofs_frames);
188 dst_frame = model->frames;
189 for (j = 0; j < header.num_frames; j++) {
190 LittleVector(src_frame->scale, dst_frame->
scale);
191 LittleVector(src_frame->translate, dst_frame->
translate);
195 for (i = 0; i < numindices; i++) {
199 src_vert = &src_frame->verts[vertIndices[i]];
200 dst_vert = &dst_mesh->
verts[j * numverts + finalIndices[i]];
202 dst_vert->
pos[0] = src_vert->v[0];
203 dst_vert->
pos[1] = src_vert->v[1];
204 dst_vert->
pos[2] = src_vert->v[2];
206 val = src_vert->lightnormalindex;
207 if (val >= NUMVERTEXNORMALS) {
208 dst_vert->
norm[0] = 0;
209 dst_vert->
norm[1] = 0;
215 for (k = 0; k < 3; k++) {
216 val = dst_vert->
pos[k];
224 VectorVectorScale(mins, dst_frame->
scale, mins);
225 VectorVectorScale(maxs, dst_frame->
scale, maxs);
232 src_frame = (dmd2frame_t *)((
byte *)src_frame + header.framesize);
237 return Q_ERR_SUCCESS;