Skip to content

Commit 1645739

Browse files
committed
Add more details and make three-server faster
1 parent ca4cc93 commit 1645739

File tree

3 files changed

+301
-120
lines changed

3 files changed

+301
-120
lines changed

opengl_tutorial/tutorial.py

+237-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,25 @@ def construct(self):
5353
).shift(0.5 * DOWN)
5454

5555
vertices_label = OpenGLText("Vertices").next_to(vertices, UP, buff=0.5)
56-
self.add(vertices, vertices_label)
56+
vertex_data = (
57+
OpenGLVGroup(
58+
OpenGLVGroup(
59+
OpenGLText("position: (x1, y1, z1)"),
60+
OpenGLText("normal: (nx1, ny1, nz1)").shift(DOWN),
61+
),
62+
OpenGLVGroup(
63+
OpenGLText("position: (x2, y2, z2)").shift(2 * DOWN),
64+
OpenGLText("normal: (nx2, ny2, nz2)").shift(3 * DOWN),
65+
).shift(0.5 * DOWN),
66+
OpenGLVGroup(
67+
OpenGLText("position: (x3, y3, z3)").shift(4 * DOWN),
68+
OpenGLText("normal: (nx3, ny3, nz3)").shift(5 * DOWN),
69+
).shift(DOWN),
70+
)
71+
.scale(0.5)
72+
.next_to(vertices_label, DOWN, buff=0.5)
73+
)
74+
self.add(vertex_data, vertices_label)
5775

5876
# Vertices.
5977
self.play(FadeIn(vertices_label, shift=DOWN))
@@ -67,7 +85,10 @@ def construct(self):
6785
vertices_label.get_center()
6886
)
6987
self.play(FadeOut(vertices_label, shift=UP))
70-
self.play(FadeIn(vertex_shader_label, shift=DOWN))
88+
self.play(
89+
FadeIn(vertex_shader_label, shift=DOWN),
90+
ReplacementTransform(vertex_data, vertices),
91+
)
7192

7293
vertex_shader_diagram = OpenGLVGroup(vertex_shader_label, vertices)
7394
diagrams.add(vertex_shader_diagram.copy())
@@ -358,6 +379,7 @@ def construct(self):
358379
transformed_mesh.attributes["position"] = (
359380
projection @ transformed_mesh.attributes["position"].T
360381
).T
382+
print(transformed_mesh.attributes["position"])
361383

362384
untransformed_points = untransformed_mesh.attributes["position"].copy()
363385
transformed_points = transformed_mesh.attributes["position"].copy()
@@ -408,6 +430,80 @@ def toggle_grid_lines():
408430
self.interactive_embed()
409431

410432

