-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.py
136 lines (115 loc) · 3.18 KB
/
cube.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
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import math
verticies = (
(1, -1, -1),
(1, 1, -1),
(-1, 1, -1),
(-1, -1, -1),
(1, -1, 1),
(1, 1, 1),
(-1, -1, 1),
(-1, 1, 1)
)
edges = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7)
)
def Cube():
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(verticies[vertex])
glEnd()
def hsv_to_rgb(h, s, v):
h_i = int(h*6)
f = h*6 - h_i
p = v * (1 - s)
q = v * (1 - f*s)
t = v * (1 - (1 - f) * s)
if h_i == 0:
r, g, b = v, t, p
elif h_i == 1:
r, g, b = q, v, p
elif h_i == 2:
r, g, b = p, v, t
elif h_i == 3:
r, g, b = p, q, v
elif h_i == 4:
r, g, b = t, p, v
else:
r, g, b = v, p, q
return r, g, b
def draw_rainbow_rectangle_with_border(x, y, width, height, hue):
# Draw filled rectangle
glBegin(GL_QUADS)
for i in range(int(width)):
color = hsv_to_rgb((hue + i/width) % 1, 1, 1)
glColor3f(*color)
glVertex2f(x + i, y)
glVertex2f(x + i, y + height)
glVertex2f(x + i + 1, y + height)
glVertex2f(x + i + 1, y)
glEnd()
# Draw border
glLineWidth(2) # Increased line width for visibility
glBegin(GL_LINE_LOOP)
for i, vertex in enumerate([(x, y), (x + width, y), (x + width, y + height), (x, y + height)]):
color = hsv_to_rgb((hue + i/4) % 1, 1, 1)
glColor3f(*color)
glVertex2f(*vertex)
glEnd()
def main():
pygame.init()
display = (800, 600)
screen = pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0, 0.0, -5)
clock = pygame.time.Clock()
hue = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
# Draw the rotating cube
Cube()
# Set up 2D drawing
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glOrtho(0, display[0], display[1], 0, -1, 1)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
rect_width = display[0] * 0.9 # 90% of window width
rect_height = 50
rect_x = (display[0] - rect_width) / 2 # Center horizontally
# Draw the rainbow rectangle with border at the top
draw_rainbow_rectangle_with_border(rect_x, 0, rect_width, rect_height, hue)
# Draw the matching rainbow rectangle with border at the bottom
draw_rainbow_rectangle_with_border(rect_x, display[1] - rect_height, rect_width, rect_height, hue)
# Restore 3D drawing
glPopMatrix()
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
pygame.display.flip()
# Update hue for next frame
hue = (hue + 0.002) % 1 # Slow down the color transition
clock.tick(60) # Limit to 60 FPS
main()