-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimpulse_engine.nim
356 lines (323 loc) · 11.9 KB
/
impulse_engine.nim
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
##[
Copyright (c) 2013 Randy Gaul http://RandyGaul.net
##
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
##
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
##
Port to Nim by Matic Kukovec https://github.com/matkuki/Nim-Impulse-Engine
]##
import
os,
strutils,
strformat,
times,
math,
iemath,
shapes,
manifold,
scene,
glfw,
glfw/wrapper,
opengl,
opengl/glu,
textdraw,
data
var
done = false # Application exit flag
frameStepping = false
canStep = false
win: glfw.Window
centerCircle = newCircle(5.0f)
mainScene = newScene(5)
videoMode: glfw.VideoMode
bodyCounter: int = 0
viewScale = 0.0
adjustedWindowSize: tuple[w: float, h: float]
proc initOpenGL() =
loadExtensions()
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
gluOrtho2D(0, WINDOW_SIZE.w/10, WINDOW_SIZE.h/10, 0)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
proc mouseButtonCallback(win: glfw.Window,
button: MouseButton,
pressed: bool,
modKeys: set[ModifierKey]) =
# Get cursor position and adjust it to openGL settings
var curPos = win.cursorPos()
curPos.x /= 10.0f
curPos.y /= 10.0f
# Adjust position depending on the view scale
let
xRatio = adjustedWindowSize.w / WINDOW_SIZE.w.float
yRatio = adjustedWindowSize.h / WINDOW_SIZE.h.float
curPos.x *= xRatio
curPos.y *= yRatio
curPos.x += ((WINDOW_SIZE.w.float - adjustedWindowSize.w) / 2.0) / 10.0
curPos.y += ((WINDOW_SIZE.h.float - adjustedWindowSize.h) / 2.0) / 10.0
# Filter only mouse press events
if pressed == true:
case button:
of mbLeft:
if LEFT_CLICK_RANDOM_POLYGONS:
# Create random polygon
var
poly: Polygon = newPolygon()
count: int = int(iemath.random(3, MaxPolyVertexCount))
vertices: array[MaxPolyVertexCount, Vec]
e: float = iemath.random(5.0f, 10.0f)
b: Body
for i in 0..vertices.high:
vertices[i].set(iemath.random(-e, e), iemath.random(-e, e))
poly.set(vertices, count)
b = mainScene.add(poly, curPos.x, curPos.y)
b.setOrient(iemath.random(-iemath.PI, iemath.PI))
b.restitution = 0.2f
b.dynamicFriction = 0.2f
b.staticFriction = 0.4f
else:
# Create symetric rectangles
var
poly: Polygon = newPolygon()
vertices: array[4, Vec]
e: float = 3.0f
b: Body
vertices[0].set(-e, -e)
vertices[1].set(e, -e)
vertices[2].set(e, e)
vertices[3].set(-e, e)
poly.set(vertices, vertices.len())
b = mainScene.add(poly, curPos.x, curPos.y)
b.setOrient(0.0) #(iemath.random(-iemath.PI, iemath.PI))
b.restitution = 0.9 #0.2f
b.dynamicFriction = 0.9 #0.2f
b.staticFriction = 0.9 #0.4f
bodycounter += 1
of mbRight:
# Create random circle
var
c: Circle = newCircle(iemath.random(1.0f, 3.0f))
discard mainScene.add(c, curPos.x, curPos.y)
bodycounter += 1
else:
discard
proc setScale(newOffset: float) =
# Set the scale
viewScale -= newOffset
if viewScale < -200:
viewScale = -200
proc setView() =
const ratio = float(WINDOW_SIZE.w / WINDOW_SIZE.h)
var offset: Vec
offset.x = viewScale * 10 * ratio
offset.y = viewScale * 10
# Reinitialize the viewport
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
# glViewport(-int32(offset.x/2), -int32(offset.y/2), int32(WINDOW_SIZE.w.float + offset.x), int32(WINDOW_SIZE.h.float + offset.y))
# glOrtho(-offset.x, WINDOW_SIZE.w.float+offset.x, WINDOW_SIZE.h.float+offset.y, -offset.y, 0.0f, 1.0f)
if viewScale > 0:
glViewport(-int32(offset.x/2), -int32(offset.y/2), int32(WINDOW_SIZE.w.float + offset.x), int32(WINDOW_SIZE.h.float + offset.y))
glOrtho(0, WINDOW_SIZE.w.float, WINDOW_SIZE.h.float, 0, viewScale, -1.0f)
adjustedWindowSize = (
w: (WINDOW_SIZE.w.float),
h: (WINDOW_SIZE.h.float),
)
else:
var
x = -offset.x
y = -offset.y
wx = -offset.x
hy = -offset.y
glViewport(0, 0, WINDOW_SIZE.w.int32, WINDOW_SIZE.h.int32)
glOrtho(-x, WINDOW_SIZE.w.float+wx, WINDOW_SIZE.h.float+hy, -y, 0.0f, 1.0f)
adjustedWindowSize = (
w: (x + WINDOW_SIZE.w.float+wx),
h: (y + WINDOW_SIZE.h.float+hy),
)
proc scrollCallback(win: glfw.Window,
pos: tuple[x: float64, y: float64]) =
setScale(pos.y)
proc keyCallback(win: glfw.Window,
key: Key,
scanCode: int32,
action: KeyAction,
modKeys: set[ModifierKey]) =
# Filter only keyUp events
if action != kaUp:
case key:
of keyEscape:
win.shouldClose = true
of keyF4:
if mkAlt in modKeys:
win.shouldClose = true
of keyF:
frameStepping = not frameStepping
of keyR:
viewScale = 0.0
setView()
of keyRight:
mainScene.bodies[mainScene.bodies.high].velocity += Vec(x:2.0f, y:0.0f)
of keyLeft:
mainScene.bodies[mainScene.bodies.high].velocity -= Vec(x:2.0f, y:0.0f)
of keyUp:
mainScene.bodies[mainScene.bodies.high].velocity += Vec(x:0.0f, y: -5.0f)
of keySpace:
canStep = true
else:
discard
proc initView() =
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, WINDOW_SIZE.w.float, WINDOW_SIZE.h.float, 0.0, 0.0f, 1.0f)
glViewport(0, 0, WINDOW_SIZE.w.int32, WINDOW_SIZE.h.int32)
proc physicsLoop() =
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
setView()
# Continuous or single step
if frameStepping == false:
mainScene.step(FRAME_TIME)
else:
if canStep == true:
canStep = false
mainScene.step(FRAME_TIME)
mainScene.render()
proc textLoop() =
let text_list = [
fmt"Total number of bodies: {bodycounter}",
fmt"View-scaling: {viewScale}",
"Left-click to spawn a polygon",
"Right-click to spawn a circle",
"Mouse-wheel to change perspective",
"R to reset perspective",
"Esc to quit",
]
textdraw.draw_text(
text_list.join("\n"),
Vec(x: 10, y: 10),
createColor("#7070cb")
)
proc main() =
# Initialize GLFW
glfw.initialize()
# Initialize text drawing
textdraw.init()
# Initialize the main window
var window_options = DefaultOpenglWindowConfig
window_options.size = (w: WINDOW_SIZE.w, h: WINDOW_SIZE.h)
window_options.title = "Impulse Engine (Nim) Ver.:$1" % VERSION
window_options.fullscreenMonitor = NoMonitor # No monitor specified; don't go fullscreen.
window_options.shareResourcesWith = glfw.Window(nil) # Don't share resources.
window_options.visible = true
window_options.decorated = true
window_options.resizable = false
window_options.stereo = false
window_options.srgbCapableFramebuffer = false
window_options.bits = (r: 8, g: 8, b: 8, a: 8, stencil: 8, depth: 24)
window_options.accumBufferBits = (r: 0, g: 0, b: 0, a: 0)
window_options.nAuxBuffers = 0
window_options.nMultiSamples = 0
window_options.refreshRate = some(FRAME_RATE) # 0 - use the current monitor refresh rate.
window_options.version = glv30
window_options.forwardCompat = false
window_options.debugContext = false
win = newWindow(window_options)
# Center window to screen
videoMode = glfw.getPrimaryMonitor().videoMode
win.pos = (
x: int(videoMode.size.w/2 - WINDOW_SIZE.w/2),
y: int(videoMode.size.h/2 - WINDOW_SIZE.h/2)
)
# Set the CTRL+C hook that raises the done flag
# (terminal window has to be focused!)
setControlCHook(proc() {.noconv.} = done = true)
# Set up event handlers, context and openGL
win.mouseButtonCb = mouseButtonCallback
win.keyCb = keyCallback
win.scrollCb = scrollCallback
win.makeContextCurrent()
initOpenGL()
# Set the swap interval for the current context:
# 0 - no syncing
# 1 - syncs the win.update to 1 screen refresh
when VSYNC == true:
glfw.swapInterval(1)
else:
glfw.swapInterval(0)
# Initialize view
initView()
# Initialize static(immovable) objects in the scene
var b: Body
# Middle circle
b = mainScene.add(centerCircle, 40.0f, 40.0)
b.setStatic()
# Bottom platform
var poly: Polygon = newPolygon()
poly.setBox(30.0f, 1.0f)
b = mainScene.add(poly, 40.0f, 55.0f)
b.setStatic()
b.setOrient(0)
# Left wall
poly = newPolygon()
poly.setBox(1.0f, 5.0f)
b = mainScene.add(poly, 11.0f, 49.0f)
b.setStatic()
b.setOrient(0)
# Right wall
poly = newPolygon()
poly.setBox(1.0f, 5.0f)
b = mainScene.add(poly, 69.0f, 49.0f)
b.setStatic()
b.setOrient(0)
# Main loop
while not done and not win.shouldClose:
# if mainScene.bodies.len() > 0:
# echo mainScene.bodies[mainScene.bodies.high].position, " ", mainScene.bodies[mainScene.bodies.high].velocity
when VSYNC == true:
## This consumes 100% of one CPU core on Windows OS, until another application
## needs more of the CPU (it seems to be a Windows driver issue). Once the
## CPU usage falls, it stays at the correct level!
# Impulse engine routine
physicsLoop()
# Draw diagnostic text
textLoop()
# Buffer swap + event poll.
win.swapBuffers()
glfw.pollEvents()
else:
## If someone knows a better delay mechanism,
## please contact me or open an issue on Github!
glfw.setTime(0)
# Impulse engine routine
physicsLoop()
# Draw diagnostic text
textLoop()
# Buffer swap + event poll.
win.swapBuffers()
glfw.pollEvents()
# Delay for frame syncing
var sleepTime = int(1000*(FRAME_TIME - glfw.getTime())) - 1
if sleepTime > 0:
os.sleep(sleepTime)
# Cleanup everything
win.destroy()
glfw.terminate()
echo "Application closed"
if isMainModule:
main()