Skip to content

Commit

Permalink
Merge d6b1317 into 845daf5
Browse files Browse the repository at this point in the history
  • Loading branch information
HENDRIX-ZT2 committed Dec 13, 2019
2 parents 845daf5 + d6b1317 commit fd66488
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 51 deletions.
43 changes: 13 additions & 30 deletions pyffi/formats/cgf/__init__.py
Expand Up @@ -1872,9 +1872,9 @@ def get_uv_triangles(self):
yield uvface.t_0, uvface.t_1, uvface.t_2
elif self.indices_data:
# Crysis: UV triangles coincide with triangles
it = iter(self.indices_data.indices)
while True:
yield next(it), next(it), next(it)
inds = self.indices_data.indices
for i in range(0, len(inds), 3):
yield inds[i], inds[i+1], inds[i+2]

### DEPRECATED: USE set_geometry INSTEAD ###
def set_vertices_normals(self, vertices, normals):
Expand Down Expand Up @@ -2450,20 +2450,15 @@ def set_geometry(self,
# Far Cry data preparation
self.num_vertices = numvertices
self.vertices.update_size()
selfvertices_iter = iter(self.vertices)
self.num_faces = numtriangles
self.faces.update_size()
selffaces_iter = iter(self.faces)
if not uvslist is None:
self.num_uvs = numvertices
self.uvs.update_size()
self.uv_faces.update_size()
selfuvs_iter = iter(self.uvs)
selfuv_faces_iter = iter(self.uv_faces)
if not colorslist is None:
self.has_vertex_colors = True
self.vertex_colors.update_size()
selfvertex_colors_iter = iter(self.vertex_colors)

# Crysis data preparation
self.num_indices = numtriangles * 3
Expand All @@ -2473,14 +2468,12 @@ def set_geometry(self,
self.vertices_data.bytes_per_element = 12
self.vertices_data.num_elements = numvertices
self.vertices_data.vertices.update_size()
selfvertices_data_iter = iter(self.vertices_data.vertices)

self.normals_data = CgfFormat.DataStreamChunk()
self.normals_data.data_stream_type = CgfFormat.DataStreamType.NORMALS
self.normals_data.bytes_per_element = 12
self.normals_data.num_elements = numvertices
self.normals_data.normals.update_size()
selfnormals_data_iter = iter(self.normals_data.normals)

self.indices_data = CgfFormat.DataStreamChunk()
self.indices_data.data_stream_type = CgfFormat.DataStreamType.INDICES
Expand All @@ -2495,7 +2488,6 @@ def set_geometry(self,
self.uvs_data.bytes_per_element = 8
self.uvs_data.num_elements = numvertices
self.uvs_data.uvs.update_size()
selfuvs_data_iter = iter(self.uvs_data.uvs)
# have tangent space
has_tangentspace = True
else:
Expand All @@ -2509,7 +2501,6 @@ def set_geometry(self,
self.colors_data.bytes_per_element = 4
self.colors_data.num_elements = numvertices
self.colors_data.rgba_colors.update_size()
selfcolors_data_iter = iter(self.colors_data.rgba_colors)

self.num_mesh_subsets = len(verticeslist)
self.mesh_subsets = CgfFormat.MeshSubsetsChunk()
Expand Down Expand Up @@ -2546,8 +2537,7 @@ def set_geometry(self,
meshsubset.center.z = center[2]

# set vertex coordinates and normals for Far Cry
for vert, norm in zip(vertices, normals):
cryvert = next(selfvertices_iter)
for cryvert, vert, norm in zip(self.vertices, vertices, normals):
cryvert.p.x = vert[0]
cryvert.p.y = vert[1]
cryvert.p.z = vert[2]
Expand All @@ -2556,9 +2546,9 @@ def set_geometry(self,
cryvert.n.z = norm[2]

# set vertex coordinates and normals for Crysis
for vert, norm in zip(vertices, normals):
cryvert = next(selfvertices_data_iter)
crynorm = next(selfnormals_data_iter)
for cryvert, crynorm, vert, norm in zip(
self.vertices_data.vertices, self.normals_data.normals, vertices, normals):

cryvert.x = vert[0]
cryvert.y = vert[1]
cryvert.z = vert[2]
Expand All @@ -2567,8 +2557,7 @@ def set_geometry(self,
crynorm.z = norm[2]

# set Far Cry face info
for triangle in triangles:
cryface = next(selffaces_iter)
for cryface, triangle in zip(self.faces, triangles):
cryface.v_0 = triangle[0] + firstvertexindex
cryface.v_1 = triangle[1] + firstvertexindex
cryface.v_2 = triangle[2] + firstvertexindex
Expand All @@ -2581,34 +2570,29 @@ def set_geometry(self,

if not uvs is None:
# set Far Cry uv info
for triangle in triangles:
cryuvface = next(selfuv_faces_iter)
for cryuvface, triangle in zip(self.uv_faces, triangles):
cryuvface.t_0 = triangle[0] + firstvertexindex
cryuvface.t_1 = triangle[1] + firstvertexindex
cryuvface.t_2 = triangle[2] + firstvertexindex
for uv in uvs:
cryuv = next(selfuvs_iter)
for cryuv, uv in zip(self.uvs, uvs):
cryuv.u = uv[0]
cryuv.v = uv[1]

# set Crysis uv info
for uv in uvs:
cryuv = next(selfuvs_data_iter)
for cryuv, uv in zip(self.uvs_data.uvs, uvs):
cryuv.u = uv[0]
cryuv.v = 1.0 - uv[1] # OpenGL fix

if not colors is None:
# set Far Cry color info
for color in colors:
crycolor = next(selfvertex_colors_iter)
for crycolor, color in zip(self.vertex_colors, colors):
crycolor.r = color[0]
crycolor.g = color[1]
crycolor.b = color[2]
# note: Far Cry does not support alpha color channel

# set Crysis color info
for color in colors:
crycolor = next(selfcolors_data_iter)
for crycolor, color in zip(self.colors_data.rgba_colors, colors):
crycolor.r = color[0]
crycolor.g = color[1]
crycolor.b = color[2]
Expand Down Expand Up @@ -2640,7 +2624,6 @@ def update_tangent_space(self):
self.tangents_data.bytes_per_element = 16
self.tangents_data.num_elements = self.num_vertices
self.tangents_data.tangents.update_size()
selftangents_data_iter = iter(self.tangents_data.tangents)

# set Crysis tangents info
tangents, binormals, orientations = pyffi.utils.tangentspace.getTangentSpace(
Expand Down
17 changes: 10 additions & 7 deletions pyffi/formats/nif/__init__.py
Expand Up @@ -3344,12 +3344,18 @@ def get_node_name(self):
>>> link.get_node_name()
b'Bip01'
"""
if self.node_name:
# eg. ZT2
if self.target_name:
return self.target_name
# eg. Fallout
elif self.node_name:
return self.node_name
# eg. Loki (StringPalette)
else:
return self._get_string(self.node_name_offset)

def set_node_name(self, text):
self.target_name = text
self.node_name = text
self.node_name_offset = self._add_string(text)

Expand Down Expand Up @@ -7005,12 +7011,9 @@ def set_triangles(self, triangles, stitchstrips = False):
self.has_triangles = (n > 0)
self.triangles.update_size()

# copy triangles
src = triangles.__iter__()
dst = self.triangles.__iter__()
for k in range(n):
dst_t = next(dst)
dst_t.v_1, dst_t.v_2, dst_t.v_3 = next(src)
# set triangles to triangles array
for dst_t, src_t in zip(self.triangles, triangles):
dst_t.v_1, dst_t.v_2, dst_t.v_3 = src_t

def get_strips(self):
return pyffi.utils.vertex_cache.stripify(self.get_triangles())
Expand Down
2 changes: 2 additions & 0 deletions pyffi/object_models/xml/bit_struct.py
Expand Up @@ -68,6 +68,8 @@ def __init__(cls, name, bases, dct):
cls._struct = 'H'
elif cls._numbytes == 4:
cls._struct = 'I'
elif cls._numbytes == 8:
cls._struct = 'Q'
else:
raise RuntimeError("unsupported bitstruct numbytes")

Expand Down
4 changes: 2 additions & 2 deletions pyffi/utils/quickhull.py
Expand Up @@ -347,8 +347,8 @@ def qhull3d(vertices, precision = 0.0001, verbose = False):
# as long as there are triangles with outer vertices
while outer_vertices:
# grab a triangle and its outer vertices
tmp_iter = iter(outer_vertices.items())
triangle, outer = next(tmp_iter) # tmp_iter trick to make 2to3 work
# trick to make 2to3 work
triangle, outer = list(outer_vertices.items())[0]
# calculate pivot point
pivot = max(outer)[1]
if verbose:
Expand Down
23 changes: 13 additions & 10 deletions pyffi/utils/tristrip.py
Expand Up @@ -58,21 +58,24 @@ def triangulate(strips):

for strip in strips:
if len(strip) < 3: continue # skip empty strips
i = strip.__iter__()
j = False
t1, t2 = next(i), next(i)
for k in range(2, len(strip)):
j = not j
t0, t1, t2 = t1, t2, next(i)
# make list copy incase input data does not like slice notation
strip_list = list(strip)
# flips the order of verts in every other tri
flip = False
for i in range(0, len(strip_list)-2):
flip = not flip
t0, t1, t2 = strip_list[i:i+3]
# skip degenerate tri
if t0 == t1 or t1 == t2 or t2 == t0: continue
triangles.append((t0, t1, t2) if j else (t0, t2, t1))
# append tri in correct order
triangles.append((t0, t1, t2) if flip else (t0, t2, t1))

return triangles

def _generate_faces_from_triangles(triangles):
i = triangles.__iter__()
while True:
yield (next(i), next(i), next(i))
"""Creates faces (tris) from a flat list of non-overlapping triangle indices"""
for i in range(0, len(triangles), 3):
yield triangles[i], triangles[i+1], triangles[i+2]

def _sort_triangle_indices(triangles):
"""Sorts indices of each triangle so lowest index always comes first.
Expand Down
4 changes: 2 additions & 2 deletions tests/perf/fraps_minmaxavg.py
Expand Up @@ -55,8 +55,8 @@
print("parsing {0}".format(name))
with open(os.path.join(root, name), "rb") as csvfile:
rows = csv.reader(csvfile)
header = next(rows)
numbers = next(rows)
header = rows[0]
numbers = rows[1]
for name, num in zip(header, numbers):
name = name.strip()
total[root][name].append(float(num))
Expand Down

0 comments on commit fd66488

Please sign in to comment.