Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Menu for choosing which fractal to view #13

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 87 additions & 43 deletions ray_marcher_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
win_size = (1280, 720)

#Maximum frames per second
fps = 30
max_fps = 30

#Forces an 'up' orientation when True, free-camera when False
gimbal_lock = False
Expand Down Expand Up @@ -85,7 +85,7 @@ def infinite_spheres():
def butterweed_hills():
obj = Object()
obj.add(OrbitInitZero())
for i in range(30):
for _ in range(30):
obj.add(FoldAbs())
obj.add(FoldScaleTranslate(1.5, (-1.0,-0.5,-0.2)))
obj.add(OrbitSum((0.5, 0.03, 0.0)))
Expand All @@ -97,7 +97,7 @@ def butterweed_hills():
def mandelbox():
obj = Object()
obj.add(OrbitInitInf())
for i in range(16):
for _ in range(16):
obj.add(FoldBox(1.0))
obj.add(FoldSphere(0.5, 1.0))
obj.add(FoldScaleOrigin(2.0))
Expand All @@ -108,7 +108,7 @@ def mandelbox():
def mausoleum():
obj = Object()
obj.add(OrbitInitZero())
for i in range(8):
for _ in range(8):
obj.add(FoldBox(0.34))
obj.add(FoldMenger())
obj.add(FoldScaleTranslate(3.28, (-5.27,-0.34,0.0)))
Expand All @@ -119,7 +119,7 @@ def mausoleum():

def menger():
obj = Object()
for i in range(8):
for _ in range(8):
obj.add(FoldAbs())
obj.add(FoldMenger())
obj.add(FoldScaleTranslate(3.0, (-2,-2,0)))
Expand All @@ -130,7 +130,7 @@ def menger():
def tree_planet():
obj = Object()
obj.add(OrbitInitInf())
for i in range(30):
for _ in range(30):
obj.add(FoldRotateY(0.44))
obj.add(FoldAbs())
obj.add(FoldMenger())
Expand All @@ -143,7 +143,7 @@ def tree_planet():
def sierpinski_tetrahedron():
obj = Object()
obj.add(OrbitInitZero())
for i in range(9):
for _ in range(9):
obj.add(FoldSierpinski())
obj.add(FoldScaleTranslate(2, -1))
obj.add(Tetrahedron(color=(0.8,0.8,0.5)))
Expand All @@ -152,7 +152,7 @@ def sierpinski_tetrahedron():
def snow_stadium():
obj = Object()
obj.add(OrbitInitInf())
for i in range(30):
for _ in range(30):
obj.add(FoldRotateY(3.33))
obj.add(FoldSierpinski())
obj.add(FoldRotateX(0.15))
Expand All @@ -165,7 +165,7 @@ def snow_stadium():
def test_fractal():
obj = Object()
obj.add(OrbitInitInf())
for i in range(20):
for _ in range(20):
obj.add(FoldSierpinski())
obj.add(FoldMenger())
obj.add(FoldRotateY(math.pi/2))
Expand Down Expand Up @@ -208,6 +208,11 @@ def reorthogonalize(mat):
u, s, v = np.linalg.svd(mat)
return np.dot(u, v)

# move the cursor back , only if the window is focused
def center_mouse():
if pygame.key.get_focused():
pygame.mouse.set_pos(screen_center)

#--------------------------------------------------
# Video Recording
#
Expand All @@ -227,19 +232,56 @@ def reorthogonalize(mat):
# can import the image sequence to editing software
# to convert it to a video.
#
# You can press 's' anytime for a screenshot.
# You can press 'c' anytime for a screenshot.
#---------------------------------------------------

if __name__ == '__main__':
pygame.init()
window = pygame.display.set_mode(win_size, OPENGL | DOUBLEBUF)
window = pygame.display.set_mode(win_size)
pygame.mouse.set_visible(False)
pygame.mouse.set_pos(screen_center)
center_mouse()

#======================================================
# Change the fractal here
# Create a menu screen
#======================================================
obj_render = tree_planet()
obj_render = None

menu = {
'1': ('Infinite Spheres', infinite_spheres),
'2': ('Butterweed Hills', butterweed_hills),
'3': ('Mandelbox', mandelbox),
'4': ('Mausoleum', mausoleum),
'5': ('Menger Sponge', menger),
'6': ('Tree Planet', tree_planet),
'7': ('Sierpinski Tetrahedron', sierpinski_tetrahedron),
'8': ('Snow Stadium', snow_stadium),
'9': ('Example Fractal', test_fractal)
}

font_size = 60
my_font = pygame.font.Font(None, font_size) # None gets default font

widest_title = max([my_font.size(title[0]) for title in menu.values()])[0]
menu_left_margin = (win_size[0] - widest_title) / 2

