-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Dshape.py
More file actions
138 lines (117 loc) · 3.96 KB
/
3Dshape.py
File metadata and controls
138 lines (117 loc) · 3.96 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
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
137
138
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import math
import colorsys
import numpy as np
# =======================
# CONFIG
# =======================
WIDTH, HEIGHT = 1200, 800
points_per_frame = 5
z_growth_per_cycle = 2.0 # vertical growth per spirograph cycle
# Spirograph parameters
R = 200 # outer circle radius
k = 0.3 # ratio of inner circle radius r/R
l = 0.8 # distance of pen point from inner circle center / r
# Z twist factor for 3D effect
z_twist_amplitude = 50.0
z_twist_frequency = 4.0
# Camera
camera_distance = 800
camera_lerp = 0.05
zoom_speed = 20.0
# Mouse
last_mouse_pos = None
rotation_x, rotation_y = 0, 0
pan_x, pan_y = 0, 0
# Colors
colors = [colorsys.hsv_to_rgb(h/360, 1, 1) for h in range(0, 360, 2)]
# =======================
# SPIRAL STATE
# =======================
points = []
current_step = 0
current_cycle = 0
camera_z = 0
# =======================
# FUNCTIONS
# =======================
def spiro_point(t, cycle):
"""Compute 3D spirograph coordinates."""
x = R * ((1-k) * math.cos(t) + l*k * math.cos((1-k)/k * t))
y = R * ((1-k) * math.sin(t) - l*k * math.sin((1-k)/k * t))
z = cycle * z_growth_per_cycle + z_twist_amplitude * math.sin(z_twist_frequency * t)
return (x, y, z)
def add_new_points():
global current_step, current_cycle, points
for _ in range(points_per_frame):
t = current_step * 0.02
pt = spiro_point(t, current_cycle)
points.append(pt)
current_step += 1
if current_step >= int(2*math.pi/k): # approximate full cycle
current_step = 0
current_cycle += 1
def draw_points():
glBegin(GL_LINE_STRIP)
for idx, (x, y, z) in enumerate(points):
# Depth/color mapping
c = colors[idx % len(colors)]
z_factor = (z % (z_growth_per_cycle*10)) / (z_growth_per_cycle*10)
glColor3f(c[0]*z_factor, c[1]*z_factor, c[2]*z_factor)
glVertex3f(x, y, z)
glEnd()
# =======================
# MAIN LOOP
# =======================
def main():
global camera_z, rotation_x, rotation_y, pan_x, pan_y, last_mouse_pos, camera_distance
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT), DOUBLEBUF|OPENGL)
pygame.display.set_caption("3D Spirograph")
glMatrixMode(GL_PROJECTION)
gluPerspective(45, WIDTH/HEIGHT, 0.1, 5000.0)
glEnable(GL_DEPTH_TEST)
glLineWidth(2)
clock = pygame.time.Clock()
running = True
while running:
dt = clock.tick(60)/1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEWHEEL:
camera_distance += -event.y * zoom_speed
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button in [1,2,3]:
last_mouse_pos = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONUP:
last_mouse_pos = None
if event.type == pygame.MOUSEMOTION and last_mouse_pos:
dx, dy = event.pos[0]-last_mouse_pos[0], event.pos[1]-last_mouse_pos[1]
buttons = pygame.mouse.get_pressed()
if buttons[0]:
rotation_y += dx * 0.3
rotation_x += dy * 0.3
if buttons[1]:
pan_x += dx * 0.5
pan_y -= dy * 0.5
last_mouse_pos = event.pos
add_new_points()
# Camera follows highest Z
tip_z = points[-1][2] if points else 0
target_camera_z = tip_z + camera_distance
camera_z += (target_camera_z - camera_z) * camera_lerp
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(-pan_x, -pan_y, -camera_z)
glRotatef(rotation_x, 1,0,0)
glRotatef(rotation_y, 0,1,0)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
draw_points()
pygame.display.flip()
pygame.quit()
if __name__=="__main__":
main()