~ubuntu-branches/debian/stretch/electrum/stretch

« back to all changes in this revision

Viewing changes to gui/kivy/tools/.buildozer/android/platform/python-for-android/dist/kivy/python-install/share/kivy-examples/3Drendering/objloader.py

  • Committer: Package Import Robot
  • Author(s): Tristan Seligmann
  • Date: 2016-04-04 03:02:39 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20160404030239-0szgkio8yryjv7c9
Tags: 2.6.3-1
* New upstream release.
  - Drop backported install-wizard-connect.patch.
* Add Suggests: python-zbar and update the installation hint to suggest
  apt-get instead of pip (closes: #819517).
* Bump Standards-Version to 3.9.7 (no changes).
* Update Vcs-* links.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
class MeshData(object):
 
2
    def __init__(self, **kwargs):
 
3
        self.name = kwargs.get("name")
 
4
        self.vertex_format = [
 
5
            (b'v_pos', 3, 'float'),
 
6
            (b'v_normal', 3, 'float'),
 
7
            (b'v_tc0', 2, 'float')]
 
8
        self.vertices = []
 
9
        self.indices = []
 
10
 
 
11
    def calculate_normals(self):
 
12
        for i in range(len(self.indices) / (3)):
 
13
            fi = i * 3
 
14
            v1i = self.indices[fi]
 
15
            v2i = self.indices[fi + 1]
 
16
            v3i = self.indices[fi + 2]
 
17
 
 
18
            vs = self.vertices
 
19
            p1 = [vs[v1i + c] for c in range(3)]
 
20
            p2 = [vs[v2i + c] for c in range(3)]
 
21
            p3 = [vs[v3i + c] for c in range(3)]
 
22
 
 
23
            u, v = [0, 0, 0], [0, 0, 0]
 
24
            for j in range(3):
 
25
                v[j] = p2[j] - p1[j]
 
26
                u[j] = p3[j] - p1[j]
 
27
 
 
28
            n = [0, 0, 0]
 
29
            n[0] = u[1] * v[2] - u[2] * v[1]
 
30
            n[1] = u[2] * v[0] - u[0] * v[2]
 
31
            n[2] = u[0] * v[1] - u[1] * v[0]
 
32
 
 
33
            for k in range(3):
 
34
                self.vertices[v1i + 3 + k] = n[k]
 
35
                self.vertices[v2i + 3 + k] = n[k]
 
36
                self.vertices[v3i + 3 + k] = n[k]
 
37
 
 
38
 
 
39
class ObjFile:
 
40
    def finish_object(self):
 
41
        if self._current_object is None:
 
42
            return
 
43
 
 
44
        mesh = MeshData()
 
45
        idx = 0
 
46
        for f in self.faces:
 
47
            verts = f[0]
 
48
            norms = f[1]
 
49
            tcs = f[2]
 
50
            for i in range(3):
 
51
                #get normal components
 
52
                n = (0.0, 0.0, 0.0)
 
53
                if norms[i] != -1:
 
54
                    n = self.normals[norms[i] - 1]
 
55
 
 
56
                #get texture coordinate components
 
57
                t = (0.0, 0.0)
 
58
                if tcs[i] != -1:
 
59
                    t = self.texcoords[tcs[i] - 1]
 
60
 
 
61
                #get vertex components
 
62
                v = self.vertices[verts[i] - 1]
 
63
 
 
64
                data = [v[0], v[1], v[2], n[0], n[1], n[2], t[0], t[1]]
 
65
                mesh.vertices.extend(data)
 
66
 
 
67
            tri = [idx, idx + 1, idx + 2]
 
68
            mesh.indices.extend(tri)
 
69
            idx += 3
 
70
 
 
71
        self.objects[self._current_object] = mesh
 
72
        #mesh.calculate_normals()
 
73
        self.faces = []
 
74
 
 
75
    def __init__(self, filename, swapyz=False):
 
76
        """Loads a Wavefront OBJ file. """
 
77
        self.objects = {}
 
78
        self.vertices = []
 
79
        self.normals = []
 
80
        self.texcoords = []
 
81
        self.faces = []
 
82
 
 
83
        self._current_object = None
 
84
 
 
85
        material = None
 
86
        for line in open(filename, "r"):
 
87
            if line.startswith('#'):
 
88
                continue
 
89
            if line.startswith('s'):
 
90
                continue
 
91
            values = line.split()
 
92
            if not values:
 
93
                continue
 
94
            if values[0] == 'o':
 
95
                self.finish_object()
 
96
                self._current_object = values[1]
 
97
            #elif values[0] == 'mtllib':
 
98
            #    self.mtl = MTL(values[1])
 
99
            #elif values[0] in ('usemtl', 'usemat'):
 
100
            #    material = values[1]
 
101
            if values[0] == 'v':
 
102
                v = list(map(float, values[1:4]))
 
103
                if swapyz:
 
104
                    v = v[0], v[2], v[1]
 
105
                self.vertices.append(v)
 
106
            elif values[0] == 'vn':
 
107
                v = list(map(float, values[1:4]))
 
108
                if swapyz:
 
109
                    v = v[0], v[2], v[1]
 
110
                self.normals.append(v)
 
111
            elif values[0] == 'vt':
 
112
                self.texcoords.append(map(float, values[1:3]))
 
113
            elif values[0] == 'f':
 
114
                face = []
 
115
                texcoords = []
 
116
                norms = []
 
117
                for v in values[1:]:
 
118
                    w = v.split('/')
 
119
                    face.append(int(w[0]))
 
120
                    if len(w) >= 2 and len(w[1]) > 0:
 
121
                        texcoords.append(int(w[1]))
 
122
                    else:
 
123
                        texcoords.append(-1)
 
124
                    if len(w) >= 3 and len(w[2]) > 0:
 
125
                        norms.append(int(w[2]))
 
126
                    else:
 
127
                        norms.append(-1)
 
128
                self.faces.append((face, norms, texcoords, material))
 
129
        self.finish_object()
 
130
 
 
131
 
 
132
def MTL(filename):
 
133
    contents = {}
 
134
    mtl = None
 
135
    return
 
136
    for line in open(filename, "r"):
 
137
        if line.startswith('#'):
 
138
            continue
 
139
        values = line.split()
 
140
        if not values:
 
141
            continue
 
142
        if values[0] == 'newmtl':
 
143
            mtl = contents[values[1]] = {}
 
144
        elif mtl is None:
 
145
            raise ValueError("mtl file doesn't start with newmtl stmt")
 
146
        mtl[values[0]] = values[1:]
 
147
    return contents