Skip to content

Commit 523e5f2

Browse files
committed
MD5/MD3 code factorization, add .lmp for MD3
1 parent e9dbb1f commit 523e5f2

2 files changed

Lines changed: 116 additions & 91 deletions

File tree

Quake/gl_model.c

Lines changed: 115 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4519,6 +4519,15 @@ static void MD5Anim_Load (md5animctx_t *ctx, jointinfo_t *joints, size_t numjoin
45194519
Mem_Free (ctx->animfile);
45204520
}
45214521

4522+
#define TRY_LOAD_FULLBRIGHTS(tex_name) \
4523+
do \
4524+
{ \
4525+
if (!surf->fbtextures[surf->numskins][f]) \
4526+
{ \
4527+
surf->fbtextures[surf->numskins][f] = Mod_LoadFullbrightTexture (mod, tex_name, surf->skinwidth, surf->skinheight); \
4528+
} \
4529+
} while (0);
4530+
45224531
/*
45234532
=====================
45244533
Mod_LoadMD5MeshModel
@@ -4561,6 +4570,7 @@ static void Mod_LoadMD5MeshModel (qmodel_t *mod, const void *buffer)
45614570
hdrsize += sizeof (outhdr->frames) * anim.numposes;
45624571

45634572
// alloc all aliashdr_t and their chained nextsurface, a.k.a nummeshes, in one array
4573+
// all aliashdr_t are zero-initialized by Mem_Alloc
45644574
outhdr = (aliashdr_t *)Mem_Alloc (hdrsize * nummeshes);
45654575

45664576
TEMP_ALLOC_ZEROED (jointinfo_t, joint_infos, numjoints);
@@ -4695,7 +4705,8 @@ static void Mod_LoadMD5MeshModel (qmodel_t *mod, const void *buffer)
46954705
TexMgr_LoadImage (mod, texname, fwidth, fheight, fmt, data, texname, 0, TEXPREF_ALPHA | TEXPREF_NOBRIGHT | TEXPREF_MIPMAP);
46964706

46974707
// no fullbrights by default.
4698-
surf->fbtextures[surf->numskins][f] = NULL;
4708+
assert (surf->fbtextures[surf->numskins][f] == NULL);
4709+
46994710
// initialize skinsizes, known at this point.
47004711
surf->skinwidth = surf->gltextures[0][0] ? surf->gltextures[0][0]->width : 1;
47014712
surf->skinheight = surf->gltextures[0][0] ? surf->gltextures[0][0]->height : 1;
@@ -4726,45 +4737,21 @@ static void Mod_LoadMD5MeshModel (qmodel_t *mod, const void *buffer)
47264737
// we found a 32bit base texture, try to fetch the fullbrights counterparts
47274738
// Same as skins, try first the same location as the model, then 'progs/', then 'textures/' if not found.
47284739
// glow
4729-
if (!surf->fbtextures[surf->numskins][f])
4730-
{
4731-
surf->fbtextures[surf->numskins][f] =
4732-
Mod_LoadFullbrightTexture (mod, va ("%s_%02u_%02u_glow", com_token, surf->numskins, f), surf->skinwidth, surf->skinheight);
4733-
}
4734-
if (!surf->fbtextures[surf->numskins][f])
4735-
{
4736-
surf->fbtextures[surf->numskins][f] = Mod_LoadFullbrightTexture (
4737-
mod, va ("progs/%s_%02u_%02u_glow", com_token, surf->numskins, f), surf->skinwidth, surf->skinheight);
4738-
}
4739-
if (!surf->fbtextures[surf->numskins][f])
4740-
{
4741-
surf->fbtextures[surf->numskins][f] = Mod_LoadFullbrightTexture (
4742-
mod, va ("textures/%s_%02u_%02u_glow", com_token, surf->numskins, f), surf->skinwidth, surf->skinheight);
4743-
}
4744-
4745-
// luma
4746-
if (!surf->fbtextures[surf->numskins][f])
4747-
{
4748-
surf->fbtextures[surf->numskins][f] =
4749-
Mod_LoadFullbrightTexture (mod, va ("%s_%02u_%02u_luma", com_token, surf->numskins, f), surf->skinwidth, surf->skinheight);
4750-
}
4751-
if (!surf->fbtextures[surf->numskins][f])
4752-
{
4753-
surf->fbtextures[surf->numskins][f] = Mod_LoadFullbrightTexture (
4754-
mod, va ("progs/%s_%02u_%02u_luma", com_token, surf->numskins, f), surf->skinwidth, surf->skinheight);
4755-
}
4756-
if (!surf->fbtextures[surf->numskins][f])
4757-
{
4758-
surf->fbtextures[surf->numskins][f] = Mod_LoadFullbrightTexture (
4759-
mod, va ("textures/%s_%02u_%02u_luma", com_token, surf->numskins, f), surf->skinwidth, surf->skinheight);
4760-
}
4740+
assert (surf->fbtextures[surf->numskins][f] == NULL);
4741+
4742+
TRY_LOAD_FULLBRIGHTS (va ("%s_%02u_%02u_glow", com_token, surf->numskins, f));
4743+
TRY_LOAD_FULLBRIGHTS (va ("progs/%s_%02u_%02u_glow", com_token, surf->numskins, f));
4744+
TRY_LOAD_FULLBRIGHTS (va ("textures/%s_%02u_%02u_glow", com_token, surf->numskins, f));
4745+
TRY_LOAD_FULLBRIGHTS (va ("%s_%02u_%02u_luma", com_token, surf->numskins, f));
4746+
TRY_LOAD_FULLBRIGHTS (va ("progs/%s_%02u_%02u_luma", com_token, surf->numskins, f));
4747+
TRY_LOAD_FULLBRIGHTS (va ("textures/%s_%02u_%02u_luma", com_token, surf->numskins, f));
47614748
}
47624749

47634750
Mem_Free (data);
47644751
}
47654752
else
47664753
break;
4767-
}
4754+
} // for each framegroup
47684755
if (f == 0)
47694756
break; // no images loaded...
47704757

@@ -4784,7 +4771,8 @@ static void Mod_LoadMD5MeshModel (qmodel_t *mod, const void *buffer)
47844771
surf->fbtextures[surf->numskins][3] = surf->fbtextures[surf->numskins][1];
47854772
surf->fbtextures[surf->numskins][2] = surf->fbtextures[surf->numskins][0];
47864773
}
4787-
}
4774+
} // foreach skins
4775+
47884776
// MD5 have only 1 surface pose, meaning 1 vertex-like "pose" (not to ne mixed with md5animctx_t anim poses !)
47894777
// because it uses skeletal animation instead of displaying/interpolating different frames/poses of vertices
47904778
surf->numposes = 1;
@@ -4894,10 +4882,10 @@ static void Mod_LoadMD5MeshModel (qmodel_t *mod, const void *buffer)
48944882
}
48954883
/*
48964884
=====================
4897-
Mod_LoadMD3SurfaceTexture
4885+
Mod_LoadMD3ShaderTexture
48984886
=====================
48994887
*/
4900-
static bool Mod_LoadMD3SurfaceTexture (qmodel_t *mod, aliashdr_t *surf, const char *shader_texture_name)
4888+
static bool Mod_LoadMD3ShaderTexture (qmodel_t *mod, aliashdr_t *surf, const char *shader_texture_name, int surface_index, size_t numsurfs)
49014889
{
49024890
char texturename[MAX_QPATH] = {0};
49034891

@@ -4916,66 +4904,103 @@ static bool Mod_LoadMD3SurfaceTexture (qmodel_t *mod, aliashdr_t *surf, const ch
49164904
}
49174905
else
49184906
{
4919-
// 2. Second attempt: use the mod name, build a skin name using a skin index:
4920-
q_snprintf (full_texname, sizeof (full_texname), "%s_%02u", mod->name, surf->numskins);
4907+
// 2. Second attempt: use the mod name: build a skin name using a surface index and skin index:
4908+
q_snprintf (full_texname, sizeof (full_texname), "%s_%d_%d", mod->name, surface_index, surf->numskins);
49214909
}
4922-
4923-
unsigned int fwidth, fheight;
4924-
void *data = NULL;
4925-
4926-
// 1.1 Search directly shader_name, then progs/shader_name if not found
4927-
enum srcformat fmt = SRC_RGBA;
4928-
4929-
// for Skins: try first the same location as the model, then 'progs/' if not found.
4930-
data = Image_LoadImage (full_texname, (int *)&fwidth, (int *)&fheight, &fmt, mod->path_id);
4931-
4932-
if (!data)
4933-
{
4934-
data = Image_LoadImage (va ("progs/%s", full_texname), (int *)&fwidth, (int *)&fheight, &fmt, mod->path_id);
4935-
}
4936-
4937-
if (data)
4910+
// for each framgroup:
4911+
int f = 0;
4912+
for (f = 0; f < countof (surf->gltextures[0]); f++)
49384913
{
4939-
surf->gltextures[surf->numskins][0] =
4940-
TexMgr_LoadImage (mod, full_texname, fwidth, fheight, fmt, data, full_texname, 0, TEXPREF_ALPHA | TEXPREF_NOBRIGHT | TEXPREF_MIPMAP);
4914+
unsigned int fwidth, fheight;
4915+
void *data = NULL;
49414916

4942-
// no fullbrights by default.
4943-
surf->fbtextures[surf->numskins][0] = NULL;
4944-
// initialize skinsizes, known at this point.
4945-
surf->skinwidth = surf->gltextures[0][0] ? surf->gltextures[0][0]->width : 1;
4946-
surf->skinheight = surf->gltextures[0][0] ? surf->gltextures[0][0]->height : 1;
4917+
// Search directly shader_name, then progs/shader_name if not found
4918+
enum srcformat fmt = SRC_RGBA;
49474919

4948-
surf->gltextures[surf->numskins][3] = surf->gltextures[surf->numskins][2] = surf->gltextures[surf->numskins][1] =
4949-
surf->gltextures[surf->numskins][0];
4920+
// for Skins: try first the same location as the model, then 'progs/', then 'textures/' if not found.
4921+
data = Image_LoadImage (full_texname, (int *)&fwidth, (int *)&fheight, &fmt, mod->path_id);
49504922

4951-
// 1.2. Load additional fullbrights, if any.
4952-
// Try first the same location as the model, then 'progs/' if not found.
4953-
if (!surf->fbtextures[surf->numskins][0])
4954-
{
4955-
surf->fbtextures[surf->numskins][0] = Mod_LoadFullbrightTexture (mod, va ("%s_glow", full_texname), surf->skinwidth, surf->skinheight);
4956-
}
4957-
if (!surf->fbtextures[surf->numskins][0])
4923+
if (!data)
49584924
{
4959-
surf->fbtextures[surf->numskins][0] = Mod_LoadFullbrightTexture (mod, va ("progs/%s_glow", full_texname), surf->skinwidth, surf->skinheight);
4925+
data = Image_LoadImage (va ("progs/%s", full_texname), (int *)&fwidth, (int *)&fheight, &fmt, mod->path_id);
49604926
}
4961-
if (!surf->fbtextures[surf->numskins][0])
4927+
if (!data)
49624928
{
4963-
surf->fbtextures[surf->numskins][0] = Mod_LoadFullbrightTexture (mod, va ("%s_luma", full_texname), surf->skinwidth, surf->skinheight);
4929+
data = Image_LoadImage (va ("textures/%s", full_texname), (int *)&fwidth, (int *)&fheight, &fmt, mod->path_id);
49644930
}
4965-
if (!surf->fbtextures[surf->numskins][0])
4931+
4932+
if (data)
49664933
{
4967-
surf->fbtextures[surf->numskins][0] = Mod_LoadFullbrightTexture (mod, va ("progs/%s_luma", full_texname), surf->skinwidth, surf->skinheight);
4968-
}
4934+
surf->gltextures[surf->numskins][0] =
4935+
TexMgr_LoadImage (mod, full_texname, fwidth, fheight, fmt, data, full_texname, 0, TEXPREF_ALPHA | TEXPREF_NOBRIGHT | TEXPREF_MIPMAP);
4936+
4937+
// no fullbrights by default.
4938+
assert (surf->fbtextures[surf->numskins][0] == NULL);
4939+
// initialize skinsizes, known at this point.
4940+
surf->skinwidth = surf->gltextures[0][0] ? surf->gltextures[0][0]->width : 1;
4941+
surf->skinheight = surf->gltextures[0][0] ? surf->gltextures[0][0]->height : 1;
4942+
4943+
if (fmt == SRC_INDEXED)
4944+
{
4945+
if (f == 0)
4946+
{
4947+
size_t size = fwidth * fheight;
4948+
byte *texels = (byte *)Mem_Alloc (size);
4949+
surf->texels[surf->numskins] = texels;
4950+
memcpy (texels, data, size);
4951+
}
4952+
// 8bit base texture. use it for fullbrights.
4953+
for (size_t j = 0; j < fwidth * fheight; j++)
4954+
{
4955+
if (((byte *)data)[j] > 223)
4956+
{
4957+
surf->fbtextures[surf->numskins][f] = TexMgr_LoadImage (
4958+
mod, va ("%s_luma", full_texname), fwidth, fheight, SRC_INDEXED, data, va ("%s_luma", full_texname), 0,
4959+
TEXPREF_ALPHA | TEXPREF_MIPMAP | TEXPREF_FULLBRIGHT);
4960+
break;
4961+
}
4962+
}
4963+
}
4964+
else
4965+
{
4966+
// we found a 32bit base texture, try to fetch the fullbrights counterparts:
4967+
// Same as skins, try first the same location as the model, then 'progs/', then 'textures/' if not found.
4968+
TRY_LOAD_FULLBRIGHTS (va ("%s_glow", full_texname));
4969+
TRY_LOAD_FULLBRIGHTS (va ("progs/%s_glow", full_texname));
4970+
TRY_LOAD_FULLBRIGHTS (va ("textures/%s_glow", full_texname));
4971+
TRY_LOAD_FULLBRIGHTS (va ("%s_luma", full_texname));
4972+
TRY_LOAD_FULLBRIGHTS (va ("progs/%s_luma", full_texname));
4973+
TRY_LOAD_FULLBRIGHTS (va ("textures/%s_luma", full_texname));
4974+
}
4975+
Mem_Free (data);
49694976

4970-
surf->fbtextures[surf->numskins][3] = surf->fbtextures[surf->numskins][2] = surf->fbtextures[surf->numskins][1] =
4971-
surf->fbtextures[surf->numskins][0];
4977+
is_texture_found = true;
4978+
} // end if data
4979+
else
4980+
break;
4981+
} // for each f framegroup
49724982

