-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.go
92 lines (75 loc) · 1.73 KB
/
main.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
package main
import (
"image/png"
"os"
"time"
"github.com/thegtproject/pixel/imdraw"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
var gopherimg *pixel.Sprite
var imd *imdraw.IMDraw
var uTime, uSpeed float32
func gameloop(win *pixelgl.Window) {
win.Canvas().SetUniform("uTime", &uTime)
win.Canvas().SetUniform("uSpeed", &uSpeed)
uSpeed = 5.0
win.Canvas().SetFragmentShader(fragmentShader)
start := time.Now()
for !win.Closed() {
win.Clear(pixel.RGB(0, 0, 0))
gopherimg.Draw(win, pixel.IM.Moved(win.Bounds().Center()))
uTime = float32(time.Since(start).Seconds())
if win.Pressed(pixelgl.KeyRight) {
uSpeed += 0.1
}
if win.Pressed(pixelgl.KeyLeft) {
uSpeed -= 0.1
}
win.Update()
}
}
func run() {
cfg := pixelgl.WindowConfig{
Title: "Pixel Rocks!",
Bounds: pixel.R(0, 0, 325, 348),
VSync: true,
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
f, err := os.Open("../assets/images/thegopherproject.png")
if err != nil {
panic(err)
}
img, err := png.Decode(f)
if err != nil {
panic(err)
}
pd := pixel.PictureDataFromImage(img)
gopherimg = pixel.NewSprite(pd, pd.Bounds())
gameloop(win)
}
func main() {
pixelgl.Run(run)
}
var fragmentShader = `
#version 330 core
out vec4 fragColor;
uniform sampler2D uTexture;
uniform vec4 uTexBounds;
// custom uniforms
uniform float uSpeed;
uniform float uTime;
void main() {
vec2 t = gl_FragCoord.xy / uTexBounds.zw;
vec3 influence = texture(uTexture, t).rgb;
if (influence.r + influence.g + influence.b > 0.3) {
t.y += cos(t.x * 40.0 + (uTime * uSpeed))*0.005;
t.x += cos(t.y * 40.0 + (uTime * uSpeed))*0.01;
}
vec3 col = texture(uTexture, t).rgb;
fragColor = vec4(col * vec3(0.6, 0.6, 1.2),1.0);
}
`