def test_hasData(): # Simple test to see if the has whatever functions do their job correctly empty_mesh = MeshData() assert not empty_mesh.hasNormals() assert not empty_mesh.hasColors() assert not empty_mesh.hasUVCoordinates() assert not empty_mesh.hasIndices() filled_mesh = MeshData(normals=[], colors=[], uvs=[], indices=[]) assert filled_mesh.hasNormals() assert filled_mesh.hasColors() assert filled_mesh.hasUVCoordinates() assert filled_mesh.hasIndices()
def test_hasData(): # Simple test to see if the has whatever functions do their job correctly empty_mesh = MeshData() assert not empty_mesh.hasNormals() assert not empty_mesh.hasColors() assert not empty_mesh.hasUVCoordinates() assert not empty_mesh.hasIndices() filled_mesh = MeshData(normals = [], colors = [], uvs = [], indices=[]) assert filled_mesh.hasNormals() assert filled_mesh.hasColors() assert filled_mesh.hasUVCoordinates() assert filled_mesh.hasIndices()
def _setMeshAttributes(self, mesh: MeshData) -> None: self._shader.enableAttribute("a_vertex", "vector3f", 0) vertex_count = mesh.getVertexCount() offset = vertex_count * 3 * 4 if mesh.hasNormals(): self._shader.enableAttribute("a_normal", "vector3f", offset) offset += vertex_count * 3 * 4 if mesh.hasColors(): self._shader.enableAttribute("a_color", "vector4f", offset) offset += vertex_count * 4 * 4 if mesh.hasUVCoordinates(): self._shader.enableAttribute("a_uvs", "vector2f", offset) offset += vertex_count * 2 * 4 for attribute_name in mesh.attributeNames(): attribute = mesh.getAttribute(attribute_name) self._shader.enableAttribute(attribute["opengl_name"], attribute["opengl_type"], offset) if attribute["opengl_type"] == "vector2f": offset += mesh.getVertexCount() * 2 * 4 elif attribute["opengl_type"] == "vector4f": offset += mesh.getVertexCount() * 4 * 4 elif attribute["opengl_type"] == "int": offset += mesh.getVertexCount() * 4 elif attribute["opengl_type"] == "float": offset += mesh.getVertexCount() * 4 else: Logger.log( "e", "Attribute with name [%s] uses non implemented type [%s]." % (attribute["opengl_name"], attribute["opengl_type"])) self._shader.disableAttribute(attribute["opengl_name"])
def read(self, file_name): mesh = None scene_node = None extension = os.path.splitext(file_name)[1] if extension.lower() in self._supported_extensions: vertex_list = [] normal_list = [] uv_list = [] face_list = [] scene_node = SceneNode() mesh = MeshData() scene_node.setMeshData(mesh) f = open(file_name, "rt") for line in f: parts = line.split() if len(parts) < 1: continue if parts[0] == "v": vertex_list.append([float(parts[1]), float(parts[3]), -float(parts[2])]) if parts[0] == "vn": normal_list.append([float(parts[1]), float(parts[3]), -float(parts[2])]) if parts[0] == "vt": uv_list.append([float(parts[1]), float(parts[2])]) if parts[0] == "f": parts = [i for i in map(lambda p: p.split("/"), parts)] for idx in range(1, len(parts)-2): data = [int(parts[1][0]), int(parts[idx+1][0]), int(parts[idx+2][0])] if len(parts[1]) > 2: data += [int(parts[1][2]), int(parts[idx+1][2]), int(parts[idx+2][2])] if parts[1][1] and parts[idx+1][1] and parts[idx+2][1]: data += [int(parts[1][1]), int(parts[idx+1][1]), int(parts[idx+2][1])] face_list.append(data) Job.yieldThread() f.close() mesh.reserveVertexCount(3 * len(face_list)) num_vertices = len(vertex_list) num_normals = len(normal_list) for face in face_list: # Substract 1 from index, as obj starts counting at 1 instead of 0 i = face[0] - 1 j = face[1] - 1 k = face[2] - 1 if len(face) > 3: ni = face[3] - 1 nj = face[4] - 1 nk = face[5] - 1 else: ni = -1 nj = -1 nk = -1 if len(face) > 6: ui = face[6] - 1 uj = face[7] - 1 uk = face[8] - 1 else: ui = -1 uj = -1 uk = -1 #TODO: improve this handling, this can cause weird errors (negative indexes are relative indexes, and are not properly handled) if i < 0 or i >= num_vertices: i = 0 if j < 0 or j >= num_vertices: j = 0 if k < 0 or k >= num_vertices: k = 0 if ni != -1 and nj != -1 and nk != -1: mesh.addFaceWithNormals(vertex_list[i][0], vertex_list[i][1], vertex_list[i][2], normal_list[ni][0], normal_list[ni][1], normal_list[ni][2], vertex_list[j][0], vertex_list[j][1], vertex_list[j][2], normal_list[nj][0], normal_list[nj][1], normal_list[nj][2], vertex_list[k][0], vertex_list[k][1], vertex_list[k][2],normal_list[nk][0], normal_list[nk][1], normal_list[nk][2]) else: mesh.addFace(vertex_list[i][0], vertex_list[i][1], vertex_list[i][2], vertex_list[j][0], vertex_list[j][1], vertex_list[j][2], vertex_list[k][0], vertex_list[k][1], vertex_list[k][2]) if ui != -1: mesh.setVertexUVCoordinates(mesh.getVertexCount() - 3, uv_list[ui][0], uv_list[ui][1]) if uj != -1: mesh.setVertexUVCoordinates(mesh.getVertexCount() - 2, uv_list[uj][0], uv_list[uj][1]) if uk != -1: mesh.setVertexUVCoordinates(mesh.getVertexCount() - 1, uv_list[uk][0], uv_list[uk][1]) Job.yieldThread() if not mesh.hasNormals(): mesh.calculateNormals(fast = True) return scene_node
def read(self, file_name): mesh = None scene_node = None extension = os.path.splitext(file_name)[1] if extension.lower() == self._supported_extension: vertex_list = [] normal_list = [] uv_list = [] face_list = [] scene_node = SceneNode() mesh = MeshData() scene_node.setMeshData(mesh) f = open(file_name, "rt") for line in f: parts = line.split() if len(parts) < 1: continue if parts[0] == "v": vertex_list.append([float(parts[1]), float(parts[3]), -float(parts[2])]) if parts[0] == "vn": normal_list.append([float(parts[1]), float(parts[3]), -float(parts[2])]) if parts[0] == "vt": uv_list.append([float(parts[1]), float(parts[2])]) if parts[0] == "f": parts = [i for i in map(lambda p: p.split("/"), parts)] for idx in range(1, len(parts)-2): data = [int(parts[1][0]), int(parts[idx+1][0]), int(parts[idx+2][0])] if len(parts[1]) > 2: data += [int(parts[1][2]), int(parts[idx+1][2]), int(parts[idx+2][2])] if parts[1][1] and parts[idx+1][1] and parts[idx+2][1]: data += [int(parts[1][1]), int(parts[idx+1][1]), int(parts[idx+2][1])] face_list.append(data) f.close() mesh.reserveVertexCount(3 * len(face_list)) num_vertices = len(vertex_list) num_normals = len(normal_list) for face in face_list: # Substract 1 from index, as obj starts counting at 1 instead of 0 i = face[0] - 1 j = face[1] - 1 k = face[2] - 1 if len(face) > 3: ni = face[3] - 1 nj = face[4] - 1 nk = face[5] - 1 else: ni = -1 nj = -1 nk = -1 if len(face) > 6: ui = face[6] - 1 uj = face[7] - 1 uk = face[8] - 1 else: ui = -1 uj = -1 uk = -1 #TODO: improve this handling, this can cause weird errors if i < 0 or i >= num_vertices: i = 0 if j < 0 or j >= num_vertices: j = 0 if k < 0 or k >= num_vertices: k = 0 if(ni != -1 and nj != -1 and nk != -1): mesh.addFaceWithNormals(vertex_list[i][0], vertex_list[i][1], vertex_list[i][2], normal_list[ni][0], normal_list[ni][1], normal_list[ni][2], vertex_list[j][0], vertex_list[j][1], vertex_list[j][2], normal_list[nj][0], normal_list[nj][1], normal_list[nj][2], vertex_list[k][0], vertex_list[k][1], vertex_list[k][2],normal_list[nk][0], normal_list[nk][1], normal_list[nk][2]) else: mesh.addFace(vertex_list[i][0], vertex_list[i][1], vertex_list[i][2], vertex_list[j][0], vertex_list[j][1], vertex_list[j][2], vertex_list[k][0], vertex_list[k][1], vertex_list[k][2]) if ui != -1: mesh.setVertexUVCoordinates(mesh.getVertexCount() - 3, uv_list[ui][0], uv_list[ui][1]) if uj != -1: mesh.setVertexUVCoordinates(mesh.getVertexCount() - 2, uv_list[uj][0], uv_list[uj][1]) if uk != -1: mesh.setVertexUVCoordinates(mesh.getVertexCount() - 1, uv_list[uk][0], uv_list[uk][1]) if not mesh.hasNormals(): mesh.calculateNormals(fast = True) return scene_node