Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·363 lines (318 loc) · 13.2 KB
/
Copy pathmain.py
File metadata and controls
executable file
·363 lines (318 loc) · 13.2 KB
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
357
358
359
360
361
362
363
#!/usr/bin/env -S uv run --script
"""
Interpolation: animates 3 teapots (gold/brass/pewter) along linear,
trigonometric-eased, and cubic-eased interpolation paths between the same
start and end points, to visually compare the resulting spacing/timing.
"""
import argparse
import sys
import traceback
from typing import ClassVar
import OpenGL.GL as gl
from easing import cubic_interp, trig_interp
from ncca.ngl import Mat3, Mat4, Vec3, logger, look_at, perspective
from ncca.ngl.opengl import Primitives, ShaderLib
from PySide6.QtCore import Qt, QTimer
from PySide6.QtGui import QSurfaceFormat
from PySide6.QtOpenGL import QOpenGLWindow
from PySide6.QtWidgets import QApplication
class MainWindow(QOpenGLWindow):
"""
The main window for the OpenGL application.
Inherits from QOpenGLWindow to provide a canvas for OpenGL rendering within a PySide6 GUI.
It handles user input (mouse, keyboard) for camera control and manages the OpenGL context.
"""
MATERIALS: ClassVar = {
"gold": (
Vec3(0.274725, 0.1995, 0.0745),
Vec3(0.75164, 0.60648, 0.22648),
Vec3(0.628281, 0.555802, 0.3666065),
51.2,
),
"brass": (
Vec3(0.329412, 0.223529, 0.027451),
Vec3(0.780392, 0.568627, 0.113725),
Vec3(0.992157, 0.941176, 0.807843),
27.8974,
),
"pewter": (
Vec3(0.10588, 0.058824, 0.113725),
Vec3(0.427451, 0.470588, 0.541176),
Vec3(0.3333, 0.3333, 0.521569),
9.84615,
),
}
def __init__(self, parent: object = None) -> None:
super().__init__()
# --- Camera and Transformation Attributes ---
self.mouse_global_tx: Mat4 = (
Mat4()
) # Global transformation matrix controlled by the mouse
self.view: Mat4 = Mat4()
self.project: Mat4 = Mat4()
self.model_position: Vec3 = Vec3() # Position of the model in world space
# --- Window and UI Attributes ---
self.window_width: int = 1024 # Window width
self.window_height: int = 720 # Window height
self.setTitle("Interpolation")
# --- Mouse Control Attributes for Camera Manipulation ---
self.rotate: bool = False # Flag to check if the scene is being rotated
self.translate: bool = (
False # Flag to check if the scene is being translated (panned)
)
self.spin_x_face: int = 0 # Accumulated rotation around the X-axis
self.spin_y_face: int = 0 # Accumulated rotation around the Y-axis
self.original_x_rotation: int = (
0 # Initial X position of the mouse when a rotation starts
)
self.original_y_rotation: int = (
0 # Initial Y position of the mouse when a rotation starts
)
self.original_x_pos: int = (
0 # Initial X position of the mouse when a translation starts
)
self.original_y_pos: int = (
0 # Initial Y position of the mouse when a translation starts
)
self.INCREMENT: float = 0.01 # Sensitivity for translation
self.ZOOM: float = 0.1 # Sensitivity for zooming
def initializeGL(self) -> None:
self.makeCurrent()
gl.glClearColor(0.4, 0.4, 0.4, 1.0)
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glEnable(gl.GL_MULTISAMPLE)
self.view = look_at(Vec3(0, 0, 25), Vec3(0, 0, 0), Vec3(0, 1, 0))
ShaderLib.load_shader(
"Phong", "shaders/PhongVertex.glsl", "shaders/PhongFragment.glsl"
)
Primitives.load_default_primitives()
self.start = Vec3(-8, -5, 0)
self.end = Vec3(8, 5, 0)
self.time: float = 0.0
self.animate: bool = True
def _set_material(self, name: str) -> None:
ambient, diffuse, specular, shininess = self.MATERIALS[name]
ShaderLib.set_uniform("ambient", ambient.x, ambient.y, ambient.z)
ShaderLib.set_uniform("diffuseColour", diffuse.x, diffuse.y, diffuse.z)
ShaderLib.set_uniform("specularColour", specular.x, specular.y, specular.z)
ShaderLib.set_uniform("shininess", shininess)
def paintGL(self) -> None:
self.makeCurrent()
gl.glViewport(0, 0, self.window_width, self.window_height)
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
rot_x = Mat4().rotate_x(self.spin_x_face)
rot_y = Mat4().rotate_y(self.spin_y_face)
self.mouse_global_tx = rot_y @ rot_x
self.mouse_global_tx[3, 0] = self.model_position.x
self.mouse_global_tx[3, 1] = self.model_position.y
self.mouse_global_tx[3, 2] = self.model_position.z
ShaderLib.use("Phong")
ShaderLib.set_uniform("lightPos", 5.0, 5.0, 10.0)
ShaderLib.set_uniform("viewerPos", 0.0, 0.0, 25.0)
def draw_teapot(pos: Vec3, material: str) -> None:
mv = (
self.view @ self.mouse_global_tx @ Mat4().translate(pos.x, pos.y, pos.z)
)
mvp = self.project @ mv
normal_matrix = Mat3.from_mat4(mv).inverse().transposed()
ShaderLib.set_uniform("MVP", mvp)
ShaderLib.set_uniform("MV", mv)
ShaderLib.set_uniform("normalMatrix", normal_matrix)
self._set_material(material)
Primitives.draw("teapot")
linear_pos = self.start + (self.end - self.start) * self.time
trig_pos = trig_interp(self.start, self.end, self.time) + Vec3(0, 2, 0)
cubic_pos = cubic_interp(self.start, self.end, self.time) + Vec3(0, -2, 0)
draw_teapot(linear_pos, "gold")
draw_teapot(trig_pos, "brass")
draw_teapot(cubic_pos, "pewter")
if self.animate:
self.time += 0.005
if self.time >= 1.0:
self.time = 0.0
self.update()
def resizeGL(self, w: int, h: int) -> None:
"""
Called whenever the window is resized.
It's crucial to update the viewport and projection matrix here.
Parameters
----------
w : int
The new width of the window.
h : int
The new height of the window.
"""
# Update the stored width and height, considering high-DPI displays
self.window_width = int(w * self.devicePixelRatio())
self.window_height = int(h * self.devicePixelRatio())
self.project = perspective(45.0, float(w) / h, 0.01, 350.0)
def keyPressEvent(self, event) -> None:
"""
Handles keyboard press events.
Parameters
----------
event
The QKeyEvent object containing information about the key press.
"""
key = event.key()
if key == Qt.Key_Escape:
self.close()
elif key == Qt.Key_W:
gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE)
elif key == Qt.Key_S:
gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL)
elif key == Qt.Key_Space:
self.animate = not self.animate
elif key == Qt.Key_Left:
self.time = max(0.0, self.time - 0.01)
elif key == Qt.Key_Right:
self.time = min(1.0, self.time + 0.01)
# Trigger a redraw to apply changes
self.update()
super().keyPressEvent(event)
def mouseMoveEvent(self, event) -> None:
"""
Handles mouse movement events for camera control.
Parameters
----------
event
The QMouseEvent object containing the new mouse position.
"""
# Rotate the scene if the left mouse button is pressed
if self.rotate and event.buttons() == Qt.LeftButton:
position = event.position()
diff_x = position.x() - self.original_x_rotation
diff_y = position.y() - self.original_y_rotation
self.spin_x_face += int(0.5 * diff_y)
self.spin_y_face += int(0.5 * diff_x)
self.original_x_rotation = position.x()
self.original_y_rotation = position.y()
self.update()
# Translate (pan) the scene if the right mouse button is pressed
elif self.translate and event.buttons() == Qt.RightButton:
position = event.position()
diff_x = int(position.x() - self.original_x_pos)
diff_y = int(position.y() - self.original_y_pos)
self.original_x_pos = position.x()
self.original_y_pos = position.y()
self.model_position.x += self.INCREMENT * diff_x
self.model_position.y -= self.INCREMENT * diff_y
self.update()
def mousePressEvent(self, event) -> None:
"""
Handles mouse button press events to initiate rotation or translation.
Parameters
----------
event
The QMouseEvent object.
"""
position = event.position()
# Left button initiates rotation
if event.button() == Qt.LeftButton:
self.original_x_rotation = position.x()
self.original_y_rotation = position.y()
self.rotate = True
# Right button initiates translation
elif event.button() == Qt.RightButton:
self.original_x_pos = position.x()
self.original_y_pos = position.y()
self.translate = True
def mouseReleaseEvent(self, event) -> None:
"""
Handles mouse button release events to stop rotation or translation.
Parameters
----------
event
The QMouseEvent object.
"""
# Stop rotating when the left button is released
if event.button() == Qt.LeftButton:
self.rotate = False
# Stop translating when the right button is released
elif event.button() == Qt.RightButton:
self.translate = False
def wheelEvent(self, event) -> None:
"""
Handles mouse wheel events for zooming.
Parameters
----------
event
The QWheelEvent object.
"""
# Use the vertical wheel delta (angleDelta().y()) -- .x() is only
# populated by horizontal scroll gestures, which left plain vertical
# wheel/trackpad scrolling doing almost nothing and made stray
# horizontal trackpad noise cause small unintended zoom jumps.
# Scaling by the delta magnitude (120 = one standard wheel notch)
# instead of a fixed step also makes fast scrolling zoom
# proportionally rather than being capped to one step per event.
delta = event.angleDelta().y()
self.model_position.z += self.ZOOM * (delta / 120.0)
self.update()
class DebugApplication(QApplication):
"""
A custom QApplication subclass for improved debugging.
By default, Qt's event loop can suppress exceptions that occur within event handlers
(like paintGL or mouseMoveEvent), making it very difficult to debug as the application
may simply crash or freeze without any error message. This class overrides the `notify`
method to catch these exceptions, print a full traceback to the console, and then
re-raise the exception to halt the program, making the error immediately visible.
"""
def __init__(self, argv):
super().__init__(argv)
logger.info("Running in full debug mode")
def notify(self, receiver, event):
"""
Overrides the central event handler to catch and report exceptions.
"""
try:
# Attempt to process the event as usual
return super().notify(receiver, event)
except Exception:
# If an exception occurs, print the full traceback
traceback.print_exc()
# Re-raise the exception to stop the application
raise
if __name__ == "__main__":
# --- Application Entry Point ---
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--smoketest",
nargs="?",
const=200,
default=None,
type=int,
metavar="MS",
help="run for MS milliseconds (default 200), print SMOKETEST OK and exit",
)
parser.add_argument(
"--debug",
action="store_true",
help="run with DebugApplication (tracebacks from Qt event handlers)",
)
args = parser.parse_args()
format: QSurfaceFormat = QSurfaceFormat()
# Request 4x multisampling for anti-aliasing
format.setSamples(4)
# Request OpenGL version 4.1 as this is the highest supported on macOS
format.setMajorVersion(4)
format.setMinorVersion(1)
# Request a Core Profile context, which removes deprecated, fixed-function pipeline features
format.setProfile(QSurfaceFormat.CoreProfile)
# Request a 24-bit depth buffer for proper 3D sorting
format.setDepthBufferSize(24)
# Set default format for all new OpenGL contexts
QSurfaceFormat.setDefaultFormat(format)
if args.debug:
app = DebugApplication(sys.argv)
else:
app = QApplication(sys.argv)
window = MainWindow()
# Set the initial window size
window.resize(1024, 720)
# Show the window
window.show()
if args.smoketest is not None:
QTimer.singleShot(args.smoketest, lambda: (print("SMOKETEST OK"), app.quit()))
# Start the application's event loop
sys.exit(app.exec())