-
Notifications
You must be signed in to change notification settings - Fork 7
/
MapTextures.go
255 lines (212 loc) · 7.78 KB
/
MapTextures.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package levels
import (
"math"
mgl "github.com/go-gl/mathgl/mgl32"
"github.com/inkyblackness/hacked/editor/graphics"
"github.com/inkyblackness/hacked/editor/render"
"github.com/inkyblackness/hacked/ss1/content/archive/level"
"github.com/inkyblackness/hacked/ui/opengl"
)
var mapTexturesVertexShaderSource = `
#version 150
precision mediump float;
in vec3 vertexPosition;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 uvMatrix;
out vec2 uv;
void main(void) {
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(vertexPosition, 1.0);
uv = (uvMatrix * vec4(vertexPosition, 1.0)).xy;
}
`
var mapTexturesFragmentShaderSource = `
#version 150
precision mediump float;
in vec2 uv;
uniform sampler2D palette;
uniform sampler2D bitmap;
out vec4 fragColor;
void main(void) {
vec4 pixel = texture(bitmap, uv);
fragColor = texture(palette, vec2(pixel.r, 0.5));
}
`
// TextureQuery is a getter function to retrieve the texture for the given
// level texture index.
type TextureQuery func(index level.TextureIndex) (*graphics.BitmapTexture, error)
// TileTextureQuery is a getter function to retrieve properties for rendering a texture of a tile.
type TileTextureQuery func(pos level.TilePosition) (tileType level.TileType, textureIndex level.TextureIndex, textureRotations int)
// MapTextures is a renderable for textures.
type MapTextures struct {
context *render.Context
program uint32
vao *opengl.VertexArrayObject
vertexPositionBuffer uint32
vertexPositionAttrib int32
modelMatrixUniform opengl.Matrix4Uniform
viewMatrixUniform opengl.Matrix4Uniform
projectionMatrixUniform opengl.Matrix4Uniform
uvMatrixUniform opengl.Matrix4Uniform
paletteUniform int32
bitmapUniform int32
textureQuery TextureQuery
lastTileType level.TileType
}
type tileTypeVertices struct {
vertices []float32
equivalentType level.TileType
}
var tileTypeVerticesLookup []tileTypeVertices
var uvRotations [4]*mgl.Mat4
func init() {
for i := 0; i < 4; i++ {
matrix := mgl.Translate3D(0.5, 0.5, 0.0).
Mul4(mgl.HomogRotate3DZ(math.Pi * float32(i) / -2.0)).
Mul4(mgl.Translate3D(-0.5, -0.5, 0.0)).
Mul4(mgl.Scale3D(1.0, -1.0, 1.0))
uvRotations[i] = &matrix
}
for _, tileType := range level.TileTypes() {
for int(tileType) >= len(tileTypeVerticesLookup) {
tileTypeVerticesLookup = append(tileTypeVerticesLookup, tileTypeVertices{equivalentType: level.TileTypeSolid})
}
switch tileType {
case level.TileTypeSolid:
case level.TileTypeDiagonalOpenNorthEast:
tileTypeVerticesLookup[int(tileType)] = tileTypeVertices{
vertices: []float32{
0.0, 1.0, 0.0,
1.0, 1.0, 0.0,
1.0, 0.0, 0.0,
},
equivalentType: level.TileTypeDiagonalOpenNorthEast,
}
case level.TileTypeDiagonalOpenNorthWest:
tileTypeVerticesLookup[int(tileType)] = tileTypeVertices{
vertices: []float32{
0.0, 1.0, 0.0,
1.0, 1.0, 0.0,
0.0, 0.0, 0.0,
},
equivalentType: level.TileTypeDiagonalOpenNorthWest,
}
case level.TileTypeDiagonalOpenSouthEast:
tileTypeVerticesLookup[int(tileType)] = tileTypeVertices{
vertices: []float32{
1.0, 1.0, 0.0,
1.0, 0.0, 0.0,
0.0, 0.0, 0.0,
},
equivalentType: level.TileTypeDiagonalOpenSouthEast,
}
case level.TileTypeDiagonalOpenSouthWest:
tileTypeVerticesLookup[int(tileType)] = tileTypeVertices{
vertices: []float32{
0.0, 1.0, 0.0,
1.0, 0.0, 0.0,
0.0, 0.0, 0.0,
},
equivalentType: level.TileTypeDiagonalOpenSouthWest,
}
default:
tileTypeVerticesLookup[int(tileType)] = tileTypeVertices{
vertices: []float32{
0.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 1.0, 0.0,
1.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 0.0,
},
equivalentType: level.TileTypeOpen,
}
}
}
}
// NewMapTextures returns a new instance of a renderable for tile map textures.
func NewMapTextures(context *render.Context, textureQuery TextureQuery) *MapTextures {
gl := context.OpenGL
program, programErr := opengl.LinkNewStandardProgram(gl, mapTexturesVertexShaderSource, mapTexturesFragmentShaderSource)
if programErr != nil {
panic(opengl.NamedShaderError{Name: "MapTexturesShader", Nested: programErr})
}
renderable := &MapTextures{
context: context,
program: program,
vao: opengl.NewVertexArrayObject(gl, program),
vertexPositionBuffer: gl.GenBuffers(1)[0],
vertexPositionAttrib: gl.GetAttribLocation(program, "vertexPosition"),
modelMatrixUniform: opengl.Matrix4Uniform(gl.GetUniformLocation(program, "modelMatrix")),
viewMatrixUniform: opengl.Matrix4Uniform(gl.GetUniformLocation(program, "viewMatrix")),
projectionMatrixUniform: opengl.Matrix4Uniform(gl.GetUniformLocation(program, "projectionMatrix")),
uvMatrixUniform: opengl.Matrix4Uniform(gl.GetUniformLocation(program, "uvMatrix")),
paletteUniform: gl.GetUniformLocation(program, "palette"),
bitmapUniform: gl.GetUniformLocation(program, "bitmap"),
textureQuery: textureQuery,
lastTileType: level.TileTypeSolid,
}
renderable.vao.WithSetter(func(gl opengl.OpenGL) {
gl.EnableVertexAttribArray(uint32(renderable.vertexPositionAttrib))
gl.BindBuffer(opengl.ARRAY_BUFFER, renderable.vertexPositionBuffer)
gl.VertexAttribOffset(uint32(renderable.vertexPositionAttrib), 3, opengl.FLOAT, false, 0, 0)
gl.BindBuffer(opengl.ARRAY_BUFFER, 0)
})
return renderable
}
// Dispose releases any internal resources.
func (renderable *MapTextures) Dispose() {
gl := renderable.context.OpenGL
renderable.vao.Dispose()
gl.DeleteProgram(renderable.program)
gl.DeleteBuffers([]uint32{renderable.vertexPositionBuffer})
}
// Render renders the textures.
func (renderable *MapTextures) Render(columns, rows int, tileTextureQuery TileTextureQuery, paletteTexture *graphics.PaletteTexture) {
gl := renderable.context.OpenGL
renderable.vao.OnShader(func() {
renderable.viewMatrixUniform.Set(gl, renderable.context.ViewMatrix)
renderable.projectionMatrixUniform.Set(gl, &renderable.context.ProjectionMatrix)
textureUnit := int32(0)
gl.ActiveTexture(opengl.TEXTURE0 + uint32(textureUnit))
gl.BindTexture(opengl.TEXTURE_2D, paletteTexture.Handle())
gl.Uniform1i(renderable.paletteUniform, textureUnit)
textureUnit = 1
gl.ActiveTexture(opengl.TEXTURE0 + uint32(textureUnit))
gl.Uniform1i(renderable.bitmapUniform, textureUnit)
scaling := mgl.Scale3D(level.FineCoordinatesPerTileSide, level.FineCoordinatesPerTileSide, 1.0)
for y := 0; y < rows; y++ {
for x := 0; x < columns; x++ {
tileType, textureIndex, textureRotations := tileTextureQuery(level.TilePosition{X: byte(x), Y: byte(y)})
if tileType == level.TileTypeSolid {
continue
}
texture, _ := renderable.textureQuery(textureIndex)
if texture == nil {
continue
}
modelMatrix := mgl.Translate3D(float32(x)*level.FineCoordinatesPerTileSide, float32(y)*level.FineCoordinatesPerTileSide, 0.0).
Mul4(scaling)
uvMatrix := uvRotations[textureRotations]
renderable.uvMatrixUniform.Set(gl, uvMatrix)
renderable.modelMatrixUniform.Set(gl, &modelMatrix)
gl.BindTexture(opengl.TEXTURE_2D, texture.Handle())
renderable.renderTileType(tileType)
}
}
gl.BindTexture(opengl.TEXTURE_2D, 0)
})
}
func (renderable *MapTextures) renderTileType(tileType level.TileType) {
info := tileTypeVerticesLookup[int(tileType)]
vertexCount := len(info.vertices) / 3
gl := renderable.context.OpenGL
if info.equivalentType != renderable.lastTileType {
renderable.lastTileType = info.equivalentType
gl.BindBuffer(opengl.ARRAY_BUFFER, renderable.vertexPositionBuffer)
gl.BufferData(opengl.ARRAY_BUFFER, len(info.vertices)*4, info.vertices, opengl.STATIC_DRAW)
gl.BindBuffer(opengl.ARRAY_BUFFER, 0)
}
gl.DrawArrays(opengl.TRIANGLES, 0, int32(vertexCount))
}