Skip to content

Commit 3407bfa

Browse files
committed
Add sierpinsky carpet zoom, multiple types of Julia set
1 parent 8b0871f commit 3407bfa

File tree

6 files changed

+221
-15
lines changed

6 files changed

+221
-15
lines changed

game/julia.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import arcade, arcade.gui, pyglet, json
2+
3+
from PIL import Image
4+
5+
from game.shader import create_julia_shader
6+
from utils.constants import menu_background_color, button_style
7+
from utils.preload import button_texture, button_hovered_texture
8+
9+
class JuliaViewer(arcade.gui.UIView):
10+
def __init__(self, pypresence_client):
11+
super().__init__()
12+
13+
self.pypresence_client = pypresence_client
14+
15+
with open("settings.json", "r") as file:
16+
self.settings_dict = json.load(file)
17+
18+
self.max_iter = self.settings_dict.get("julia_max_iter", 200)
19+
self.zoom = 1.0
20+
self.real_min = -self.settings_dict.get("julia_escape_radius", 2)
21+
self.real_max = self.settings_dict.get("julia_escape_radius", 2)
22+
self.imag_min = -self.settings_dict.get("julia_escape_radius", 2)
23+
self.imag_max = self.settings_dict.get("julia_escape_radius", 2)
24+
25+
def zoom_at(self, center_x, center_y, zoom_factor):
26+
center_real = self.real_min + (center_x / self.width) * (self.real_max - self.real_min)
27+
center_imag = self.imag_min + (center_y / self.height) * (self.imag_max - self.imag_min)
28+
29+
new_real_range = (self.real_max - self.real_min) / zoom_factor
30+
new_imag_range = (self.imag_max - self.imag_min) / zoom_factor
31+
32+
self.real_min = center_real - new_real_range / 2
33+
self.real_max = center_real + new_real_range / 2
34+
self.imag_min = center_imag - new_imag_range / 2
35+
self.imag_max = center_imag + new_imag_range / 2
36+
37+
def on_show_view(self):
38+
super().on_show_view()
39+
40+
self.shader_program, self.julia_image = create_julia_shader(self.window.width, self.window.height, self.settings_dict.get("julia_precision", "Single").lower(), self.settings_dict.get("julia_escape_radius", 2), self.settings_dict.get("julia_type", "Classic swirling"))
41+
42+
self.julia_sprite = pyglet.sprite.Sprite(img=self.julia_image)
43+
44+
self.create_image()
45+
46+
self.pypresence_client.update(state='Viewing Julia', details=f'Zoom: {self.zoom}\nMax Iterations: {self.max_iter}', start=self.pypresence_client.start_time)
47+
48+
self.setup_ui()
49+
50+
def main_exit(self):
51+
from menus.main import Main
52+
self.window.show_view(Main(self.pypresence_client))
53+
54+
def setup_ui(self):
55+
self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1)))
56+
57+
self.info_box = self.anchor.add(arcade.gui.UIBoxLayout(space_between=10, vertical=False), anchor_x="center", anchor_y="top")
58+
self.zoom_label = self.info_box.add(arcade.gui.UILabel(text=f"Zoom: {self.zoom}", font_name="Protest Strike", font_size=16))
59+
self.max_iter_label = self.info_box.add(arcade.gui.UILabel(text=f"Max Iterations: {self.max_iter}", font_name="Protest Strike", font_size=16))
60+
61+
self.back_button = arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='<--', style=button_style, width=100, height=50)
62+
self.back_button.on_click = lambda event: self.main_exit()
63+
self.anchor.add(self.back_button, anchor_x="left", anchor_y="top", align_x=5, align_y=-5)
64+
65+
def create_image(self):
66+
with self.shader_program:
67+
self.shader_program['u_maxIter'] = self.max_iter
68+
self.shader_program['u_resolution'] = (self.window.width, self.window.height)
69+
self.shader_program['u_real_range'] = (self.real_min, self.real_max)
70+
self.shader_program['u_imag_range'] = (self.imag_min, self.imag_max)
71+
self.shader_program.dispatch(self.julia_image.width, self.julia_image.height, 1, barrier=pyglet.gl.GL_ALL_BARRIER_BITS)
72+
73+
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> bool | None:
74+
super().on_mouse_press(x, y, button, modifiers)
75+
76+
if button == arcade.MOUSE_BUTTON_LEFT:
77+
zoom = self.settings_dict.get("julia_zoom_increase", 2)
78+
elif button == arcade.MOUSE_BUTTON_RIGHT:
79+
zoom = 1 / self.settings_dict.get("julia_zoom_increase", 2)
80+
else:
81+
return
82+
83+
self.zoom *= zoom
84+
85+
self.zoom_label.text = f"Zoom: {self.zoom}"
86+
87+
self.zoom_at(self.window.mouse.data["x"], self.window.mouse.data["y"], zoom)
88+
self.create_image()
89+
90+
self.pypresence_client.update(state='Viewing Julia', details=f'Zoom: {self.zoom}\nMax Iterations: {self.max_iter}', start=self.pypresence_client.start_time)
91+
92+
def on_draw(self):
93+
self.window.clear()
94+
self.julia_sprite.draw()
95+
self.ui.draw()

