-
Notifications
You must be signed in to change notification settings - Fork 8
/
mesh.go
50 lines (41 loc) · 1.28 KB
/
mesh.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
package meshview
import (
"github.com/fogleman/fauxgl"
"github.com/go-gl/gl/v2.1/gl"
)
type MeshData struct {
Buffer []float32
Box fauxgl.Box
}
type Mesh struct {
Transform fauxgl.Matrix
VertexBuffer uint32
VertexCount int32
}
func NewMesh(data *MeshData) *Mesh {
// compute transform to scale and center mesh
scale := fauxgl.V(2, 2, 2).Div(data.Box.Size()).MinComponent()
transform := fauxgl.Identity()
transform = transform.Translate(data.Box.Center().Negate())
transform = transform.Scale(fauxgl.V(scale, scale, scale))
// generate vbo
var vbo uint32
gl.GenBuffers(1, &vbo)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
gl.BufferData(gl.ARRAY_BUFFER, len(data.Buffer)*4, gl.Ptr(data.Buffer), gl.STATIC_DRAW)
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
// compute number of vertices
count := int32(len(data.Buffer) / 3)
return &Mesh{transform, vbo, count}
}
func (mesh *Mesh) Draw(positionAttrib uint32) {
gl.BindBuffer(gl.ARRAY_BUFFER, mesh.VertexBuffer)
gl.EnableVertexAttribArray(positionAttrib)
gl.VertexAttribPointer(positionAttrib, 3, gl.FLOAT, false, 12, gl.PtrOffset(0))
gl.DrawArrays(gl.TRIANGLES, 0, mesh.VertexCount)
gl.DisableVertexAttribArray(positionAttrib)
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
}
func (mesh *Mesh) Destroy() {
gl.DeleteBuffers(1, &mesh.VertexBuffer)
}