-
Notifications
You must be signed in to change notification settings - Fork 30
/
util.py
96 lines (77 loc) · 2.37 KB
/
util.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import numpy as np
from PyQt5 import QtCore, QtOpenGL
import moderngl
from converter import parse_mesh
from onmyoji_converter import _parse_mesh
class QModernGLWidget(QtOpenGL.QGLWidget):
def __init__(self, parent=None):
fmt = QtOpenGL.QGLFormat()
fmt.setVersion(3, 3)
fmt.setProfile(QtOpenGL.QGLFormat.CoreProfile)
fmt.setSampleBuffers(True)
self.timer = QtCore.QElapsedTimer()
super().__init__(fmt, parent=parent)
def initializeGL(self):
pass
def paintGL(self):
self.ctx = moderngl.create_context()
self.screen = self.ctx.detect_framebuffer()
self.init()
self.render()
self.paintGL = self.render
def init(self):
pass
def render(self):
pass
def data_from_path(path):
data = None
with open(path, 'rb') as f:
data = f.read()
return data
def text_from_path(path):
text = None
with open(path) as f:
text = f.read()
return text
def shader_from_path(path):
return text_from_path('shader/' + path)
def res_from_path(path):
return data_from_path('res/' + path)
def mesh_from_path(path):
try:
mesh = parse_mesh(path)
except Exception as _:
mesh = _parse_mesh(path)
pos = np.array(mesh['position'])
pos[:,0] = -pos[:,0]
norm = np.array(mesh['normal'])
norm[:,0] = -norm[:,0]
dat = np.hstack((pos, norm))
mesh['gldat'] = dat
index = np.array(mesh['face'])
index = index[:,[1,0,2]]
mesh['glindex'] = index
return mesh
def log(*args, **kwargs):
print('log: ', *args, **kwargs)
def grid(size, steps):
u = np.repeat(np.linspace(-size, size, steps), 2)
v = np.tile([-size, size], steps)
w = np.zeros(steps * 2)
return np.concatenate([np.dstack([u, w, v]), np.dstack([v, w, u])])
def file_names_from_dir(path):
file_names = os.listdir(path)
file_names = list(filter(lambda s: s.endswith('.mesh'), file_names))
return file_names
def file_paths_from_dir(path):
file_names = file_names_from_dir(path)
file_paths = list(map(lambda s: path + '/' + s, file_names))
return file_paths
help_text = '''
Key Up: change to prev mesh
Key Down: change to next mesh
Mouse Middle: Orbit
Mouse Middle Scroll: Dolly
Dhift + Mouse Middle: Pan
'''