-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectObject.py
More file actions
76 lines (65 loc) · 2.49 KB
/
Copy pathSelectObject.py
File metadata and controls
76 lines (65 loc) · 2.49 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
from ncca.ngl import Mat3, Transform
from ncca.ngl.opengl import DefaultShader, Primitives, ShaderLib
def color_id_generator():
"""
A generator that yields unique, sequential RGB color IDs (0-255).
"""
# The maximum number of unique colors is 256*256*256
max_colors = 1 << 24
for i in range(max_colors):
# Use bitwise operators to derive R, G, B from a single integer.
# This is cleaner and faster than manual nested loops.
r = i & 0xFF
g = (i >> 8) & 0xFF
b = (i >> 16) & 0xFF
yield (r, g, b)
# 1. Create a single, shared instance of the generator
color_gen = color_id_generator()
class SelectObject:
def __init__(self, position):
self.position = position
self.selected = False
try:
self.colour_id = next(color_gen)
except StopIteration:
print("Error: Color ID generator is exhausted.")
self.colour_id = (-1, -1, -1)
def check_selection_colour(self, colour):
if colour == self.colour_id:
self.selected ^= True
return True
return False
def load_matrices_to_shader(self, local_tx, global_tx, view, project):
ShaderLib.use(DefaultShader.DIFFUSE)
model_matrix = global_tx @ local_tx.matrix()
MV = view @ model_matrix
mvp = project @ MV
normal_matrix = Mat3.from_mat4(MV)
normal_matrix = normal_matrix.inverse().transposed()
ShaderLib.set_uniform("MVP", mvp)
ShaderLib.set_uniform("MV", MV)
ShaderLib.set_uniform("normalMatrix", normal_matrix)
def load_matrices_to_colour_shader(self, local_tx, global_tx, view, project):
ShaderLib.use(DefaultShader.COLOUR)
ShaderLib.set_uniform(
"Colour",
self.colour_id[0] / 255.0,
self.colour_id[1] / 255.0,
self.colour_id[2] / 255.0,
1.0,
)
model_matrix = global_tx @ local_tx.matrix()
MV = view @ model_matrix
mvp = project @ MV
ShaderLib.set_uniform("MVP", mvp)
def draw(self, selection, global_tx, view, project):
tx = Transform()
tx.set_position(self.position.x, self.position.y, self.position.z)
if selection:
self.load_matrices_to_colour_shader(tx, global_tx, view, project)
else:
self.load_matrices_to_shader(tx, global_tx, view, project)
if self.selected:
Primitives.draw("teapot")
else:
Primitives.draw("cube")