66
77
88class Cube :
9- def __init__ (self , ctx , width = 1.0 , outer_color = (1.0 , 0.0 , 0.0 , 1.0 ), inner_color = (0.0 , 1.0 , 0.0 , 1.0 ), scale = 1.0 ):
9+ def __init__ (self , ctx , outer_size , inner_size , outer_color = (1.0 , 0.0 , 0.0 , 1.0 ), inner_color = (0.0 , 1.0 , 0.0 , 1.0 ), scale = 1.0 ):
1010 self .ctx = ctx
11- self .width = width
11+ self .outer_size = outer_size
12+ self .inner_size = inner_size
1213 self .outer_color = outer_color
1314 self .inner_color = inner_color
1415 self .scale = scale # The scale factor applied in the shader
1516
1617 # Define the outer and inner cube vertices with their respective colors
1718 outer_vertices = np .array ([
1819 # Outer cube vertices (x, y, z, r, g, b, a)
19- - 0.5 , - 0.5 , - 0.5 , * outer_color ,
20- 0.5 , - 0.5 , - 0.5 , * outer_color ,
21- 0.5 , 0.5 , - 0.5 , * outer_color ,
22- - 0.5 , 0.5 , - 0.5 , * outer_color ,
23- - 0.5 , - 0.5 , 0.5 , * outer_color ,
24- 0.5 , - 0.5 , 0.5 , * outer_color ,
25- 0.5 , 0.5 , 0.5 , * outer_color ,
26- - 0.5 , 0.5 , 0.5 , * outer_color ,
20+ - self . outer_size [ 0 ] / 2 , - self . outer_size [ 1 ] / 2 , - self . outer_size [ 2 ] / 2 , * outer_color ,
21+ self . outer_size [ 0 ] / 2 , - self . outer_size [ 1 ] / 2 , - self . outer_size [ 2 ] / 2 , * outer_color ,
22+ self . outer_size [ 0 ] / 2 , self . outer_size [ 1 ] / 2 , - self . outer_size [ 2 ] / 2 , * outer_color ,
23+ - self . outer_size [ 0 ] / 2 , self . outer_size [ 1 ] / 2 , - self . outer_size [ 2 ] / 2 , * outer_color ,
24+ - self . outer_size [ 0 ] / 2 , - self . outer_size [ 1 ] / 2 , self . outer_size [ 2 ] / 2 , * outer_color ,
25+ self . outer_size [ 0 ] / 2 , - self . outer_size [ 1 ] / 2 , self . outer_size [ 2 ] / 2 , * outer_color ,
26+ self . outer_size [ 0 ] / 2 , self . outer_size [ 1 ] / 2 , self . outer_size [ 2 ] / 2 , * outer_color ,
27+ - self . outer_size [ 0 ] / 2 , self . outer_size [ 1 ] / 2 , self . outer_size [ 2 ] / 2 , * outer_color ,
2728 ], dtype = 'f4' )
2829
2930 inner_vertices = np .array ([
3031 # Inner cube vertices (x, y, z, r, g, b, a)
31- - 0.5 + width , - 0.5 + width , - 0.5 + width , * inner_color ,
32- 0.5 - width , - 0.5 + width , - 0.5 + width , * inner_color ,
33- 0.5 - width , 0.5 - width , - 0.5 + width , * inner_color ,
34- - 0.5 + width , 0.5 - width , - 0.5 + width , * inner_color ,
35- - 0.5 + width , - 0.5 + width , 0.5 - width , * inner_color ,
36- 0.5 - width , - 0.5 + width , 0.5 - width , * inner_color ,
37- 0.5 - width , 0.5 - width , 0.5 - width , * inner_color ,
38- - 0.5 + width , 0.5 - width , 0.5 - width , * inner_color ,
32+ - self . inner_size [ 0 ] / 2 , - self . inner_size [ 1 ] / 2 , - self . inner_size [ 2 ] / 2 , * inner_color ,
33+ self . inner_size [ 0 ] / 2 , - self . inner_size [ 1 ] / 2 , - self . inner_size [ 2 ] / 2 , * inner_color ,
34+ self . inner_size [ 0 ] / 2 , self . inner_size [ 1 ] / 2 , - self . inner_size [ 2 ] / 2 , * inner_color ,
35+ - self . inner_size [ 0 ] / 2 , self . inner_size [ 1 ] / 2 , - self . inner_size [ 2 ] / 2 , * inner_color ,
36+ - self . inner_size [ 0 ] / 2 , - self . inner_size [ 1 ] / 2 , self . inner_size [ 2 ] / 2 , * inner_color ,
37+ self . inner_size [ 0 ] / 2 , - self . inner_size [ 1 ] / 2 , self . inner_size [ 2 ] / 2 , * inner_color ,
38+ self . inner_size [ 0 ] / 2 , self . inner_size [ 1 ] / 2 , self . inner_size [ 2 ] / 2 , * inner_color ,
39+ - self . inner_size [ 0 ] / 2 , self . inner_size [ 1 ] / 2 , self . inner_size [ 2 ] / 2 , * inner_color ,
3940 ], dtype = 'f4' )
4041
4142 # Combine the vertices
@@ -108,16 +109,17 @@ def set_inner_color(self, color):
108109 self .vertices [3 ::7 ] = color [3 ]
109110 self .vbo .write (self .vertices )
110111
111- def update_rotation (self , angle , axis ):
112+ def update_rotation (self , angle ):
112113 rotation = Matrix44 .from_eulers ((angle , angle , 0 ), dtype = 'f4' )
113114 self .rotation_matrix = rotation
114115
115116 def render (self ):
116- self .ctx .clear ()
117117 self .program ['model' ].write (self .rotation_matrix )
118118 self .program ['scale' ].value = self .scale # Pass the scale to the shader
119119 self .vao .render (moderngl .TRIANGLES )
120120
121+ def lerp_color (color1 , color2 , t ):
122+ return tuple ((1 - t ) * c1 + t * c2 for c1 , c2 in zip (color1 , color2 ))
121123
122124# Initialize Pygame and ModernGL
123125pygame .init ()
@@ -127,14 +129,19 @@ def render(self):
127129ctx .enable (moderngl .BLEND )
128130ctx .blend_func = moderngl .SRC_ALPHA , moderngl .ONE_MINUS_SRC_ALPHA
129131
132+ big_inner_color = (0 , 1 , 0 )
133+ big_outer_color = (1 , 0 , 0 )
134+
130135# Create multiple cubes with progressively smaller sizes
131136cubes = []
132- num_cubes = 5
137+ num_cubes = 25
138+ delta = 1 / num_cubes
133139for i in range (num_cubes ):
134- scale = 1.0 - (i * 0.1 ) # Make each cube smaller by 10%
135- outer_color = (1.0 - i * 0.2 , i * 0.2 , 0.0 , 0.5 ) # Gradient color for outer part
136- inner_color = (i * 0.2 , 1.0 - i * 0.2 , 0.0 , 0.5 ) # Gradient color for inner part
137- cubes .append (Cube (ctx , width = 0.1 , outer_color = outer_color , inner_color = inner_color , scale = scale ))
140+ outer_size = np .array ([1.0 , 1.0 , 1.0 ]) * (1.0 - (i * delta )) # Outer size gradually decreases
141+ inner_size = np .array ([1.0 , 1.0 , 1.0 ]) * (1.0 - (i * delta )) # Inner size slightly smaller
142+ outer_color = (1.0 - i * delta , i * delta , 0.0 , 0.5 ) # Gradient color for outer part (1, 0, 0, 0.5)
143+ inner_color = (i * delta , 1.0 - i * delta , 0.0 , 0.5 ) # Gradient color for inner part (0, 1, 0, 0.5)
144+ cubes .append (Cube (ctx , outer_size = outer_size , inner_size = inner_size , outer_color = outer_color , inner_color = inner_color , scale = 1.0 ))
138145
139146# Main loop
140147clock = pygame .time .Clock ()
@@ -145,11 +152,23 @@ def render(self):
145152 if event .type == pygame .QUIT :
146153 running = False
147154
155+ ctx .clear ()
156+
148157 # Update rotation for each cube
149158 for cube in cubes :
150- cube .update_rotation (angle , axis = 'y' )
159+ cube .update_rotation (angle )
151160 cube .render ()
152161
162+ new_inner_color = (0.0 , 1.0 , 0.0 , 0.5 ) # New color for inner part
163+ new_outer_color = (1.0 , 0.0 , 0.0 , 0.5 ) # New color for outer part
164+
165+ for i in range (num_cubes ):
166+ cube = cubes [i ]
167+ outer_color = (1.0 - i * delta , i * delta , 0.0 , 0.5 ) # Gradient color for outer part
168+ inner_color = (i * delta , 1.0 - i * delta , 0.0 , 0.5 ) # Gradient color for inner part
169+ cube .set_outer_color (outer_color )
170+ cube .set_inner_color (inner_color )
171+
153172 # Increment angle for rotation
154173 angle += 0.01
155174
0 commit comments