Fix M2 white shell artifact from missing textures, add opacity track support

Batches whose named texture fails to load now render invisible instead of
white (the swampreeds01a.blp case causing a white shell around aquatic plants).

Also implements proper M2 opacity plumbing:
- Parse texture weight tracks (M2Track<fixed16>) and color animation alpha
  tracks (M2Color.alpha) to resolve per-batch opacity at load time
- Skip batches with batchOpacity < 0.01 in the render loop
- Apply M2Texture.flags (bit0=WrapS, bit1=WrapT) to GL sampler wrap mode
- Upload both UV sets (texCoords[0] and texCoords[1]) and select via
  textureUnit uniform, so batches referencing UV set 1 render correctly
This commit is contained in:
Kelsi 2026-02-17 23:52:44 -08:00
parent 4ba10e772b
commit 9a950ce09f
4 changed files with 141 additions and 15 deletions

View file

@ -965,6 +965,37 @@ M2Model M2Loader::load(const std::vector<uint8_t>& m2Data) {
model.textureLookup = readArray<uint16_t>(m2Data, header.ofsTexLookup, header.nTexLookup);
}
// Parse color animation alpha values (M2Color: vec3 color track + fixed16 alpha track).
// Each M2Color is two M2TrackDisk headers (20+20 = 40 bytes).
// We only need the alpha track (at offset 20) — controls per-batch opacity.
if (header.nColors > 0 && header.ofsColors > 0 && header.nColors < 4096) {
static constexpr uint32_t M2COLOR_SIZE = 40; // 20-byte color track + 20-byte alpha track
model.colorAlphas.reserve(header.nColors);
for (uint32_t ci = 0; ci < header.nColors; ci++) {
uint32_t alphaTrackOfs = header.ofsColors + ci * M2COLOR_SIZE + 20; // skip vec3 track
if (alphaTrackOfs + sizeof(M2TrackDisk) > m2Data.size()) {
model.colorAlphas.push_back(1.0f);
continue;
}
M2TrackDisk td = readValue<M2TrackDisk>(m2Data, alphaTrackOfs);
float alpha = 1.0f;
if (td.nKeys > 0 && td.ofsKeys > 0 && td.nKeys < 4096) {
for (uint32_t si = 0; si < td.nKeys; si++) {
uint32_t hdOfs = td.ofsKeys + si * 8;
if (hdOfs + 8 > m2Data.size()) break;
uint32_t count = readValue<uint32_t>(m2Data, hdOfs);
uint32_t offset = readValue<uint32_t>(m2Data, hdOfs + 4);
if (count == 0 || offset == 0) continue;
if (offset + sizeof(uint16_t) > m2Data.size()) continue;
uint16_t rawVal = readValue<uint16_t>(m2Data, offset);
alpha = std::min(1.0f, rawVal / 32767.0f);
break;
}
}
model.colorAlphas.push_back(alpha);
}
}
// Read bone lookup table (vertex bone indices reference this to get actual bone index)
if (header.nBoneLookupTable > 0 && header.ofsBoneLookupTable > 0) {
model.boneLookupTable = readArray<uint16_t>(m2Data, header.ofsBoneLookupTable, header.nBoneLookupTable);
@ -1021,10 +1052,43 @@ M2Model M2Loader::load(const std::vector<uint8_t>& m2Data) {
}
// Read texture transform lookup (nTransLookup)
// Note: ofsTransLookup holds the transparency track lookup table (indexed by batch.transparencyIndex).
if (header.nTransLookup > 0 && header.ofsTransLookup > 0) {
model.textureTransformLookup = readArray<uint16_t>(m2Data, header.ofsTransLookup, header.nTransLookup);
}
// Parse transparency tracks (M2Track<fixed16>) — controls per-batch opacity.
// fixed16 = uint16_t / 32767.0f, range 0 (transparent) to 1 (opaque).
// We extract the "at-rest" value from the first available keyframe.
if (header.nTransparency > 0 && header.ofsTransparency > 0 &&
header.nTransparency < 4096) {
model.textureWeights.reserve(header.nTransparency);
for (uint32_t ti = 0; ti < header.nTransparency; ti++) {
uint32_t trackOfs = header.ofsTransparency + ti * sizeof(M2TrackDisk);
if (trackOfs + sizeof(M2TrackDisk) > m2Data.size()) {
model.textureWeights.push_back(1.0f);
continue;
}
M2TrackDisk td = readValue<M2TrackDisk>(m2Data, trackOfs);
float opacity = 1.0f;
// Scan sub-arrays until we find one with keyframe data
if (td.nKeys > 0 && td.ofsKeys > 0 && td.nKeys < 4096) {
for (uint32_t si = 0; si < td.nKeys; si++) {
uint32_t hdOfs = td.ofsKeys + si * 8;
if (hdOfs + 8 > m2Data.size()) break;
uint32_t count = readValue<uint32_t>(m2Data, hdOfs);
uint32_t offset = readValue<uint32_t>(m2Data, hdOfs + 4);
if (count == 0 || offset == 0) continue;
if (offset + sizeof(uint16_t) > m2Data.size()) continue;
uint16_t rawVal = readValue<uint16_t>(m2Data, offset);
opacity = std::min(1.0f, rawVal / 32767.0f);
break;
}
}
model.textureWeights.push_back(opacity);
}
}
// Read attachment points (vanilla uses 48-byte struct, WotLK uses 40-byte)
if (header.nAttachments > 0 && header.ofsAttachments > 0) {
model.attachments.reserve(header.nAttachments);