game/mandelbrot.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
from PIL import Image
44

55
from game.shader import create_mandelbrot_shader
6-
from utils.constants import menu_background_color, button_style, initial_real_min, initial_real_max, initial_imag_min, initial_imag_max
6+
from utils.constants import menu_background_color, button_style, mandelbrot_initial_real_min, mandelbrot_initial_real_max, mandelbrot_initial_imag_min, mandelbrot_initial_imag_max
77
from utils.preload import button_texture, button_hovered_texture
88

99
class MandelbrotViewer(arcade.gui.UIView):
1010
def __init__(self, pypresence_client):
1111
super().__init__()
1212

1313
self.pypresence_client = pypresence_client
14-
self.real_min = initial_real_min
15-
self.real_max = initial_real_max
16-
self.imag_min = initial_imag_min
17-
self.imag_max = initial_imag_max
14+
self.real_min = mandelbrot_initial_real_min
15+
self.real_max = mandelbrot_initial_real_max
16+
self.imag_min = mandelbrot_initial_imag_min
17+
self.imag_max = mandelbrot_initial_imag_max
1818

1919
with open("settings.json", "r") as file:
2020
self.settings_dict = json.load(file)

game/play.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ def on_show_view(self):
2929
self.sierpinsky_carpet_button = self.grid.add(arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='Sierpinsky Carpet', style=button_style, width=200, height=200), row=0, column=1)
3030
self.sierpinsky_carpet_button.on_click = lambda event: self.sierpinsky_carpet()
3131

32+
self.julia_button = self.grid.add(arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='Julia', style=button_style, width=200, height=200), row=0, column=2)
33+
self.julia_button.on_click = lambda event: self.julia()
34+
3235
def main_exit(self):
3336
from menus.main import Main
3437
self.window.show_view(Main(self.pypresence_client))
@@ -40,3 +43,7 @@ def mandelbrot(self):
4043
def sierpinsky_carpet(self):
4144
from game.sierpinsky_carpet import SierpinskyCarpetViewer
4245
self.window.show_view(SierpinskyCarpetViewer(self.pypresence_client))
46+
47+
def julia(self):
48+
from game.julia import JuliaViewer
49+
self.window.show_view(JuliaViewer(self.pypresence_client))

game/shader.py

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import pyglet
2+
from utils.constants import c_for_julia_type
3+
24
mandelbrot_compute_source = """#version 430 core
35
46
uniform int u_maxIter;
@@ -54,11 +56,17 @@
5456
5557
uniform int u_depth;
5658
uniform int u_zoom;
59+
uniform vec2 u_center;
5760
layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
5861
layout(location = 0, rgba32f) uniform image2D img_output;
5962
6063
void main() {
61-
ivec2 coord = ivec2(gl_GlobalInvocationID.xy);
64+
{vec2type} centered = {vec2type}(gl_GlobalInvocationID.xy) - u_center;
65+
{vec2type} zoomed = centered / u_zoom;
66+
{vec2type} final_coord = zoomed + u_center;
67+
68+
ivec2 coord = ivec2(final_coord);
69+
6270
bool isHole = false;
6371
6472
for (int i = 0; i < u_depth; ++i) {
@@ -75,9 +83,66 @@
7583
7684
"""
7785