433+
class InputTypes(Scene):
434+
def construct(self):
435+
shader = Shader(
436+
self.renderer.context,
437+
source=dict(
438+
vertex_shader="""
439+
#version 330
440+
441+
in vec4 position;
442+
uniform mat4 projection;
443+
out vec4 color;
444+
445+
void main() {
446+
if (position.x > 0.0) {
447+
color = vec4(0, 0, 1, 1);
448+
} else {
449+
color = vec4(1, 0, 0, 1);
450+
}
451+
gl_Position = projection * position;
452+
}
453+
""",
454+
fragment_shader="""
455+
#version 330
456+
457+
in vec4 color;
458+
out vec4 frag_color;
459+
460+
void main() {
461+
frag_color = color;
462+
}
463+
""",
464+
),
465+
)
466+
467+
attributes = np.zeros(3, dtype=[("position", np.float32, (4,))])
468+
attributes["position"] = np.array(
469+
[
470+
[-1, -1, 0, 1],
471+
[-1, 1, 0, 1],
472+
[1, 1, 0, 1],
473+
]
474+
)
475+
476+
mesh = Mesh(shader, attributes)
477+
478+
width = config["frame_width"]
479+
height = config["frame_height"]
480+
depth = 20
481+
projection = np.array(
482+
[
483+
[2 / width, 0, 0, 0],
484+
[0, 2 / height, 0, 0],
485+
[0, 0, 2 / depth, 0],
486+
[0, 0, 0, 1],
487+
]
488+
)
489+
mesh.shader.set_uniform("projection", tuple(projection.T.ravel()))
490+
491+
self.add(mesh)
492+
493+
self.renderer.camera = OpenGLCamera(orthographic=True)
494+
grid = tutorial_utils.get_grid_lines(7, 5)
495+
496+
def toggle_grid_lines():
497+
if grid in self.mobjects:
498+
self.remove(grid)
499+
else:
500+
self.add(grid)
501+
502+
self.set_key_function(" ", toggle_grid_lines)
503+
504+
self.interactive_embed()
505+
506+
411507
class ModelVisualization(Scene):
412508
def construct(self):
413509
shader = Shader(
@@ -508,6 +604,86 @@ def scale_callback(sender, data):
508604
self.interactive_embed()
509605

510606

607+
class UpdateMesh(Scene):
608+
def construct(self):
609+
shader = Shader(
610+
self.renderer.context,
611+
source=dict(
612+
vertex_shader="""
613+
#version 330
614+
615+
uniform mat4 projection;
616+
uniform mat4 model;
617+
in vec4 position;
618+
619+
void main() {
620+
gl_Position = projection * model * position;
621+
}
622+
""",
623+
fragment_shader="""
624+
#version 330
625+
626+
out vec4 frag_color;
627+
628+
void main() {
629+
frag_color = vec4(1, 0, 0, 1);
630+
}
631+
""",
632+
),
633+
)
634+
635+
projection = opengl.orthographic_projection_matrix(near=-10, far=10)
636+
shader.shader_program["projection"] = projection
637+
638+
model = tuple(np.eye(4).T.ravel())
639+
shader.shader_program["model"] = model
640+
641+
attributes = np.zeros(6, dtype=[("position", np.float32, (4,))])
642+
attributes["position"] = np.array(
643+
[
644+
[-1, -1, 0, 1],
645+
[-1, 1, 0, 1],
646+
[1, 1, 0, 1],
647+
[-1, -1, 0, 1],
648+
[1, -1, 0, 1],
649+
[1, 1, 0, 1],
650+
]
651+
)
652+
mesh = Mesh(shader, attributes)
653+
654+
# attributes = np.zeros(4, dtype=[("position", np.float32, (4,))])
655+
# attributes["position"] = np.array(
656+
# [
657+
# [-1, -1, 0, 1],
658+
# [-1, 1, 0, 1],
659+
# [1, 1, 0, 1],
660+
# [1, -1, 0, 1],
661+
# ]
662+
# )
663+
# mesh = Mesh(shader, attributes, indices=np.array([0, 1, 2, 0, 3, 2]))
664+
665+
self.add(mesh)
666+
667+
t = 0
668+
669+
def update_mesh(dt):
670+
nonlocal t
671+
transformation_matrix = np.array(
672+
[
673+
[1, 0, 0, t],
674+
[0, 1, 0, 0],
675+
[0, 0, 1, 0],
676+
[0, 0, 0, 1],
677+
]
678+
)
679+
t += dt
680+
mesh.shader.set_uniform("model", tuple(transformation_matrix.T.ravel()))
681+
682+
self.add_updater(update_mesh)
683+
684+
self.interactive_embed()
685+
686+
511687
class ViewVisualization(Scene):
512688
def construct(self):
513689
self.grid_size = 5
@@ -1215,8 +1391,8 @@ def update_earth(mob, dt):
12151391

12161392
class Gallery(Scene):
12171393
def construct(self):
1218-
config["background_color"] = "#ece6e2"
1219-
default_light_position = [3, 3, 1]
1394+
config["background_color"] = "#2A2A2A"
1395+
default_light_position = [3, 3, 2]
12201396
self.point_lights.append(
12211397
{
12221398
"position": default_light_position,
@@ -1320,6 +1496,9 @@ def light_position_callback(sender, data):
13201496
"IcosahedronGeometry",
13211497
"TetrahedronGeometry",
13221498
"CylinderGeometry",
1499+
"ConeGeometry",
1500+
"CircleGeometry",
1501+
"PlaneGeometry",
13231502
],
13241503
"default_value": default_geometry_name,
13251504
"callback": geometry_callback,
@@ -1343,7 +1522,7 @@ def light_position_callback(sender, data):
13431522
"name": "background color",
13441523
"widget": "color_edit3",
13451524
"callback": background_color_callback,
1346-
"default_value": (0, 0, 0, 0),
1525+
"default_value": (42, 42, 42, 0),
13471526
}
13481527
)
13491528
self.widgets.append(
@@ -1357,6 +1536,59 @@ def light_position_callback(sender, data):
13571536
}
13581537
)
13591538

1539+
translation_matrix = np.eye(4)
1540+
rotation_matrix = np.eye(4)
1541+
scale_matrix = np.eye(4)
1542+
1543+
def update_mesh(dt):
1544+
model_matrix = translation_matrix @ rotation_matrix @ scale_matrix
1545+
current_mesh.model_matrix = model_matrix
1546+
# current_mesh.normal_matrix = model_matrix
1547+
1548+
def translation_callback(sender, data):
1549+
nonlocal translation_matrix
1550+
coords = dearpygui.core.get_value(sender)
1551+
translation_matrix = opengl.translation_matrix(*coords)
1552+
1553+
def rotation_callback(sender, data):
1554+
nonlocal rotation_matrix
1555+
coords = dearpygui.core.get_value(sender)
1556+
rotation_matrix = opengl.rotation_matrix(*coords)
1557+
1558+
def scale_callback(sender, data):
1559+
nonlocal scale_matrix
1560+
val = dearpygui.core.get_value(sender)
1561+
scale_matrix = opengl.scale_matrix(val)
1562+
1563+
self.widgets.extend(
1564+
[
1565+
{
1566+
"name": "translation",
1567+
"widget": "slider_float3",
1568+
"callback": translation_callback,
1569+
"min_value": -10,
1570+
"max_value": 10,
1571+
},
1572+
{
1573+
"name": "rotation",
1574+
"widget": "slider_float3",
1575+
"callback": rotation_callback,
1576+
"min_value": -PI,
1577+
"max_value": PI,
1578+
},
1579+
{
1580+
"name": "scale",
1581+
"widget": "slider_float",
1582+
"callback": scale_callback,
1583+
"min_value": -PI,
1584+
"max_value": PI,
1585+
"default_value": 1,
1586+
},
1587+
]
1588+
)
1589+
1590+
self.add_updater(update_mesh)
1591+
13601592
self.interactive_embed()
13611593

13621594

0 commit comments

Comments
 (0)