-
Notifications
You must be signed in to change notification settings - Fork 7
/
MapColors.go
179 lines (145 loc) · 5.63 KB
/
MapColors.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package levels
import (
mgl "github.com/go-gl/mathgl/mgl32"
"github.com/inkyblackness/hacked/editor/render"
"github.com/inkyblackness/hacked/ss1/content/archive/level"
"github.com/inkyblackness/hacked/ui/opengl"
)
var mapColorsVertexShaderSource = `
#version 150
precision mediump float;
in vec4 vertexColor;
in vec2 vertexPosition;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
out vec4 color;
void main(void) {
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(vertexPosition.xy, 0.0, 1.0);
color = vertexColor;
}
`
var mapColorsFragmentShaderSource = `
#version 150
precision mediump float;
in vec4 color;
out vec4 fragColor;
void main(void) {
fragColor = color;
}
`
// ColorQuery returns the color for the specified tile.
type ColorQuery func(level.TilePosition) [4]float32
// MapColors is a renderable for the tile colorings.
type MapColors struct {
context *render.Context
program uint32
vao *opengl.VertexArrayObject
vertexPositionBuffer uint32
vertexPositionAttrib int32
vertexColorBuffer uint32
vertexColorAttrib int32
modelMatrixUniform opengl.Matrix4Uniform
viewMatrixUniform opengl.Matrix4Uniform
projectionMatrixUniform opengl.Matrix4Uniform
lastColorGrid [][][4]float32
lastColorGridColumns int
lastColorGridRows int
}
// NewMapColors returns a new instance of a renderable for tile colorings.
func NewMapColors(context *render.Context) *MapColors {
gl := context.OpenGL
program, programErr := opengl.LinkNewStandardProgram(gl, mapColorsVertexShaderSource, mapColorsFragmentShaderSource)
if programErr != nil {
panic(opengl.NamedShaderError{Name: "MapColorsShader", Nested: programErr})
}
renderable := &MapColors{
context: context,
program: program,
vao: opengl.NewVertexArrayObject(gl, program),
vertexPositionBuffer: gl.GenBuffers(1)[0],
vertexPositionAttrib: gl.GetAttribLocation(program, "vertexPosition"),
vertexColorBuffer: gl.GenBuffers(1)[0],
vertexColorAttrib: gl.GetAttribLocation(program, "vertexColor"),
modelMatrixUniform: opengl.Matrix4Uniform(gl.GetUniformLocation(program, "modelMatrix")),
viewMatrixUniform: opengl.Matrix4Uniform(gl.GetUniformLocation(program, "viewMatrix")),
projectionMatrixUniform: opengl.Matrix4Uniform(gl.GetUniformLocation(program, "projectionMatrix")),
}
{
top := float32(level.FineCoordinatesPerTileSide)
left := float32(0.0)
right := float32(level.FineCoordinatesPerTileSide)
bottom := float32(0.0)
vertices := []float32{
left, bottom,
left, top,
right, top,
right, top,
right, bottom,
left, bottom}
gl.BindBuffer(opengl.ARRAY_BUFFER, renderable.vertexPositionBuffer)
gl.BufferData(opengl.ARRAY_BUFFER, len(vertices)*4, vertices, opengl.STATIC_DRAW)
gl.BindBuffer(opengl.ARRAY_BUFFER, 0)
}
renderable.vao.WithSetter(func(gl opengl.OpenGL) {
gl.EnableVertexAttribArray(uint32(renderable.vertexPositionAttrib))
gl.EnableVertexAttribArray(uint32(renderable.vertexColorAttrib))
gl.BindBuffer(opengl.ARRAY_BUFFER, renderable.vertexPositionBuffer)
gl.VertexAttribOffset(uint32(renderable.vertexPositionAttrib), 2, opengl.FLOAT, false, 0, 0)
gl.BindBuffer(opengl.ARRAY_BUFFER, renderable.vertexColorBuffer)
gl.VertexAttribOffset(uint32(renderable.vertexColorAttrib), 4, opengl.FLOAT, false, 0, 0)
gl.BindBuffer(opengl.ARRAY_BUFFER, 0)
})
return renderable
}
// Dispose releases any internal resources.
func (renderable *MapColors) Dispose() {
gl := renderable.context.OpenGL
gl.DeleteProgram(renderable.program)
gl.DeleteBuffers([]uint32{renderable.vertexPositionBuffer})
renderable.vao.Dispose()
}
// Render renders the renderable.
func (renderable *MapColors) Render(columnCount, rowCount int, query ColorQuery) {
gl := renderable.context.OpenGL
if (renderable.lastColorGridColumns != columnCount) || (renderable.lastColorGridRows != rowCount) {
renderable.lastColorGrid = make([][][4]float32, rowCount+1)
for y := 0; y < rowCount; y++ {
renderable.lastColorGrid[y] = make([][4]float32, columnCount+1)
}
renderable.lastColorGrid[rowCount] = make([][4]float32, columnCount+1)
renderable.lastColorGridColumns = columnCount
renderable.lastColorGridRows = rowCount
}
for y, row := range renderable.lastColorGrid {
for x := 0; x < columnCount; x++ {
row[x] = query(level.TilePosition{X: byte(x), Y: byte(y)})
}
}
renderable.vao.OnShader(func() {
var colors [24]float32
renderable.viewMatrixUniform.Set(gl, renderable.context.ViewMatrix)
renderable.projectionMatrixUniform.Set(gl, &renderable.context.ProjectionMatrix)
gl.BindBuffer(opengl.ARRAY_BUFFER, renderable.vertexColorBuffer)
for y := 0; y < rowCount; y++ {
for x := 0; x < columnCount; x++ {
colorBottomLeft := renderable.lastColorGrid[y][x]
colorBottomRight := renderable.lastColorGrid[y][x+1]
colorTopLeft := renderable.lastColorGrid[y+1][x]
colorTopRight := renderable.lastColorGrid[y+1][x+1]
modelMatrix := mgl.Ident4().
Mul4(mgl.Translate3D((float32(x))*level.FineCoordinatesPerTileSide, (float32(y))*level.FineCoordinatesPerTileSide, 0.0))
renderable.modelMatrixUniform.Set(gl, &modelMatrix)
copy(colors[0:4], colorBottomLeft[:])
copy(colors[4:8], colorTopLeft[:])
copy(colors[8:12], colorTopRight[:])
copy(colors[12:16], colorTopRight[:])
copy(colors[16:20], colorBottomRight[:])
copy(colors[20:24], colorBottomLeft[:])
gl.BufferData(opengl.ARRAY_BUFFER, len(colors)*4, colors[:], opengl.STATIC_DRAW)
gl.DrawArrays(opengl.TRIANGLES, 0, 6)
}
}
gl.BindBuffer(opengl.ARRAY_BUFFER, 0)
})
}