The last index of the last triangle strip in a segment is properly included + cleaner reading of STRP

This commit is contained in:
William Herald Snyder 2022-01-27 18:34:53 -05:00
parent a962b4475e
commit 40343a2f69
1 changed files with 18 additions and 26 deletions

View File

@ -323,6 +323,7 @@ def _read_segm(segm: Reader, materials_list: List[Material]) -> GeometrySegment:
for _ in range(num_texcoords): for _ in range(num_texcoords):
geometry_seg.texcoords.append(Vector(uv0l.read_f32(2))) geometry_seg.texcoords.append(Vector(uv0l.read_f32(2)))
# TODO: Can't remember exact issue here, but this chunk sometimes fails # TODO: Can't remember exact issue here, but this chunk sometimes fails
elif next_header == "NDXL": elif next_header == "NDXL":
@ -347,46 +348,37 @@ def _read_segm(segm: Reader, materials_list: List[Material]) -> GeometrySegment:
for _ in range(num_tris): for _ in range(num_tris):
geometry_seg.triangles.append(ndxt.read_u16(3)) geometry_seg.triangles.append(ndxt.read_u16(3))
# There could be major issues with this, so far it hasn't failed but its inelegance irks me # Try catch for safety's sake
elif next_header == "STRP": elif next_header == "STRP":
strips : List[List[int]] = [] strips : List[List[int]] = []
with segm.read_child() as strp: with segm.read_child() as strp:
num_indicies = strp.read_u32()
num_indicies_read = 0 try:
num_indicies = strp.read_u32()
curr_strip = [] indices = strp.read_u16(num_indicies)
previous_flag = False
if num_indicies > 0: strip_indices = []
index, index1 = strp.read_u16(2)
curr_strip = [index & 0x7fff, index1 & 0x7fff]
num_indicies_read += 2
for i in range(num_indicies - 2): for i in range(num_indicies - 1):
index = strp.read_u16(1) if indices[i] & 0x8000 > 0 and indices[i+1] & 0x8000 > 0:
strip_indices.append(i)
if index & 0x8000 > 0: strip_indices.append(num_indicies)
index = index & 0x7fff
if previous_flag: for i in range(len(strip_indices) - 1):
previous_flag = False start = strip_indices[i]
curr_strip.append(index) end = strip_indices[i+1]
strips.append(curr_strip[:-2])
curr_strip = curr_strip[-2:]
continue
else:
previous_flag = True
else: strips.append(list([indices[start] & 0x7fff, indices[start+1] & 0x7fff]) + list(indices[start+2 : end]))
previous_flag = False except:
print("Failed to read triangle strips")
curr_strip.append(index) geometry_seg.triangle_strips = []
geometry_seg.triangle_strips = strips geometry_seg.triangle_strips = strips
# TODO: Dont know how to handle trailing 0 bug yet: https://schlechtwetterfront.github.io/ze_filetypes/msh.html#STRP # TODO: Dont know if/how to handle trailing 0 bug yet: https://schlechtwetterfront.github.io/ze_filetypes/msh.html#STRP
#if segm.read_u16 != 0: #if segm.read_u16 != 0:
# segm.skip_bytes(-2) # segm.skip_bytes(-2)