forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vertex.go
104 lines (91 loc) · 2.37 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
package canvas3d
import (
"github.com/gorustyt/fyne/v2"
"github.com/gorustyt/fyne/v2/canvas3d/context"
"github.com/gorustyt/fyne/v2/internal/painter/gl"
)
var (
_ gl.Canvas3D = (*VertexFloat32Array)(nil)
_ gl.Canvas3DPainter = (*VertexFloat32Array)(nil)
)
type VertexFloat32Array struct {
Arr []float32
Index []uint32
PositionSize []int //大小和偏移
NormalSize []int
TexCoordSize []int
ColorSize []int
vbo, veo context.Buffer
}
func (v *VertexFloat32Array) NeedShader() bool {
return true
}
func (v *VertexFloat32Array) InitOnce(p *gl.Painter3D) {
}
func NewVertexFloat32Array() *VertexFloat32Array {
return &VertexFloat32Array{}
}
func (v *VertexFloat32Array) Init(p *gl.Painter3D) {
}
func (v *VertexFloat32Array) Draw(p *gl.Painter3D, pos fyne.Position, frame fyne.Size) {
points := v.AddOffset(v.Arr, pos, frame)
if len(v.Index) > 0 {
v.vbo, v.veo = p.MakeVaoWithEbo(points, v.Index)
} else {
v.vbo = p.MakeVao(points)
}
stride := v.getStride()
if len(v.PositionSize) == 2 {
p.DefineVertexArray("position", v.PositionSize[0], stride, v.PositionSize[1])
}
if len(v.NormalSize) == 2 {
p.DefineVertexArray("normal", v.NormalSize[0], stride, v.NormalSize[1])
}
if len(v.TexCoordSize) == 2 {
p.DefineVertexArray("texCoord", v.TexCoordSize[0], stride, v.TexCoordSize[1])
}
if len(v.ColorSize) == 2 {
p.DefineVertexArray("color", v.ColorSize[0], stride, v.ColorSize[1])
}
if len(v.Index) != 0 {
p.DrawTrianglesByElement(v.Index)
} else {
p.DrawTriangles(len(points) / stride)
}
}
func (v *VertexFloat32Array) getStride() (stride int) {
if len(v.PositionSize) == 2 {
stride += v.PositionSize[0]
}
if len(v.NormalSize) == 2 {
stride += v.NormalSize[0]
}
if len(v.TexCoordSize) == 2 {
stride += v.TexCoordSize[0]
}
if len(v.ColorSize) == 2 {
stride += v.ColorSize[0]
}
return
}
func (v *VertexFloat32Array) AddOffset(points []float32, pos fyne.Position, size fyne.Size) (res []float32) {
stride := v.getStride()
offsetX := pos.X / size.Width
offsetY := pos.Y / size.Height
res = make([]float32, len(points))
copy(res, points)
for i := 0; i < len(points); i += stride {
begin := i + v.PositionSize[1]
res[begin] += offsetX
res[begin+1] += offsetY
}
return res
}
func (v *VertexFloat32Array) After(p *gl.Painter3D) {
if v.vbo != 0 {
p.DeleteBuffer(v.vbo)
}
if v.veo != 0 {
p.DeleteBuffer(v.veo)
}
}