-
Notifications
You must be signed in to change notification settings - Fork 3
/
mesh.py
227 lines (170 loc) · 6.35 KB
/
mesh.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
'''This module contains mesh'''
import numpy as np
import vulk.vulkanconstant as vc
import vulk.vulkanobject as vo
class VertexAttribute():
def __init__(self, location, attribute_format):
'''
*Parameters:*
- `location`: Attribute location in shader
- `attribute_format`: `Format` constant from `vulkanconstant`
'''
dtype, num_components, size = vc.format_info(attribute_format)
self.location = location
self.format = attribute_format
self.dtype = dtype
self.components = num_components
self.size = size
self.offset = 0
class VertexAttributes():
def __init__(self, attributes):
'''
*Parameters:*
- `attributes`: `list` of `VertexAttribute`
'''
self.attributes = attributes
offset = 0
for attr in attributes:
attr.offset = offset
offset += attr.size
@property
def size(self):
return sum([a.size for a in self.attributes])
def __iter__(self):
return iter(self.attributes)
class Mesh():
def __init__(self, context, max_vertices, max_indices, attributes):
'''
*Parameters:*
- `context`: `VulkContext`
- `max_vertices`: Maximum number of vertices for this mesh
- `max_indices`: Maximum number of indice for this mesh
- `attributes`: `VertexAttributes`
'''
self.index_type = vc.IndexType.UINT16
if max_vertices > 65535:
self.index_type = vc.IndexType.UINT32
self.attributes = attributes
self.has_indices = max_indices > 0
# Create numpy type based on vertex attributes
numpy_dtype = []
for attr in attributes:
numpy_dtype.append(
('', vc.DataTypeNumpy[attr.dtype], attr.components))
# Create vertices array and buffer
self.vertices_array = np.zeros(max_vertices, dtype=numpy_dtype)
self.vertices_buffer = vo.HighPerformanceBuffer(
context, self.vertices_array.nbytes,
vc.BufferUsage.VERTEX_BUFFER)
# Create indices array and buffer
if self.has_indices:
self.indices_array = np.zeros(max_indices, dtype=np.uint16)
self.indices_buffer = vo.HighPerformanceBuffer(
context, max_indices * vc.index_type_size(self.index_type),
vc.BufferUsage.INDEX_BUFFER)
# Create others attributes
self.dirty_indices = True
self.dirty_vertices = True
def set_indices(self, indices, offset=0):
'''Set indices of mesh
*Parameters:*
- `indices`: `list` of `float`
- `offset`: Offset in mesh indices array
**Note: Mesh must be indexed**
'''
if not self.has_indices:
raise Exception('No index in this mesh')
self.indices_array[offset:] = indices
self.dirty_indices = True
def set_vertex(self, index, vertex):
'''Set one vertex of the mesh at position `index`
*Parameters:*
- `index`: Vertex index
- `vertex`: Vertex data (tuple format)
*Exemple:*
For a mesh with two attributes (2 components and 4 components):
```
vertex = ([x, y], [r, g, b, a])
mesh.set_vertex(idx, vertex)
```
**Note: Vertex data type depends on `VertexAttributes` of the mesh.
It must be a tuple containing an array for each attributes**
**Note: Once mesh vertices are updated, you need to `upload` the mesh
to take into account the changes.**
'''
self.vertices_array[index] = vertex
self.dirty_vertices = True
def set_vertices(self, vertices, offset=0):
'''Set vertices of the mesh.
Report to `set_vertex`, it works the same but it waits for an array
of vertex.
*Parameters:*
- `vertices`: `list` of Vertex data (see `set_vertex`)
- `offset`: Offset in the mesh vertices array
'''
self.vertices_array[offset:] = vertices
self.dirty_vertices = True
def upload_indices(self, context):
'''
Upload indices to graphic card
*Parameters:*
- `context`: `VulkContext`
**Note: Mesh must be indexed**
'''
if not self.has_indices:
raise Exception('No index in this mesh')
if not self.dirty_indices:
return
self.dirty_indices = False
with self.indices_buffer.bind(context) as b:
np.copyto(np.array(b, copy=False),
self.indices_array.view(dtype=np.uint8),
casting='no')
def upload_vertices(self, context):
'''
Upload vertices to graphic card
*Parameters:*
- `context`: `VulkContext`
'''
if not self.dirty_vertices:
return
self.dirty_vertices = False
with self.vertices_buffer.bind(context) as b:
np.copyto(np.array(b, copy=False),
self.vertices_array.view(dtype=np.uint8),
casting='no')
def upload(self, context):
'''
Upload vertices and indices to graphic card
*Parameters:*
- `context`: `VulkContext`
'''
self.upload_vertices(context)
if self.has_indices:
self.upload_indices(context)
def bind(self, cmd):
'''Bind the buffers during command buffer registering
*Parameters:*
- `command`: `CommandBufferRegister`
'''
cmd.bind_vertex_buffers(
0, 1, [self.vertices_buffer.final_buffer], [0])
if self.has_indices:
cmd.bind_index_buffer(
self.indices_buffer.final_buffer, 0, self.index_type)
def draw(self, cmd, offset=0, count=0):
'''Draw the mesh during command buffer registration
*Parameters:*
- `cmd`: `CommandBufferRegister`
- `offset`: Start drawing at `offset` vertices
- `count`: Draw `count` vertices
**Note: `offset` and `count` target indices if mesh is indexed**
'''
if self.has_indices:
if not count:
count = len(self.indices_array) - offset
cmd.draw_indexed(count, offset)
else:
if not count:
count = len(self.vertices_array) - offset
cmd.draw(count, offset)