160 dmd2frame_t *src_frame;
162 dmd2triangle_t *src_tri;
163 dmd2stvert_t *src_st;
168 char skinname[MAX_QPATH];
173 if (length <
sizeof(header)) {
174 return Q_ERR_FILE_TOO_SMALL;
178 header = *(dmd2header_t *)rawdata;
179 for (i = 0; i <
sizeof(header) / 4; i++) {
180 ((uint32_t *)&header)[i] = LittleLong(((uint32_t *)&header)[i]);
186 if (ret == Q_ERR_TOO_FEW) {
188 model->type = MOD_EMPTY;
189 return Q_ERR_SUCCESS;
195 model->type = MOD_ALIAS;
198 model->tris = MOD_Malloc(header.num_tris *
sizeof(
maliastri_t));
199 model->numtris = header.num_tris;
201 src_tri = (dmd2triangle_t *)((
byte *)rawdata + header.ofs_tris);
202 dst_tri = model->tris;
203 for (i = 0; i < header.num_tris; i++, src_tri++, dst_tri++) {
204 for (j = 0; j < 3; j++) {
205 uint16_t idx_xyz = LittleShort(src_tri->index_xyz[j]);
206 uint16_t idx_st = LittleShort(src_tri->index_st[j]);
208 if (idx_xyz >= header.num_xyz || idx_st >= header.num_st) {
209 ret = Q_ERR_BAD_INDEX;
219 model->sts = MOD_Malloc(header.num_st *
sizeof(
maliasst_t));
220 model->numsts = header.num_st;
221 model->skinwidth = header.skinwidth;
222 model->skinheight = header.skinheight;
224 src_st = (dmd2stvert_t *)((
byte *)rawdata + header.ofs_st);
226 for (i = 0; i < header.num_st; i++, src_st++, dst_st++) {
227 dst_st->
s = (int16_t)LittleShort(src_st->s);
228 dst_st->
t = (int16_t)LittleShort(src_st->t);
230 if (dst_st->
s < 0 || dst_st->
s >= header.skinwidth) {
231 ret = Q_ERR_BAD_INDEX;
235 if (dst_st->
t < 0 || dst_st->
t >= header.skinheight) {
236 ret = Q_ERR_BAD_INDEX;
242 model->frames = MOD_Malloc(header.num_frames *
sizeof(
maliasframe_t));
243 model->numframes = header.num_frames;
244 model->numverts = header.num_xyz;
246 src_frame = (dmd2frame_t *)((
byte *)rawdata + header.ofs_frames);
247 dst_frame = model->frames;
248 for (i = 0; i < header.num_frames; i++, dst_frame++) {
249 for (j = 0; j < 3; j++) {
250 dst_frame->
scale[j] = LittleFloat(src_frame->scale[j]);
251 dst_frame->
translate[j] = LittleFloat(src_frame->translate[j]);
256 memcpy(dst_frame->
verts, src_frame->verts, header.num_xyz *
sizeof(
maliasvert_t));
259 for (j = 0; j < header.num_xyz; j++) {
260 if (dst_frame->
verts[j].lightnormalindex > NUMVERTEXNORMALS) {
261 ret = Q_ERR_BAD_INDEX;
266 src_frame = (dmd2frame_t *)((
byte *)src_frame + header.framesize);
270 src_skin = (
char *)rawdata + header.ofs_skins;
271 for (i = 0; i < header.num_skins; i++) {
272 if (!
Q_memccpy(skinname, src_skin, 0,
sizeof(skinname))) {
273 ret = Q_ERR_STRING_TRUNCATED;
277 model->skins[i] =
IMG_Find(skinname, IT_SKIN, IF_NONE);
278 src_skin += MD2_MAX_SKINNAME;
280 model->numskins = header.num_skins;
283 return Q_ERR_SUCCESS;