4973-
is_texture_found = true;
4974-
surf->numskins++;
4983+
if (f == 0)
4984+
continue; // no images loaded, make another try
4985+
// this stuff is hideous.
4986+
if (f < 2)
4987+
{
4988+
surf->gltextures[surf->numskins][1] = surf->gltextures[surf->numskins][0];
4989+
surf->fbtextures[surf->numskins][1] = surf->fbtextures[surf->numskins][0];
4990+
}
4991+
if (f == 3)
4992+
{
4993+
Con_Warning ("%s_%02u_##: 3 skinframes found...\n", com_token, surf->numskins);
49754994
}
4995+
if (f < 4)
4996+
{
4997+
surf->gltextures[surf->numskins][3] = surf->gltextures[surf->numskins][1];
4998+
surf->gltextures[surf->numskins][2] = surf->gltextures[surf->numskins][0];
49764999

4977-
Mem_Free (data);
4978-
}
5000+
surf->fbtextures[surf->numskins][3] = surf->fbtextures[surf->numskins][1];
5001+
surf->fbtextures[surf->numskins][2] = surf->fbtextures[surf->numskins][0];
5002+
}
5003+
} // foreach try
49795004

49805005
return is_texture_found;
49815006
}
@@ -5096,21 +5121,21 @@ static void Mod_LoadMD3Model (qmodel_t *mod, const void *buffer)
50965121
surf->scale[j] = MD3_XYZ_SCALE;
50975122
}
50985123

