-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
300 lines (241 loc) · 7.96 KB
/
Copy pathmain.py
File metadata and controls
300 lines (241 loc) · 7.96 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
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
#Autor del código base: Ivan Olmos Pineda
#Curso: Multiagentes - Graficas Computacionales
import math
import pygame
import requests
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from src.models import TrashCar
from src.models import TrashBlock
from src.models import FurnaceBlock
URL_BASE = "http://127.0.0.1:5000"
r = requests.post(URL_BASE+ "/games", allow_redirects=False)
information = r.json()
LOCATION = information["Location"]
agent_list = information["agents"]
# Set up display and camera
screen_width = 700
screen_height = 700
FOVY = 60.0
ZNEAR = 0.01
ZFAR = 900.0
BASE_RADIUS = 400.0
BASE_HEIGHT = 150.0
ELEVATION_ANGLE = 0.0
Theta = 45.0
EYE_Y = BASE_HEIGHT
EYE_X = 300.0
EYE_Y = 200.0
EYE_Z = 300.0
CENTER_X = 0
CENTER_Y = 0
CENTER_Z = 0
UP_X = 0
UP_Y = 1
UP_Z = 0
# Set up axis and plane dimensions
X_MIN = -700
X_MAX = 700
Y_MIN = -700
Y_MAX = 700
Z_MIN = -700
Z_MAX = 700
board_limit = 200
pygame.init()
agent_dict = {}
car_list = []
car_number = 4
trash_list = []
trash_number = 10
robot_positions=[
(board_limit,-board_limit),
(-board_limit,-board_limit),
(board_limit,board_limit),
(-board_limit,board_limit)
]
velocidad = 2.0
texture_list = []
filename_1 = "src/img/metalAmarillo.bmp"
filename_2 = "src/img/cemento.bmp"
filename_3 = "src/img/carro.bmp"
filename_4 = "src/img/carroAtras.bmp"
filename_5 = "src/img/carroVentana.bmp"
filename_6 = "src/img/carroPuerta.bmp"
filename_7 = "src/img/basura.bmp"
filename_8 = "src/img/furnace_front_on.bmp"
filename_9 = "src/img/furnace_front.bmp"
filename_10 = "src/img/furnace_side.bmp"
filename_11 = "src/img/furnace_top.bmp"
Theta = 0
Direction = [300.0,200.0,300.0]
ELEVATION_ANGLE = 0.0
def DegToRad(g):
return ((g*math.pi)/182.0)
def LookAt():
global EYE_X, EYE_Y, EYE_Z
rad = math.radians(Theta)
EYE_X = CENTER_X + BASE_RADIUS * math.cos(rad)
EYE_Z = CENTER_Z + BASE_RADIUS * math.sin(rad)
EYE_Y = BASE_HEIGHT + ELEVATION_ANGLE
def DisplayAxis():
glShadeModel(GL_FLAT)
glLineWidth(3.0)
#X axis in red
glColor3f(1.0,0.0,0.0)
glBegin(GL_LINES)
glVertex3f(X_MIN,0.0,0.0)
glVertex3f(X_MAX,0.0,0.0)
glEnd()
#Y axis in green
glColor3f(0.0,1.0,0.0)
glBegin(GL_LINES)
glVertex3f(0.0,Y_MIN,0.0)
glVertex3f(0.0,Y_MAX,0.0)
glEnd()
#Z axis in blue
glColor3f(0.0,0.0,1.0)
glBegin(GL_LINES)
glVertex3f(0.0,0.0,Z_MIN)
glVertex3f(0.0,0.0,Z_MAX)
glEnd()
glLineWidth(1.0)
def InitSimulation():
pygame.display.set_mode((screen_width, screen_height), DOUBLEBUF | OPENGL)
pygame.display.set_caption("OpenGL: Carros recogedores con MESA")
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(FOVY, screen_width/screen_height, ZNEAR, ZFAR)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(EYE_X,EYE_Y,EYE_Z,CENTER_X,CENTER_Y,CENTER_Z,UP_X,UP_Y,UP_Z)
glClearColor(0,0,0,0)
glEnable(GL_DEPTH_TEST)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
LoadTexture(filename_1)
LoadTexture(filename_2)
LoadTexture(filename_3)
LoadTexture(filename_4)
LoadTexture(filename_5)
LoadTexture(filename_6)
LoadTexture(filename_7)
LoadTexture(filename_8)
LoadTexture(filename_9)
LoadTexture(filename_10)
LoadTexture(filename_11)
#Identificar los tipos de agentes
for agent in agent_list:
if agent["type"] == "Robot":
cubo = TrashCar(agent["x"], agent["z"],1) #HACE FALTA ENCONTRAR LA FORMA DE PASAR EL ID EN VEZ DE 1
car_list.append(cubo)
elif agent["type"] == "FurnaceBlock":
cubo = FurnaceBlock(agent["x"] * 20 - 200, agent["z"] * 20 - 200)
else:
cubo = TrashBlock(board_limit)
trash_list.append(cubo)
agent_dict[agent["id"]] = cubo
def DisplayPlane():
glColor3f(230, 230, 230)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, texture_list[1])
glBegin(GL_QUADS)
glTexCoord2f(0.0, 0.0)
glVertex3d(-board_limit, 0.0, -board_limit)
glTexCoord2f(1.0, 0.0)
glVertex3d(board_limit, 0.0, -board_limit)
glTexCoord2f(1.0, 1.0)
glVertex3d(board_limit, 0.0, board_limit)
glTexCoord2f(0.0, 1.0)
glVertex3d(-board_limit, 0.0, board_limit)
glEnd()
glDisable(GL_TEXTURE_2D)
def display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
DisplayAxis()
DisplayPlane()
response = requests.get(URL_BASE + LOCATION)
agent_list = response.json()
for agent in agent_list:
if agent["type"] == "Robot":
agent_dict[agent["id"]].draw(texture_list,0, 2, 3, 4, 5)
agent_dict[agent["id"]].update(agent["x"] * 20 - 200, agent["z"] * 20 - 200, agent["platform_height"])
elif agent["type"] == "TrashBlock" and agent["condition"] == 1:
agent_dict[agent["id"]].drawTrash(texture_list,6, agent["x"] * 20 - 200, agent["platform_height"], agent["z"] * 20 - 200, agent["condition"])
elif agent["type"] == "FurnaceBlock":
if agent["condition"] == 0:
agent_dict[agent["id"]].draw(texture_list, 8, 9, 10)
else:
agent_dict[agent["id"]].draw(texture_list, 7, 9, 10)
done = False
def LoadTexture(filepath):
# Generate a new texture ID and add it to the texture list
texture_list.append(glGenTextures(1))
id = len(texture_list) - 1
# Bind the texture and set parameters
glBindTexture(GL_TEXTURE_2D, texture_list[id])
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
# Load the image and convert it to a suitable format
image = pygame.image.load(filepath).convert()
width, height = image.get_rect().size
image_data = pygame.image.tostring(image, "RGBA")
# Upload the image data to the currently bound texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data)
# Generate mipmaps for the texture
glGenerateMipmap(GL_TEXTURE_2D)
def HandleKeyEvent(event):
global Theta, ELEVATION_ANGLE, CENTER_X, CENTER_Z
move_step = 10.0
if event.key == pygame.K_RIGHT:
Theta += 2.0
if Theta > 359.0:
Theta -= 360.0
elif event.key == pygame.K_LEFT:
Theta -= 2.0
if Theta < 0.0:
Theta += 360.0
elif event.key == pygame.K_UP:
ELEVATION_ANGLE += 5.0
elif event.key == pygame.K_DOWN:
ELEVATION_ANGLE -= 5.0
# WASD camera translation
elif event.key == pygame.K_w:
CENTER_X -= move_step * math.cos(math.radians(Theta))
CENTER_Z -= move_step * math.sin(math.radians(Theta))
elif event.key == pygame.K_s:
CENTER_X += move_step * math.cos(math.radians(Theta))
CENTER_Z += move_step * math.sin(math.radians(Theta))
elif event.key == pygame.K_d:
CENTER_X += move_step * math.cos(math.radians(Theta - 90))
CENTER_Z += move_step * math.sin(math.radians(Theta - 90))
elif event.key == pygame.K_a:
CENTER_X += move_step * math.cos(math.radians(Theta + 90))
CENTER_Z += move_step * math.sin(math.radians(Theta + 90))
elif event.key == pygame.K_q:
ELEVATION_ANGLE -= move_step
elif event.key == pygame.K_r:
ELEVATION_ANGLE += move_step
def main():
global Theta, ELEVATION_ANGLE
Theta = 45.0
ELEVATION_ANGLE = 0.0
InitSimulation()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
HandleKeyEvent(event)
elif event.type == pygame.QUIT:
done = True
LookAt()
glLoadIdentity()
gluLookAt(EYE_X, EYE_Y, EYE_Z, CENTER_X, CENTER_Y, CENTER_Z, UP_X, UP_Y, UP_Z)
display()
pygame.display.flip()
pygame.time.wait(40)
pygame.quit()
if __name__ == "__main__":
main()