-
Notifications
You must be signed in to change notification settings - Fork 2
/
vertex.go
285 lines (247 loc) · 6.94 KB
/
vertex.go
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package glhf
import (
"fmt"
"runtime"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/gopxl/mainthread/v2"
"github.com/pkg/errors"
)
// VertexSlice points to a portion of (or possibly whole) vertex array. It is used as a pointer,
// contrary to Go's builtin slices. This is, so that append can be 'in-place'. That's for the good,
// because Begin/End-ing a VertexSlice would become super confusing, if append returned a new
// VertexSlice.
//
// It also implements all basic slice-like operations: appending, sub-slicing, etc.
//
// Note that you need to Begin a VertexSlice before getting or updating it's elements or drawing it.
// After you're done with it, you need to End it.
type VertexSlice struct {
va *vertexArray
i, j int
}
// MakeVertexSlice allocates a new vertex array with specified capacity and returns a VertexSlice
// that points to it's first len elements.
//
// Note, that a vertex array is specialized for a specific shader and can't be used with another
// shader.
func MakeVertexSlice(shader *Shader, len, cap int) *VertexSlice {
if len > cap {
panic("failed to make vertex slice: len > cap")
}
return &VertexSlice{
va: newVertexArray(shader, cap),
i: 0,
j: len,
}
}
// VertexFormat returns the format of vertex attributes inside the underlying vertex array of this
// VertexSlice.
func (vs *VertexSlice) VertexFormat() AttrFormat {
return vs.va.format
}
// Stride returns the number of float32 elements occupied by one vertex.
func (vs *VertexSlice) Stride() int {
return vs.va.stride / 4
}
// Len returns the length of the VertexSlice (number of vertices).
func (vs *VertexSlice) Len() int {
return vs.j - vs.i
}
// Cap returns the capacity of an underlying vertex array.
func (vs *VertexSlice) Cap() int {
return vs.va.cap - vs.i
}
// SetLen resizes the VertexSlice to length len.
func (vs *VertexSlice) SetLen(len int) {
vs.End() // vs must have been Begin-ed before calling this method
*vs = vs.grow(len)
vs.Begin()
}
// grow returns supplied vs with length changed to len. Allocates new underlying vertex array if
// necessary. The original content is preserved.
func (vs VertexSlice) grow(len int) VertexSlice {
if len <= vs.Cap() {
// capacity sufficient
return VertexSlice{
va: vs.va,
i: vs.i,
j: vs.i + len,
}
}
// grow the capacity
newCap := vs.Cap()
if newCap < 1024 {
newCap += newCap
} else {
newCap += newCap / 4
}
if newCap < len {
newCap = len
}
newVs := VertexSlice{
va: newVertexArray(vs.va.shader, newCap),
i: 0,
j: len,
}
// preserve the original content
newVs.Begin()
newVs.Slice(0, vs.Len()).SetVertexData(vs.VertexData())
newVs.End()
return newVs
}
// Slice returns a sub-slice of this VertexSlice covering the range [i, j) (relative to this
// VertexSlice).
//
// Note, that the returned VertexSlice shares an underlying vertex array with the original
// VertexSlice. Modifying the contents of one modifies corresponding contents of the other.
func (vs *VertexSlice) Slice(i, j int) *VertexSlice {
if i < 0 || j < i || j > vs.va.cap {
panic("failed to slice vertex slice: index out of range")
}
return &VertexSlice{
va: vs.va,
i: vs.i + i,
j: vs.i + j,
}
}
// SetVertexData sets the contents of the VertexSlice.
//
// The data is a slice of float32's, where each vertex attribute occupies a certain number of
// elements. Namely, Float occupies 1, Vec2 occupies 2, Vec3 occupies 3 and Vec4 occupies 4. The
// attribues in the data slice must be in the same order as in the vertex format of this Vertex
// Slice.
//
// If the length of vertices does not match the length of the VertexSlice, this methdo panics.
func (vs *VertexSlice) SetVertexData(data []float32) {
if len(data)/vs.Stride() != vs.Len() {
fmt.Println(len(data)/vs.Stride(), vs.Len())
panic("set vertex data: wrong length of vertices")
}
vs.va.setVertexData(vs.i, vs.j, data)
}
// VertexData returns the contents of the VertexSlice.
//
// The data is in the same format as with SetVertexData.
func (vs *VertexSlice) VertexData() []float32 {
return vs.va.vertexData(vs.i, vs.j)
}
// Draw draws the content of the VertexSlice.
func (vs *VertexSlice) Draw() {
vs.va.draw(vs.i, vs.j)
}
// Begin binds the underlying vertex array. Calling this method is necessary before using the VertexSlice.
func (vs *VertexSlice) Begin() {
vs.va.begin()
}
// End unbinds the underlying vertex array. Call this method when you're done with VertexSlice.
func (vs *VertexSlice) End() {
vs.va.end()
}
type vertexArray struct {
vao, vbo binder
cap int
format AttrFormat
stride int
offset []int
shader *Shader
}
const vertexArrayMinCap = 4
func newVertexArray(shader *Shader, cap int) *vertexArray {
if cap < vertexArrayMinCap {
cap = vertexArrayMinCap
}
va := &vertexArray{
vao: binder{
restoreLoc: gl.VERTEX_ARRAY_BINDING,
bindFunc: func(obj uint32) {
gl.BindVertexArray(obj)
},
},
vbo: binder{
restoreLoc: gl.ARRAY_BUFFER_BINDING,
bindFunc: func(obj uint32) {
gl.BindBuffer(gl.ARRAY_BUFFER, obj)
},
},
cap: cap,
format: shader.VertexFormat(),
stride: shader.VertexFormat().Size(),
offset: make([]int, len(shader.VertexFormat())),
shader: shader,
}
offset := 0
for i, attr := range va.format {
switch attr.Type {
case Float, Vec2, Vec3, Vec4:
default:
panic(errors.New("failed to create vertex array: invalid attribute type"))
}
va.offset[i] = offset
offset += attr.Type.Size()
}
gl.GenVertexArrays(1, &va.vao.obj)
va.vao.bind()
gl.GenBuffers(1, &va.vbo.obj)
defer va.vbo.bind().restore()
emptyData := make([]byte, cap*va.stride)
gl.BufferData(gl.ARRAY_BUFFER, len(emptyData), gl.Ptr(emptyData), gl.DYNAMIC_DRAW)
for i, attr := range va.format {
loc := gl.GetAttribLocation(shader.program.obj, gl.Str(attr.Name+"\x00"))
var size int32
switch attr.Type {
case Float:
size = 1
case Vec2:
size = 2
case Vec3:
size = 3
case Vec4:
size = 4
}
gl.VertexAttribPointerWithOffset(
uint32(loc),
size,
gl.FLOAT,
false,
int32(va.stride),
uintptr(va.offset[i]),
)
gl.EnableVertexAttribArray(uint32(loc))
}
va.vao.restore()
runtime.SetFinalizer(va, (*vertexArray).delete)
return va
}
func (va *vertexArray) delete() {
mainthread.CallNonBlock(func() {
gl.DeleteVertexArrays(1, &va.vao.obj)
gl.DeleteBuffers(1, &va.vbo.obj)
})
}
func (va *vertexArray) begin() {
va.vao.bind()
va.vbo.bind()
}
func (va *vertexArray) end() {
va.vbo.restore()
va.vao.restore()
}
func (va *vertexArray) draw(i, j int) {
gl.DrawArrays(gl.TRIANGLES, int32(i), int32(j-i))
}
func (va *vertexArray) setVertexData(i, j int, data []float32) {
if j-i == 0 {
// avoid setting 0 bytes of buffer data
return
}
gl.BufferSubData(gl.ARRAY_BUFFER, i*va.stride, len(data)*4, gl.Ptr(data))
}
func (va *vertexArray) vertexData(i, j int) []float32 {
if j-i == 0 {
// avoid getting 0 bytes of buffer data
return nil
}
data := make([]float32, (j-i)*va.stride/4)
gl.GetBufferSubData(gl.ARRAY_BUFFER, i*va.stride, len(data)*4, gl.Ptr(data))
return data
}