/
gfx.go
398 lines (338 loc) · 9.34 KB
/
gfx.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright 2014 The Azul3D Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package tmx implements routines for rendering tmx maps.
//
// It loads a 2D tmx map into a few meshes stored in a *gfx.Object and applies
// textures such that it would render properly.
//
// At present the package only supports orthogonal tile map rendering, and has
// some issues with proper ordering of perspective (I.e. non-uniformly sized)
// tiles (see for instance tiled-qt/examples/perspective_walls.tmx).
package tmx
import (
"image"
"image/draw"
"io/ioutil"
"os"
"path/filepath"
"azul3d.org/gfx.v2-unstable"
"azul3d.org/lmath.v1"
)
var (
glslVert = []byte(`
#version 120
attribute vec3 Vertex;
attribute vec2 TexCoord0;
uniform mat4 MVP;
varying vec2 tc0;
void main()
{
tc0 = TexCoord0;
gl_Position = MVP * vec4(Vertex, 1.0);
}
`)
glslFrag = []byte(`
#version 120
varying vec2 tc0;
uniform sampler2D Texture0;
uniform bool BinaryAlpha;
void main()
{
gl_FragColor = texture2D(Texture0, tc0);
if(BinaryAlpha && gl_FragColor.a < 0.5) {
discard;
}
}
`)
)
var (
cw90, cwn90, horizFlip, vertFlip lmath.Mat4
Shader *gfx.Shader
)
func init() {
Shader = &gfx.Shader{
Name: "tmx.Shader",
GLSL: &gfx.GLSLSources{
Vertex: glslVert,
Fragment: glslFrag,
},
}
// Setup rotations
cw90 = lmath.Mat4FromAxisAngle(
lmath.Vec3{0, 1, 0},
lmath.Radians(90),
lmath.CoordSysZUpRight,
)
cwn90 = lmath.Mat4FromAxisAngle(
lmath.Vec3{0, 1, 0},
lmath.Radians(-90),
lmath.CoordSysZUpRight,
)
// Setup horizontal flip
horizFlip = lmath.Mat4FromAxisAngle(
lmath.Vec3{0, 0, 1},
lmath.Radians(180),
lmath.CoordSysZUpRight,
)
// Setup vertical flip
vertFlip = lmath.Mat4FromAxisAngle(
lmath.Vec3{1, 0, 0},
lmath.Radians(180),
lmath.CoordSysZUpRight,
)
}
func appendCard(m *gfx.Mesh, l, r, b, t, depth float32, rect, tex image.Rectangle) {
addv := func(x, y float32) {
m.Vertices = append(m.Vertices, gfx.Vec3{x, depth, y})
}
addt := func(u, v float32) {
if len(m.TexCoords) == 0 {
m.TexCoords = make([]gfx.TexCoordSet, 1)
}
m.TexCoords[0].Slice = append(m.TexCoords[0].Slice, gfx.TexCoord{u, v})
}
w := float32(tex.Dx())
h := float32(tex.Dy())
halfTexUnitX := 1.0 / w
halfTexUnitY := 1.0 / w
u0 := (float32(rect.Min.X) / w) + halfTexUnitX
u1 := (float32(rect.Max.X) / w) - halfTexUnitX
v0 := (float32(rect.Min.Y) / h) + halfTexUnitY
v1 := (float32(rect.Max.Y) / h) - halfTexUnitY
// Left triangle.
addv(l, t)
addv(l, b)
addv(r, b)
// Right triangle.
addv(l, t)
addv(r, b)
addv(r, t)
// Left triangle.
addt(u0, v0)
addt(u0, v1)
addt(u1, v1)
// Right triangle.
addt(u0, v0)
addt(u1, v1)
addt(u1, v0)
}
// Config represents a tmx mesh configuration
type Config struct {
// The value which is used to offset each layer on the Y axis.
LayerOffset float64
// The value to offset each individual tile from one another on the Y axis.
TileOffset float64
}
// Load loads the given tmx map, m, and returns a slice of *gfx.Object with the
// proper meshes and textures attached to them.
//
// If the configuration, c, is non-nil then it is used in place of the default
// configuration.
//
// The tsImages map should be a map of tileset image filenames and their
// associated loaded RGBA images. Tiles who reference tilesets who are not
// found in the map will be omited (not rendered) in the returned objects.
func Load(m *Map, c *Config, tsImages map[string]*image.RGBA) (layers map[string]map[string]*gfx.Object) {
if c == nil {
c = &Config{
LayerOffset: 0.001,
TileOffset: 0.000001,
}
}
// A map of layer names to a slice of objects each containing one texture
// and mesh.
layers = make(map[string]map[string]*gfx.Object, len(m.Layers))
var layerOffset float64
for _, layer := range m.Layers {
// A slice of objects which contain a single texture and mesh.
texObjects := make(map[string]*gfx.Object)
var tileOffset float64
for x := 0; x < m.Width; x++ {
for y := 0; y < m.Height; y++ {
gid, hasTile := layer.Tiles[Coord{x, y}]
if !hasTile {
continue
}
tileset := m.FindTileset(gid)
// Load the tileset texture if needed
tsImage := filepath.Base(tileset.Image.Source)
rgba, haveTilesetImage := tsImages[tsImage]
if !haveTilesetImage {
// We weren't given a RGBA image for the tileset, so we
// will just omit this tile.
continue
}
// Create a textured mesh object, if needed.
obj, ok := texObjects[tsImage]
if !ok {
// Create texture.
t := gfx.NewTexture()
t.Source = rgba
t.Bounds = rgba.Bounds()
t.WrapU = gfx.Clamp
t.WrapV = gfx.Clamp
t.MinFilter = gfx.LinearMipmapLinear
t.MagFilter = gfx.Linear
// And the object.
obj = gfx.NewObject()
obj.Shader = Shader
obj.Meshes = []*gfx.Mesh{gfx.NewMesh()}
obj.Textures = []*gfx.Texture{t}
// Disable face culling because of the flipped cards.
obj.State = gfx.NewState()
obj.State.FaceCulling = gfx.NoFaceCulling
obj.State.AlphaMode = gfx.AlphaToCoverage
texObjects[tsImage] = obj
}
r := m.TilesetRect(tileset, rgba.Bounds().Dx(), rgba.Bounds().Dy(), true, gid)
halfWidth := float32(tileset.Width) / 2.0
halfHeight := float32(tileset.Height) / 2.0
cardStart := len(obj.Meshes[0].Vertices)
appendCard(
obj.Meshes[0],
-halfWidth,
halfWidth,
-halfHeight,
halfHeight,
0, r, rgba.Bounds(),
)
cardEnd := len(obj.Meshes[0].Vertices)
// apply necessary flips
flip := lmath.Mat4Identity
diagFlipped := (gid & FLIPPED_DIAGONALLY_FLAG) > 0
horizFlipped := (gid & FLIPPED_HORIZONTALLY_FLAG) > 0
vertFlipped := (gid & FLIPPED_VERTICALLY_FLAG) > 0
if diagFlipped {
if horizFlipped && vertFlipped {
flip = cw90.Mul(flip)
flip = horizFlip.Mul(flip)
} else if horizFlipped {
flip = cw90.Mul(flip)
} else if vertFlipped {
flip = cwn90.Mul(flip)
} else {
flip = horizFlip.Mul(flip)
flip = cw90.Mul(flip)
}
} else {
if horizFlipped {
flip = horizFlip.Mul(flip)
}
if vertFlipped {
flip = vertFlip.Mul(flip)
}
}
// Move the card,
move := lmath.Mat4FromTranslation(lmath.Vec3{
float64(x*m.TileWidth) + float64(halfWidth),
layerOffset + tileOffset,
float64((m.Height-y)*m.TileHeight) - float64(halfHeight),
})
tileOffset -= c.TileOffset
trans := flip.Mul(move)
// Apply transformation.
verts := obj.Meshes[0].Vertices
for i, v := range verts[cardStart:cardEnd] {
vt := v.Vec3().TransformMat4(trans)
verts[cardStart+i] = gfx.Vec3{float32(vt.X), float32(vt.Y), float32(vt.Z)}
}
}
}
// Add the slice to the map of layers.
layers[layer.Name] = texObjects
// Decrease layer offset
layerOffset -= c.LayerOffset
}
/*
m := new(gfx.Mesh)
o := &gfx.Object{
State: gfx.DefaultState,
Meshes: []*gfx.Mesh{m},
Textures: texturesSlice,
}
for _, layer := range m.Layers {
// Collect all the little tile meshes
for tex, tsImageNode := range texturedNodes {
_, collected := geom.Collect(tsImageNode)
collected.SetParent(baseNode)
tsImageNode.Detatch()
//collected := tsImageNode
// Add the tileset texture to the collected set of tile meshes
tex.SetWrapModeU(texture.Clamp)
tex.SetWrapModeV(texture.Clamp)
texture.Set(collected, texture.DefaultLayer, tex)
// Decrease Y by layer offset
x, _, z := collected.Pos()
collected.SetPos(x, float64(layerOffset), z)
}
}
*/
return layers
}
// LoadFile works just like Load except it loads all associated dependencies
// (external tsx tileset files, tileset texture images) for you.
//
// Advanced clients who wish to have more control over file IO will use Load()
// directly instead of using this function.
func LoadFile(path string, c *Config) (*Map, map[string]map[string]*gfx.Object, error) {
f, err := os.Open(path)
if err != nil {
return nil, nil, err
}
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, nil, err
}
m, err := Parse(data)
if err != nil {
return nil, nil, err
}
relativeDir := filepath.Dir(path)
// External tilesets in the map must be loaded seperately
for _, ts := range m.Tilesets {
if len(ts.Source) > 0 {
// Open tsx file
f, err := os.Open(filepath.Join(relativeDir, filepath.Base(ts.Source)))
if err != nil {
return nil, nil, err
}
// Read file data
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, nil, err
}
// Load the tileset
err = ts.Load(data)
if err != nil {
return nil, nil, err
}
}
}
// We must also load the images of the tileset
tsImages := make(map[string]*image.RGBA)
for _, ts := range m.Tilesets {
// Name of the tileset image file
tsImage := filepath.Base(ts.Image.Source)
// Open tileset image
f, err := os.Open(filepath.Join(relativeDir, tsImage))
if err != nil {
return nil, nil, err
}
// Decode the image
src, _, err := image.Decode(f)
if err != nil {
return nil, nil, err
}
// If need be, convert to RGBA
rgba, ok := src.(*image.RGBA)
if !ok {
b := src.Bounds()
rgba = image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(rgba, rgba.Bounds(), src, b.Min, draw.Src)
}
// Put into the tileset images map
tsImages[tsImage] = rgba
}
return m, Load(m, c, tsImages), nil
}