-
Notifications
You must be signed in to change notification settings - Fork 10
/
meshattr.go
584 lines (516 loc) · 13.2 KB
/
meshattr.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
// Copyright 2012 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package glh
import (
"github.com/go-gl/gl"
"unsafe"
)
// Pre-defined attribute names.
//
// These are used by all render modes other than RenderShader
// to deterine what the purpose of a given attribute is.
const (
mbPositionKey = "position"
mbColorKey = "color"
mbNormalKey = "normal"
mbTexCoordKey = "texcoord"
mbIndexKey = "index"
)
// An Attr describes the type and size of a single vertex component.
// These tell the MeshBuffer how to interpret mesh data.
type Attr struct {
data interface{} // Data store.
name string // Attribute name.
vbo gl.Buffer // Vertex buffer identity.
target gl.GLenum // Buffer type.
usage gl.GLenum // Usage type of this attribute.
typ gl.GLenum // Attribute type.
size int // Component size (number of elements).
stride int // Size of component in bytes.
gpuSize int // Size of data on GPU.
invalid bool // Do we require re-committing?
}
// NewAttr creates a new mesh attribute for the given size,
// type, usage value and name.
//
// In RenderShader mode, the name is the variable by which the component
// can be referenced from a shader program.
//
// In other modes, the MeshBuffer uses this name to identify the attribute's
// purpose. In these cases, it is advised to use the NewIndexAttr,
// NewPositionAttr, etc. wrappers.
func NewAttr(name string, size int, typ, usage gl.GLenum) *Attr {
a := new(Attr)
a.name = name
if size == 0 {
return a
}
a.target = gl.ARRAY_BUFFER
a.size = size
a.usage = usage
a.typ = typ
a.Clear()
a.stride = int(Sizeof(typ))
return a
}
// NewPositionAttr creates a new vertex position attribute.
func NewPositionAttr(size int, typ, usage gl.GLenum) *Attr {
return NewAttr(mbPositionKey, size, typ, usage)
}
// NewColorAttr creates a new vertex color attribute.
func NewColorAttr(size int, typ, usage gl.GLenum) *Attr {
return NewAttr(mbColorKey, size, typ, usage)
}
// NewNormalAttr creates a new surface normal attribute.
func NewNormalAttr(size int, typ, usage gl.GLenum) *Attr {
return NewAttr(mbNormalKey, size, typ, usage)
}
// NewTexCoordAttr creates a new vertex texture coordinate attribute.
func NewTexCoordAttr(size int, typ, usage gl.GLenum) *Attr {
return NewAttr(mbTexCoordKey, size, typ, usage)
}
// NewIndexAttr creates a new index attribute.
func NewIndexAttr(size int, typ, usage gl.GLenum) *Attr {
a := NewAttr(mbIndexKey, size, typ, usage)
a.target = gl.ELEMENT_ARRAY_BUFFER
return a
}
// init initializes some of the attribute fields.
// These will be defined by the mesh buffer.
func (a *Attr) init(mode RenderMode) {
switch mode {
case RenderClassic, RenderArrays:
// No VBO in classic and vertex array modes.
default:
a.vbo = gl.GenBuffer()
}
}
// release release attribute resources.
func (a *Attr) release() {
if a.vbo != 0 {
a.vbo.Delete()
a.vbo = 0
}
a.gpuSize = 0
a.data = nil
}
// Clear clears the attribute buffer.
func (a *Attr) Clear() {
a.gpuSize = 0
switch a.typ {
case 0: // Null attribute -- do nothing
case gl.BYTE:
a.data = []int8{}
case gl.UNSIGNED_BYTE:
a.data = []uint8{}
case gl.SHORT:
a.data = []int16{}
case gl.UNSIGNED_SHORT:
a.data = []uint16{}
case gl.INT:
a.data = []int32{}
case gl.UNSIGNED_INT:
a.data = []uint32{}
case gl.FLOAT:
a.data = []float32{}
case gl.DOUBLE:
a.data = []float64{}
default:
panic("Invalid attribute type")
}
}
// Name returns the attribute name.
func (a *Attr) Name() string { return a.name }
// Data returns the atribute data store.
//
// This value should be asserted to a concrete type, which would
// be a slice of the attrbute's type. E.g.: []float32, []uint8, etc.
func (a *Attr) Data() interface{} { return a.data }
// Invalid returns true if the data store needs to be re-committed.
func (a *Attr) Invalid() bool { return a.invalid }
// Invalidate marks the data store as invalid.
// This should be done any time the data is modified externally.
// It will be re-committed on the next render pass.
func (a *Attr) Invalidate() { a.invalid = true }
// Size returns the number of elements in a vertext component for this attribute.
func (a *Attr) Size() int { return a.size }
// Stride returns the stride value for the data type this attribute holds.
func (a *Attr) Stride() int { return a.stride }
// Type returns the data type of the attribute.
func (a *Attr) Type() gl.GLenum { return a.typ }
// bind binds the buffer.
func (a *Attr) bind() { a.vbo.Bind(a.target) }
// unbind unbinds the buffer.
func (a *Attr) unbind() { a.vbo.Unbind(a.target) }
// Target returns the buffer target.
func (a *Attr) Target() gl.GLenum { return a.target }
// SetTarget sets the buffer target.
func (a *Attr) SetTarget(t gl.GLenum) { a.target = t }
// Len returns the number of elements in the data store.
func (a *Attr) Len() int {
switch v := a.data.(type) {
case []int8:
return len(v)
case []uint8:
return len(v)
case []int16:
return len(v)
case []uint16:
return len(v)
case []int32:
return len(v)
case []uint32:
return len(v)
case []float32:
return len(v)
case []float64:
return len(v)
}
return 0
}
// buffer buffers the mesh data on the GPU.
// This calls glBufferData or glBufferSubData where appropriate.
func (a *Attr) buffer() {
switch v := a.data.(type) {
case []int8:
size := len(v) * a.stride
if size != a.gpuSize {
gl.BufferData(a.target, size, v, a.usage)
a.gpuSize = size
} else {
gl.BufferSubData(a.target, 0, size, v)
}
case []uint8:
size := len(v) * a.stride
if size != a.gpuSize {
gl.BufferData(a.target, size, v, a.usage)
a.gpuSize = size
} else {
gl.BufferSubData(a.target, 0, size, v)
}
case []int16:
size := len(v) * a.stride
if size != a.gpuSize {
gl.BufferData(a.target, size, v, a.usage)
a.gpuSize = size
} else {
gl.BufferSubData(a.target, 0, size, v)
}
case []uint16:
size := len(v) * a.stride
if size != a.gpuSize {
gl.BufferData(a.target, size, v, a.usage)
a.gpuSize = size
} else {
gl.BufferSubData(a.target, 0, size, v)
}
case []int32:
size := len(v) * a.stride
if size != a.gpuSize {
gl.BufferData(a.target, size, v, a.usage)
a.gpuSize = size
} else {
gl.BufferSubData(a.target, 0, size, v)
}
case []uint32:
size := len(v) * a.stride
if size != a.gpuSize {
gl.BufferData(a.target, size, v, a.usage)
a.gpuSize = size
} else {
gl.BufferSubData(a.target, 0, size, v)
}
case []float32:
size := len(v) * a.stride
if size != a.gpuSize {
gl.BufferData(a.target, size, v, a.usage)
a.gpuSize = size
} else {
gl.BufferSubData(a.target, 0, size, v)
}
case []float64:
size := len(v) * a.stride
if size != a.gpuSize {
gl.BufferData(a.target, size, v, a.usage)
a.gpuSize = size
} else {
gl.BufferSubData(a.target, 0, size, v)
}
}
a.invalid = false
}
// increment increments the value at the given range by the supplied value.
// This is used internally by the mesh buffer.
func (a *Attr) increment(start int, value float64) {
switch v := a.data.(type) {
case []int8:
for i := start; i < len(v); i++ {
v[i] += int8(value)
}
case []uint8:
for i := start; i < len(v); i++ {
v[i] += uint8(value)
}
case []int16:
for i := start; i < len(v); i++ {
v[i] += int16(value)
}
case []uint16:
for i := start; i < len(v); i++ {
v[i] += uint16(value)
}
case []int32:
for i := start; i < len(v); i++ {
v[i] += int32(value)
}
case []uint32:
for i := start; i < len(v); i++ {
v[i] += uint32(value)
}
case []float32:
for i := start; i < len(v); i++ {
v[i] += float32(value)
}
case []float64:
for i := start; i < len(v); i++ {
v[i] += value
}
}
a.invalid = true
}
// append appends the given slice to the data store.
// We expect a slice of the appropriate type. E.g.: []uint8, []float32, etc.
func (a *Attr) append(data interface{}) int {
var n int
switch va := a.data.(type) {
case []int8:
vb := data.([]int8)
a.data = append(va, vb...)
n = len(vb)
case []uint8:
vb := data.([]uint8)
a.data = append(va, vb...)
n = len(vb)
case []int16:
vb := data.([]int16)
a.data = append(va, vb...)
n = len(vb)
case []uint16:
vb := data.([]uint16)
a.data = append(va, vb...)
n = len(vb)
case []int32:
vb := data.([]int32)
a.data = append(va, vb...)
n = len(vb)
case []uint32:
vb := data.([]uint32)
a.data = append(va, vb...)
n = len(vb)
case []float32:
vb := data.([]float32)
a.data = append(va, vb...)
n = len(vb)
case []float64:
vb := data.([]float64)
a.data = append(va, vb...)
n = len(vb)
}
a.invalid = true
return n
}
// Ptr returns a pointer to the element indicated by index.
// Used in RenderArrays mode.
func (a *Attr) ptr(index int) uintptr {
switch v := a.data.(type) {
case []int8:
return uintptr(unsafe.Pointer(&v[index]))
case []uint8:
return uintptr(unsafe.Pointer(&v[index]))
case []int16:
return uintptr(unsafe.Pointer(&v[index]))
case []uint16:
return uintptr(unsafe.Pointer(&v[index]))
case []int32:
return uintptr(unsafe.Pointer(&v[index]))
case []uint32:
return uintptr(unsafe.Pointer(&v[index]))
case []float32:
return uintptr(unsafe.Pointer(&v[index]))
case []float64:
return uintptr(unsafe.Pointer(&v[index]))
}
return 0
}
// vertex draws vertices.
// Used in classic render mode.
func (a *Attr) vertex(i int) {
i *= a.size
switch a.size {
case 2:
switch v := a.data.(type) {
case []int16:
gl.Vertex2s(v[i], v[i+1])
case []int32:
gl.Vertex2i(int(v[i]), int(v[i+1]))
case []float32:
gl.Vertex2f(v[i], v[i+1])
case []float64:
gl.Vertex2d(v[i], v[i+1])
}
case 3:
switch v := a.data.(type) {
case []int16:
gl.Vertex3s(v[i], v[i+1], v[i+2])
case []int32:
gl.Vertex3i(int(v[i]), int(v[i+1]), int(v[i+2]))
case []float32:
gl.Vertex3f(v[i], v[i+1], v[i+2])
case []float64:
gl.Vertex3d(v[i], v[i+1], v[i+2])
}
case 4:
switch v := a.data.(type) {
case []int16:
gl.Vertex4s(v[i], v[i+1], v[i+2], v[i+3])
case []int32:
gl.Vertex4i(int(v[i]), int(v[i+1]), int(v[i+2]), int(v[i+3]))
case []float32:
gl.Vertex4f(v[i], v[i+1], v[i+2], v[i+3])
case []float64:
gl.Vertex4d(v[i], v[i+1], v[i+2], v[i+3])
}
}
}
// texcoord defines vertex texture coordinates.
// Used in classic render mode.
func (a *Attr) texcoord(i int) {
i *= a.size
switch a.size {
case 1:
switch v := a.data.(type) {
case []int16:
gl.TexCoord1s(v[i])
case []int32:
gl.TexCoord1i(int(v[i]))
case []float32:
gl.TexCoord1f(v[i])
case []float64:
gl.TexCoord1d(v[i])
}
case 2:
switch v := a.data.(type) {
case []int16:
gl.TexCoord2s(v[i], v[i+1])
case []int32:
gl.TexCoord2i(int(v[i]), int(v[i+1]))
case []float32:
gl.TexCoord2f(v[i], v[i+1])
case []float64:
gl.TexCoord2d(v[i], v[i+1])
}
case 3:
switch v := a.data.(type) {
case []int16:
gl.TexCoord3s(v[i], v[i+1], v[i+2])
case []int32:
gl.TexCoord3i(int(v[i]), int(v[i+1]), int(v[i+2]))
case []float32:
gl.TexCoord3f(v[i], v[i+1], v[i+2])
case []float64:
gl.TexCoord3d(v[i], v[i+1], v[i+2])
}
case 4:
switch v := a.data.(type) {
case []int16:
gl.TexCoord4s(v[i], v[i+1], v[i+2], v[i+3])
case []int32:
gl.TexCoord4i(int(v[i]), int(v[i+1]), int(v[i+2]), int(v[i+3]))
case []float32:
gl.TexCoord4f(v[i], v[i+1], v[i+2], v[i+3])
case []float64:
gl.TexCoord4d(v[i], v[i+1], v[i+2], v[i+3])
}
}
}
// normal defines surface normals.
// Used in classic render mode.
func (a *Attr) normal(i int) {
if a.size != 3 {
return
}
i *= a.size
switch v := a.data.(type) {
case []int8:
gl.Normal3b(v[i], v[i+1], v[i+2])
case []int16:
gl.Normal3s(v[i], v[i+1], v[i+2])
case []int32:
gl.Normal3i(int(v[i]), int(v[i+1]), int(v[i+2]))
case []float32:
gl.Normal3f(v[i], v[i+1], v[i+2])
case []float64:
gl.Normal3d(v[i], v[i+1], v[i+2])
}
}
// Used in classic render mode.
// Defines vertex colors.
func (a *Attr) color(i int) {
i *= a.size
switch a.size {
case 3:
switch v := a.data.(type) {
case []int8:
gl.Color3b(v[i], v[i+1], v[i+2])
case []uint8:
gl.Color3ub(v[i], v[i+1], v[i+2])
case []int16:
gl.Color3s(v[i], v[i+1], v[i+2])
case []int32:
gl.Color3i(int(v[i]), int(v[i+1]), int(v[i+2]))
case []float32:
gl.Color3f(v[i], v[i+1], v[i+2])
case []float64:
gl.Color3d(v[i], v[i+1], v[i+2])
}
case 4:
switch v := a.data.(type) {
case []int8:
gl.Color4b(v[i], v[i+1], v[i+2], v[i+3])
case []uint8:
gl.Color4ub(v[i], v[i+1], v[i+2], v[i+3])
case []int16:
gl.Color4s(v[i], v[i+1], v[i+2], v[i+3])
case []int32:
gl.Color4i(int(v[i]), int(v[i+1]), int(v[i+2]), int(v[i+3]))
case []float32:
gl.Color4f(v[i], v[i+1], v[i+2], v[i+3])
case []float64:
gl.Color4d(v[i], v[i+1], v[i+2], v[i+3])
}
}
}
// index returns the index at the given offset.
// Used in classic render mode.
func (a *Attr) index(offset int) int {
switch v := a.data.(type) {
case []int8:
return int(v[offset*a.size])
case []uint8:
return int(v[offset*a.size])
case []int16:
return int(v[offset*a.size])
case []uint16:
return int(v[offset*a.size])
case []int32:
return int(v[offset*a.size])
case []uint32:
return int(v[offset*a.size])
case []float32:
return int(v[offset*a.size])
case []float64:
return int(v[offset*a.size])
}
return 0
}