/
box.go
131 lines (108 loc) · 3.79 KB
/
box.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
package shapes
import (
"image"
"github.com/remogatto/mathgl"
gl "github.com/remogatto/opengles2"
"github.com/remogatto/shaders"
)
var (
// DefaultBoxVS is a default vertex shader for boxes.
DefaultBoxVS = (shaders.VertexShader)(
`
precision mediump float;
attribute vec4 pos;
attribute vec4 color;
attribute vec2 texIn;
varying vec2 texOut;
varying vec4 vColor;
uniform mat4 model;
uniform mat4 projection;
uniform mat4 view;
void main() {
gl_Position = projection*model*view*pos;
vColor = color;
texOut = texIn;
}`)
// DefaultBoxVS is a default fragment shader for boxes.
DefaultBoxFS = (shaders.FragmentShader)(
`
precision mediump float;
varying vec4 vColor;
varying vec2 texOut;
uniform sampler2D texture;
uniform float texRatio;
void main() {
vec2 flippedTexCoords = vec2(texOut.x, 1.0 - texOut.y);
vec4 texColor = texture2D(texture, flippedTexCoords) * texRatio;
vec4 vertColor = vColor * (1.0 - texRatio);
gl_FragColor = texColor + vertColor;
}`)
)
// Box represents a box shape.
type Box struct {
Base
}
// NewBox creates a new box of given sizes. It takes as arguments a
// linked program and width and height values.
func NewBox(program shaders.Program, width, height float32) *Box {
box := new(Box)
// The box is built around its center at (0, 0)
box.vertices = []float32{
-width / 2, -height / 2,
width / 2, -height / 2,
-width / 2, height / 2,
width / 2, height / 2,
}
// Set the default color
box.SetColor(DefaultColor)
box.program = program
box.program.Use()
// Get variables IDs from shaders
box.posId = program.GetAttribute("pos")
box.colorId = program.GetAttribute("color")
box.projMatrixId = program.GetUniform("projection")
box.modelMatrixId = program.GetUniform("model")
box.viewMatrixId = program.GetUniform("view")
box.texInId = program.GetAttribute("texIn")
box.textureId = program.GetUniform("texture")
box.texRatioId = program.GetUniform("texRatio")
// Fill the model matrix with the identity.
box.modelMatrix = mathgl.Ident4f()
// Create the bounding rectangle for the shape.
box.bounds = image.Rect(
int(-width/2), int(-height/2),
int(width/2), int(height/2),
)
return box
}
// Draw actually renders the shape on the surface.
func (box *Box) Draw() {
box.program.Use()
gl.VertexAttribPointer(box.posId, 2, gl.FLOAT, false, 0, &box.vertices[0])
gl.EnableVertexAttribArray(box.posId)
gl.VertexAttribPointer(box.colorId, 4, gl.FLOAT, false, 0, &box.vColor[0])
gl.EnableVertexAttribArray(box.colorId)
gl.UniformMatrix4fv(int32(box.modelMatrixId), 1, false, (*float32)(&box.modelMatrix[0]))
gl.UniformMatrix4fv(int32(box.projMatrixId), 1, false, (*float32)(&box.projMatrix[0]))
gl.UniformMatrix4fv(int32(box.viewMatrixId), 1, false, (*float32)(&box.viewMatrix[0]))
gl.Uniform1f(int32(box.texRatioId), 0.0)
// Texture
if len(box.texCoords) > 0 {
gl.Uniform1f(int32(box.texRatioId), 1.0)
gl.VertexAttribPointer(box.texInId, 2, gl.FLOAT, false, 0, &box.texCoords[0])
gl.EnableVertexAttribArray(box.texInId)
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, box.texBuffer)
gl.Uniform1i(int32(box.textureId), 0)
}
gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4)
gl.Flush()
gl.Finish()
}
// Clone makes a copy of the shape.
func (box *Box) Clone() Shape {
b := NewBox(box.program, float32(box.bounds.Dx()), float32(box.bounds.Dy()))
b.SetColor(box.color)
b.SetTexture(box.texBuffer, box.texCoords)
return b
}