2016-11-21 15:59:03 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2016-11-22 15:13:35 +01:00
|
|
|
from math import sqrt
|
|
|
|
|
2016-11-25 14:28:39 +01:00
|
|
|
from ..geometry import Vector
|
2016-11-22 17:17:37 +01:00
|
|
|
|
2016-11-29 15:40:30 +01:00
|
|
|
from .mesh import Material, MeshPart
|
|
|
|
|
2016-11-25 14:28:39 +01:00
|
|
|
Vertex = Vector
|
2016-11-21 15:59:03 +01:00
|
|
|
Normal = Vertex
|
|
|
|
TexCoord = Vertex
|
|
|
|
|
|
|
|
class FaceVertex:
|
2016-11-29 12:13:43 +01:00
|
|
|
def __init__(self, vertex = None, tex_coord = None, normal = None):
|
2016-11-21 15:59:03 +01:00
|
|
|
self.vertex = vertex
|
2016-11-29 12:13:43 +01:00
|
|
|
self.tex_coord = tex_coord
|
2016-11-21 15:59:03 +01:00
|
|
|
self.normal = normal
|
|
|
|
|
|
|
|
def from_array(self, arr):
|
2016-11-22 11:40:58 +01:00
|
|
|
self.vertex = int(arr[0]) if len(arr) > 0 else None
|
2016-11-23 16:36:32 +01:00
|
|
|
|
|
|
|
try:
|
2016-11-29 12:13:43 +01:00
|
|
|
self.tex_coord = int(arr[1]) if len(arr) > 1 else None
|
2016-11-23 16:36:32 +01:00
|
|
|
except:
|
2016-11-29 12:13:43 +01:00
|
|
|
self.tex_coord = None
|
2016-11-23 16:36:32 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.normal = int(arr[2]) if len(arr) > 2 else None
|
|
|
|
except:
|
|
|
|
self.normal = None
|
|
|
|
|
2016-11-21 15:59:03 +01:00
|
|
|
return self
|
|
|
|
|
|
|
|
class Face:
|
2016-11-29 12:13:43 +01:00
|
|
|
def __init__(self, a = None, b = None, c = None, material = None):
|
2016-11-21 15:59:03 +01:00
|
|
|
self.a = a
|
|
|
|
self.b = b
|
|
|
|
self.c = c
|
2016-11-29 12:13:43 +01:00
|
|
|
self.material = material
|
2016-11-21 15:59:03 +01:00
|
|
|
|
|
|
|
# Expects array of array
|
|
|
|
def from_array(self, arr):
|
|
|
|
self.a = FaceVertex().from_array(arr[0])
|
|
|
|
self.b = FaceVertex().from_array(arr[1])
|
|
|
|
self.c = FaceVertex().from_array(arr[2])
|
|
|
|
return self
|
|
|
|
|
|
|
|
class ModelParser:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.vertices = []
|
|
|
|
self.normals = []
|
|
|
|
self.tex_coords = []
|
2016-11-29 15:40:30 +01:00
|
|
|
self.parts = []
|
|
|
|
self.current_part = None
|
2016-11-22 12:09:54 +01:00
|
|
|
self.bounding_box = BoundingBox()
|
|
|
|
self.center_and_scale = True
|
2016-11-23 15:38:50 +01:00
|
|
|
self.vertex_vbo = None
|
|
|
|
self.tex_coord_vbo = None
|
|
|
|
self.normal_vbo = None
|
2016-11-29 12:05:03 +01:00
|
|
|
self.path = None
|
2016-11-21 15:59:03 +01:00
|
|
|
|
2016-11-29 15:40:30 +01:00
|
|
|
def init_textures(self):
|
|
|
|
for part in self.parts:
|
|
|
|
part.init_texture()
|
|
|
|
|
2016-11-21 15:59:03 +01:00
|
|
|
def add_vertex(self, vertex):
|
|
|
|
self.vertices.append(vertex)
|
2016-11-22 12:09:54 +01:00
|
|
|
self.bounding_box.add(vertex)
|
2016-11-21 15:59:03 +01:00
|
|
|
|
|
|
|
def add_tex_coord(self, tex_coord):
|
|
|
|
self.tex_coords.append(tex_coord)
|
|
|
|
|
|
|
|
def add_normal(self, normal):
|
|
|
|
self.normals.append(normal)
|
|
|
|
|
2016-11-21 16:18:49 +01:00
|
|
|
def add_face(self, face):
|
2016-11-29 15:40:30 +01:00
|
|
|
if self.current_part is None or face.material != self.current_part.material:
|
|
|
|
self.current_part = MeshPart(self)
|
2016-11-30 10:08:37 +01:00
|
|
|
self.current_part.material = face.material if face.material is not None else Material.DEFAULT_MATERIAL
|
2016-11-29 15:40:30 +01:00
|
|
|
self.parts.append(self.current_part)
|
|
|
|
|
|
|
|
self.current_part.add_face(face)
|
2016-11-21 15:59:03 +01:00
|
|
|
|
|
|
|
def parse_line(self, string):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def parse_file(self, path):
|
2016-11-29 12:05:03 +01:00
|
|
|
self.path = path
|
2016-11-21 15:59:03 +01:00
|
|
|
with open(path) as f:
|
|
|
|
for line in f.readlines():
|
|
|
|
line = line.rstrip()
|
|
|
|
self.parse_line(line)
|
|
|
|
|
2016-11-25 17:08:57 +01:00
|
|
|
def draw(self):
|
2016-11-22 11:40:58 +01:00
|
|
|
import OpenGL.GL as gl
|
|
|
|
|
2016-11-22 12:09:54 +01:00
|
|
|
if self.center_and_scale:
|
|
|
|
center = self.bounding_box.get_center()
|
|
|
|
scale = self.bounding_box.get_scale() / 2
|
|
|
|
gl.glPushMatrix()
|
|
|
|
gl.glScalef(1/scale, 1/scale, 1/scale)
|
|
|
|
gl.glTranslatef(-center.x, -center.y, -center.z)
|
|
|
|
|
2016-11-29 15:40:30 +01:00
|
|
|
for part in self.parts:
|
|
|
|
part.draw()
|
2016-11-22 11:40:58 +01:00
|
|
|
|
2016-11-22 12:09:54 +01:00
|
|
|
if self.center_and_scale:
|
|
|
|
gl.glPopMatrix()
|
|
|
|
|
2016-11-23 15:38:50 +01:00
|
|
|
def generate_vbos(self):
|
2016-11-29 15:40:30 +01:00
|
|
|
for part in self.parts:
|
|
|
|
part.generate_vbos()
|
2016-11-23 12:07:47 +01:00
|
|
|
|
2016-11-22 15:13:35 +01:00
|
|
|
def generate_vertex_normals(self):
|
2016-11-29 15:40:30 +01:00
|
|
|
|
2016-11-22 15:13:35 +01:00
|
|
|
self.normals = [Normal() for i in self.vertices]
|
|
|
|
|
2016-11-29 15:40:30 +01:00
|
|
|
for part in self.parts:
|
|
|
|
for face in part.faces:
|
|
|
|
v1 = Vertex.from_points(self.vertices[face.a.vertex], self.vertices[face.b.vertex])
|
|
|
|
v2 = Vertex.from_points(self.vertices[face.a.vertex], self.vertices[face.c.vertex])
|
|
|
|
cross = Vertex.cross_product(v1, v2)
|
|
|
|
self.normals[face.a.vertex] += cross
|
|
|
|
self.normals[face.b.vertex] += cross
|
|
|
|
self.normals[face.c.vertex] += cross
|
2016-11-22 15:13:35 +01:00
|
|
|
|
|
|
|
for normal in self.normals:
|
|
|
|
normal.normalize()
|
|
|
|
|
2016-11-29 15:40:30 +01:00
|
|
|
for part in self.parts:
|
|
|
|
for face in part.faces:
|
|
|
|
face.a.normal = face.a.vertex
|
|
|
|
face.b.normal = face.b.vertex
|
|
|
|
face.c.normal = face.c.vertex
|
2016-11-22 15:13:35 +01:00
|
|
|
|
|
|
|
def generate_face_normals(self):
|
|
|
|
|
2016-11-30 10:08:37 +01:00
|
|
|
# Build array of faces
|
|
|
|
faces = sum(map(lambda x: x.faces, self.parts), [])
|
|
|
|
self.normals = [Normal()] * len(faces)
|
2016-11-22 15:13:35 +01:00
|
|
|
|
2016-11-30 10:08:37 +01:00
|
|
|
for (index, face) in enumerate(faces):
|
2016-11-22 15:13:35 +01:00
|
|
|
|
|
|
|
v1 = Vertex.from_points(self.vertices[face.a.vertex], self.vertices[face.b.vertex])
|
|
|
|
v2 = Vertex.from_points(self.vertices[face.a.vertex], self.vertices[face.c.vertex])
|
|
|
|
cross = Vertex.cross_product(v1, v2)
|
|
|
|
cross.normalize()
|
|
|
|
self.normals[index] = cross
|
|
|
|
|
|
|
|
face.a.normal = index
|
|
|
|
face.b.normal = index
|
|
|
|
face.c.normal = index
|
|
|
|
|
|
|
|
|
2016-11-22 12:09:54 +01:00
|
|
|
class BoundingBox:
|
|
|
|
def __init__(self):
|
|
|
|
self.min_x = +float('inf')
|
|
|
|
self.min_y = +float('inf')
|
|
|
|
self.min_z = +float('inf')
|
|
|
|
|
|
|
|
self.max_x = -float('inf')
|
|
|
|
self.max_y = -float('inf')
|
|
|
|
self.max_z = -float('inf')
|
|
|
|
|
|
|
|
def add(self, vertex):
|
|
|
|
self.min_x = min(self.min_x, vertex.x)
|
|
|
|
self.min_y = min(self.min_y, vertex.y)
|
|
|
|
self.min_z = min(self.min_z, vertex.z)
|
|
|
|
|
|
|
|
self.max_x = max(self.max_x, vertex.x)
|
|
|
|
self.max_y = max(self.max_y, vertex.y)
|
|
|
|
self.max_z = max(self.max_z, vertex.z)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "[{},{}],[{},{}],[{},{}]".format(
|
|
|
|
self.min_x,
|
|
|
|
self.min_y,
|
|
|
|
self.min_z,
|
|
|
|
self.max_x,
|
|
|
|
self.max_y,
|
|
|
|
self.max_z)
|
|
|
|
|
|
|
|
def get_center(self):
|
|
|
|
return Vertex(
|
|
|
|
(self.min_x + self.max_x) / 2,
|
|
|
|
(self.min_y + self.max_y) / 2,
|
|
|
|
(self.min_z + self.max_z) / 2)
|
|
|
|
|
|
|
|
def get_scale(self):
|
|
|
|
return max(
|
|
|
|
abs(self.max_x - self.min_x),
|
|
|
|
abs(self.max_y - self.min_y),
|
|
|
|
abs(self.max_z - self.min_z))
|
|
|
|
|
2016-11-22 11:40:58 +01:00
|
|
|
|
2016-11-21 15:59:03 +01:00
|
|
|
class Exporter:
|
|
|
|
def __init__(self, model):
|
|
|
|
self.model = model
|
|
|
|
|
2016-11-22 11:27:42 +01:00
|
|
|
|