-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab6.py
More file actions
138 lines (115 loc) · 2.68 KB
/
lab6.py
File metadata and controls
138 lines (115 loc) · 2.68 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 *
x, y = 0, 0
vx, vy = 0.01, 0.06
surfaces = (
(0, 1, 2, 3),
(3, 2, 7, 6),
(6, 7, 5, 4),
(4, 5, 1, 0),
(1, 5, 7, 2),
(4, 0, 3, 6),
)
normals = [
(0, 0, -1),
(-1, 0, 0),
(0, 0, 1),
(1, 0, 0),
(0, 1, 0),
(0, -1, 0)
]
colors = (
(1, 1, 1),
(0, 1, 0),
(0, 0, 1),
(0, 1, 0),
(0, 0, 1),
(1, 0, 1),
(0, 1, 0),
(1, 0, 1),
(0, 1, 0),
(0, 0, 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():
global x, y
glBegin(GL_QUADS)
verticies = (
(x + 1, y - 1, -1),
(x + 1, y + 1, -1),
(x - 1, y + 1, -1),
(x - 1, y - 1, -1),
(x + 1, y - 1, 1),
(x + 1, y + 1, 1),
(x - 1, y - 1, 1),
(x - 1, y + 1, 1),
)
for i_surface, surface in enumerate(surfaces):
n = 0
glNormal3fv(normals[i_surface])
for vertex in surface:
n += 1
glColor3fv(colors[n])
glVertex3fv(verticies[vertex])
glEnd()
glColor3fv(colors[0])
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(verticies[vertex])
glEnd()
def main():
global surfaces
global x, y, vx, vy
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
clock = pygame.time.Clock()
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
glTranslatef(0, 0, -5)
# источники света
glLight(GL_LIGHT0, GL_POSITION, (5, 5, 5, 1)) # позиции света справа, сверху, со стороны наблюдателя
glLightfv(GL_LIGHT0, GL_AMBIENT, (0, 0, 0, 1))
glLightfv(GL_LIGHT0, GL_DIFFUSE, (1, 1, 1, 1))
glEnable(GL_DEPTH_TEST)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
glRotatef(1, 3, 1, 1)
Cube()
x += vx
y += vy
if x >= 1 or x <= -1:
vx = -vx
if y >= 1 or y <= -1:
vy = -vy
glDisable(GL_LIGHT0)
glDisable(GL_LIGHTING)
glDisable(GL_COLOR_MATERIAL)
pygame.display.flip()
clock.tick(60)
main()