-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathopengl.py
136 lines (111 loc) · 4.86 KB
/
opengl.py
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
from manim import *
from manim.opengl import *
import os
from pathlib import Path
# Copied from https://3b1b.github.io/manim/getting_started/example_scenes.html#surfaceexample.
# Lines that do not yet work with the Community Version are commented.
class InteractiveDevelopment(Scene):
def construct(self):
circle = OpenGLCircle()
circle.set_fill(BLUE, opacity=0.5)
circle.set_stroke(BLUE_E, width=4)
square = OpenGLSquare()
self.play(ShowCreation(square))
self.wait()
# This opens an iPython termnial where you can keep writing
# lines as if they were part of this construct method.
# In particular, 'square', 'circle' and 'self' will all be
# part of the local namespace in that terminal.
self.embed()
# Try copying and pasting some of the lines below into
# the interactive shell
self.play(ReplacementTransform(square, circle))
self.wait()
self.play(circle.animate.stretch(4, 0))
self.play(Rotate(circle, 90 * DEGREES))
self.play(circle.animate.shift(2 * RIGHT).scale(0.25))
# text = Text("""
# In general, using the interactive shell
# is very helpful when developing new scenes
# """)
# self.play(Write(text))
# # In the interactive shell, you can just type
# # play, add, remove, clear, wait, save_state and restore,
# # instead of self.play, self.add, self.remove, etc.
# # To interact with the window, type touch(). You can then
# # scroll in the window, or zoom by holding down 'z' while scrolling,
# # and change camera perspective by holding down 'd' while moving
# # the mouse. Press 'r' to reset to the standard camera position.
# # Press 'q' to stop interacting with the window and go back to
# # typing new commands into the shell.
# # In principle you can customize a scene to be responsive to
# # mouse and keyboard interactions
# always(circle.move_to, self.mouse_point)
class SurfaceExample(Scene):
def construct(self):
# surface_text = Text("For 3d scenes, try using surfaces")
# surface_text.fix_in_frame()
# surface_text.to_edge(UP)
# self.add(surface_text)
# self.wait(0.1)
torus1 = OpenGLTorus(r1=1, r2=1)
torus2 = OpenGLTorus(r1=3, r2=1)
sphere = OpenGLSphere(radius=3, resolution=torus1.resolution)
# You can texture a surface with up to two images, which will
# be interpreted as the side towards the light, and away from
# the light. These can be either urls, or paths to a local file
# in whatever you've set as the image directory in
# the custom_config.yml file
script_location = Path(os.path.realpath(__file__)).parent
day_texture = (
script_location / "assets" / "1280px-Whole_world_-_land_and_oceans.jpg"
)
night_texture = script_location / "assets" / "1280px-The_earth_at_night.jpg"
surfaces = [
OpenGLTexturedSurface(surface, day_texture, night_texture)
for surface in [sphere, torus1, torus2]
]
for mob in surfaces:
mob.shift(IN)
mob.mesh = OpenGLSurfaceMesh(mob)
mob.mesh.set_stroke(BLUE, 1, opacity=0.5)
# Set perspective
frame = self.renderer.camera
frame.set_euler_angles(
theta=-30 * DEGREES,
phi=70 * DEGREES,
)
surface = surfaces[0]
self.play(
FadeIn(surface),
ShowCreation(surface.mesh, lag_ratio=0.01, run_time=3),
)
for mob in surfaces:
mob.add(mob.mesh)
surface.save_state()
self.play(Rotate(surface, PI / 2), run_time=2)
for mob in surfaces[1:]:
mob.rotate(PI / 2)
self.play(Transform(surface, surfaces[1]), run_time=3)
self.play(
Transform(surface, surfaces[2]),
# Move camera frame during the transition
frame.animate.increment_phi(-10 * DEGREES),
frame.animate.increment_theta(-20 * DEGREES),
run_time=3,
)
# Add ambient rotation
frame.add_updater(lambda m, dt: m.increment_theta(-0.1 * dt))
# Play around with where the light is
# light_text = Text("You can move around the light source")
# light_text.move_to(surface_text)
# light_text.fix_in_frame()
# self.play(FadeTransform(surface_text, light_text))
light = self.camera.light_source
self.add(light)
light.save_state()
self.play(light.animate.move_to(3 * IN), run_time=5)
self.play(light.animate.shift(10 * OUT), run_time=5)
# drag_text = Text("Try moving the mouse while pressing d or s")
# drag_text.move_to(light_text)
# drag_text.fix_in_frame()