86+
julia_compute_source = """#version 430 core
87+
88+
uniform int u_maxIter;
89+
uniform vec2 u_resolution;
90+
uniform vec2 u_real_range;
91+
uniform vec2 u_imag_range;
92+
93+
layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
94+
layout(location = 0, rgba32f) uniform image2D img_output;
95+
96+
{vec2type} map_pixel({floattype} x, {floattype} y, {vec2type} resolution, {vec2type} real_range, {vec2type} imag_range) {
97+
{floattype} real = real_range.x + (x / resolution.x) * (real_range.y - real_range.x);
98+
{floattype} imag = imag_range.x + (y / resolution.y) * (imag_range.y - imag_range.x);
99+
return {vec2type}(real, imag);
100+
}
101+
102+
void main() {
103+
ivec2 texel_coord = ivec2(gl_GlobalInvocationID.xy);
104+
105+
int R = {escape_radius};
106+
{vec2type} c = {vec2type}{julia_c};
107+
108+
{vec2type} z = map_pixel({floattype}(texel_coord.x), {floattype}(texel_coord.y), u_resolution, u_real_range, u_imag_range);
109+
110+
int iters = 0;
111+
112+
while ((z.x * z.x + z.y * z.y) < pow(R, 2) && iters < u_maxIter) {
113+
{floattype} xtemp = z.x * z.x - z.y * z.y;
114+
z.y = 2 * z.x * z.y + c.y;
115+
z.x = xtemp + c.x;
116+
117+
iters = iters + 1;
118+
}
119+
120+
vec4 value = vec4(0.0, 0.0, 0.0, 1.0);
121+
122+
if (iters != u_maxIter) {
123+
float t = float(iters) / float(u_maxIter);
124+
float pow_amount = 0.7;
125+
t = pow(t, pow_amount);
126+
127+
value.r = 9.0 * (1.0 - t) * t * t * t;
128+
value.g = 15.0 * (1.0 - t) * (1.0 - t) * t * t;
129+
value.b = 8.5 * (1.0 - t) * (1.0 - t) * (1.0 - t) * t;
130+
}
131+
132+
imageStore(img_output, texel_coord, value);
133+
}
134+
"""
135+
78136
def create_sierpinsky_carpet_shader(width, height, precision="single"):
79137
shader_source = sierpinsky_carpet_compute_source
80138

139+
if precision == "single":
140+
shader_source = shader_source.replace("{vec2type}", "vec2").replace("{floattype}", "float")
141+
elif precision == "double":
142+
shader_source = shader_source.replace("{vec2type}", "dvec2").replace("{floattype}", "double")
143+
else:
144+
raise TypeError("Invalid Precision")
145+
81146
shader_program = pyglet.graphics.shader.ComputeShaderProgram(shader_source)
82147

83148
sierpinsky_carpet_image = pyglet.image.Texture.create(width, height, internalformat=pyglet.gl.GL_RGBA32F)
@@ -87,6 +152,31 @@ def create_sierpinsky_carpet_shader(width, height, precision="single"):
87152

88153
return shader_program, sierpinsky_carpet_image
89154

155+
def create_julia_shader(width, height, precision="single", escape_radius=2, julia_type="Classic swirling"):
156+
shader_source = julia_compute_source
157+
158+
if precision == "single":
159+
shader_source = shader_source.replace("{vec2type}", "vec2").replace("{floattype}", "float")
160+
elif precision == "double":
161+
shader_source = shader_source.replace("{vec2type}", "dvec2").replace("{floattype}", "double")
162+
else:
163+
raise TypeError("Invalid Precision")
164+
165+
julia_c = c_for_julia_type[julia_type]
166+
shader_source = shader_source.replace("{julia_c}", str(julia_c))
167+
168+
shader_source = shader_source.replace("{escape_radius}", str(escape_radius))
169+
170+
shader_program = pyglet.graphics.shader.ComputeShaderProgram(shader_source)
171+
172+
julia_image = pyglet.image.Texture.create(width, height, internalformat=pyglet.gl.GL_RGBA32F)
173+
174+
uniform_location = shader_program['img_output']
175+
julia_image.bind_image_texture(unit=uniform_location)
176+
177+
return shader_program, julia_image
178+
179+
90180
def create_mandelbrot_shader(width, height, precision="single"):
91181
shader_source = mandelbrot_compute_source
92182

game/sierpinsky_carpet.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self, pypresence_client):
2222
def on_show_view(self):
2323
super().on_show_view()
2424

25-
self.shader_program, self.sierpinsky_carpet_image = create_sierpinsky_carpet_shader(self.window.width, self.window.height, self.settings_dict.get("precision", "Single").lower())
25+
self.shader_program, self.sierpinsky_carpet_image = create_sierpinsky_carpet_shader(self.window.width, self.window.height, self.settings_dict.get("sierpinsky_precision", "Single").lower())
2626

