This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
/
vbo.go
77 lines (62 loc) · 1.54 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
package goga
import (
"github.com/go-gl/gl/v3.2-core/gl"
"unsafe"
)
// Vertex Buffer Object.
type VBO struct {
id uint32
target uint32
size int32
}
// Creates a new VBO with given target.
func NewVBO(target uint32) *VBO {
vbo := &VBO{target: target}
gl.GenBuffers(1, &vbo.id)
return vbo
}
// Drops this VBO.
func (v *VBO) Drop() {
gl.DeleteBuffers(1, &v.id)
}
// Binds VBO for rendering.
func (v *VBO) Bind() {
gl.BindBuffer(v.target, v.id)
}
// Unbinds.
func (v *VBO) Unbind() {
gl.BindBuffer(v.target, 0)
}
// Fills VBO with data.
// An unsafe pointer must be used out of Gos unsafe package.
func (v *VBO) Fill(data unsafe.Pointer, elements, size int, use uint32) {
v.size = int32(size)
v.Bind()
gl.BufferData(v.target, elements*size, data, use)
v.Unbind()
}
// Updates data or part of data.
// An unsafe pointer must be used out of Gos unsafe package.
func (v *VBO) Update(data unsafe.Pointer, elements, offset, size int) {
v.size = int32(size)
v.Bind()
gl.BufferSubData(v.target, offset, elements*size, data)
v.Unbind()
}
// Sets the attribute pointer for rendering.
// Used together with shader.
func (v *VBO) AttribPointer(attribLocation int32, size int32, btype uint32, normalized bool, stride int32) {
gl.VertexAttribPointer(uint32(attribLocation), size, btype, normalized, stride, nil)
}
// Returns the GL ID.
func (v *VBO) GetId() uint32 {
return v.id
}
// Returns the target.
func (v *VBO) GetTarget() uint32 {
return v.target
}
// Returns the number of elements within this VBO.
func (v *VBO) Size() int32 {
return v.size
}