forked from drgomesp/midgarts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vbo.go
107 lines (88 loc) · 2.02 KB
/
vbo.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
package opengl
import (
"unsafe"
"github.com/go-gl/gl/v3.2-core/gl"
)
type VBOAttributeType int
const (
VertexPosition = VBOAttributeType(iota)
VertexColor
VertexTexCoord
NumVertexAttributes
)
var attribTypeNames = map[VBOAttributeType]string{
VertexPosition: "VertexPosition",
VertexColor: "VertexColor",
VertexTexCoord: "VertexTexCoord",
}
var attributeTypeSizes = map[VBOAttributeType]int{
VertexPosition: 3,
VertexColor: 3,
VertexTexCoord: 2,
}
type VBO struct {
gls *State
initialized bool
buffers [NumVertexAttributes][]float32
usage uint32
attributes []VBOAttribute
handles [NumVertexAttributes]uint32
shouldUpdate bool
buffAllocated bool
}
type VBOAttribute struct {
Type VBOAttributeType
Name string
NumElements int32
ByteOffset uint32
ElementType uint32
}
func NewVBO(buffers [NumVertexAttributes][]float32) *VBO {
vbo := &VBO{
gls: nil,
buffers: buffers,
usage: gl.STATIC_DRAW,
attributes: make([]VBOAttribute, 0),
shouldUpdate: true,
}
return vbo
}
func (vbo *VBO) AddAttribute(t VBOAttributeType) *VBO {
vbo.attributes = append(vbo.attributes, VBOAttribute{
Type: t,
Name: attribTypeNames[t],
NumElements: int32(attributeTypeSizes[t]),
ByteOffset: 0,
ElementType: gl.FLOAT,
})
return vbo
}
func (vbo *VBO) Load(gls *State) {
if len(vbo.attributes) == 0 {
return
}
if !vbo.buffAllocated {
for loc := range vbo.attributes {
gl.GenBuffers(int32(NumVertexAttributes), &vbo.handles[loc])
}
vbo.buffAllocated = true
}
for loc, attrib := range vbo.attributes {
gl.BindBuffer(gl.ARRAY_BUFFER, vbo.handles[loc])
gl.BufferData(gl.ARRAY_BUFFER, len(vbo.buffers[loc])*4, gl.Ptr(vbo.buffers[loc]), vbo.usage)
gl.EnableVertexAttribArray(uint32(loc))
gl.VertexAttribPointer(
uint32(loc),
attrib.NumElements,
attrib.ElementType,
false,
0,
unsafe.Pointer(uintptr(attrib.ByteOffset)),
)
}
vbo.gls = gls
if !vbo.shouldUpdate {
return
}
vbo.shouldUpdate = false
}