+ subtask = task.task("Reordering faces", 0.5)
+ self.reorder_faces(subtask)
+
+ subtask = task.task("Building sequence", 1.0)
+ if self.use_strips:
+ self.build_tristrip_sequence(subtask)
+ else:
+ self.build_triangle_sequence(subtask)
+
+ if self.use_lines:
+ self.build_line_sequence()
+
+ self.reorder_vertices()
+
+ def build_tristrip_sequence(self, task):
+ sequence = None
+ for i, f in enumerate(self.faces):
+ if sequence:
+ if len(sequence)==3:
+ # Rotate the first three vertices so that the new face can be added
+ if sequence[0] in f.vertices and sequence[1] not in f.vertices:
+ sequence.append(sequence[0])
+ del sequence[0]
+ elif sequence[2] not in f.vertices and sequence[1] in f.vertices:
+ sequence.insert(0, sequence[-1])
+ del sequence[-1]
+
+ if sequence[-1] not in f.vertices:
+ sequence = None
+ else:
+ to_add = [v for v in f.vertices if v!=sequence[-1] and v!=sequence[-2]]
+ if len(to_add)==2:
+ if (f.vertices[1]==sequence[-1]) != (len(sequence)%2==1):
+ to_add.reverse()
+ sequence.append(sequence[-1])
+ sequence += to_add
+
+ if not sequence:
+ self.batches.append(Batch("TRIANGLE_STRIP"))
+ sequence = self.batches[-1].vertices
+ sequence += f.vertices
+
+ task.set_progress(i/len(self.faces))
+
+ def build_triangle_sequence(self, task):
+ batch = Batch("TRIANGLES")
+ for f in self.faces:
+ batch.vertices += f.vertices
+ self.batches.append(batch)
+
+ def build_line_sequence(self):
+ batch = Batch("LINES")
+ for l in self.lines:
+ batch.vertices += l.vertices
+ self.batches.append(batch)
+
+ def build_patch_sequence(self, task):
+ current_size = 0
+ sequence = None
+ for f in self.faces:
+ if len(f.vertices)!=current_size:
+ current_size = len(f.vertices)
+ self.batches.append(Batch("PATCHES"))
+ self.batches[-1].patch_size = current_size
+ sequence = self.batches[-1].vertices
+
+ sequence += f.vertices
+
+ def reorder_faces(self, task):
+ # Tom Forsyth's vertex cache optimization algorithm
+ # http://eelpi.gotdns.org/papers/fast_vert_cache_opt.html