/
particle.go
158 lines (128 loc) · 3.46 KB
/
particle.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package gameblocks
import (
"bytes"
"encoding/binary"
"math/rand"
"time"
mgl "github.com/go-gl/mathgl/mgl32"
"golang.org/x/mobile/gl"
)
// TODO: ...
// Ref: https://github.com/krux02/turnt-octo-wallhack/blob/master/particles/ParticleSystem.go
type Emitter interface {
Shape
Tick(time.Duration)
MoveTo(mgl.Vec3)
}
func RandomParticle(origin mgl.Vec3, force float32) *particle {
return &particle{
position: origin,
velocity: mgl.Vec3{
(0.5 - rand.Float32()) * force,
rand.Float32() * force,
(0.5 - rand.Float32()) * force,
},
}
}
const particleLen = 9 * 3
type particle struct {
position mgl.Vec3
velocity mgl.Vec3
}
func (p *particle) Vertices() []float32 {
v := p.velocity.Normalize()
return []float32{
p.position[0] + v[0]*0.1,
p.position[1] + v[1]*0.1,
p.position[2] + v[2]*0.1,
p.position[0] - v[0]*0.05,
p.position[1] - v[1]*0.05,
p.position[2] + v[2]*0.05,
p.position[0] + v[0]*0.05,
p.position[1] - v[1]*0.05,
p.position[2] + v[2]*0.05,
}
}
func (p *particle) Tick(force mgl.Vec3) {
p.velocity = force.Add(p.velocity)
p.position = p.position.Add(p.velocity)
}
var particleForce float32 = 0.09
var gravityForce = mgl.Vec3{0, -0.2, 0}
func ParticleEmitter(glctx gl.Context, origin mgl.Vec3, num int, rate float32) Emitter {
bufSize := num * particleLen * vecSize
vbo := glctx.CreateBuffer()
glctx.BindBuffer(gl.ARRAY_BUFFER, vbo)
glctx.BufferInit(gl.ARRAY_BUFFER, bufSize, gl.DYNAMIC_DRAW)
return &particleEmitter{
glctx: glctx,
VBO: vbo,
origin: origin,
rate: rate,
particles: make([]*particle, 0, num),
num: num,
}
}
type particleEmitter struct {
glctx gl.Context
VBO gl.Buffer
origin mgl.Vec3
rate float32
particles []*particle
num int
}
func (emitter *particleEmitter) MoveTo(pos mgl.Vec3) {
emitter.origin = pos
}
func (emitter *particleEmitter) Tick(interval time.Duration) {
// Randomize emitting
t := float32(interval.Seconds())
n := int(emitter.rate * t * rand.Float32())
extra := len(emitter.particles) + 1 + n - emitter.num
if extra > 0 {
// Pop oldest particles
emitter.particles = emitter.particles[extra:]
}
for i := 0; i <= n; i++ {
p := RandomParticle(emitter.origin, particleForce)
emitter.particles = append(emitter.particles, p)
}
f := gravityForce.Mul(t)
for _, particle := range emitter.particles {
particle.Tick(f)
}
emitter.Buffer()
}
func (emitter *particleEmitter) Buffer() {
data := emitter.Bytes()
if len(data) > 0 {
emitter.glctx.BindBuffer(gl.ARRAY_BUFFER, emitter.VBO)
emitter.glctx.BufferSubData(gl.ARRAY_BUFFER, 0, data)
}
}
func (emitter *particleEmitter) Len() int {
return len(emitter.particles)
}
func (emitter *particleEmitter) Bytes() []byte {
buf := bytes.Buffer{}
for _, particle := range emitter.particles {
binary.Write(&buf, binary.LittleEndian, particle.Vertices())
}
return buf.Bytes()
}
func (emitter *particleEmitter) Stride() int {
return vecSize * vertexDim
}
func (emitter *particleEmitter) Draw(ctx DrawContext) {
shader := ctx.Shader
glctx := ctx.GL
glctx.BindBuffer(gl.ARRAY_BUFFER, emitter.VBO)
glctx.EnableVertexAttribArray(shader.Attrib("vertCoord"))
glctx.VertexAttribPointer(shader.Attrib("vertCoord"), vertexDim, gl.FLOAT, false, emitter.Stride(), 0)
glctx.DrawArrays(gl.TRIANGLES, 0, emitter.Len()*particleLen/vertexDim)
glctx.DisableVertexAttribArray(shader.Attrib("vertCoord"))
}
func (emitter *particleEmitter) Close() error {
emitter.glctx.DeleteBuffer(emitter.VBO)
return nil
}