-
Notifications
You must be signed in to change notification settings - Fork 7
/
BackgroundGrid.go
142 lines (112 loc) · 3.85 KB
/
BackgroundGrid.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
package levels
import (
"fmt"
"github.com/inkyblackness/hacked/editor/render"
"github.com/inkyblackness/hacked/ui/opengl"
)
var gridVertexShaderSource = `
#version 150
precision mediump float;
in vec3 vertexPosition;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
out vec4 gridColor;
out vec3 originalPosition;
void main(void) {
gl_Position = projectionMatrix * viewMatrix * vec4(vertexPosition, 1.0);
gridColor = vec4(0.0, 0.1, 0.0, 0.6);
originalPosition = vertexPosition;
}
`
var gridFragmentShaderSource = `
#version 150
precision mediump float;
in vec4 gridColor;
in vec3 originalPosition;
out vec4 fragColor;
float modulo(float x, float y) {
return x - y * floor(x/y);
}
float nearGrid(float stepSize, float value) {
float remainder = modulo(value - (stepSize / 2.0), stepSize) * 2.0;
if (remainder >= stepSize) {
remainder = (stepSize * 2.0) - remainder;
}
return remainder / stepSize;
}
void main(void) {
float alphaX = nearGrid(256.0, originalPosition.x);
float alphaY = nearGrid(256.0, originalPosition.y);
bool beyondX = (originalPosition.x / 256.0) >= 64.0 || (originalPosition.x < 0.0);
bool beyondY = (originalPosition.y / 256.0) >= 64.0 || (originalPosition.y < 0.0);
float alpha = 0.0;
if (!beyondX && !beyondY) {
alpha = max(alphaX, alphaY);
} else if (beyondX && !beyondY) {
alpha = alphaX;
} else if (beyondY && !beyondX) {
alpha = alphaY;
} else {
alpha = min(alphaX, alphaY);
}
alpha = pow(2.0, 10.0 * (alpha - 1.0));
fragColor = vec4(gridColor.rgb, gridColor.a * alpha);
}
`
// BackgroundGrid renders a grid with transparent holes.
type BackgroundGrid struct {
context *render.Context
program uint32
vao *opengl.VertexArrayObject
vertexPositionBuffer uint32
vertexPositionAttrib int32
viewMatrixUniform opengl.Matrix4Uniform
projectionMatrixUniform opengl.Matrix4Uniform
}
// NewBackgroundGrid returns a new instance of BackgroundGrid.
func NewBackgroundGrid(context *render.Context) *BackgroundGrid {
gl := context.OpenGL
program, programErr := opengl.LinkNewStandardProgram(gl, gridVertexShaderSource, gridFragmentShaderSource)
if programErr != nil {
panic(fmt.Errorf("BackgroundGrid shader failed: %v", programErr))
}
grid := &BackgroundGrid{
context: context,
program: program,
vao: opengl.NewVertexArrayObject(gl, program),
vertexPositionBuffer: gl.GenBuffers(1)[0],
vertexPositionAttrib: gl.GetAttribLocation(program, "vertexPosition"),
viewMatrixUniform: opengl.Matrix4Uniform(gl.GetUniformLocation(program, "viewMatrix")),
projectionMatrixUniform: opengl.Matrix4Uniform(gl.GetUniformLocation(program, "projectionMatrix"))}
{
tilesPerMapSide := float32(64.0) // TODO
gl.BindBuffer(opengl.ARRAY_BUFFER, grid.vertexPositionBuffer)
half := fineCoordinatesPerTileSide / float32(2.0)
limit := fineCoordinatesPerTileSide*tilesPerMapSide + half
var vertices = []float32{
-half, -half, 0.0,
limit, -half, 0.0,
limit, limit, 0.0,
limit, limit, 0.0,
-half, limit, 0.0,
-half, -half, 0.0}
gl.BufferData(opengl.ARRAY_BUFFER, len(vertices)*4, vertices, opengl.STATIC_DRAW)
gl.BindBuffer(opengl.ARRAY_BUFFER, 0)
}
grid.vao.WithSetter(func(gl opengl.OpenGL) {
gl.EnableVertexAttribArray(uint32(grid.vertexPositionAttrib))
gl.BindBuffer(opengl.ARRAY_BUFFER, grid.vertexPositionBuffer)
gl.VertexAttribOffset(uint32(grid.vertexPositionAttrib), 3, opengl.FLOAT, false, 0, 0)
gl.BindBuffer(opengl.ARRAY_BUFFER, 0)
})
return grid
}
// Render renders
func (grid *BackgroundGrid) Render() {
gl := grid.context.OpenGL
grid.vao.OnShader(func() {
grid.viewMatrixUniform.Set(gl, grid.context.ViewMatrix)
grid.projectionMatrixUniform.Set(gl, &grid.context.ProjectionMatrix)
gl.DrawArrays(opengl.TRIANGLES, 0, 6)
})
}