-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcharactershadow.go
132 lines (104 loc) · 3.33 KB
/
charactershadow.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
package main
import (
"encoding/binary"
"fmt"
"math"
"github.com/go-gl/mathgl/mgl32"
"github.com/goxjs/gl"
"golang.org/x/mobile/exp/f32"
)
type characterShadow struct {
vertexCount int
program gl.Program
pMatrixUniform gl.Uniform
mvMatrixUniform gl.Uniform
vertexPositionAttribute gl.Attrib
vertexAlphaAttribute gl.Attrib
vertexPositionBuffer gl.Buffer
vertexAlphaBuffer gl.Buffer
}
func newCharacterShadow() (*characterShadow, error) {
l := new(characterShadow)
const (
vertexSource = `//#version 120 // OpenGL 2.1.
//#version 100 // WebGL.
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
attribute vec3 aVertexPosition;
attribute float aVertexAlpha;
varying float vPixelAlpha;
void main() {
vPixelAlpha = aVertexAlpha;
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
}
`
fragmentSource = `//#version 120 // OpenGL 2.1.
//#version 100 // WebGL.
#ifdef GL_ES
precision lowp float;
#endif
varying float vPixelAlpha;
void main() {
gl_FragColor = vec4(0, 0, 0, vPixelAlpha);
}
`
)
var err error
l.program, err = createValidateUseProgram(vertexSource, fragmentSource)
if err != nil {
return nil, err
}
l.pMatrixUniform = gl.GetUniformLocation(l.program, "uPMatrix")
l.mvMatrixUniform = gl.GetUniformLocation(l.program, "uMVMatrix")
l.vertexPositionAttribute = gl.GetAttribLocation(l.program, "aVertexPosition")
l.vertexAlphaAttribute = gl.GetAttribLocation(l.program, "aVertexAlpha")
positionData := append([]float32{0, 0}, circle(mgl32.Vec2{}, mgl32.Vec2{16, 16}.Mul(1.75), 16)...)
l.vertexCount = len(positionData) / 2
l.vertexPositionBuffer = createVbo3Float(positionData)
alphaData := make([]uint8, l.vertexCount)
alphaData[0] = 76 // First vertex is the circle center, it has non-zero alpha. The rest are zero.
l.vertexAlphaBuffer = createVbo3Ubyte(alphaData)
if err := gl.GetError(); err != 0 {
return nil, fmt.Errorf("gl.GetError: %v", err)
}
return l, nil
}
func (l *characterShadow) setup() {
gl.UseProgram(l.program)
gl.BindBuffer(gl.ARRAY_BUFFER, l.vertexPositionBuffer)
gl.EnableVertexAttribArray(l.vertexPositionAttribute)
gl.VertexAttribPointer(l.vertexPositionAttribute, 2, gl.FLOAT, false, 0, 0)
gl.BindBuffer(gl.ARRAY_BUFFER, l.vertexAlphaBuffer)
gl.EnableVertexAttribArray(l.vertexAlphaAttribute)
gl.VertexAttribPointer(l.vertexAlphaAttribute, 1, gl.UNSIGNED_BYTE, true, 0, 0)
gl.Enable(gl.BLEND)
}
func (l *characterShadow) cleanup() {
gl.Disable(gl.BLEND)
gl.DisableVertexAttribArray(l.vertexPositionAttribute)
gl.DisableVertexAttribArray(l.vertexAlphaAttribute)
}
func (l *characterShadow) render() {
gl.DrawArrays(gl.TRIANGLE_FAN, 0, l.vertexCount)
}
func circle(pos mgl32.Vec2, size mgl32.Vec2, slices int) (vertices []float32) {
x := float64(slices)
for i := slices; i >= 0; i-- {
vertices = append(vertices,
pos[0]+float32(math.Sin(Tau*float64(i)/x))*size[0]/2,
pos[1]+float32(math.Cos(Tau*float64(i)/x))*size[1]/2)
}
return vertices
}
func createVbo3Float(data []float32) gl.Buffer {
vbo := gl.CreateBuffer()
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
gl.BufferData(gl.ARRAY_BUFFER, f32.Bytes(binary.LittleEndian, data...), gl.STATIC_DRAW)
return vbo
}
func createVbo3Ubyte(data []uint8) gl.Buffer {
vbo := gl.CreateBuffer()
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
gl.BufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW)
return vbo
}