-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphiclab3.py
154 lines (120 loc) · 3.87 KB
/
graphiclab3.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
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import math
def drawCar():
glutInit()
glClearColor(0.6, 0.6, 0.6, 1.0) # Тут цвет фона
glPushMatrix()
glColor3ub(236, 151, 4)
glTranslated(0, 0, 0)
glutSolidCube(0.5)
glColor3ub(236, 0, 0)
glTranslated(-0.6, 0, 0.125)
glutSolidCube(0.75)
glTranslated(0.6, 0, -0.125)
glutSolidCube(0.05)
glTranslated(-1, 0, 0.125)
glutSolidCube(0.75)
glTranslated(1, 0, -0.125)
glutSolidCube(0.05)
glColor3ub(0, 0, 0)
glTranslated(-1, -0.3, -0.3)
glutSolidSphere(0.1, 20, 20)
glTranslated(0, 0.6, 0)
glutSolidSphere(0.1, 20, 20)
glTranslated(1, -0.1, 0)
glutSolidSphere(0.1, 20, 20)
glTranslated(0, -0.4, 0)
glutSolidSphere(0.1, 20, 20)
glPopMatrix()
glPushMatrix()
glColor3ub(0, 0, 0)
glTranslated(0.22, -0.1, 0.05)
glutSolidCube(0.1)
glColor3ub(0, 0, 0)
glTranslated(0, 0.2, 0)
glutSolidCube(0.1)
glColor3ub(240, 0, 230)
glTranslated(0, -0.1, -0.1)
glutSolidCube(0.2)
glPopMatrix()
pygame.init()
display = (1200, 900)
scree = pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glShadeModel(GL_SMOOTH)
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
glEnable(GL_LIGHT0)
glLightfv(GL_LIGHT0, GL_AMBIENT, [0.5, 0.5, 0.5, 1])
glLightfv(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1])
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
gluLookAt(0, -8, 0, 0, 0, 0, 0, 0, 1)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
glLoadIdentity()
# init mouse movement and center mouse on screen
displayCenter = [scree.get_size()[i] // 2 for i in range(2)]
mouseMove = [0, 0]
pygame.mouse.set_pos(displayCenter)
pygame.mouse.set_visible(False)
up_down_angle = 0.0
paused = False
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE or event.key == pygame.K_RETURN:
run = False
if event.key == pygame.K_PAUSE or event.key == pygame.K_p:
paused = not paused
pygame.mouse.set_pos(displayCenter)
if not paused:
if event.type == pygame.MOUSEMOTION:
mouseMove = [event.pos[i] - displayCenter[i] for i in range(2)]
pygame.mouse.set_pos(displayCenter)
if not paused:
# get keys
keypress = pygame.key.get_pressed()
# mouseMove = pygame.mouse.get_rel()
# init model view matrix
glLoadIdentity()
# apply the look up and down
up_down_angle += mouseMove[1] * 0.1
glRotatef(up_down_angle, 1.0, 0.0, 0.0)
# init the view matrix
glPushMatrix()
glLoadIdentity()
ms = 0.06
# apply the movement
if keypress[pygame.K_w]:
glTranslatef(0, 0, ms)
if keypress[pygame.K_s]:
glTranslatef(0, 0, -ms)
if keypress[pygame.K_d]:
glTranslatef(-ms, 0, 0)
if keypress[pygame.K_a]:
glTranslatef(ms, 0, 0)
# apply the left and right rotation
glRotatef(mouseMove[0] * 0.1, 0.0, 1.0, 0.0)
# multiply the current matrix by the get the new view matrix and store the final vie matrix
glMultMatrixf(viewMatrix)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
# apply view matrix
glPopMatrix()
glMultMatrixf(viewMatrix)
glLightfv(GL_LIGHT0, GL_POSITION, [1, -1, 1, 0])
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glPushMatrix()
drawCar()
glPopMatrix()
pygame.display.flip()
pygame.time.wait(10)
pygame.quit()