i = 2
for k, v in menu.items():
text = k + " : " + v[0]
text_surface = my_font.render(text, True, ( 255, 255, 255 ))
window.blit(text_surface, ( menu_left_margin, i * font_size ))
i += 1
text_surface = my_font.render('Fractal Menu', True, ( 255, 255, 255 ))
window.blit(text_surface, ( (win_size[0] - text_surface.get_width()) / 2, 60 ))
pygame.display.flip()

while True:
# sleep until a key is pressed
event = pygame.event.wait()
if event.type == pygame.QUIT: sys.exit(0)
if event.type == pygame.KEYDOWN and event.unicode in '123456789':
obj_render = menu[event.unicode][1]()
break
window = pygame.display.set_mode(win_size, OPENGL | DOUBLEBUF)
#======================================================

#======================================================
Expand All @@ -255,10 +297,10 @@ def reorthogonalize(mat):
program = shader.compile(camera)
print("Compiled!")

matID = glGetUniformLocation(program, "iMat");
prevMatID = glGetUniformLocation(program, "iPrevMat");
resID = glGetUniformLocation(program, "iResolution");
ipdID = glGetUniformLocation(program, "iIPD");
matID = glGetUniformLocation(program, "iMat")
prevMatID = glGetUniformLocation(program, "iPrevMat")
resID = glGetUniformLocation(program, "iResolution")
ipdID = glGetUniformLocation(program, "iIPD")

glUseProgram(program)
glUniform2fv(resID, 1, win_size)
Expand Down Expand Up @@ -319,7 +361,7 @@ def start_playback():
elif event.key == pygame.K_ESCAPE:
sys.exit(0)

mat[3,:3] += vel / fps
mat[3,:3] += vel * (clock.get_time() / 1000)

if auto_velocity:
de = obj_render.DE(mat[3]) * auto_multiplier
Expand Down Expand Up @@ -352,38 +394,40 @@ def start_playback():
mouse_pos = pygame.mouse.get_pos()
dx,dy = 0,0
if prev_mouse_pos is not None:
pygame.mouse.set_pos(screen_center)
dx = mouse_pos[0] - screen_center[0]
dy = mouse_pos[1] - screen_center[1]
center_mouse()
time_rate = (clock.get_time() / 1000.0) / (1 / max_fps)
dx = (mouse_pos[0] - screen_center[0]) * time_rate
dy = (mouse_pos[1] - screen_center[1]) * time_rate

if gimbal_lock:
look_x += dx * look_speed
look_y += dy * look_speed
look_y = min(max(look_y, -math.pi/2), math.pi/2)
if pygame.key.get_focused():
if gimbal_lock:
look_x += dx * look_speed
look_y += dy * look_speed
look_y = min(max(look_y, -math.pi/2), math.pi/2)

rx = make_rot(look_x, 1)
ry = make_rot(look_y, 0)
rx = make_rot(look_x, 1)
ry = make_rot(look_y, 0)

mat[:3,:3] = np.dot(ry, rx)
else:
rx = make_rot(dx * look_speed, 1)
ry = make_rot(dy * look_speed, 0)
mat[:3,:3] = np.dot(ry, rx)
else:
rx = make_rot(dx * look_speed, 1)
ry = make_rot(dy * look_speed, 0)

mat[:3,:3] = np.dot(ry, np.dot(rx, mat[:3,:3]))
mat[:3,:3] = reorthogonalize(mat[:3,:3])
mat[:3,:3] = np.dot(ry, np.dot(rx, mat[:3,:3]))
mat[:3,:3] = reorthogonalize(mat[:3,:3])

acc = np.zeros((3,), dtype=np.float32)
if all_keys[pygame.K_a]:
acc[0] -= speed_accel / fps
acc[0] -= speed_accel / max_fps
if all_keys[pygame.K_d]:
acc[0] += speed_accel / fps
acc[0] += speed_accel / max_fps
if all_keys[pygame.K_w]:
acc[2] -= speed_accel / fps
acc[2] -= speed_accel / max_fps
if all_keys[pygame.K_s]:
acc[2] += speed_accel / fps
acc[2] += speed_accel / max_fps

if np.dot(acc, acc) == 0.0:
vel *= speed_decel
vel *= speed_decel # TODO
else:
vel += np.dot(mat[:3,:3].T, acc)
vel_ratio = min(max_velocity, de) / (np.linalg.norm(vel) + 1e-12)
Expand Down Expand Up @@ -414,11 +458,11 @@ def start_playback():
shader.set('pos', mat[3,:3])

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glUniformMatrix4fv(matID, 1, False, mat);
glUniformMatrix4fv(prevMatID, 1, False, prevMat);
glUniformMatrix4fv(matID, 1, False, mat)
glUniformMatrix4fv(prevMatID, 1, False, prevMat)
prevMat = np.copy(mat)

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)
pygame.display.flip()
clock.tick(fps)
clock.tick(max_fps)
frame_num += 1