-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
323 lines (271 loc) · 7.86 KB
/
test.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
""" PyCube
Author: Michael King
Based and modified from original version found at:
http://stackoverflow.com/questions/30745703/rotating-a-cube-using-quaternions-in-pyopengl
"""
from quat import *
from geometry import *
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
cube_stickers = (
# 0 0
(-2.95, 1.025, 3.01),
(-2.95, 2.95, 3.01),
(-1.025, 2.95, 3.01),
(-1.025, 1.025, 3.01),
# 0 1
(-0.9625, 1.025, 3.01),
(-0.9625, 2.95, 3.01),
(0.9625, 2.95, 3.01),
(0.9625, 1.025, 3.01),
# 0 2
(1.025, 1.025, 3.01),
(1.025, 2.95, 3.01),
(2.95, 2.95, 3.01),
(2.95, 1.025, 3.01),
# 1 0
(-2.95, -0.9625, 3.01),
(-2.95, 0.9625, 3.01),
(-1.025, 0.9625, 3.01),
(-1.025, -0.9625, 3.01),
# 1 1
(-0.9625, -0.9625, 3.01),
(-0.9625, 0.9625, 3.01),
(0.9625, 0.9625, 3.01),
(0.9625, -0.9625, 3.01),
# 1 2
(1.025, -0.9625, 3.01),
(1.025, 0.9625, 3.01),
(2.95, 0.9625, 3.01),
(2.95, -0.9625, 3.01),
# 2 0
(-2.95, -2.95, 3.01),
(-2.95, -1.025, 3.01),
(-1.025, -1.025, 3.01),
(-1.025, -2.95, 3.01),
# 2 1
(-0.9625, -2.95, 3.01),
(-0.9625, -1.025, 3.01),
(0.9625, -1.025, 3.01),
(0.9625, -2.95, 3.01),
# 2 2
(1.025, -2.95, 3.01),
(1.025, -1.025, 3.01),
(2.95, -1.025, 3.01),
(2.95, -2.95, 3.01)
)
cube_pieces = (
(-2.95, -2.95, 2.95),
(-2.95, -1.025, 2.95),
(-1.025, -1.025, 2.95),
(-1.025, -2.95, 2.95),
(-2.95, -2.95, 1.025),
(-2.95, -1.025, 1.025),
(-1.025, -1.025, 1.025),
(-1.025, -2.95, 1.025)
)
up_face = (
(-3.0, 1.0, 3.0),
(-3.0, 3.0, 3.0), # 1
(3.0, 3.0, 3.0), # 2
(3.0, 1.0, 3.0),
(-3.0, 1.0, -3.0),
(-3.0, 3.0, -3.0), # 5
(3.0, 3.0, -3.0), # 6
(3.0, 1.0, -3.0)
# (0, 1, 2, 3), # Front
# (3, 2, 6, 7), # Right
# (7, 6, 5, 4), # Back
# (4, 5, 1, 0), # Left
# (1, 5, 6, 2), # Top
# (4, 0, 3, 7) # Bottom
)
'''
These pattern are for each set of edge pieces and corner
pieces on each face. They will shift when the faces are
rotated so these patterns will keep track of them.
_______________
| 1 | 2 | 2 |
|____|____|____|
| 1 | | 3 |
|____|____|____|
| 0 | 0 | 3 |
|____|____|____|
'''
# face_patterns = [
# [0, 1, 2, 3], # 0 Front
# [0, 1, 2, 3], # 1 Back
# [0, 1, 2, 3], # 2 Left
# [0, 1, 2, 3], # 3 Right
# [0, 1, 2, 3], # 4 Up
# [0, 1, 2, 3], # 5 Down
# ]
#
# front_edges = [
# [0, 1], # x
# [0, 3] # y
# ]
#
# back_edges = [
# [2, 3], # x
# [1, 2] # y
# ]
#
# left_edges = [
# [0, 1], # y
# [0, 1] # z
# ]
#
# right_edges = [
# [2, 3], # y
# [2, 3] # z
# ]
def draw_face():
glBegin(GL_LINES)
glColor3fv((0.5, 0.5, 0.5))
for edge in cube_edges:
for vertex in edge:
glVertex3fv(up_face[vertex])
glEnd()
def draw_stickers():
glBegin(GL_QUADS)
for v in range(len(cube_stickers)):
glVertex3fv(cube_stickers[v])
glEnd()
def cube():
# glBegin(GL_QUADS)
# for color, surface in zip(cube_colors, cube_surfaces):
# glColor3fv(color)
# for vertex in surface:
# glVertex3fv(cube_verts[vertex])
# glEnd()
glBegin(GL_QUADS)
for surface in cube_surfaces:
glColor3fv((0.3, 0.3, 0.3))
for vertex in surface:
glVertex3fv(cube_verts[vertex])
glEnd()
#
# White
glColor3fv((1.0, 1.0, 1.0))
draw_stickers()
glRotate(90, 1, 0, 0)
# Blue
glColor3fv((0.0, 0.318, 0.729))
draw_stickers()
glRotate(90, 1, 0, 0)
# Yellow
glColor3fv((1.0, 0.835, 0.0))
draw_stickers()
glRotate(90, 1, 0, 0)
# Green
glColor3fv((0.0, 0.62, 0.376))
draw_stickers()
glRotate(90, 0, 1, 0)
# Orange
glColor3fv((1.0, 0.345, 0.0))
draw_stickers()
glRotate(180, 0, 1, 0)
# Red
glColor3fv((0.8, 0.118, 0.118))
draw_stickers()
glBegin(GL_LINES)
glColor3fv((0.5, 0.5, 0.5))
for edge in cube_edges:
for vertex in edge:
glVertex3fv(cube_verts[vertex])
for edge in cube_edges:
for vertex in edge:
glVertex3fv(cube_pieces[vertex])
# for edge in cube_edges:
# for vertex in edge:
# glVertex3fv(up_face[vertex])
glEnd()
def main():
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
pygame.display.set_caption('PyCube')
# Using depth test to make sure closer colors are shown over further ones
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LESS)
# Default view
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0] / display[1]), 0.5, 40)
glTranslatef(0.0, 0.0, -17.5)
# set initial rotation
# glRotate(90, 1, 0, 0)
# glRotate(-15, 0, 0, 1)
# glRotate(15, 1, 0, 0)
inc_x = 0
inc_y = 0
accum = (1, 0, 0, 0)
zoom = 1
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
# Rotating about the x axis
if event.key == pygame.K_UP or event.key == pygame.K_w:
inc_x = pi / 100
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
inc_x = -pi / 100
# Rotating about the y axis
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
inc_y = pi / 100
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
inc_y = -pi / 100
if event.key == pygame.K_u:
print('up')
# Reset to default view
if event.key == pygame.K_SPACE:
inc_x = 0
inc_y = 0
accum = (1, 0, 0, 0)
zoom = 1
if event.type == pygame.KEYUP:
# Stoping rotation
if event.key == pygame.K_UP or event.key == pygame.K_DOWN or \
event.key == pygame.K_w or event.key == pygame.K_s:
inc_x = 0.0
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or \
event.key == pygame.K_a or event.key == pygame.K_d or \
event.key == pygame.K_u:
inc_y = 0.0
if event.type == pygame.MOUSEBUTTONDOWN:
# Increase scale (zoom) value
if event.button == 4:
if zoom < 1.6:
zoom += 0.05
# print('scroll up', zoom)
if event.type == pygame.MOUSEBUTTONUP:
# Increase scale (zoom) value
if event.button == 5:
if zoom > 0.2:
zoom -= 0.05
# print('scroll down', zoom)
# Get relative movement of mouse coordinates and update x and y incs
if pygame.mouse.get_pressed()[0] == 1:
(tmp_x, tmp_y) = pygame.mouse.get_rel()
# print(tmp_x, tmp_y)
inc_x = -tmp_y * pi / 450
inc_y = -tmp_x * pi / 450
pygame.mouse.get_rel() # prevents the cube from instantly rotating to a newly clicked mouse coordinate
rot_x = normalize(axisangle_to_q((1.0, 0.0, 0.0), inc_x))
rot_y = normalize(axisangle_to_q((0.0, 1.0, 0.0), inc_y))
accum = q_mult(accum, rot_x)
accum = q_mult(accum, rot_y)
glMatrixMode(GL_MODELVIEW)
glLoadMatrixf(q_to_mat4(accum))
glScalef(zoom, zoom, zoom)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
cube()
# drawFace()
# draw_axis()
pygame.display.flip()
# pygame.time.wait(1)
main()