-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
158 lines (133 loc) · 3.47 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
156
157
158
package main
import (
"log"
"math"
"github.com/go-gl/gl"
glfw "github.com/go-gl/glfw3"
)
var drawing int
var (
running bool = true
m *Map
currentBrushValue int = 0
currentBrushSize int = 0
brushValues []int = []int{145, 170, 195, 245}
brushSizes []int = []int{24, 32, 48, 64, 128}
cursorVerts []float64 = buildCursor(brushSizes[currentBrushSize])
camera [2]float32 = [2]float32{0, 0}
)
func main() {
if !glfw.Init() {
log.Fatal("glfw failed to initialize")
}
defer glfw.Terminate()
window, err := glfw.CreateWindow(640, 480, "Deformable", nil, nil)
if err != nil {
log.Fatal(err.Error())
}
window.MakeContextCurrent()
glfw.SwapInterval(1)
window.SetMouseButtonCallback(handleMouseButton)
window.SetKeyCallback(handleKeyDown)
window.SetInputMode(glfw.Cursor, glfw.CursorHidden)
gl.Init()
initGL()
i := 16
m = GenerateMap(1600/i, 1200/i, i)
for running && !window.ShouldClose() {
x, y := window.GetCursorPosition()
if drawing != 0 {
m.Add(int(x)+int(camera[0]), int(y)+int(camera[1]), drawing, brushSizes[currentBrushSize])
}
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.LoadIdentity()
gl.PushMatrix()
gl.PushAttrib(gl.CURRENT_BIT | gl.ENABLE_BIT | gl.LIGHTING_BIT | gl.POLYGON_BIT | gl.LINE_BIT)
gl.Translatef(-camera[0], -camera[1], 0)
m.Draw()
gl.PopAttrib()
gl.PopMatrix()
gl.PushAttrib(gl.COLOR_BUFFER_BIT)
gl.LineWidth(2)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.ONE_MINUS_DST_COLOR, gl.ZERO)
// gl.Enable(gl.LINE_SMOOTH)
// gl.Hint(gl.LINE_SMOOTH_HINT, gl.NICEST)
gl.Translatef(float32(x), float32(y), 0)
gl.EnableClientState(gl.VERTEX_ARRAY)
gl.VertexPointer(2, gl.DOUBLE, 0, cursorVerts)
gl.DrawArrays(gl.LINE_LOOP, 0, 24)
gl.PopAttrib()
window.SwapBuffers()
glfw.PollEvents()
}
}
func initGL() {
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
gl.Viewport(0, 0, 800, 600)
gl.Ortho(0, 800, 600, 0, -1.0, 1.0)
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
gl.ClearColor(0.1, 0.05, 0.0, 1.0)
}
func buildCursor(size int) []float64 {
vl := make([]float64, 0)
for i := 0; i < 24; i++ {
a := float64(i) / 24 * (math.Pi * 2)
v := float64(size)
vl = append(vl, math.Cos(a)*v, math.Sin(a)*v)
}
return vl
}
func handleKeyDown(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
if action == glfw.Release {
return
}
switch key {
case glfw.KeyLeft, glfw.KeyA:
camera[0] -= 8
case glfw.KeyRight, glfw.KeyD:
camera[0] += 8
case glfw.KeyUp, glfw.KeyW:
camera[1] -= 8
case glfw.KeyDown, glfw.KeyS:
camera[1] += 8
case '1':
currentBrushValue = 0
case '2':
currentBrushValue = 1
case '3':
currentBrushValue = 2
case '4':
currentBrushValue = 3
case '[':
currentBrushSize--
if currentBrushSize < 0 {
currentBrushSize = 0
}
cursorVerts = buildCursor(brushSizes[currentBrushSize])
case ']':
currentBrushSize++
if currentBrushSize > len(brushSizes)-1 {
currentBrushSize = len(brushSizes) - 1
}
cursorVerts = buildCursor(brushSizes[currentBrushSize])
case glfw.KeyEscape:
running = false
case glfw.KeyTab:
m.renderMode = (m.renderMode + 1) % 2
case '\\':
m.renderSmooth = !m.renderSmooth
}
}
func handleMouseButton(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
switch {
case button == 0 && action == glfw.Press:
drawing = brushValues[currentBrushValue]
case button == 1 && action == glfw.Press:
drawing = 1
default:
drawing = 0
}
}