Spaces:
Running
Running
File size: 1,202 Bytes
2d48693 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import numpy
from PIL import Image
from .core import Vec4d
class Model:
def __init__(self, filename, texture_filename):
"""
https://en.wikipedia.org/wiki/Wavefront_.obj_file#Vertex_normal_indices
"""
self.vertices = []
self.uv_vertices = []
self.uv_indices = []
self.indices = []
texture = Image.open(texture_filename)
self.texture_array = numpy.array(texture)
self.texture_width, self.texture_height = texture.size
with open(filename) as f:
for line in f:
if line.startswith("v "):
x, y, z = [float(d) for d in line.strip("v").strip().split(" ")]
self.vertices.append(Vec4d(x, y, z, 1))
elif line.startswith("vt "):
u, v = [float(d) for d in line.strip("vt").strip().split(" ")]
self.uv_vertices.append([u, v])
elif line.startswith("f "):
facet = [d.split("/") for d in line.strip("f").strip().split(" ")]
self.indices.append([int(d[0]) for d in facet])
self.uv_indices.append([int(d[1]) for d in facet])
|