This repository has been archived by the owner on Sep 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
graphics.go
341 lines (270 loc) · 8.86 KB
/
graphics.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
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build js
package main
import (
"fmt"
"log"
"math"
"syscall/js"
"github.com/laremere/space-agon/client/webgl"
"github.com/laremere/space-agon/game"
)
type graphics struct {
w *webgl.WebGL
width int
height int
spritesheet *webgl.Texture
spriteShader *webgl.Program
coords []float32
coordsBuffer *webgl.Buffer
textureCoords []float32
textureCoordsBuffer *webgl.Buffer
uCenter [2]float32
uScale [2]float32
written int
pointShader *webgl.Program
pointCoords []float32
pointsWritten int
}
func NewGraphics() (*graphics, error) {
g := &graphics{}
// document.getElementById("game").parentElement.removeChild(document.getElementById("game"));
canvas := js.Global().Get("document").Call("getElementById", "game")
if canvas.Type() != js.TypeNull {
canvas.Get("parentElement").Call("removeChild", canvas)
}
canvas = js.Global().Get("document").Call("createElement", "canvas")
g.width = js.Global().Get("window").Get("innerWidth").Int()
g.height = js.Global().Get("window").Get("innerHeight").Int()
canvas.Set("width", g.width)
canvas.Set("height", g.height)
canvas.Set("id", "game")
js.Global().Get("document").Call("getElementById", "container").Call("appendChild", canvas)
var err error
g.w, err = webgl.InitWebgl(canvas)
if err != nil {
return nil, err
}
g.w.Enable(g.w.BLEND)
g.w.BlendFunc(g.w.SRC_ALPHA, g.w.ONE_MINUS_SRC_ALPHA)
spritesheetElement := js.Global().Get("document").Call("getElementById", "spritesheet")
// log.Println("WIDTH", spritesheetElement.Get("width"))
// log.Println("HEIGHT", spritesheetElement.Get("height"))
g.spritesheet = webgl.LoadTexture(g.w, spritesheetElement)
g.spriteShader, err = webgl.CreateProgram(
g.w,
`
uniform vec2 uCenter;
uniform vec2 uScale;
attribute vec2 aVertexPosition;
attribute vec2 aTextureCoord;
varying highp vec2 vTextureCoord;
void main() {
gl_Position = vec4((aVertexPosition-uCenter)/uScale, 0.0, 1.0);
vTextureCoord = aTextureCoord;
}`,
`
precision highp float;
varying highp vec2 vTextureCoord;
uniform sampler2D uSampler;
void main() {
gl_FragColor = texture2D(uSampler, vTextureCoord);
}
`,
)
if err != nil {
return nil, fmt.Errorf("Error building spriteShader: %w", err)
}
g.coords = make([]float32, webgl.MaxArrayLength/(3*2*2))
g.textureCoords = make([]float32, len(g.coords))
g.written = 0
g.coordsBuffer = g.w.CreateBuffer()
g.w.BindBuffer(g.w.ARRAY_BUFFER, g.coordsBuffer)
// 4 comes from 4 bytes per float32
g.w.BufferDataSize(g.w.ARRAY_BUFFER, 4*len(g.coords), g.w.DYNAMIC_DRAW)
g.textureCoordsBuffer = g.w.CreateBuffer()
g.w.BindBuffer(g.w.ARRAY_BUFFER, g.textureCoordsBuffer)
g.w.BufferDataSize(g.w.ARRAY_BUFFER, 4*len(g.textureCoords), g.w.DYNAMIC_DRAW)
g.pointShader, err = webgl.CreateProgram(
g.w,
`
uniform vec2 uCenter;
uniform vec2 uScale;
attribute vec2 aVertexPosition;
void main() {
gl_Position = vec4((aVertexPosition-uCenter)/uScale, 0.0, 1.0);
}`,
`
precision highp float;
void main() {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
`,
)
if err != nil {
return nil, fmt.Errorf("Error building spriteShader: %w", err)
}
g.pointCoords = make([]float32, len(g.coords))
return g, nil
}
func (g *graphics) Flush() {
if g.pointsWritten > 0 {
g.w.UseProgram(g.pointShader)
uCenter := g.w.GetUniformLocation(g.pointShader, "uCenter")
g.w.Uniform2fv(uCenter, g.uCenter)
uScale := g.w.GetUniformLocation(g.pointShader, "uScale")
g.w.Uniform2fv(uScale, g.uScale)
g.w.BindBuffer(g.w.ARRAY_BUFFER, g.coordsBuffer)
g.w.BufferSubDataF32(g.w.ARRAY_BUFFER, 0, g.pointCoords[:g.pointsWritten])
aVertexPosition := g.w.GetAttribLocation(g.pointShader, "aVertexPosition")
g.w.EnableVertexAttribArray(aVertexPosition)
// Bind current array buffer to the given vertex attribute
g.w.VertexAttribPointer(aVertexPosition, 2, g.w.FLOAT, false, 0, 0) // 2 = points per vertex
g.w.DrawArrays(g.w.POINTS, 0, g.pointsWritten/2) // two floats per vertex
g.pointsWritten = 0
}
if g.written > 0 {
g.w.UseProgram(g.spriteShader)
uCenter := g.w.GetUniformLocation(g.spriteShader, "uCenter")
g.w.Uniform2fv(uCenter, g.uCenter)
uScale := g.w.GetUniformLocation(g.spriteShader, "uScale")
g.w.Uniform2fv(uScale, g.uScale)
g.w.BindBuffer(g.w.ARRAY_BUFFER, g.coordsBuffer)
g.w.BufferSubDataF32(g.w.ARRAY_BUFFER, 0, g.coords[:g.written])
aVertexPosition := g.w.GetAttribLocation(g.spriteShader, "aVertexPosition")
g.w.EnableVertexAttribArray(aVertexPosition)
// Bind current array buffer to the given vertex attribute
g.w.VertexAttribPointer(aVertexPosition, 2, g.w.FLOAT, false, 0, 0) // 2 = points per vertex
g.w.BindBuffer(g.w.ARRAY_BUFFER, g.textureCoordsBuffer)
g.w.BufferSubDataF32(g.w.ARRAY_BUFFER, 0, g.textureCoords[:g.written])
aTextureCoord := g.w.GetAttribLocation(g.spriteShader, "aTextureCoord")
g.w.EnableVertexAttribArray(aTextureCoord)
// Bind current array buffer to the given vertex attribute
g.w.VertexAttribPointer(aTextureCoord, 2, g.w.FLOAT, false, 0, 0) // 2 = points per vertex
g.w.ActiveTexture(g.w.TEXTURE0)
g.w.BindTexture(g.w.TEXTURE_2D, g.spritesheet)
uSampler := g.w.GetUniformLocation(g.spriteShader, "uSampler")
g.w.Uniform1i(uSampler, 0)
g.w.DrawArrays(g.w.TRIANGLES, 0, g.written/2) // two floats per vertex
g.written = 0
}
glError := g.w.GetError()
if glError.Int() != 0 {
log.Println("GL Error:", g.w.GetError())
}
}
type Sprite struct {
textureCoords []float32
size float32
}
var spritemap = map[game.Sprite]*Sprite{
game.SpriteShip: &Sprite{
textureCoords: genTexCoords(0, 0, 512, 512),
size: 1,
},
game.SpriteEnemyShip: &Sprite{
textureCoords: genTexCoords(512, 512, 1024, 1024),
size: 1,
},
game.SpriteStar: &Sprite{
textureCoords: genTexCoords(0, 512, 512, 1024),
size: 10,
},
game.SpriteExplosionFlash: &Sprite{
textureCoords: genTexCoords(0, 512, 512, 1024),
// *2 because radius not diameter, *2 because the circle only takes up half
// the sprite texture size. Except that seems to big??
size: game.ExplosionRadius * 2, // * 2 * 2,
},
game.SpriteMissile: &Sprite{
textureCoords: genTexCoords(512, 0, 1024, 512),
size: 1,
},
}
func genTexCoords(xStart, yStart, xEnd, yEnd float32) []float32 {
const width = 2048
const height = width
xStart /= width
xEnd /= width
yStart /= height
yEnd /= height
return []float32{
xStart, yStart,
xStart, yEnd,
xEnd, yEnd,
xStart, yStart,
xEnd, yEnd,
xEnd, yStart,
}
}
//
// 0 1 : 6 7 ---------- : 10 11
// | \_ |
// | \_ |
// | \_ |
// | \_ |
// | \_ |
// 2 3 : ----------4 5 : 8 9
//
func (g *graphics) Point(x, y float32) {
g.pointCoords[g.pointsWritten] = x
g.pointCoords[g.pointsWritten+1] = y
g.pointsWritten += 2
if g.pointsWritten >= len(g.pointCoords) {
g.Flush()
}
}
// TODO: spriteId -> size and texture location, rotation
func (g *graphics) Sprite(s *Sprite, centerx, centery, rotation float32) {
coords := g.coords[g.written : g.written+12]
textureCoords := g.textureCoords[g.written : g.written+12]
cosSize := s.size * float32(math.Cos(float64(rotation)+(math.Pi/4))) * math.Sqrt2 / 2
sinSize := s.size * float32(math.Sin(float64(rotation)+(math.Pi/4))) * math.Sqrt2 / 2
coords[0] = centerx - sinSize
coords[1] = centery + cosSize
coords[2] = centerx - cosSize
coords[3] = centery - sinSize
coords[4] = centerx + sinSize
coords[5] = centery - cosSize
coords[6] = coords[0]
coords[7] = coords[1]
coords[8] = coords[4]
coords[9] = coords[5]
coords[10] = centerx + cosSize
coords[11] = centery + sinSize
for i := 0; i < 12; i++ {
textureCoords[i] = s.textureCoords[i]
}
g.written += 12
if g.written >= len(g.coords) {
g.Flush()
}
}
func (g *graphics) Clear() {
g.w.ClearColor(0.039, 0.102, 0.247, 1)
g.w.Clear(g.w.COLOR_BUFFER_BIT)
}
func (g *graphics) SetCamera(xmin, ymin, xmax, ymax float32) {
g.uCenter[0] = (xmin + xmax) / 2
g.uCenter[1] = (ymin + ymax) / 2
g.uScale[0] = (xmax - xmin) / 2
g.uScale[1] = (ymax - ymin) / 2
aspectRatio := float32(g.width) / float32(g.height)
if g.uScale[0]/aspectRatio > g.uScale[1] {
g.uScale[1] = g.uScale[0] / aspectRatio
} else {
g.uScale[0] = g.uScale[1] * aspectRatio
}
}