5099-
// guess at skin sizes
5100-
surf->skinwidth = 320;
5101-
surf->skinheight = 200;
5102-
51035124
// load the textures, listed as "shader" names.
5104-
// if not found
51055125
int numshaders = pinsurface->numShaders;
51065126
pinshader = (md3Shader_t *)((byte *)pinsurface + LittleLong (pinsurface->ofsShaders));
51075127

5108-
// reset for Mod_LoadMD3SurfaceTexture
5128+
// reset for Mod_LoadMD3ShaderTexture
51095129
surf->numskins = 0;
51105130

5131+
// TODO: Load .skin file first...
51115132
for (int skin_index = 0; skin_index < q_min (numshaders, MAX_SKINS); skin_index++, pinshader++)
51125133
{
5113-
if (false == Mod_LoadMD3SurfaceTexture (mod, surf, pinshader->name))
5134+
if (Mod_LoadMD3ShaderTexture(mod, surf, pinshader->name, m, numsurfs))
5135+
{
5136+
surf->numskins++;
5137+
}
5138+
else
51145139
break;
51155140
}
51165141

Quake/mem.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void Mem_Init ()
8585

8686
/*
8787
====================
88-
Mem_Alloc
88+
Mem_Alloc : initialize memory to zero by default.
8989
====================
9090
*/
9191
void *Mem_Alloc (const size_t size)

0 commit comments

Comments
 (0)