2727
self.sierpinsky_carpet_sprite = pyglet.sprite.Sprite(img=self.sierpinsky_carpet_image)
2828

@@ -50,9 +50,8 @@ def setup_ui(self):
5050
def create_image(self):
5151
with self.shader_program:
5252
self.shader_program['u_depth'] = self.depth
53-
#self.shader_program['u_zoom'] = int(self.zoom)
54-
#self.shader_program['u_resolution'] = self.window.size
55-
#self.shader_program['u_center'] = self.click_center
53+
self.shader_program['u_zoom'] = int(self.zoom)
54+
self.shader_program['u_center'] = self.click_center
5655
self.shader_program.dispatch(self.sierpinsky_carpet_image.width, self.sierpinsky_carpet_image.height, 1, barrier=pyglet.gl.GL_ALL_BARRIER_BITS)
5756

5857
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> bool | None:
@@ -73,7 +72,7 @@ def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> bool |
7372

7473
self.create_image()
7574

76-
self.pypresence_client.update(state='Viewing Sierpinsky Carpet', details=f'Zoom: {self.zoom}\nMax Iterations: {self.depth}', start=self.pypresence_client.start_time)
75+
self.pypresence_client.update(state='Viewing Sierpinsky Carpet', details=f'Zoom: {self.zoom}\nDepth: {self.depth}', start=self.pypresence_client.start_time)
7776

7877
def on_draw(self):
7978
self.window.clear()

utils/constants.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@
77
log_dir = 'logs'
88
discord_presence_id = 1365949409254441000
99

10-
initial_real_min = -2.0
11-
initial_real_max = 1.0
12-
initial_imag_min = -1.0
13-
initial_imag_max = 1.0
10+
mandelbrot_initial_real_min = -2.0
11+
mandelbrot_initial_real_max = 1.0
12+
mandelbrot_initial_imag_min = -1.0
13+
mandelbrot_initial_imag_max = 1.0
14+
15+
c_for_julia_type = {
16+
"Classic swirling": (-0.7, 0.27015),
17+
"Douady rabbit": (-0.123, 0.745),
18+
"Nebula-style": (0.285, 0),
19+
"Snowflake": (-0.8, 0.156)
20+
}
1421

1522
button_style = {'normal': UITextureButtonStyle(font_name="Protest Strike", font_color=arcade.color.BLACK), 'hover': UITextureButtonStyle(font_name="Protest Strike", font_color=arcade.color.BLACK),
1623
'press': UITextureButtonStyle(font_name="Protest Strike", font_color=arcade.color.BLACK), 'disabled': UITextureButtonStyle(font_name="Protest Strike", font_color=arcade.color.BLACK)}
@@ -32,9 +39,17 @@
3239
"Max Iterations": {"type": "slider", "min": 100, "max": 10000, "config_key": "mandelbrot_max_iter", "default": 200}
3340
},
3441
"Sierpinsky Carpet": {
42+
"Float Precision": {"type": "option", "options": ["Single", "Double"], "config_key": "sierpinsky_precision", "default": "Single"},
3543
"Zoom Increase Per Click": {"type": "slider", "min": 2, "max": 100, "config_key": "sierpinsky_zoom_increase", "default": 2},
3644
"Depth": {"type": "slider", "min": 2, "max": 10000, "config_key": "sierpinsky_depth", "default": 10}
3745
},
46+
"Julia": {
47+
"Type": {"type": "option", "options": ["Classic swirling", "Douady rabbit", "Nebula-style", "Snowflake"], "config_key": "julia_type", "default": "Classic swirling"},
48+
"Float Precision": {"type": "option", "options": ["Single", "Double"], "config_key": "julia_precision", "default": "Single"},
49+
"Escape Radius": {"type": "slider", "min": 1, "max": 10, "config_key": "julia_escape_radius", "default": 2},
50+
"Zoom Increase Per Click": {"type": "slider", "min": 2, "max": 100, "config_key": "julia_zoom_increase", "default": 2},
51+
"Max Iterations": {"type": "slider", "min": 100, "max": 10000, "config_key": "julia_max_iter", "default": 200}
52+
},
3853
"Graphics": {
3954
"Window Mode": {"type": "option", "options": ["Windowed", "Fullscreen", "Borderless"], "config_key": "window_mode", "default": "Windowed"},
4055
"Resolution": {"type": "option", "options": ["1366x768", "1440x900", "1600x900", "1920x1080", "2560x1440", "3840x2160"], "config_key": "resolution"},

0 commit comments

Comments
 (0)