/
shape.go
136 lines (112 loc) · 3.19 KB
/
shape.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
package main
import "golang.org/x/mobile/gl"
const vertexDim = 3
const textureDim = 2
const normalDim = 3
const vecSize = 4
type Shape interface {
Close() error
Stride() int
Len() int
Draw(Shader, Camera)
}
func NewStaticShape() *StaticShape {
shape := &StaticShape{}
shape.VBO = gl.CreateBuffer()
shape.IBO = gl.CreateBuffer()
return shape
}
type StaticShape struct {
VBO gl.Buffer
IBO gl.Buffer
Texture gl.Texture
vertices []float32 // Vec3
textures []float32 // Vec2 (UV)
normals []float32 // Vec3
indices []uint8
}
func (s *StaticShape) Len() int {
return len(s.vertices) / vertexDim
}
func (shape *StaticShape) Stride() int {
r := vertexDim
if len(shape.textures) > 0 {
r += textureDim
}
if len(shape.normals) > 0 {
r += normalDim
}
return r * vecSize
}
func (shape *StaticShape) Bytes() []byte {
return shape.BytesOffset(0)
}
func (shape *StaticShape) Close() error {
gl.DeleteBuffer(shape.VBO)
gl.DeleteBuffer(shape.IBO)
return nil
}
func (shape *StaticShape) BytesOffset(n int) []byte {
objects := []DimSlicer{NewDimSlice(vertexDim, shape.vertices)}
if len(shape.textures) > 0 {
objects = append(objects, NewDimSlice(textureDim, shape.textures))
}
if len(shape.normals) > 0 {
objects = append(objects, NewDimSlice(normalDim, shape.normals))
}
length := len(shape.vertices) / vertexDim
return EncodeObjects(n, length, objects...)
}
func (shape *StaticShape) Draw(shader Shader, camera Camera) {
gl.BindBuffer(gl.ARRAY_BUFFER, shape.VBO)
stride := shape.Stride()
gl.EnableVertexAttribArray(shader.Attrib("vertCoord"))
gl.VertexAttribPointer(shader.Attrib("vertCoord"), vertexDim, gl.FLOAT, false, stride, 0)
if len(shape.normals) > 0 {
gl.EnableVertexAttribArray(shader.Attrib("vertNormal"))
gl.VertexAttribPointer(shader.Attrib("vertNormal"), normalDim, gl.FLOAT, false, stride, vertexDim*vecSize)
}
// TODO: texture
if len(shape.indices) > 0 {
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, shape.IBO)
gl.DrawElements(gl.TRIANGLES, len(shape.indices), gl.UNSIGNED_BYTE, 0)
} else {
gl.DrawArrays(gl.TRIANGLES, 0, shape.Len())
}
gl.DisableVertexAttribArray(shader.Attrib("vertCoord"))
if len(shape.normals) > 0 {
gl.DisableVertexAttribArray(shader.Attrib("vertNormal"))
}
// TODO: texture
}
func (shape *StaticShape) Buffer() {
data := shape.Bytes()
if len(data) > 0 {
gl.BindBuffer(gl.ARRAY_BUFFER, shape.VBO)
gl.BufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW)
}
if len(shape.indices) > 0 {
data = EncodeObjects(0, len(shape.indices), NewDimSlice(1, shape.indices))
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, shape.IBO)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, data, gl.STATIC_DRAW)
}
}
func NewDynamicShape(bufSize int) *DynamicShape {
shape := &DynamicShape{}
shape.VBO = gl.CreateBuffer()
gl.BindBuffer(gl.ARRAY_BUFFER, shape.VBO)
gl.BufferInit(gl.ARRAY_BUFFER, bufSize, gl.DYNAMIC_DRAW)
return shape
}
type DynamicShape struct {
StaticShape
}
func (shape *DynamicShape) Buffer(offset int) {
gl.BindBuffer(gl.ARRAY_BUFFER, shape.VBO)
data := shape.BytesOffset(offset)
if len(data) == 0 {
return
}
gl.BufferSubData(gl.ARRAY_BUFFER, offset*shape.Stride(), data)
}
// TODO: Good render loop: http://www.java-gaming.org/index.php?topic=18710.0