-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
155 lines (121 loc) · 3.16 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
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
package main
import (
"fmt"
"log"
"os"
"runtime"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/mazznoer/colorgrad"
"github.com/rbrick/fractal-explorer/engine"
"github.com/go-gl/gl/v4.1-core/gl"
)
var (
DefaultPreprocessor = &engine.Preprocessor{
Directives: map[string]engine.ProcessFunc{
"include": engine.Include,
},
}
)
func init() {
runtime.LockOSThread()
}
func main() {
if err := glfw.Init(); err != nil {
log.Fatalln("failed to initialize:", err) // failed to initialize GLFW
}
w, err := engine.NewWindow("Fractal Explorer", 1200, 900, nil,
engine.ContextVersion(4, 1),
engine.ContextProfile(glfw.OpenGLCoreProfile),
engine.ContextForwardCompatible(true),
engine.WindowResizable(true),
engine.Decorated(true),
)
if err != nil {
log.Fatalln("failed to create window:", err)
}
panX, panY := 0., 0.
zoom := 1.
iterations := 200
mode := 0
w.WindowHandle.SetKeyCallback(func(win *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
if action == glfw.Press || action == glfw.Repeat {
switch key {
case glfw.KeyEscape:
win.SetShouldClose(true)
case glfw.KeyP:
// increment the zoom
zoom *= 1.05
case glfw.KeyO:
zoom /= 1.05
case glfw.KeyUp:
// Pan up
panY -= 0.02 / zoom
case glfw.KeyDown:
// Pan down
panY += 0.02 / zoom
case glfw.KeyRight:
// pan right
panX += 0.02 / zoom
case glfw.KeyLeft:
// pan left
panX -= 0.02 / zoom
case glfw.KeyI:
if glfw.ModShift == mods {
iterations -= 50
} else {
iterations += 50
}
case glfw.KeyM:
mode += 1
if mode > 1 {
mode = 0
}
}
fmt.Println("panX", panX, "panY", panY, "zoom", zoom)
}
})
v, _ := os.Open("shaders/vertex.glsl")
f, _ := os.Open("shaders/mandelbrot.glsl")
glfw.WindowHint(glfw.Floating, glfw.True)
w.Init()
vert, err := engine.ReadShader(v, gl.VERTEX_SHADER, DefaultPreprocessor)
if err != nil {
log.Fatalln(err)
}
frag, err := engine.ReadShader(f, gl.FRAGMENT_SHADER, DefaultPreprocessor)
if err != nil {
fmt.Println("fail")
log.Fatalln(err)
}
program := engine.NewProgram()
// set up our shader program
program.Bind()
program.Attach(vert)
program.Attach(frag)
program.Link()
program.Unbind()
// setup our VAO
vao := engine.CreateBuffer(engine.BufferType{})
for !w.ShouldClose() {
program.Bind()
fw, fh := glfw.GetCurrentContext().GetFramebufferSize()
program.GetUniform("Resolution").Vecf(float32(fw), float32(fh))
program.GetUniform("Pan").Vecf(float32(panX), float32(panY))
program.GetUniform("Time").Float(float32(glfw.GetTime()))
program.GetUniform("Zoom").Float(float32(zoom))
program.GetUniform("MaxIterations").Int(int32(iterations))
program.GetUniform("Mode").Int(int32(mode))
var colors []int32
for _, c := range colorgrad.Blues().ColorfulColors(256) {
r, g, b := c.RGB255()
colors = append(colors, ((int32(r)&0x0ff)<<16)|((int32(g)&0x0ff)<<8)|(int32(b)&0x0ff))
}
program.GetUniform("palette").IntArray(colors)
vao.Bind(gl.ARRAY_BUFFER)
gl.DrawArrays(gl.TRIANGLES, 0, 6)
vao.Unbind(gl.ARRAY_BUFFER)
program.Unbind()
w.WindowHandle.SwapBuffers()
glfw.PollEvents()
}
}