42 #include "format/md2.h"
43 #include "format/md3.h"
44 #include "format/sp2.h"
48 #if MAX_ALIAS_VERTS > TESS_MAX_VERTICES
49 #error TESS_MAX_VERTICES
52 #if MD2_MAX_TRIANGLES > TESS_MAX_INDICES / 3
53 #error TESS_MAX_INDICES
58 for (
int idx_mesh = 0; idx_mesh < model->nummeshes; ++idx_mesh)
63 float * stangents = Z_Malloc(mesh->
numverts * 2 * 3 *
sizeof(
float));
64 float * ttangents = stangents + (mesh->
numverts * 3);
66 for (
int idx_frame = 0; idx_frame < model->numframes; ++idx_frame)
68 memset(stangents, 0, mesh->
numverts * 2 * 3 *
sizeof(
float));
73 for (
int idx_tri = 0; idx_tri < mesh->
numtris; ++idx_tri)
75 uint32_t iA = mesh->
indices[idx_tri * 3 + 0];
76 uint32_t iB = mesh->
indices[idx_tri * 3 + 1];
77 uint32_t iC = mesh->
indices[idx_tri * 3 + 2];
79 float const * pA = (
float const *)mesh->
positions + ((offset + iA) * 3);
80 float const * pB = (
float const *)mesh->
positions + ((offset + iB) * 3);
81 float const * pC = (
float const *)mesh->
positions + ((offset + iC) * 3);
83 float const * tA = (
float const *)mesh->
tex_coords + ((offset + iA) * 2);
84 float const * tB = (
float const *)mesh->
tex_coords + ((offset + iB) * 2);
85 float const * tC = (
float const *)mesh->
tex_coords + ((offset + iC) * 2);
88 VectorSubtract(pB, pA, dP0);
89 VectorSubtract(pC, pA, dP1);
92 Vector2Subtract(tB, tA, dt0);
93 Vector2Subtract(tC, tA, dt1);
95 float r = 1.f / (dt0[0] * dt1[1] - dt1[0] * dt0[1]);
98 (dt1[1] * dP0[0] - dt0[1] * dP1[0]) * r,
99 (dt1[1] * dP0[1] - dt0[1] * dP1[1]) * r,
100 (dt1[1] * dP0[2] - dt0[1] * dP1[2]) * r };
103 (dt0[0] * dP1[0] - dt1[0] * dP0[0]) * r,
104 (dt0[0] * dP1[1] - dt1[0] * dP0[1]) * r,
105 (dt0[0] * dP1[2] - dt1[0] * dP0[2]) * r };
107 VectorAdd(stangents + (iA * 3), sdir, stangents + (iA * 3));
108 VectorAdd(stangents + (iB * 3), sdir, stangents + (iB * 3));
109 VectorAdd(stangents + (iC * 3), sdir, stangents + (iC * 3));
111 VectorAdd(ttangents + (iA * 3), tdir, ttangents + (iA * 3));
112 VectorAdd(ttangents + (iB * 3), tdir, ttangents + (iB * 3));
113 VectorAdd(ttangents + (iC * 3), tdir, ttangents + (iC * 3));
116 for (
int idx_vert = 0; idx_vert < mesh->
numverts; ++idx_vert)
118 float const * normal = (
float const *)mesh->
normals + ((offset + idx_vert) * 3);
119 float const * stan = stangents + (idx_vert * 3);
120 float const * ttan = ttangents + (idx_vert * 3);
122 float * tangent = (
float *)mesh->
tangents + ((offset+idx_vert) * 4);
125 VectorScale(normal, DotProduct(normal, stan), t);
126 VectorSubtract(stan, t, t);
130 CrossProduct(normal, t, cross);
131 float dot = DotProduct(cross, ttan);
132 tangent[3] = dot < 0.0f ? -1.0f : 1.0f;
141 for (
int idx_frame = 0; idx_frame < model->numframes; ++idx_frame)
143 char path[MAX_OSPATH];
144 sprintf(path, path_pattern, idx_frame);
145 FILE* file = fopen(path,
"w");
150 int mesh_vertex_offset = 1;
152 for (
int idx_mesh = 0; idx_mesh < model->nummeshes; ++idx_mesh)
156 offset = idx_frame * mesh->
numverts;
158 for (
int idx_vert = 0; idx_vert < mesh->
numverts; ++idx_vert)
160 float const * p = (
float const*)mesh->
positions + (offset + idx_vert) * 3;
161 float const * n = (
float const*)mesh->
normals + (offset + idx_vert) * 3;
162 float const * t = (
float const*)mesh->
tex_coords + (offset + idx_vert) * 2;
163 fprintf(file,
"v %.3f %.3f %.3f\n", p[0], p[1], p[2]);
164 fprintf(file,
"vn %.3f %.3f %.3f\n", n[0], n[1], n[2]);
165 fprintf(file,
"vt %.3f %.3f\n", t[0], t[1]);
168 fprintf(file,
"g mesh_%d\n", idx_mesh);
170 for (
int idx_tri = 0; idx_tri < mesh->
numtris; ++idx_tri)
172 int iA = mesh->
indices[idx_tri * 3 + 0] + mesh_vertex_offset;
173 int iB = mesh->
indices[idx_tri * 3 + 1] + mesh_vertex_offset;
174 int iC = mesh->
indices[idx_tri * 3 + 2] + mesh_vertex_offset;
176 fprintf(file,
"f %d/%d/%d %d/%d/%d %d/%d/%d\n", iA, iA, iA, iB, iB, iB, iC, iC, iC);
179 mesh_vertex_offset += mesh->
numverts;
189 dmd2frame_t *src_frame;
190 dmd2trivertx_t *src_vert;
191 dmd2triangle_t *src_tri;
192 dmd2stvert_t *src_tc;
201 int numverts, numindices;
202 char skinname[MAX_QPATH];
203 vec_t scale_s, scale_t;
207 if (length <
sizeof(header)) {
208 return Q_ERR_FILE_TOO_SMALL;
212 header = *(dmd2header_t *)rawdata;
213 for (
int i = 0; i <
sizeof(header) / 4; i++) {
214 ((uint32_t *)&header)[i] = LittleLong(((uint32_t *)&header)[i]);
220 if (ret == Q_ERR_TOO_FEW) {
222 model->type = MOD_EMPTY;
223 return Q_ERR_SUCCESS;
230 src_tri = (dmd2triangle_t *)((
byte *)rawdata + header.ofs_tris);
231 for (
int i = 0; i < header.num_tris; i++) {
233 for (
int j = 0; j < 3; j++) {
234 uint16_t idx_xyz = LittleShort(src_tri->index_xyz[j]);
235 uint16_t idx_st = LittleShort(src_tri->index_st[j]);
238 if (idx_xyz >= header.num_xyz || idx_st >= header.num_st) {
243 vertIndices[numindices + j] = idx_xyz;
244 tcIndices[numindices + j] = idx_st;
253 if (numindices < 3) {
254 return Q_ERR_TOO_FEW;
257 qboolean all_normals_same = qtrue;
258 int same_normal = -1;
260 src_frame = (dmd2frame_t *)((
byte *)rawdata + header.ofs_frames);
261 for (
int i = 0; i < numindices; i++)
263 int v = vertIndices[i];
264 int normal = src_frame->verts[v].lightnormalindex;
269 same_normal = normal;
270 else if (normal != same_normal)
271 all_normals_same = qfalse;
274 for (
int i = 0; i < numindices; i++) {
280 src_tc = (dmd2stvert_t *)((
byte *)rawdata + header.ofs_st);
281 for (
int i = 0; i < numindices; i++) {
282 if (remap[i] != 0xFFFF) {
287 if (!all_normals_same)
289 for (
int j = i + 1; j < numindices; j++) {
290 if (vertIndices[i] == vertIndices[j] &&
291 (src_tc[tcIndices[i]].s == src_tc[tcIndices[j]].s &&
292 src_tc[tcIndices[i]].t == src_tc[tcIndices[j]].t)) {
295 finalIndices[j] = numverts;
302 finalIndices[i] = numverts++;
306 model->type = MOD_ALIAS;
307 model->nummeshes = 1;
308 model->numframes = header.num_frames;
310 model->frames = MOD_Malloc(header.num_frames *
sizeof(
maliasframe_t));
312 dst_mesh = model->meshes;
313 dst_mesh->
numtris = numindices / 3;
316 dst_mesh->
numskins = header.num_skins;
317 dst_mesh->
positions = MOD_Malloc(numverts * header.num_frames *
sizeof(vec3_t));
318 dst_mesh->
normals = MOD_Malloc(numverts * header.num_frames *
sizeof(vec3_t));
319 dst_mesh->
tex_coords = MOD_Malloc(numverts * header.num_frames *
sizeof(vec2_t));
320 dst_mesh->
tangents = MOD_Malloc(numverts * header.num_frames *
sizeof(vec4_t));
321 dst_mesh->
indices = MOD_Malloc(numindices *
sizeof(
int));
323 if (dst_mesh->
numtris != header.num_tris) {
324 Com_DPrintf(
"%s has %d bad triangles\n", model->name, header.num_tris - dst_mesh->
numtris);
328 for (
int i = 0; i < numindices; i++) {
329 dst_mesh->
indices[i] = finalIndices[i];
333 src_skin = (
char *)rawdata + header.ofs_skins;
334 for (
int i = 0; i < header.num_skins; i++) {
335 if (!
Q_memccpy(skinname, src_skin, 0,
sizeof(skinname))) {
336 ret = Q_ERR_STRING_TRUNCATED;
343 Com_EPrintf(
"error finding material '%s'\n", skinname);
347 image_t* image_emissive = NULL;
349 if (image_diffuse != R_NOTEXTURE)
352 if (!
Q_strlcpy(skinname, src_skin, strlen(src_skin) - 3))
353 return Q_ERR_STRING_TRUNCATED;
355 Q_concat(skinname,
sizeof(skinname), skinname,
"_n.tga", NULL);
357 image_normals =
IMG_Find(skinname, IT_SKIN, IF_NONE);
358 if (image_normals == R_NOTEXTURE) image_normals = NULL;
361 if (!
Q_strlcpy(skinname, src_skin, strlen(src_skin) - 3))
362 return Q_ERR_STRING_TRUNCATED;
364 Q_concat(skinname,
sizeof(skinname), skinname,
"_light.tga", NULL);
366 image_emissive =
IMG_Find(skinname, IT_SKIN, IF_SRGB);
367 if (image_emissive == R_NOTEXTURE) image_emissive = NULL;
374 src_skin += MD2_MAX_SKINNAME;
378 src_tc = (dmd2stvert_t *)((
byte *)rawdata + header.ofs_st);
379 scale_s = 1.0f / header.skinwidth;
380 scale_t = 1.0f / header.skinheight;
383 src_frame = (dmd2frame_t *)((
byte *)rawdata + header.ofs_frames);
384 dst_frame = model->frames;
385 for (
int j = 0; j < header.num_frames; j++) {
386 LittleVector(src_frame->scale, dst_frame->
scale);
387 LittleVector(src_frame->translate, dst_frame->
translate);
392 for (
int i = 0; i < numindices; i++) {
396 src_vert = &src_frame->verts[vertIndices[i]];
397 vec3_t *dst_pos = &dst_mesh->
positions [j * numverts + finalIndices[i]];
398 vec3_t *dst_nrm = &dst_mesh->
normals [j * numverts + finalIndices[i]];
399 vec2_t *dst_tc = &dst_mesh->
tex_coords[j * numverts + finalIndices[i]];
401 (*dst_tc)[0] = scale_s * src_tc[tcIndices[i]].s;
402 (*dst_tc)[1] = scale_t * src_tc[tcIndices[i]].t;
404 (*dst_pos)[0] = src_vert->v[0] * dst_frame->
scale[0] + dst_frame->
translate[0];
405 (*dst_pos)[1] = src_vert->v[1] * dst_frame->
scale[1] + dst_frame->
translate[1];
406 (*dst_pos)[2] = src_vert->v[2] * dst_frame->
scale[2] + dst_frame->
translate[2];
408 (*dst_nrm)[0] = 0.0f;
409 (*dst_nrm)[1] = 0.0f;
410 (*dst_nrm)[2] = 0.0f;
412 val = src_vert->lightnormalindex;
414 if (val < NUMVERTEXNORMALS) {
420 for (
int k = 0; k < 3; k++) {
430 if (all_normals_same)
432 for (
int tri = 0; tri < numindices / 3; tri++)
434 int i0 = j * numverts + finalIndices[tri * 3 + 0];
435 int i1 = j * numverts + finalIndices[tri * 3 + 1];
436 int i2 = j * numverts + finalIndices[tri * 3 + 2];
443 VectorSubtract(*p1, *p0, e1);
444 VectorSubtract(*p2, *p0, e2);
445 CrossProduct(e2, e1, n);
448 VectorCopy(n, dst_mesh->
normals[i0]);
449 VectorCopy(n, dst_mesh->
normals[i1]);
450 VectorCopy(n, dst_mesh->
normals[i2]);
454 VectorVectorScale(mins, dst_frame->
scale, mins);
455 VectorVectorScale(maxs, dst_frame->
scale, maxs);
462 src_frame = (dmd2frame_t *)((
byte *)src_frame + header.framesize);
467 for (
int i = 0; i < dst_mesh->
numindices; i += 3) {
468 int tmp = dst_mesh->
indices[i + 1];
470 dst_mesh->
indices[i + 2] = tmp;
476 return Q_ERR_SUCCESS;
485 #define TAB_SIN(x) qvk.sintab[(x) & 255]
486 #define TAB_COS(x) qvk.sintab[((x) + 64) & 255]
488 static qerror_t MOD_LoadMD3Mesh(model_t *model,
maliasmesh_t *mesh,
489 const byte *rawdata,
size_t length,
size_t *offset_p)
493 dmd3vertex_t *src_vert;
495 dmd3skin_t *src_skin;
502 char skinname[MAX_QPATH];
505 if (length <
sizeof(header))
506 return Q_ERR_BAD_EXTENT;
509 header = *(dmd3mesh_t *)rawdata;
510 for (i = 0; i <
sizeof(header) / 4; i++)
511 ((uint32_t *)&header)[i] = LittleLong(((uint32_t *)&header)[i]);
513 if (header.meshsize <
sizeof(header) || header.meshsize > length)
514 return Q_ERR_BAD_EXTENT;
515 if (header.num_verts < 3)
516 return Q_ERR_TOO_FEW;
518 return Q_ERR_TOO_MANY;
519 if (header.num_tris < 1)
520 return Q_ERR_TOO_FEW;
522 return Q_ERR_TOO_MANY;
523 if (header.num_skins > MAX_ALIAS_SKINS)
524 return Q_ERR_TOO_MANY;
525 end = header.ofs_skins + header.num_skins *
sizeof(dmd3skin_t);
526 if (end < header.ofs_skins || end > length)
527 return Q_ERR_BAD_EXTENT;
528 end = header.ofs_verts + header.num_verts * model->numframes *
sizeof(dmd3vertex_t);
529 if (end < header.ofs_verts || end > length)
530 return Q_ERR_BAD_EXTENT;
531 end = header.ofs_tcs + header.num_verts *
sizeof(dmd3coord_t);
532 if (end < header.ofs_tcs || end > length)
533 return Q_ERR_BAD_EXTENT;
534 end = header.ofs_indexes + header.num_tris * 3 *
sizeof(uint32_t);
535 if (end < header.ofs_indexes || end > length)
536 return Q_ERR_BAD_EXTENT;
538 mesh->
numtris = header.num_tris;
542 mesh->
positions = MOD_Malloc(header.num_verts * model->numframes *
sizeof(vec3_t));
543 mesh->
normals = MOD_Malloc(header.num_verts * model->numframes *
sizeof(vec3_t));
544 mesh->
tex_coords = MOD_Malloc(header.num_verts * model->numframes *
sizeof(vec2_t));
545 mesh->
tangents = MOD_Malloc(header.num_verts * header.num_frames *
sizeof(vec4_t));
546 mesh->
indices = MOD_Malloc(
sizeof(
int) * header.num_tris * 3);
549 src_skin = (dmd3skin_t *)(rawdata + header.ofs_skins);
550 for (i = 0; i < header.num_skins; i++) {
551 if (!
Q_memccpy(skinname, src_skin->name, 0,
sizeof(skinname)))
552 return Q_ERR_STRING_TRUNCATED;
557 Com_EPrintf(
"error finding material '%s'\n", skinname);
561 image_t* image_emissive = NULL;
563 if (image_diffuse != R_NOTEXTURE)
566 if (!
Q_strlcpy(skinname, src_skin->name, strlen(src_skin->name) - 3))
567 return Q_ERR_STRING_TRUNCATED;
569 Q_concat(skinname,
sizeof(skinname), skinname,
"_n.tga", NULL);
571 image_normals =
IMG_Find(skinname, IT_SKIN, IF_NONE);
572 if (image_normals == R_NOTEXTURE) image_normals = NULL;
575 if (!
Q_strlcpy(skinname, src_skin->name, strlen(src_skin->name) - 3))
576 return Q_ERR_STRING_TRUNCATED;
578 Q_concat(skinname,
sizeof(skinname), skinname,
"_light.tga", NULL);
580 image_emissive =
IMG_Find(skinname, IT_SKIN, IF_SRGB);
581 if (image_emissive == R_NOTEXTURE) image_emissive = NULL;
590 src_vert = (dmd3vertex_t *)(rawdata + header.ofs_verts);
595 for (
int frame = 0; frame < header.num_frames; frame++)
597 src_tc = (dmd3coord_t *)(rawdata + header.ofs_tcs);
599 for (i = 0; i < header.num_verts; i++)
601 (*dst_vert)[0] = (float)(src_vert->point[0]) / 64.f;
602 (*dst_vert)[1] = (float)(src_vert->point[1]) / 64.f;
603 (*dst_vert)[2] = (float)(src_vert->point[2]) / 64.f;
605 unsigned int lat = src_vert->norm[0];
606 unsigned int lng = src_vert->norm[1];
614 (*dst_tc)[0] = LittleFloat(src_tc->st[0]);
615 (*dst_tc)[1] = LittleFloat(src_tc->st[1]);
617 (*dst_tan)[0] = 0.0f;
618 (*dst_tan)[1] = 0.0f;
619 (*dst_tan)[2] = 0.0f;
620 (*dst_tan)[3] = 0.0f;
622 src_vert++; dst_vert++; dst_norm++;
623 src_tc++; dst_tc++; dst_tan++;
629 src_idx = (uint32_t *)(rawdata + header.ofs_indexes);
631 for (i = 0; i < header.num_tris; i++)
633 dst_idx[0] = LittleLong(src_idx[2]);
634 dst_idx[1] = LittleLong(src_idx[1]);
635 dst_idx[2] = LittleLong(src_idx[0]);
637 if (dst_idx[0] >= header.num_verts)
638 return Q_ERR_BAD_INDEX;
644 *offset_p = header.meshsize;
646 return Q_ERR_SUCCESS;
649 qerror_t
MOD_LoadMD3_RTX(model_t *model,
const void *rawdata,
size_t length)
652 size_t end, offset, remaining;
653 dmd3frame_t *src_frame;
655 const byte *src_mesh;
659 if (length <
sizeof(header))
660 return Q_ERR_FILE_TOO_SMALL;
663 header = *(dmd3header_t *)rawdata;
664 for (i = 0; i <
sizeof(header) / 4; i++)
665 ((uint32_t *)&header)[i] = LittleLong(((uint32_t *)&header)[i]);
667 if (header.ident != MD3_IDENT)
668 return Q_ERR_UNKNOWN_FORMAT;
669 if (header.version != MD3_VERSION)
670 return Q_ERR_UNKNOWN_FORMAT;
671 if (header.num_frames < 1)
672 return Q_ERR_TOO_FEW;
673 if (header.num_frames > MD3_MAX_FRAMES)
674 return Q_ERR_TOO_MANY;
675 end = header.ofs_frames +
sizeof(dmd3frame_t) * header.num_frames;
676 if (end < header.ofs_frames || end > length)
677 return Q_ERR_BAD_EXTENT;
678 if (header.num_meshes < 1)
679 return Q_ERR_TOO_FEW;
680 if (header.num_meshes > MD3_MAX_MESHES)
681 return Q_ERR_TOO_MANY;
682 if (header.ofs_meshes > length)
683 return Q_ERR_BAD_EXTENT;
686 model->type = MOD_ALIAS;
687 model->numframes = header.num_frames;
688 model->nummeshes = header.num_meshes;
689 model->meshes = MOD_Malloc(
sizeof(
maliasmesh_t) * header.num_meshes);
690 model->frames = MOD_Malloc(
sizeof(
maliasframe_t) * header.num_frames);
693 src_frame = (dmd3frame_t *)((
byte *)rawdata + header.ofs_frames);
694 dst_frame = model->frames;
695 for (i = 0; i < header.num_frames; i++) {
696 LittleVector(src_frame->translate, dst_frame->
translate);
697 VectorSet(dst_frame->
scale, MD3_XYZ_SCALE, MD3_XYZ_SCALE, MD3_XYZ_SCALE);
699 LittleVector(src_frame->mins, dst_frame->
bounds[0]);
700 LittleVector(src_frame->maxs, dst_frame->
bounds[1]);
701 dst_frame->
radius = LittleFloat(src_frame->radius);
703 src_frame++; dst_frame++;
707 src_mesh = (
const byte *)rawdata + header.ofs_meshes;
708 remaining = length - header.ofs_meshes;
709 for (i = 0; i < header.num_meshes; i++) {
710 ret = MOD_LoadMD3Mesh(model, &model->meshes[i], src_mesh, remaining, &offset);
723 return Q_ERR_SUCCESS;
733 int mesh_idx, skin_idx, frame_idx;
736 switch (model->type) {
738 for (mesh_idx = 0; mesh_idx < model->nummeshes; mesh_idx++) {
740 for (skin_idx = 0; skin_idx < mesh->
numskins; skin_idx++) {
746 for (frame_idx = 0; frame_idx < model->numframes; frame_idx++) {
753 Com_Error(ERR_FATAL,
"%s: bad model type", __func__);