-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
275 lines (228 loc) · 5.77 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
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
package examples
import (
"math"
"math/rand"
"runtime"
"github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/go-gl/mathgl/mgl32"
. "github.com/jakecoffman/cp/v2"
)
var width = 640
var height = 480
var Mouse Vector
var RightClick = false
var RightDown = false
var Keyboard Vector
var mouseBody *Body
var mouseJoint *Constraint
var accumulator float64
var lastTime, lastFps float64
var frames, fps int
func init() {
runtime.LockOSThread()
rand.Seed(45073)
}
func DefaultDraw(space *Space) {
DrawSpace(space, NewDrawOptions(
DRAW_SHAPES|DRAW_CONSTRAINTS|DRAW_COLLISION_POINTS,
FColor{200.0 / 255.0, 210.0 / 255.0, 230.0 / 255.0, 1},
FColor{0, 0.75, 0, 1},
FColor{1, 0, 0, 1},
nil,
))
}
func Display(space *Space, tick float64, update UpdateFunc, draw DrawFunc) {
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
gl.Translatef(0.0, 0.0, 0.0)
gl.Scalef(1, 1, 1)
Update(space, tick, update)
ClearRenderer()
ClearTextRenderer()
draw(space)
// gives buffer to open-gl to draw
FlushRenderer()
DrawInstructions()
DrawInfo(space)
gl.MatrixMode(gl.MODELVIEW)
gl.PushMatrix()
gl.LoadIdentity()
FlushTextRenderer()
gl.PopMatrix()
}
type UpdateFunc func(*Space, float64)
type DrawFunc func(*Space)
func Update(space *Space, tick float64, update UpdateFunc) {
t := glfw.GetTime()
dt := t - lastTime
if dt > 0.2 {
dt = 0.2
}
lastTime = t
frames++
if t-lastFps >= 1 {
fps = frames
frames = 0
lastFps += 1
}
for accumulator += dt; accumulator > tick; accumulator -= tick {
newPoint := mouseBody.Position().Lerp(Mouse, 0.25)
mouseBody.SetVelocityVector(newPoint.Sub(mouseBody.Position()).Mult(60.0))
mouseBody.SetPosition(newPoint)
update(space, tick)
RightDown = false
}
}
func Main(space *Space, tick float64, update UpdateFunc, draw DrawFunc) {
if err := glfw.Init(); err != nil {
panic(err)
}
defer glfw.Terminate()
glfw.WindowHint(glfw.Resizable, glfw.False)
glfw.WindowHint(glfw.ContextVersionMajor, 2)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
window, err := glfw.CreateWindow(width, height, "Cube", nil, nil)
if err != nil {
panic(err)
}
defer window.Destroy()
window.MakeContextCurrent()
glfw.SwapInterval(1)
if err := gl.Init(); err != nil {
panic(err)
}
DrawInit()
TextInit()
gl.ClearColor(52.0/255.0, 62.0/255.0, 72.0/255.0, 1.0)
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.Enable(gl.LINE_SMOOTH)
gl.Enable(gl.POINT_SMOOTH)
gl.Hint(gl.LINE_SMOOTH_HINT, gl.DONT_CARE)
gl.Hint(gl.POINT_SMOOTH_HINT, gl.DONT_CARE)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
wi, hi := window.GetFramebufferSize()
w := float64(wi)
h := float64(hi)
gl.Viewport(0, 0, int32(wi), int32(hi))
scale := math.Min(w/float64(width), h/float64(height))
hw := w * (0.5 / scale)
hh := h * (0.5 / scale)
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
gl.Ortho(-hw, hw, -hh, hh, -1, 1)
window.SetCharCallback(func(w *glfw.Window, char rune) {
if char == 'q' {
window.SetShouldClose(true)
return
}
})
window.SetCursorPosCallback(func(w *glfw.Window, xpos float64, ypos float64) {
ww, wh := w.GetSize()
Mouse = MouseToSpace(xpos, ypos, ww, wh)
})
Mouse = Vector{}
window.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
if button == glfw.MouseButton1 {
if action == glfw.Press {
// give the mouse click a little radius to make it easier to click small shapes.
radius := 5.0
info := space.PointQueryNearest(Mouse, radius, GrabFilter)
if info.Shape != nil && info.Shape.Body().Mass() < INFINITY {
var nearest Vector
if info.Distance > 0 {
nearest = info.Point
} else {
nearest = Mouse
}
body := info.Shape.Body()
mouseJoint = NewPivotJoint2(mouseBody, body, Vector{}, body.WorldToLocal(nearest))
mouseJoint.SetMaxForce(50000)
mouseJoint.SetErrorBias(math.Pow(1.0-0.15, 60.0))
space.AddConstraint(mouseJoint)
}
} else if mouseJoint != nil {
space.RemoveConstraint(mouseJoint)
mouseJoint = nil
}
} else if button == glfw.MouseButton2 {
RightDown = action == glfw.Press
RightClick = RightDown
}
})
vsync := true
window.SetKeyCallback(func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
switch key {
case glfw.KeyUp:
if action == glfw.Press {
Keyboard.Y += 1
} else if action == glfw.Release {
Keyboard.Y -= 1
}
case glfw.KeyDown:
if action == glfw.Press {
Keyboard.Y -= 1
} else if action == glfw.Release {
Keyboard.Y += 1
}
case glfw.KeyRight:
if action == glfw.Press {
Keyboard.X += 1
} else if action == glfw.Release {
Keyboard.X -= 1
}
case glfw.KeyLeft:
if action == glfw.Press {
Keyboard.X -= 1
} else if action == glfw.Release {
Keyboard.X += 1
}
case glfw.KeyV:
if action != glfw.Release {
return
}
if vsync {
glfw.SwapInterval(0)
} else {
glfw.SwapInterval(1)
}
vsync = !vsync
}
})
mouseBody = NewKinematicBody()
for !window.ShouldClose() {
Display(space, tick, update, draw)
window.SwapBuffers()
glfw.PollEvents()
gl.Clear(gl.COLOR_BUFFER_BIT)
}
}
func MouseToSpace(x, y float64, ww, wh int) Vector {
var model [16]float64
gl.GetDoublev(gl.MODELVIEW_MATRIX, &model[0])
modelMat := mgl32.Mat4{}
for i, m := range model {
modelMat[i] = float32(m)
}
var proj [16]float64
gl.GetDoublev(gl.PROJECTION_MATRIX, &proj[0])
projMat := mgl32.Mat4{}
for i, p := range proj {
projMat[i] = float32(p)
}
var view [4]int32
gl.GetIntegerv(gl.VIEWPORT, &view[0])
//var mx, my, mz float64
obj, err := mgl32.UnProject(
mgl32.Vec3{float32(x), float32(float64(wh) - y), 0},
modelMat,
projMat,
0, 0,
ww, wh,
)
if err != nil {
panic(err)
}
return Vector{float64(obj.X()), float64(obj.Y())}
}